blob: a5f8a51bd7337ab6bdc1187dbb2ea7c4b68a2c0e [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 Chan64a1c8e2019-01-23 15:03:17 -0800326 ExecutorService neighborExecutor;
Charles Chan9b7217c2018-04-05 16:31:15 -0700327
328 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chane849c192016-01-11 18:28:54 -0800329 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700330 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700331 * Used to keep track on MPLS group information.
Charles Chane849c192016-01-11 18:28:54 -0800332 */
Charles Chan47933752017-11-30 15:37:50 -0800333 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das7bcbe702017-06-13 15:35:54 -0700334 dsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800335 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700336 * Per device next objective ID store with (device id + vlanid) as key.
337 * Used to keep track on L2 flood group information.
Charles Chane849c192016-01-11 18:28:54 -0800338 */
Charles Chan47933752017-11-30 15:37:50 -0800339 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan59cc16d2017-02-02 16:20:42 -0800340 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800341 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700342 * Per device next objective ID store with (device id + port + treatment + meta) as key.
343 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chane849c192016-01-11 18:28:54 -0800344 */
Charles Chan47933752017-11-30 15:37:50 -0800345 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800346 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800347
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700348 /**
349 * Per port dummy VLAN ID store with (connect point + ip address) as key.
350 * Used to keep track on dummy VLAN ID allocation.
351 */
352 private EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId>
353 dummyVlanIdStore = null;
354
Saurav Das4ce45962015-11-24 23:21:05 -0800355 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
356 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700357
Saurav Das7bcbe702017-06-13 15:35:54 -0700358 private AtomicBoolean programmingScheduled = new AtomicBoolean();
359
Charles Chand55e84d2016-03-30 17:54:24 -0700360 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700361 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
362 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700363 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700364 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800365 public SegmentRoutingDeviceConfig createConfig() {
366 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700367 }
368 };
Pier Ventref34966c2016-11-07 16:21:04 -0800369
Charles Chand55e84d2016-03-30 17:54:24 -0700370 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700371 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
372 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700373 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800374 @Override
375 public SegmentRoutingAppConfig createConfig() {
376 return new SegmentRoutingAppConfig();
377 }
378 };
Pier Ventref34966c2016-11-07 16:21:04 -0800379
Charles Chanfc5c7802016-05-17 13:13:55 -0700380 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
381 new ConfigFactory<ApplicationId, XConnectConfig>(
382 SubjectFactories.APP_SUBJECT_FACTORY,
383 XConnectConfig.class, "xconnect") {
384 @Override
385 public XConnectConfig createConfig() {
386 return new XConnectConfig();
387 }
388 };
Pier Ventref34966c2016-11-07 16:21:04 -0800389
Charles Chand55e84d2016-03-30 17:54:24 -0700390 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700391 new ConfigFactory<ApplicationId, McastConfig>(
392 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700393 McastConfig.class, "multicast") {
394 @Override
395 public McastConfig createConfig() {
396 return new McastConfig();
397 }
398 };
399
Charles Chan116188d2016-02-18 14:22:42 -0800400 /**
401 * Segment Routing App ID.
402 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800403 public static final String APP_NAME = "org.onosproject.segmentrouting";
Saurav Das0e99e2b2015-10-28 12:39:42 -0700404
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700405 /**
406 * Minumum and maximum value of dummy VLAN ID to be allocated.
407 */
408 public static final int MIN_DUMMY_VLAN_ID = 2;
409 public static final int MAX_DUMMY_VLAN_ID = 4093;
410
Charles Chan64a1c8e2019-01-23 15:03:17 -0800411 private static final int DEFAULT_POOL_SIZE = 32;
412
Saurav Das9a554292018-04-27 18:42:30 -0700413 Instant lastEdgePortEvent = Instant.EPOCH;
414
Charles Chan8bdb2252019-02-27 11:46:32 -0800415 protected void bindXconnectService(XconnectService xconnectService) {
416 if (this.xconnectService == null) {
417 log.info("Binding XconnectService");
418 this.xconnectService = xconnectService;
419 } else {
420 log.warn("Trying to bind XconnectService but it is already bound");
421 }
422 }
423
424 protected void unbindXconnectService(XconnectService xconnectService) {
425 if (this.xconnectService == xconnectService) {
426 log.info("Unbinding XconnectService");
427 this.xconnectService = null;
428 } else {
429 log.warn("Trying to unbind XconnectService but it is already unbound");
430 }
431 }
432
sanghob35a6192015-04-01 13:05:26 -0700433 @Activate
Charles Chan47933752017-11-30 15:37:50 -0800434 protected void activate(ComponentContext context) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800435 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700436
Charles Chan64a1c8e2019-01-23 15:03:17 -0800437 mainEventExecutor = Executors.newSingleThreadScheduledExecutor(
438 groupedThreads("onos/sr", "event-main-%d", log));
439 hostEventExecutor = Executors.newSingleThreadScheduledExecutor(
440 groupedThreads("onos/sr", "event-host-%d", log));
441 routeEventExecutor = Executors.newSingleThreadScheduledExecutor(
442 groupedThreads("onos/sr", "event-route-%d", log));
443 mcastEventExecutor = Executors.newSingleThreadScheduledExecutor(
444 groupedThreads("onos/sr", "event-mcast-%d", log));
445 packetExecutor = Executors.newSingleThreadExecutor(groupedThreads("onos/sr", "packet-%d", log));
446 neighborExecutor = Executors.newFixedThreadPool(DEFAULT_POOL_SIZE,
447 groupedThreads("onos/sr", "neighbor-%d", log));
Charles Chan50bb6ef2018-04-18 18:41:05 -0700448
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700449 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das7bcbe702017-06-13 15:35:54 -0700450 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700451 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700452 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700453 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700454 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700455 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700456 .build();
Saurav Das7bcbe702017-06-13 15:35:54 -0700457 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700458
Charles Chan59cc16d2017-02-02 16:20:42 -0800459 log.debug("Creating EC map vlannextobjectivestore");
460 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
461 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
462 vlanNextObjStore = vlanNextObjMapBuilder
463 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700464 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700465 .withTimestampProvider((k, v) -> new WallClockTimestamp())
466 .build();
467
Saurav Das4ce45962015-11-24 23:21:05 -0800468 log.debug("Creating EC map subnetnextobjectivestore");
469 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
470 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
471 portNextObjStore = portNextObjMapBuilder
472 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700473 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800474 .withTimestampProvider((k, v) -> new WallClockTimestamp())
475 .build();
476
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700477 EventuallyConsistentMapBuilder<DummyVlanIdStoreKey, VlanId>
478 dummyVlanIdMapBuilder = storageService.eventuallyConsistentMapBuilder();
479 dummyVlanIdStore = dummyVlanIdMapBuilder
480 .withName("dummyvlanidstore")
481 .withSerializer(createSerializer())
482 .withTimestampProvider((k, v) -> new WallClockTimestamp())
483 .build();
484
sangho0b2b6d12015-05-20 22:16:38 -0700485 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
486 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700487 tunnelStore = tunnelMapBuilder
488 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700489 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700490 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700491 .build();
492
493 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
494 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700495 policyStore = policyMapBuilder
496 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700497 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700498 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700499 .build();
500
Saurav Das80980c72016-03-23 11:22:49 -0700501 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800502 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700503 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800504 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800505 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
506 "requestInterceptsEnabled", "false");
Charles Chand3baaba2017-08-08 15:13:37 -0700507 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800508 "requestInterceptsEnabled", "false");
Charles Chanc760f382017-07-24 15:56:10 -0700509 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800510 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800511 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
512 "greedyLearningIpv6", "true");
Charles Chanc6d227e2017-02-28 15:15:17 -0800513 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
514 "forceUnprovision", "true");
Charles Chanef624ed2017-08-10 16:57:28 -0700515 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan73316522017-07-20 16:16:25 -0700516 "distributed", "true");
Charles Chan35a32322017-08-14 11:42:11 -0700517 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
518 "multihomingEnabled", "true");
Charles Chan807d87a2017-11-29 19:54:20 -0800519 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
520 "staleLinkAge", "15000");
521 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
522 "allowDuplicateIps", "false");
Yi Tsengfe13f3e2018-08-19 03:09:54 +0800523 // For P4 switches
524 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Charles Chanc76685f2018-08-30 17:30:45 -0700525 "fallbackFlowPollFrequency", "4");
Yi Tsengfe13f3e2018-08-19 03:09:54 +0800526 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Charles Chanc76685f2018-08-30 17:30:45 -0700527 "fallbackGroupPollFrequency", "3");
Charles Chan47933752017-11-30 15:37:50 -0800528 compCfgService.registerProperties(getClass());
529 modified(context);
Saurav Das80980c72016-03-23 11:22:49 -0700530
Charles Chanb8e10c82015-10-14 11:24:40 -0700531 processor = new InternalPacketProcessor();
532 linkListener = new InternalLinkListener();
533 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700534 appCfgHandler = new AppConfigHandler(this);
535 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700536 mcastHandler = new McastHandler(this);
537 hostHandler = new HostHandler(this);
Saurav Das45f48152018-01-18 12:07:33 -0800538 linkHandler = new LinkHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700539 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800540 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800541 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Pier Luigi83f919b2018-02-15 16:33:08 +0100542 topologyHandler = new TopologyHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700543
Charles Chan3e783d02016-02-26 22:19:52 -0800544 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700545 cfgService.registerConfigFactory(deviceConfigFactory);
546 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700547 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700548 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase7f51012018-02-09 17:26:45 -0800549 log.info("Configuring network before adding listeners");
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200550
Charles Chan132393a2018-01-04 14:26:07 -0800551 cfgListener.configureNetwork();
552
Charles Chan5270ed02016-01-30 23:22:37 -0800553 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700554 packetService.addProcessor(processor, PacketProcessor.director(2));
555 linkService.addListener(linkListener);
556 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700557 multicastRouteService.addListener(mcastListener);
Charles Chanc7a8a682017-06-19 00:43:31 -0700558 routeService.addListener(routeListener);
Pier Luigi83f919b2018-02-15 16:33:08 +0100559 topologyService.addListener(topologyListener);
Charles Chan50bb6ef2018-04-18 18:41:05 -0700560 mastershipService.addListener(mastershipListener);
Saurav Das201762d2018-04-21 17:19:48 -0700561 clusterService.addListener(clusterListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700562
Charles Chanb39777c2018-03-09 15:53:44 -0800563 linkHandler.init();
Andreas Pantelopoulos4a768c02018-01-11 07:53:48 -0800564 l2TunnelHandler.init();
565
Ray Milkeyec9e4de2018-11-16 15:15:14 -0800566 synchronized (networkConfigCompletionLock) {
567 networkConfigCompletion.whenComplete((value, ex) -> {
568 //setting to null for easier fall through
569 networkConfigCompletion = null;
570 //process all queued events
571 queuedEvents.forEach(event -> {
572 mainEventExecutor.execute(new InternalEventHandler(event));
573 });
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200574 });
Ray Milkeyec9e4de2018-11-16 15:15:14 -0800575 }
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200576
sanghob35a6192015-04-01 13:05:26 -0700577 log.info("Started");
578 }
579
Saurav Das45f48152018-01-18 12:07:33 -0800580 KryoNamespace.Builder createSerializer() {
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700581 return new KryoNamespace.Builder()
582 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700583 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800584 VlanNextObjectiveStoreKey.class,
585 DestinationSet.class,
Andreas Pantelopoulos67e09ae2018-05-29 13:11:14 -0700586 DestinationSet.DestinationSetType.class,
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800587 NextNeighbors.class,
588 Tunnel.class,
589 DefaultTunnel.class,
590 Policy.class,
591 TunnelPolicy.class,
592 Policy.Type.class,
593 PortNextObjectiveStoreKey.class,
594 XConnectStoreKey.class,
595 L2Tunnel.class,
596 L2TunnelPolicy.class,
597 DefaultL2Tunnel.class,
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700598 DefaultL2TunnelPolicy.class,
599 DummyVlanIdStoreKey.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700600 );
601 }
602
sanghob35a6192015-04-01 13:05:26 -0700603 @Deactivate
604 protected void deactivate() {
Charles Chan50bb6ef2018-04-18 18:41:05 -0700605 mainEventExecutor.shutdown();
606 hostEventExecutor.shutdown();
607 routeEventExecutor.shutdown();
608 mcastEventExecutor.shutdown();
Charles Chan07f15f22018-05-08 21:35:50 -0700609 packetExecutor.shutdown();
Charles Chan64a1c8e2019-01-23 15:03:17 -0800610 neighborExecutor.shutdown();
Charles Chan50bb6ef2018-04-18 18:41:05 -0700611
Ray Milkey749996b2018-05-11 09:59:19 -0700612 mainEventExecutor = null;
613 hostEventExecutor = null;
614 routeEventExecutor = null;
615 mcastEventExecutor = null;
616 packetExecutor = null;
Charles Chan64a1c8e2019-01-23 15:03:17 -0800617 neighborExecutor = null;
Ray Milkey749996b2018-05-11 09:59:19 -0700618
Charles Chand6832882015-10-05 17:50:33 -0700619 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700620 cfgService.unregisterConfigFactory(deviceConfigFactory);
621 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700622 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700623 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan47933752017-11-30 15:37:50 -0800624 compCfgService.unregisterProperties(getClass(), false);
Charles Chand6832882015-10-05 17:50:33 -0700625
Charles Chanc7a8a682017-06-19 00:43:31 -0700626 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700627 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700628 linkService.removeListener(linkListener);
629 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700630 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700631 routeService.removeListener(routeListener);
Pier Luigi83f919b2018-02-15 16:33:08 +0100632 topologyService.removeListener(topologyListener);
Charles Chan50bb6ef2018-04-18 18:41:05 -0700633 mastershipService.removeListener(mastershipListener);
Saurav Das201762d2018-04-21 17:19:48 -0700634 clusterService.removeListener(clusterListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700635
Charles Chan0aa674e2017-02-23 15:44:08 -0800636 neighbourResolutionService.unregisterNeighbourHandlers(appId);
637
sanghob35a6192015-04-01 13:05:26 -0700638 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700639 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700640 deviceListener = null;
Charles Chan6577f612018-04-19 13:10:01 -0700641 groupHandlerMap.forEach((k, v) -> v.shutdown());
Charles Chanb8e10c82015-10-14 11:24:40 -0700642 groupHandlerMap.clear();
Saurav Dasc6ff8f02018-04-23 18:42:12 -0700643 defaultRoutingHandler.shutdown();
Charles Chanb8e10c82015-10-14 11:24:40 -0700644
Saurav Das7bcbe702017-06-13 15:35:54 -0700645 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800646 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700647 portNextObjStore.destroy();
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700648 dummyVlanIdStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700649 tunnelStore.destroy();
650 policyStore.destroy();
Pier Luigib72201b2018-01-25 16:16:02 +0100651
652 mcastHandler.terminate();
sanghob35a6192015-04-01 13:05:26 -0700653 log.info("Stopped");
654 }
655
Charles Chan47933752017-11-30 15:37:50 -0800656 @Modified
657 private void modified(ComponentContext context) {
658 Dictionary<?, ?> properties = context.getProperties();
659 if (properties == null) {
660 return;
661 }
662
Ray Milkey88dd7e22018-10-24 10:04:03 -0700663 String strActiveProbing = Tools.get(properties, PROP_ACTIVE_PROBING);
Andreas Pantelopoulosb2518012018-06-18 12:50:35 -0700664 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProbing);
Charles Chan47933752017-11-30 15:37:50 -0800665 if (expectActiveProbing != activeProbing) {
666 activeProbing = expectActiveProbing;
667 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
668 }
Saurav Das9a554292018-04-27 18:42:30 -0700669
Ray Milkeye52652c2018-11-02 17:12:21 -0700670
671 String strSymmetricProbing = Tools.get(properties, PROP_SYMMETRIC_PROBING);
672 boolean expectSymmetricProbing = Boolean.parseBoolean(strSymmetricProbing);
Mayank Tiwaria61b0a82018-10-29 18:27:35 -0400673 if (expectSymmetricProbing != symmetricProbing) {
674 symmetricProbing = expectSymmetricProbing;
675 log.info("{} symmetric probing", symmetricProbing ? "Enabling" : "Disabling");
676 }
677
Ray Milkey88dd7e22018-10-24 10:04:03 -0700678 String strSingleHomedDown = Tools.get(properties, PROP_SINGLE_HOMED_DOWN);
Saurav Das9a554292018-04-27 18:42:30 -0700679 boolean expectSingleHomedDown = Boolean.parseBoolean(strSingleHomedDown);
680 if (expectSingleHomedDown != singleHomedDown) {
681 singleHomedDown = expectSingleHomedDown;
682 log.info("{} downing of single homed hosts for lost uplinks",
683 singleHomedDown ? "Enabling" : "Disabling");
684 if (singleHomedDown && linkHandler != null) {
685 hostService.getHosts().forEach(host -> host.locations()
686 .forEach(loc -> {
687 if (interfaceService.isConfigured(loc)) {
688 linkHandler.checkUplinksForHost(loc);
689 }
690 }));
691 } else {
692 log.warn("Disabling singleHomedDown does not re-enable already "
693 + "downed ports for single-homed hosts");
694 }
695 }
Andreas Pantelopoulosb2518012018-06-18 12:50:35 -0700696
Ray Milkey88dd7e22018-10-24 10:04:03 -0700697 String strRespondToUnknownHosts = Tools.get(properties, PROP_RESPOND_TO_UNKNOWN_HOSTS);
Andreas Pantelopoulosb2518012018-06-18 12:50:35 -0700698 boolean expectRespondToUnknownHosts = Boolean.parseBoolean(strRespondToUnknownHosts);
699 if (expectRespondToUnknownHosts != respondToUnknownHosts) {
700 respondToUnknownHosts = expectRespondToUnknownHosts;
701 log.info("{} responding to ARPs/NDPs from unknown hosts", respondToUnknownHosts ? "Enabling" : "Disabling");
702 }
Charles Chan57465d32018-07-19 14:55:31 -0700703
Ray Milkey88dd7e22018-10-24 10:04:03 -0700704 String strRouteDoubleTaggedHosts = Tools.get(properties, PROP_ROUTE_DOUBLE_TAGGED_HOSTS);
Charles Chanb67dfdd2018-07-24 16:40:35 -0700705 boolean expectRouteDoubleTaggedHosts = Boolean.parseBoolean(strRouteDoubleTaggedHosts);
706 if (expectRouteDoubleTaggedHosts != routeDoubleTaggedHosts) {
707 routeDoubleTaggedHosts = expectRouteDoubleTaggedHosts;
708 log.info("{} routing for double tagged hosts", routeDoubleTaggedHosts ? "Enabling" : "Disabling");
Charles Chan57465d32018-07-19 14:55:31 -0700709
Charles Chanb67dfdd2018-07-24 16:40:35 -0700710 if (routeDoubleTaggedHosts) {
Charles Chan57465d32018-07-19 14:55:31 -0700711 hostHandler.populateAllDoubleTaggedHost();
712 } else {
713 hostHandler.revokeAllDoubleTaggedHost();
714 }
715 }
Saurav Das09c2c4d2018-08-13 15:34:26 -0700716
Ray Milkey88dd7e22018-10-24 10:04:03 -0700717 String strDefaultInternalVlan = Tools.get(properties, PROP_DEFAULT_INTERNAL_VLAN);
Saurav Das09c2c4d2018-08-13 15:34:26 -0700718 int defIntVlan = Integer.parseInt(strDefaultInternalVlan);
719 if (defIntVlan != defaultInternalVlan) {
720 if (canUseVlanId(defIntVlan)) {
721 log.warn("Default internal vlan value changed from {} to {}.. "
722 + "re-programming filtering rules, but NOT any groups already "
723 + "created with the former value", defaultInternalVlan, defIntVlan);
724 VlanId oldDefIntVlan = VlanId.vlanId((short) defaultInternalVlan);
725 defaultInternalVlan = defIntVlan;
726 routingRulePopulator
727 .updateSpecialVlanFilteringRules(true, oldDefIntVlan,
728 VlanId.vlanId((short) defIntVlan));
729 } else {
730 log.warn("Cannot change default internal vlan to unusable "
731 + "value {}", defIntVlan);
732 }
733 }
734
Ray Milkey88dd7e22018-10-24 10:04:03 -0700735 String strPwTxpVlan = Tools.get(properties, PROP_PW_TRANSPORT_VLAN);
Saurav Das09c2c4d2018-08-13 15:34:26 -0700736 int pwTxpVlan = Integer.parseInt(strPwTxpVlan);
737 if (pwTxpVlan != pwTransportVlan) {
738 if (canUseVlanId(pwTxpVlan)) {
739 log.warn("Pseudowire transport vlan value changed from {} to {}.. "
740 + "re-programming filtering rules, but NOT any groups already "
741 + "created with the former value", pwTransportVlan,
742 pwTxpVlan);
743 VlanId oldPwTxpVlan = VlanId.vlanId((short) pwTransportVlan);
744 pwTransportVlan = pwTxpVlan;
745 routingRulePopulator
746 .updateSpecialVlanFilteringRules(false, oldPwTxpVlan,
747 VlanId.vlanId((short) pwTxpVlan));
748 } else {
749 log.warn("Cannot change pseudowire transport vlan to unusable "
750 + "value {}", pwTxpVlan);
751 }
752 }
753
754 }
755
756 /**
757 * Returns true if given vlan id is not being used in the system currently,
758 * either as one of the default system wide vlans or as one of the
759 * configured interface vlans.
760 *
761 * @param vlanId given vlan id
762 * @return true if vlan is not currently in use
763 */
764 public boolean canUseVlanId(int vlanId) {
765 if (vlanId >= 4095 || vlanId <= 1) {
766 log.error("Vlan id {} value is not in valid range 2 <--> 4094",
767 vlanId);
768 return false;
769 }
770
771 VlanId vid = VlanId.vlanId((short) vlanId);
772 if (getDefaultInternalVlan().equals(vid) || getPwTransportVlan().equals(vid)) {
773 log.warn("Vlan id {} value is already in use system-wide. "
774 + "DefaultInternalVlan:{} PwTransportVlan:{} ", vlanId,
775 getDefaultInternalVlan(), getPwTransportVlan());
776 return false;
777 }
778
779 if (interfaceService.inUse(vid)) {
780 log.warn("Vlan id {} value is already in use on a configured "
781 + "interface in the system", vlanId);
782 return false;
783 }
784 return true;
785 }
786
787 /**
788 * Returns the VlanId assigned internally by default to unconfigured ports.
789 *
790 * @return the default internal vlan id
791 */
792 public VlanId getDefaultInternalVlan() {
793 return VlanId.vlanId((short) defaultInternalVlan);
794 }
795
796 /**
797 * Returns the Vlan id used to transport pseudowire traffic across the
798 * network.
799 *
800 * @return the pseudowire transport vlan id
801 */
802 public VlanId getPwTransportVlan() {
803 return VlanId.vlanId((short) pwTransportVlan);
Charles Chan47933752017-11-30 15:37:50 -0800804 }
805
sangho1e575652015-05-14 00:39:53 -0700806 @Override
807 public List<Tunnel> getTunnels() {
808 return tunnelHandler.getTunnels();
809 }
810
811 @Override
sangho71abe1b2015-06-29 14:58:47 -0700812 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
813 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700814 }
815
816 @Override
sangho71abe1b2015-06-29 14:58:47 -0700817 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700818 for (Policy policy: policyHandler.getPolicies()) {
819 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
820 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
821 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
822 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700823 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700824 }
825 }
826 }
sangho71abe1b2015-06-29 14:58:47 -0700827 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700828 }
829
830 @Override
sangho71abe1b2015-06-29 14:58:47 -0700831 public PolicyHandler.Result removePolicy(Policy policy) {
832 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700833 }
834
835 @Override
sangho71abe1b2015-06-29 14:58:47 -0700836 public PolicyHandler.Result createPolicy(Policy policy) {
837 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700838 }
839
840 @Override
841 public List<Policy> getPolicies() {
842 return policyHandler.getPolicies();
843 }
844
Saurav Das59232cf2016-04-27 18:35:50 -0700845 @Override
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700846 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
847 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700848 }
849
850 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800851 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700852 return l2TunnelHandler.getL2Tunnels();
853 }
854
855 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800856 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700857 return l2TunnelHandler.getL2Policies();
858 }
859
Saurav Das5a356042018-04-06 20:16:01 -0700860 @Override
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700861 @Deprecated
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700862 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
863
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700864 // get both added and pending pseudowires
865 List<L2TunnelDescription> pseudowires = new ArrayList<>();
866 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
867 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700868 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700869
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700870 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
871
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700872 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
873 L2TunnelHandler.Result res;
874 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
875 res = addPseudowire(pw);
876 if (res != L2TunnelHandler.Result.SUCCESS) {
877 log.error("Pseudowire with id {} can not be instantiated !", res);
878 retRes = res;
879 }
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700880 }
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700881
882 return retRes;
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700883 }
884
885 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800886 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700887 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700888 }
889
890 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800891 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700892 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700893 }
894
895 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700896 public void rerouteNetwork() {
897 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700898 }
899
Charles Chanc81c45b2016-10-20 17:02:44 -0700900 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800901 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
902 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800903 deviceConfiguration.getRouters().forEach(device ->
904 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chanc81c45b2016-10-20 17:02:44 -0700905 return deviceSubnetMap;
906 }
907
Saurav Dasc88d4662017-05-15 15:34:25 -0700908
909 @Override
910 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
911 if (defaultRoutingHandler != null) {
912 return defaultRoutingHandler.getCurrentEmcpSpgMap();
913 } else {
914 return null;
915 }
916 }
917
918 @Override
Charles Chand5814aa2018-08-19 19:21:46 -0700919 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDstNextObjStore() {
Saurav Das7bcbe702017-06-13 15:35:54 -0700920 if (dsNextObjStore != null) {
921 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700922 } else {
923 return ImmutableMap.of();
924 }
925 }
926
Saurav Dasceccf242017-08-03 18:30:35 -0700927 @Override
Charles Chand5814aa2018-08-19 19:21:46 -0700928 public ImmutableMap<VlanNextObjectiveStoreKey, Integer> getVlanNextObjStore() {
929 if (vlanNextObjStore != null) {
930 return ImmutableMap.copyOf(vlanNextObjStore.entrySet());
931 } else {
932 return ImmutableMap.of();
933 }
934 }
935
936 @Override
937 public ImmutableMap<PortNextObjectiveStoreKey, Integer> getPortNextObjStore() {
938 if (portNextObjStore != null) {
939 return ImmutableMap.copyOf(portNextObjStore.entrySet());
940 } else {
941 return ImmutableMap.of();
942 }
943 }
944
945 @Override
946 public ImmutableMap<String, NextObjective> getPwInitNext() {
947 if (l2TunnelHandler != null) {
948 return l2TunnelHandler.getInitNext();
949 } else {
950 return ImmutableMap.of();
951 }
952 }
953
954 @Override
955 public ImmutableMap<String, NextObjective> getPwTermNext() {
956 if (l2TunnelHandler != null) {
957 return l2TunnelHandler.getTermNext();
958 } else {
959 return ImmutableMap.of();
960 }
961 }
962
963 @Override
964 public void invalidateNextObj(int nextId) {
965 if (dsNextObjStore != null) {
966 dsNextObjStore.entrySet().forEach(e -> {
967 if (e.getValue().nextId() == nextId) {
968 dsNextObjStore.remove(e.getKey());
969 }
970 });
971 }
972 if (vlanNextObjStore != null) {
973 vlanNextObjStore.entrySet().forEach(e -> {
974 if (e.getValue() == nextId) {
975 vlanNextObjStore.remove(e.getKey());
976 }
977 });
978 }
979 if (portNextObjStore != null) {
980 portNextObjStore.entrySet().forEach(e -> {
981 if (e.getValue() == nextId) {
982 portNextObjStore.remove(e.getKey());
983 }
984 });
985 }
986 if (mcastHandler != null) {
987 mcastHandler.removeNextId(nextId);
988 }
989 if (l2TunnelHandler != null) {
990 l2TunnelHandler.removeNextId(nextId);
991 }
992 if (xconnectService != null) {
993 xconnectService.removeNextId(nextId);
994 }
995 }
996
997 @Override
Saurav Dasceccf242017-08-03 18:30:35 -0700998 public void verifyGroups(DeviceId id) {
999 DefaultGroupHandler gh = groupHandlerMap.get(id);
1000 if (gh != null) {
1001 gh.triggerBucketCorrector();
1002 }
1003 }
1004
Saurav Dasc568c342018-01-25 09:49:01 -08001005 @Override
1006 public ImmutableMap<Link, Boolean> getSeenLinks() {
1007 return linkHandler.getSeenLinks();
1008 }
1009
1010 @Override
1011 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
1012 return linkHandler.getDownedPorts();
1013 }
1014
Pier Luigib29144d2018-01-15 18:06:43 +01001015 @Override
1016 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
Charles Chand5814aa2018-08-19 19:21:46 -07001017 return mcastHandler.getNextIds(mcastIp);
Pier Luigib29144d2018-01-15 18:06:43 +01001018 }
1019
1020 @Override
Pier Luigi96fe0772018-02-28 12:10:50 +01001021 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigib29144d2018-01-15 18:06:43 +01001022 return mcastHandler.getMcastRoles(mcastIp);
1023 }
1024
1025 @Override
Pier3e793752018-04-19 16:47:06 +02001026 public Map<McastRoleStoreKey, McastRole> getMcastRoles(IpAddress mcastIp, ConnectPoint sourcecp) {
1027 return mcastHandler.getMcastRoles(mcastIp, sourcecp);
1028 }
1029
1030 @Override
Pier Luigib29144d2018-01-15 18:06:43 +01001031 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
1032 return mcastHandler.getMcastPaths(mcastIp);
1033 }
1034
Pier96f63cb2018-04-17 16:29:56 +02001035 @Override
Pierb1fe7382018-04-17 17:25:22 +02001036 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
1037 ConnectPoint sourcecp) {
1038 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
1039 }
1040
1041 @Override
Pier96f63cb2018-04-17 16:29:56 +02001042 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
1043 return mcastHandler.getMcastLeaders(mcastIp);
1044 }
1045
Charles Chan8bc75ee2018-04-17 18:56:53 -07001046 @Override
1047 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
1048 return defaultRoutingHandler == null ? ImmutableMap.of() :
1049 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
1050 }
1051
1052 @Override
1053 public Map<DeviceId, Boolean> getShouldProgramCache() {
1054 return defaultRoutingHandler == null ? ImmutableMap.of() :
1055 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
1056 }
1057
Charles Chanc7b3c452018-06-19 20:31:57 -07001058 @Override
jayakumarthazhath655b9a82018-10-01 00:51:54 +05301059 public boolean shouldProgram(DeviceId deviceId) {
1060 return defaultRoutingHandler.shouldProgram(deviceId);
1061 }
1062
1063 @Override
Ray Milkeye4afdb52017-04-05 09:42:04 -07001064 public ApplicationId appId() {
1065 return appId;
1066 }
1067
1068 /**
1069 * Returns the device configuration.
1070 *
1071 * @return device configuration
1072 */
1073 public DeviceConfiguration deviceConfiguration() {
1074 return deviceConfiguration;
1075 }
1076
1077 /**
Saurav Das7bcbe702017-06-13 15:35:54 -07001078 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -07001079 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -07001080 *
1081 * @return next objective ID store
1082 */
Saurav Das7bcbe702017-06-13 15:35:54 -07001083 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
1084 dsNextObjStore() {
1085 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -07001086 }
1087
1088 /**
Charles Chan5bc32632017-08-22 15:07:34 -07001089 * Per device next objective ID store with (device id + vlanid) as key.
1090 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -07001091 *
1092 * @return vlan next object store
1093 */
1094 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
1095 return vlanNextObjStore;
1096 }
1097
1098 /**
Charles Chan5bc32632017-08-22 15:07:34 -07001099 * Per device next objective ID store with (device id + port + treatment + meta) as key.
1100 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -07001101 *
1102 * @return port next object store.
1103 */
1104 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
1105 return portNextObjStore;
1106 }
1107
1108 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001109 * Per port dummy VLAN ID store with (connect point + ip address) as key.
1110 * Used to keep track on dummy VLAN ID allocation.
1111 *
1112 * @return dummy vlan id store.
1113 */
1114 public EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId> dummyVlanIdStore() {
1115 return dummyVlanIdStore;
1116 }
1117
1118 /**
Saurav Das7bcbe702017-06-13 15:35:54 -07001119 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
1120 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -07001121 *
1122 * @return MPLS-ECMP value
1123 */
1124 public boolean getMplsEcmp() {
1125 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
1126 .getConfig(this.appId, SegmentRoutingAppConfig.class);
1127 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
1128 }
1129
1130 /**
sanghof9d0bf12015-05-19 11:57:42 -07001131 * Returns the tunnel object with the tunnel ID.
1132 *
1133 * @param tunnelId Tunnel ID
1134 * @return Tunnel reference
1135 */
sangho1e575652015-05-14 00:39:53 -07001136 public Tunnel getTunnel(String tunnelId) {
1137 return tunnelHandler.getTunnel(tunnelId);
1138 }
1139
Charles Chanc7b3c452018-06-19 20:31:57 -07001140 @Override
Pier Luigi96fe0772018-02-28 12:10:50 +01001141 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chan971d7ba2018-05-01 11:50:20 -07001142 VlanId untaggedVlanId = interfaceService.getUntaggedVlanId(connectPoint);
1143 VlanId nativeVlanId = interfaceService.getNativeVlanId(connectPoint);
Charles Chanf9a52702017-06-16 15:19:24 -07001144 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
1145 }
1146
Charles Chanc7b3c452018-06-19 20:31:57 -07001147 @Override
1148 public Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
Charles Chan65238242017-06-22 18:03:14 -07001149 SegmentRoutingDeviceConfig deviceConfig =
1150 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1151 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
1152 }
Charles Chanc7b3c452018-06-19 20:31:57 -07001153
1154 @Override
Saurav Das9a554292018-04-27 18:42:30 -07001155 public Optional<PortNumber> getPairLocalPort(DeviceId deviceId) {
Charles Chan65238242017-06-22 18:03:14 -07001156 SegmentRoutingDeviceConfig deviceConfig =
1157 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1158 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
1159 }
1160
1161 /**
Charles Chan9640c812017-08-23 13:55:39 -07001162 * Returns locations of given resolved route.
1163 *
1164 * @param resolvedRoute resolved route
1165 * @return locations of nexthop. Might be empty if next hop is not found
1166 */
1167 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
1168 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
1169 return Optional.ofNullable(hostService.getHost(hostId))
1170 .map(Host::locations).orElse(Sets.newHashSet())
1171 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
1172 }
1173
1174 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -08001175 * Returns vlan port map of given device.
1176 *
1177 * @param deviceId device id
1178 * @return vlan-port multimap
1179 */
1180 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
1181 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
1182
1183 interfaceService.getInterfaces().stream()
1184 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
1185 .forEach(intf -> {
1186 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -07001187 intf.vlanTagged().forEach(vlanTagged ->
1188 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
1189 );
Charles Chan7ffd81f2017-02-08 15:52:08 -08001190 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
1191 });
1192 vlanPortMap.removeAll(VlanId.NONE);
1193
1194 return vlanPortMap;
1195 }
1196
1197 /**
Charles Chan59cc16d2017-02-02 16:20:42 -08001198 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -08001199 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -07001200 *
1201 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -08001202 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -08001203 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -07001204 */
Charles Chan65238242017-06-22 18:03:14 -07001205 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -07001206 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -08001207 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
1208 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001209 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -08001210 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -08001211 + "device {} not found", deviceId);
1212 return -1;
1213 }
1214 }
1215
1216 /**
1217 * Returns the next objective ID for the given portNumber, given the treatment.
1218 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -07001219 * would result in different objectives. If the next object does not exist,
1220 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -08001221 *
1222 * @param deviceId Device ID
1223 * @param portNum port number on device for which NextObjective is queried
1224 * @param treatment the actions to apply on the packets (should include outport)
1225 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -07001226 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -07001227 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -08001228 */
1229 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
1230 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -07001231 TrafficSelector meta,
1232 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -08001233 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1234 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -07001235 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -08001236 } else {
Charles Chane849c192016-01-11 18:28:54 -08001237 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
1238 + " not found", deviceId);
1239 return -1;
1240 }
1241 }
1242
Saurav Dasc88d4662017-05-15 15:34:25 -07001243 /**
1244 * Returns the group handler object for the specified device id.
1245 *
1246 * @param devId the device identifier
1247 * @return the groupHandler object for the device id, or null if not found
1248 */
Charles Chan65238242017-06-22 18:03:14 -07001249 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001250 return groupHandlerMap.get(devId);
1251 }
1252
1253 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001254 * Returns the default routing handler object.
1255 *
1256 * @return the default routing handler object
1257 */
1258 public DefaultRoutingHandler getRoutingHandler() {
1259 return defaultRoutingHandler;
1260 }
1261
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001262 /**
1263 * Returns new dummy VLAN ID.
1264 * Dummy VLAN ID should be unique in each connect point.
1265 *
1266 * @param cp connect point
1267 * @param ipAddress IP address
1268 * @return new dummy VLAN ID. Returns VlanId.NONE if no VLAN ID is available.
1269 */
1270 public synchronized VlanId allocateDummyVlanId(ConnectPoint cp, IpAddress ipAddress) {
1271 Set<VlanId> usedVlanId = Sets.union(getVlanPortMap(cp.deviceId()).keySet(),
1272 dummyVlanIdStore.entrySet().stream().filter(entry ->
1273 (entry.getKey()).connectPoint().equals(cp))
1274 .map(Map.Entry::getValue)
1275 .collect(Collectors.toSet()));
1276
1277 VlanId dummyVlanId = IntStream.range(MIN_DUMMY_VLAN_ID, MAX_DUMMY_VLAN_ID).mapToObj(
1278 i -> VlanId.vlanId((short) (i & 0xFFFF))
1279 ).filter(vlanId -> !usedVlanId.contains(vlanId)).findFirst().orElse(VlanId.NONE);
1280
1281 if (!dummyVlanId.equals(VlanId.NONE)) {
1282 this.dummyVlanIdStore.put(new DummyVlanIdStoreKey(cp, ipAddress), dummyVlanId);
1283 log.debug("Dummy VLAN ID {} is allocated to {}, {}", dummyVlanId, cp, ipAddress);
1284 } else {
1285 log.error("Failed to allocate dummy VLAN ID for {}, {}", cp, ipAddress);
1286 }
1287 return dummyVlanId;
1288 }
1289
1290
sanghob35a6192015-04-01 13:05:26 -07001291 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -07001292 @Override
1293 public void process(PacketContext context) {
Charles Chan07f15f22018-05-08 21:35:50 -07001294 packetExecutor.execute(() -> processPacketInternal(context));
1295 }
sanghob35a6192015-04-01 13:05:26 -07001296
Charles Chan07f15f22018-05-08 21:35:50 -07001297 private void processPacketInternal(PacketContext context) {
sanghob35a6192015-04-01 13:05:26 -07001298 if (context.isHandled()) {
1299 return;
1300 }
1301
1302 InboundPacket pkt = context.inPacket();
1303 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -08001304
1305 if (ethernet == null) {
1306 return;
1307 }
1308
Saurav Das7bcbe702017-06-13 15:35:54 -07001309 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1310 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001311 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001312 log.warn("Received unexpected ARP packet on {}",
1313 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -07001314 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -08001315 return;
sanghob35a6192015-04-01 13:05:26 -07001316 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -08001317 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1318 //ipHandler.addToPacketBuffer(ipv4Packet);
1319 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1320 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001321 } else {
Charles Chan50035632017-01-13 17:20:44 -08001322 // NOTE: We don't support IP learning at this moment so this
1323 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -08001324 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001325 }
Pier Ventre10bd8d12016-11-26 21:05:22 -08001326 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1327 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -08001328 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -08001329 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -08001330 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1331 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1332 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1333 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1334 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1335 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -07001336 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -07001337 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -08001338 }
1339 } else {
1340 // NOTE: We don't support IP learning at this moment so this
1341 // is not necessary. Also it causes duplication of DHCPv6 packets.
1342 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1343 }
sanghob35a6192015-04-01 13:05:26 -07001344 }
1345 }
1346 }
1347
sanghob35a6192015-04-01 13:05:26 -07001348 private class InternalEventHandler implements Runnable {
Charles Chan9b7217c2018-04-05 16:31:15 -07001349 private Event event;
1350
1351 InternalEventHandler(Event event) {
1352 this.event = event;
1353 }
1354
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -07001355 @Override
sanghob35a6192015-04-01 13:05:26 -07001356 public void run() {
Charles Chan9b7217c2018-04-05 16:31:15 -07001357 try {
1358 // TODO We should also change SR routing and PW to listen to TopologyEvents
1359 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1360 event.type() == LinkEvent.Type.LINK_UPDATED) {
1361 linkHandler.processLinkAdded((Link) event.subject());
1362 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1363 linkHandler.processLinkRemoved((Link) event.subject());
1364 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1365 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1366 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1367 DeviceId deviceId = ((Device) event.subject()).id();
1368 if (deviceService.isAvailable(deviceId)) {
1369 log.info("** DEVICE UP Processing device event {} "
1370 + "for available device {}",
1371 event.type(), ((Device) event.subject()).id());
1372 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001373 } else {
Charles Chan7040b692018-08-29 21:21:51 -07001374 if (event.type() == DeviceEvent.Type.DEVICE_ADDED) {
1375 // Note: For p4 devices, the device will be added but unavailable at the beginning.
1376 // The device will later on being marked as available once the pipeline is pushed
1377 // to the device.
1378 log.info("** DEVICE ADDED but unavailable. Ignore");
1379 return;
1380 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001381 log.info(" ** DEVICE DOWN Processing device event {}"
1382 + " for unavailable device {}",
1383 event.type(), ((Device) event.subject()).id());
1384 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001385 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001386 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1387 // typically these calls come when device is added first time
1388 // so port filtering rules are handled at the device_added event.
1389 // port added calls represent all ports on the device,
1390 // enabled or not.
1391 log.trace("** PORT ADDED {}/{} -> {}",
1392 ((DeviceEvent) event).subject().id(),
1393 ((DeviceEvent) event).port().number(),
1394 event.type());
1395 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1396 // these calls happen for every subsequent event
1397 // ports enabled, disabled, switch goes away, comes back
1398 log.info("** PORT UPDATED {}/{} -> {}",
1399 event.subject(),
1400 ((DeviceEvent) event).port(),
1401 event.type());
Saurav Das9a554292018-04-27 18:42:30 -07001402 processPortUpdatedInternal(((Device) event.subject()),
Charles Chan9b7217c2018-04-05 16:31:15 -07001403 ((DeviceEvent) event).port());
1404 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1405 // Process topology event, needed for all modules relying on
1406 // topology service for path computation
1407 TopologyEvent topologyEvent = (TopologyEvent) event;
1408 log.info("Processing topology event {}, topology age {}, reasons {}",
1409 event.type(), topologyEvent.subject().time(),
1410 topologyEvent.reasons().size());
1411 topologyHandler.processTopologyChange(topologyEvent.reasons());
1412 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1413 hostHandler.processHostAddedEvent((HostEvent) event);
1414 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1415 hostHandler.processHostMovedEvent((HostEvent) event);
1416 routeHandler.processHostMovedEvent((HostEvent) event);
1417 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1418 hostHandler.processHostRemovedEvent((HostEvent) event);
1419 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1420 hostHandler.processHostUpdatedEvent((HostEvent) event);
1421 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1422 routeHandler.processRouteAdded((RouteEvent) event);
1423 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1424 routeHandler.processRouteUpdated((RouteEvent) event);
1425 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1426 routeHandler.processRouteRemoved((RouteEvent) event);
1427 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1428 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1429 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1430 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1431 event.type() == McastEvent.Type.SINKS_ADDED ||
1432 event.type() == McastEvent.Type.SINKS_REMOVED ||
1433 event.type() == McastEvent.Type.ROUTE_ADDED ||
1434 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1435 mcastHandler.processMcastEvent((McastEvent) event);
1436 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1437 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1438 Class configClass = netcfgEvent.configClass();
1439 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1440 appCfgHandler.processAppConfigAdded(netcfgEvent);
1441 log.info("App config event .. configuring network");
1442 cfgListener.configureNetwork();
1443 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1444 log.info("Segment Routing Device Config added for {}", event.subject());
1445 cfgListener.configureNetwork();
1446 } else if (configClass.equals(XConnectConfig.class)) {
1447 xConnectHandler.processXConnectConfigAdded(netcfgEvent);
1448 } else if (configClass.equals(InterfaceConfig.class)) {
1449 log.info("Interface Config added for {}", event.subject());
1450 cfgListener.configureNetwork();
1451 } else {
1452 log.error("Unhandled config class: {}", configClass);
1453 }
1454 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1455 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1456 Class configClass = netcfgEvent.configClass();
1457 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1458 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1459 log.info("App config event .. configuring network");
1460 cfgListener.configureNetwork();
1461 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1462 log.info("Segment Routing Device Config updated for {}", event.subject());
1463 createOrUpdateDeviceConfiguration();
1464 } else if (configClass.equals(XConnectConfig.class)) {
1465 xConnectHandler.processXConnectConfigUpdated(netcfgEvent);
1466 } else if (configClass.equals(InterfaceConfig.class)) {
1467 log.info("Interface Config updated for {}", event.subject());
1468 createOrUpdateDeviceConfiguration();
1469 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1470 (InterfaceConfig) netcfgEvent.prevConfig().get());
1471 } else {
1472 log.error("Unhandled config class: {}", configClass);
1473 }
1474 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1475 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1476 Class configClass = netcfgEvent.configClass();
1477 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1478 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1479 log.info("App config event .. configuring network");
1480 cfgListener.configureNetwork();
1481 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1482 // TODO Handle sr device config removal
1483 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
1484 } else if (configClass.equals(XConnectConfig.class)) {
1485 xConnectHandler.processXConnectConfigRemoved(netcfgEvent);
1486 } else if (configClass.equals(InterfaceConfig.class)) {
1487 // TODO Handle interface removal
1488 log.info("InterfaceConfig removal is not handled in current implementation");
1489 } else {
1490 log.error("Unhandled config class: {}", configClass);
1491 }
Saurav Das201762d2018-04-21 17:19:48 -07001492 } else if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
1493 MastershipEvent me = (MastershipEvent) event;
1494 DeviceId deviceId = me.subject();
1495 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1496 log.info(" ** MASTERSHIP CHANGED Invalidating shouldProgram cache"
1497 + " for {}/pair={} due to change", deviceId, pairDeviceId);
1498 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1499 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1500 defaultRoutingHandler.checkFullRerouteForMasterChange(deviceId, me);
Charles Chan9b7217c2018-04-05 16:31:15 -07001501 } else {
1502 log.warn("Unhandled event type: {}", event.type());
sanghob35a6192015-04-01 13:05:26 -07001503 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001504 } catch (Exception e) {
1505 log.error("SegmentRouting event handler thread thrown an exception: {}",
1506 e.getMessage(), e);
sanghob35a6192015-04-01 13:05:26 -07001507 }
sanghob35a6192015-04-01 13:05:26 -07001508 }
1509 }
1510
Saurav Das45f48152018-01-18 12:07:33 -08001511 void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001512 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001513
1514 // NOTE: Punt ARP/NDP even when the device is not configured.
1515 // Host learning without network config is required for CORD config generator.
1516 routingRulePopulator.populateIpPunts(device.id());
1517 routingRulePopulator.populateArpNdpPunts(device.id());
1518
Charles Chan0b4e6182015-11-03 10:42:14 -08001519 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001520 log.warn("Device configuration unavailable. Device {} will be "
1521 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001522 return;
1523 }
Charles Chan2199c302016-04-23 17:36:10 -07001524 processDeviceAddedInternal(device.id());
1525 }
1526
1527 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001528 // Irrespective of whether the local is a MASTER or not for this device,
1529 // we need to create a SR-group-handler instance. This is because in a
1530 // multi-instance setup, any instance can initiate forwarding/next-objectives
1531 // for any switch (even if this instance is a SLAVE or not even connected
1532 // to the switch). To handle this, a default-group-handler instance is necessary
1533 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001534 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1535 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001536 DefaultGroupHandler groupHandler;
1537 try {
1538 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001539 createGroupHandler(deviceId,
1540 appId,
1541 deviceConfiguration,
1542 linkService,
1543 flowObjectiveService,
1544 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001545 } catch (DeviceConfigNotFoundException e) {
1546 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1547 return;
1548 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001549 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001550 deviceId);
1551 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001552 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001553
Charles Chan2199c302016-04-23 17:36:10 -07001554 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001555 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001556 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001557 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001558 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001559 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001560 }
Charles Chan5270ed02016-01-30 23:22:37 -08001561
Charles Chan03a73e02016-10-24 14:52:01 -07001562 appCfgHandler.init(deviceId);
Charles Chan94549652018-04-29 18:11:37 -07001563 hostEventExecutor.execute(() -> hostHandler.init(deviceId));
1564 routeEventExecutor.execute(() -> routeHandler.init(deviceId));
sanghob35a6192015-04-01 13:05:26 -07001565 }
1566
Saurav Das80980c72016-03-23 11:22:49 -07001567 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001568 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001569 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan47933752017-11-30 15:37:50 -08001570 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001571 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001572 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001573 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001574 portNextObjStore.entrySet().stream()
1575 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001576 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Das45f48152018-01-18 12:07:33 -08001577 linkHandler.processDeviceRemoved(device);
Charles Chaned3742352017-06-15 00:44:51 -07001578
Saurav Dasceccf242017-08-03 18:30:35 -07001579 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1580 if (gh != null) {
1581 gh.shutdown();
1582 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001583 // Note that a switch going down is associated with all of its links
1584 // going down as well, but it is treated as a single switch down event
Saurav Das5a356042018-04-06 20:16:01 -07001585 // while the link-downs are ignored. We cannot rely on the ordering of
1586 // events - i.e we cannot expect all link-downs to come before the
1587 // switch down - so we purge all seen-links for the switch before
1588 // handling route-path changes for the switch-down
Saurav Dasc88d4662017-05-15 15:34:25 -07001589 defaultRoutingHandler
Saurav Das604ab3a2018-03-18 21:28:15 -07001590 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Dasc568c342018-01-25 09:49:01 -08001591 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001592 xConnectHandler.removeDevice(device.id());
Saurav Dasa4020382018-02-14 14:14:54 -08001593
1594 // Cleanup all internal groupHandler stores for this device. Should be
1595 // done after all rerouting or rehashing has been completed
1596 groupHandlerMap.entrySet()
1597 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Das80980c72016-03-23 11:22:49 -07001598 }
1599
Saurav Dasc6ff8f02018-04-23 18:42:12 -07001600 /**
1601 * Purge the destinationSet nextObjective store of entries with this device
1602 * as key. Erases app-level knowledge of hashed groups in this device.
1603 *
1604 * @param devId the device identifier
1605 */
Saurav Das201762d2018-04-21 17:19:48 -07001606 void purgeHashedNextObjectiveStore(DeviceId devId) {
Saurav Dasc6ff8f02018-04-23 18:42:12 -07001607 log.debug("Purging hashed next-obj store for dev:{}", devId);
Saurav Das201762d2018-04-21 17:19:48 -07001608 dsNextObjStore.entrySet().stream()
1609 .filter(entry -> entry.getKey().deviceId().equals(devId))
1610 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
1611 }
1612
Saurav Das9a554292018-04-27 18:42:30 -07001613 private void processPortUpdatedInternal(Device device, Port port) {
Saurav Das1a129a02016-11-18 15:21:57 -08001614 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1615 log.warn("Device configuration uploading. Not handling port event for"
1616 + "dev: {} port: {}", device.id(), port.number());
1617 return;
1618 }
Saurav Das018605f2017-02-18 14:05:44 -08001619
Saurav Das9a554292018-04-27 18:42:30 -07001620 if (interfaceService.isConfigured(new ConnectPoint(device.id(), port.number()))) {
1621 lastEdgePortEvent = Instant.now();
1622 }
1623
Saurav Das018605f2017-02-18 14:05:44 -08001624 if (!mastershipService.isLocalMaster(device.id())) {
1625 log.debug("Not master for dev:{} .. not handling port updated event"
1626 + "for port {}", device.id(), port.number());
1627 return;
1628 }
Saurav Das9a554292018-04-27 18:42:30 -07001629 processPortUpdated(device.id(), port);
1630 }
Saurav Das018605f2017-02-18 14:05:44 -08001631
Saurav Das9a554292018-04-27 18:42:30 -07001632 /**
1633 * Adds or remove filtering rules for the given switchport. If switchport is
1634 * an edge facing port, additionally handles host probing and broadcast
1635 * rules. Must be called by local master of device.
1636 *
1637 * @param deviceId the device identifier
1638 * @param port the port to update
1639 */
1640 void processPortUpdated(DeviceId deviceId, Port port) {
Saurav Das018605f2017-02-18 14:05:44 -08001641 // first we handle filtering rules associated with the port
1642 if (port.isEnabled()) {
1643 log.info("Switchport {}/{} enabled..programming filters",
Saurav Das9a554292018-04-27 18:42:30 -07001644 deviceId, port.number());
1645 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001646 } else {
1647 log.info("Switchport {}/{} disabled..removing filters",
Saurav Das9a554292018-04-27 18:42:30 -07001648 deviceId, port.number());
1649 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001650 }
Saurav Das1a129a02016-11-18 15:21:57 -08001651
1652 // portUpdated calls are for ports that have gone down or up. For switch
1653 // to switch ports, link-events should take care of any re-routing or
1654 // group editing necessary for port up/down. Here we only process edge ports
1655 // that are already configured.
Saurav Das9a554292018-04-27 18:42:30 -07001656 ConnectPoint cp = new ConnectPoint(deviceId, port.number());
Charles Chan971d7ba2018-05-01 11:50:20 -07001657 VlanId untaggedVlan = interfaceService.getUntaggedVlanId(cp);
1658 VlanId nativeVlan = interfaceService.getNativeVlanId(cp);
1659 Set<VlanId> taggedVlans = interfaceService.getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001660
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001661 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001662 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das9a554292018-04-27 18:42:30 -07001663 + "dev/port: {}/{}", deviceId, port.number());
Saurav Das1a129a02016-11-18 15:21:57 -08001664 return;
1665 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001666 if (untaggedVlan != null) {
Saurav Das9a554292018-04-27 18:42:30 -07001667 processEdgePort(deviceId, port, untaggedVlan, true);
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001668 }
1669 if (nativeVlan != null) {
Saurav Das9a554292018-04-27 18:42:30 -07001670 processEdgePort(deviceId, port, nativeVlan, true);
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001671 }
1672 if (!taggedVlans.isEmpty()) {
Saurav Das9a554292018-04-27 18:42:30 -07001673 taggedVlans.forEach(tag -> processEdgePort(deviceId, port, tag, false));
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001674 }
Saurav Das1a129a02016-11-18 15:21:57 -08001675 }
1676
Saurav Das9a554292018-04-27 18:42:30 -07001677 private void processEdgePort(DeviceId deviceId, Port port, VlanId vlanId,
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001678 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001679 boolean portUp = port.isEnabled();
1680 if (portUp) {
Saurav Das9a554292018-04-27 18:42:30 -07001681 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", deviceId,
Charles Chan59cc16d2017-02-02 16:20:42 -08001682 port.number(), vlanId);
Charles Chan94549652018-04-29 18:11:37 -07001683 hostEventExecutor.execute(() -> hostHandler.processPortUp(new ConnectPoint(deviceId, port.number())));
Saurav Das1a129a02016-11-18 15:21:57 -08001684 } else {
Saurav Das9a554292018-04-27 18:42:30 -07001685 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", deviceId,
Charles Chan59cc16d2017-02-02 16:20:42 -08001686 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001687 }
1688
Saurav Das9a554292018-04-27 18:42:30 -07001689 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001690 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001691 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001692 } else {
1693 log.warn("Group handler not found for dev:{}. Not handling edge port"
Saurav Das9a554292018-04-27 18:42:30 -07001694 + " {} event for port:{}", deviceId,
Saurav Das1a129a02016-11-18 15:21:57 -08001695 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001696 }
1697 }
sangho1e575652015-05-14 00:39:53 -07001698
Charles Chan27fe1a52017-10-20 16:06:55 -07001699 private void createOrUpdateDeviceConfiguration() {
1700 if (deviceConfiguration == null) {
Saurav Dase7f51012018-02-09 17:26:45 -08001701 log.info("Creating new DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001702 deviceConfiguration = new DeviceConfiguration(this);
1703 } else {
Saurav Dase7f51012018-02-09 17:26:45 -08001704 log.info("Updating DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001705 deviceConfiguration.updateConfig();
1706 }
1707 }
1708
Charles Chan50bb6ef2018-04-18 18:41:05 -07001709 private void createOrUpdateDefaultRoutingHandler() {
1710 if (defaultRoutingHandler == null) {
1711 log.info("Creating new DefaultRoutingHandler");
1712 defaultRoutingHandler = new DefaultRoutingHandler(this);
1713 } else {
1714 log.info("Updating DefaultRoutingHandler");
1715 defaultRoutingHandler.update(this);
1716 }
1717 }
1718
Pier Ventre10bd8d12016-11-26 21:05:22 -08001719 /**
1720 * Registers the given connect point with the NRS, this is necessary
1721 * to receive the NDP and ARP packets from the NRS.
1722 *
1723 * @param portToRegister connect point to register
1724 */
1725 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001726 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001727 portToRegister,
1728 neighbourHandler,
1729 appId
1730 );
1731 }
1732
Charles Chand6832882015-10-05 17:50:33 -07001733 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001734 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001735 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001736
Charles Chane849c192016-01-11 18:28:54 -08001737 /**
1738 * Constructs the internal network config listener.
1739 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001740 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001741 */
Charles Chan65238242017-06-22 18:03:14 -07001742 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001743 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001744 }
1745
Charles Chane849c192016-01-11 18:28:54 -08001746 /**
1747 * Reads network config and initializes related data structure accordingly.
1748 */
Charles Chan47933752017-11-30 15:37:50 -08001749 void configureNetwork() {
Saurav Dase7f51012018-02-09 17:26:45 -08001750 log.info("Configuring network ...");
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001751
1752 // Setting handling of network configuration events completable future
1753 // The completable future is needed because of the async behaviour of the configureNetwork,
1754 // listener registration and event arrival
1755 // Enables us to buffer the events and execute them when the configure network is done.
Ray Milkeyec9e4de2018-11-16 15:15:14 -08001756 synchronized (networkConfigCompletionLock) {
1757 networkConfigCompletion = new CompletableFuture<>();
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001758
Ray Milkeyec9e4de2018-11-16 15:15:14 -08001759 // add a small delay to absorb multiple network config added notifications
1760 if (!programmingScheduled.get()) {
1761 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
1762 programmingScheduled.set(true);
1763 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
1764 }
1765
1766 createOrUpdateDeviceConfiguration();
1767
1768 arpHandler = new ArpHandler(srManager);
1769 icmpHandler = new IcmpHandler(srManager);
1770 ipHandler = new IpHandler(srManager);
1771 routingRulePopulator = new RoutingRulePopulator(srManager);
1772 createOrUpdateDefaultRoutingHandler();
1773
1774 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1775 groupHandlerMap, tunnelStore);
1776 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1777 flowObjectiveService,
1778 tunnelHandler, policyStore);
1779 networkConfigCompletion.complete(true);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001780 }
1781
Charles Chan2199c302016-04-23 17:36:10 -07001782 mcastHandler.init();
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001783
Charles Chan4636be02015-10-07 14:21:45 -07001784 }
1785
Charles Chand6832882015-10-05 17:50:33 -07001786 @Override
1787 public void event(NetworkConfigEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001788 if (mainEventExecutor == null) {
1789 return;
1790 }
Charles Chan7a888e82018-03-21 16:57:47 -07001791 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
1792 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1793 switch (event.type()) {
1794 case CONFIG_ADDED:
1795 case CONFIG_UPDATED:
1796 case CONFIG_REMOVED:
1797 log.trace("Schedule Network Config event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001798 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1799 mainEventExecutor.execute(new InternalEventHandler(event));
1800 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001801 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001802 }
Charles Chan7a888e82018-03-21 16:57:47 -07001803 break;
1804 default:
1805 break;
Charles Chand6832882015-10-05 17:50:33 -07001806 }
1807 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001808
Charles Chan50443e82018-01-03 16:26:32 -08001809 @Override
1810 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase7f51012018-02-09 17:26:45 -08001811 if (event.type() == CONFIG_REGISTERED ||
1812 event.type() == CONFIG_UNREGISTERED) {
1813 log.debug("Ignore event {} due to type mismatch", event);
1814 return false;
Charles Chan50443e82018-01-03 16:26:32 -08001815 }
Saurav Dase7f51012018-02-09 17:26:45 -08001816
1817 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1818 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1819 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscd339592018-02-23 14:18:00 -08001820 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase7f51012018-02-09 17:26:45 -08001821 log.debug("Ignore event {} due to class mismatch", event);
1822 return false;
1823 }
1824
1825 return true;
Charles Chan50443e82018-01-03 16:26:32 -08001826 }
1827
Saurav Das7bcbe702017-06-13 15:35:54 -07001828 private final class ConfigChange implements Runnable {
1829 @Override
1830 public void run() {
1831 programmingScheduled.set(false);
Saurav Dase7f51012018-02-09 17:26:45 -08001832 log.info("Reacting to config changes after buffer delay");
Saurav Das7bcbe702017-06-13 15:35:54 -07001833 for (Device device : deviceService.getDevices()) {
1834 processDeviceAdded(device);
1835 }
1836 defaultRoutingHandler.startPopulationProcess();
1837 }
1838 }
Charles Chand6832882015-10-05 17:50:33 -07001839 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001840
Charles Chan7a888e82018-03-21 16:57:47 -07001841 private class InternalLinkListener implements LinkListener {
1842 @Override
1843 public void event(LinkEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001844 if (mainEventExecutor == null) {
1845 return;
1846 }
Charles Chan7a888e82018-03-21 16:57:47 -07001847 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1848 event.type() == LinkEvent.Type.LINK_UPDATED ||
1849 event.type() == LinkEvent.Type.LINK_REMOVED) {
1850 log.trace("Schedule Link event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001851 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1852 mainEventExecutor.execute(new InternalEventHandler(event));
1853 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001854 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001855 }
Charles Chan7a888e82018-03-21 16:57:47 -07001856 }
1857 }
1858 }
1859
1860 private class InternalDeviceListener implements DeviceListener {
1861 @Override
1862 public void event(DeviceEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001863 if (mainEventExecutor == null) {
1864 return;
1865 }
Charles Chan7a888e82018-03-21 16:57:47 -07001866 switch (event.type()) {
1867 case DEVICE_ADDED:
1868 case PORT_UPDATED:
1869 case PORT_ADDED:
1870 case DEVICE_UPDATED:
1871 case DEVICE_AVAILABILITY_CHANGED:
1872 log.trace("Schedule Device event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001873 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1874 mainEventExecutor.execute(new InternalEventHandler(event));
1875 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001876 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001877 }
Charles Chan7a888e82018-03-21 16:57:47 -07001878 break;
1879 default:
1880 }
1881 }
1882 }
1883
1884 private class InternalTopologyListener implements TopologyListener {
1885 @Override
1886 public void event(TopologyEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001887 if (mainEventExecutor == null) {
1888 return;
1889 }
Charles Chan7a888e82018-03-21 16:57:47 -07001890 switch (event.type()) {
1891 case TOPOLOGY_CHANGED:
1892 log.trace("Schedule Topology event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001893 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1894 mainEventExecutor.execute(new InternalEventHandler(event));
1895 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001896 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001897 }
Charles Chan7a888e82018-03-21 16:57:47 -07001898 break;
1899 default:
1900 }
1901 }
1902 }
1903
Charles Chan68aa62d2015-11-09 16:37:23 -08001904 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001905 @Override
1906 public void event(HostEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001907 if (hostEventExecutor == null) {
1908 return;
1909 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001910 switch (event.type()) {
1911 case HOST_ADDED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001912 case HOST_MOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001913 case HOST_REMOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08001914 case HOST_UPDATED:
Charles Chan7a888e82018-03-21 16:57:47 -07001915 log.trace("Schedule Host event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001916 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chan68aa62d2015-11-09 16:37:23 -08001917 break;
1918 default:
1919 log.warn("Unsupported host event type: {}", event.type());
1920 break;
1921 }
1922 }
1923 }
1924
Charles Chand55e84d2016-03-30 17:54:24 -07001925 private class InternalMcastListener implements McastListener {
1926 @Override
1927 public void event(McastEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001928 if (mcastEventExecutor == null) {
1929 return;
1930 }
Charles Chand55e84d2016-03-30 17:54:24 -07001931 switch (event.type()) {
Pier3ee24552018-03-14 16:47:32 -07001932 case SOURCES_ADDED:
1933 case SOURCES_REMOVED:
1934 case SINKS_ADDED:
1935 case SINKS_REMOVED:
Charles Chand55e84d2016-03-30 17:54:24 -07001936 case ROUTE_REMOVED:
Pier96f63cb2018-04-17 16:29:56 +02001937 case ROUTE_ADDED:
Charles Chan7a888e82018-03-21 16:57:47 -07001938 log.trace("Schedule Mcast event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001939 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi9930da52018-02-02 16:19:11 +01001940 break;
Charles Chand55e84d2016-03-30 17:54:24 -07001941 default:
Charles Chan7a888e82018-03-21 16:57:47 -07001942 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chand55e84d2016-03-30 17:54:24 -07001943 break;
1944 }
1945 }
1946 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001947
Charles Chan03a73e02016-10-24 14:52:01 -07001948 private class InternalRouteEventListener implements RouteListener {
1949 @Override
1950 public void event(RouteEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001951 if (routeEventExecutor == null) {
1952 return;
1953 }
Charles Chan03a73e02016-10-24 14:52:01 -07001954 switch (event.type()) {
1955 case ROUTE_ADDED:
Charles Chan03a73e02016-10-24 14:52:01 -07001956 case ROUTE_UPDATED:
Charles Chan03a73e02016-10-24 14:52:01 -07001957 case ROUTE_REMOVED:
Charles Chan2fde6d42017-08-23 14:46:43 -07001958 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan7a888e82018-03-21 16:57:47 -07001959 log.trace("Schedule Route event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07001960 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan2fde6d42017-08-23 14:46:43 -07001961 break;
Charles Chan03a73e02016-10-24 14:52:01 -07001962 default:
Charles Chan7a888e82018-03-21 16:57:47 -07001963 log.warn("Unsupported route event type: {}", event.type());
Charles Chan03a73e02016-10-24 14:52:01 -07001964 break;
1965 }
1966 }
1967 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001968
Charles Chan50bb6ef2018-04-18 18:41:05 -07001969 private class InternalMastershipListener implements MastershipListener {
1970 @Override
1971 public void event(MastershipEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001972 if (mainEventExecutor == null) {
1973 return;
1974 }
Charles Chan50bb6ef2018-04-18 18:41:05 -07001975 switch (event.type()) {
Saurav Das201762d2018-04-21 17:19:48 -07001976 case MASTER_CHANGED:
1977 log.debug("Mastership event: {}/{}", event.subject(),
1978 event.roleInfo());
1979 mainEventExecutor.execute(new InternalEventHandler(event));
1980 break;
1981 case BACKUPS_CHANGED:
1982 case SUSPENDED:
1983 default:
1984 log.debug("Mastership event type {} not handled", event.type());
1985 break;
Charles Chan50bb6ef2018-04-18 18:41:05 -07001986 }
1987 }
1988 }
1989
Saurav Das201762d2018-04-21 17:19:48 -07001990 class InternalClusterListener implements ClusterEventListener {
1991 private Instant lastClusterEvent = Instant.EPOCH;
1992
1993 long timeSinceLastClusterEvent() {
1994 return Instant.now().toEpochMilli() - lastClusterEvent.toEpochMilli();
1995 }
1996
1997 @Override
1998 public void event(ClusterEvent event) {
1999 switch (event.type()) {
2000 case INSTANCE_ACTIVATED:
2001 case INSTANCE_ADDED:
2002 case INSTANCE_READY:
2003 log.debug("Cluster event {} ignored", event.type());
2004 break;
2005 case INSTANCE_DEACTIVATED:
2006 case INSTANCE_REMOVED:
2007 log.info("** Cluster event {}", event.type());
2008 lastClusterEvent = Instant.now();
2009 break;
2010 default:
2011 break;
2012 }
2013
2014 }
2015
2016 }
2017
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002018 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
2019 try {
2020 Set<Interface> intfs = conf.getInterfaces();
2021 Set<Interface> prevIntfs = prevConf.getInterfaces();
2022
2023 // Now we only handle one interface config at each port.
2024 if (intfs.size() != 1 || prevIntfs.size() != 1) {
2025 log.warn("Interface update aborted - one at a time is allowed, " +
2026 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
2027 return;
2028 }
2029
Andrea Campanella6468e1f2018-04-17 12:09:34 +02002030 //The system is in an incoherent state, abort
2031 if (defaultRoutingHandler == null) {
2032 log.warn("Interface update aborted, defaultRoutingHandler is null");
2033 return;
2034 }
2035
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002036 Interface intf = intfs.stream().findFirst().get();
2037 Interface prevIntf = prevIntfs.stream().findFirst().get();
2038
2039 DeviceId deviceId = intf.connectPoint().deviceId();
2040 PortNumber portNum = intf.connectPoint().port();
2041
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002042 removeSubnetConfig(prevIntf.connectPoint(),
2043 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
2044 new HashSet<>(intf.ipAddressesList())));
2045
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002046 if (!prevIntf.vlanNative().equals(VlanId.NONE)
2047 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
2048 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
2049 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
2050 // Update filtering objective and L2IG group bucket
2051 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
2052 } else {
2053 // RemoveVlanNative
2054 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
2055 }
2056 }
2057
2058 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
2059 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2060 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2061 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
2062 // Update filtering objective and L2IG group bucket
2063 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
2064 } else {
2065 // RemoveVlanUntagged
2066 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
2067 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002068 }
2069
2070 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2071 // RemoveVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002072 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
2073 .filter(i -> !intf.vlanUntagged().equals(i))
2074 .filter(i -> !intf.vlanNative().equals(i))
2075 .forEach(vlanId -> updateVlanConfigInternal(
2076 deviceId, portNum, vlanId, false, false));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002077 }
2078
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002079 if (!intf.vlanNative().equals(VlanId.NONE)
2080 && !prevIntf.vlanNative().equals(intf.vlanNative())
2081 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2082 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
2083 // Update filtering objective and L2IG group bucket
2084 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
2085 } else {
2086 // AddVlanNative
2087 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
2088 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002089 }
2090
2091 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2092 // AddVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002093 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
2094 .filter(i -> !prevIntf.vlanUntagged().equals(i))
2095 .filter(i -> !prevIntf.vlanNative().equals(i))
2096 .forEach(vlanId -> updateVlanConfigInternal(
2097 deviceId, portNum, vlanId, false, true)
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002098 );
2099 }
2100
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002101 if (!intf.vlanUntagged().equals(VlanId.NONE)
2102 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2103 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
2104 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
2105 // Update filtering objective and L2IG group bucket
2106 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
2107 } else {
2108 // AddVlanUntagged
2109 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
2110 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002111 }
2112 addSubnetConfig(prevIntf.connectPoint(),
2113 Sets.difference(new HashSet<>(intf.ipAddressesList()),
2114 new HashSet<>(prevIntf.ipAddressesList())));
2115 } catch (ConfigException e) {
2116 log.error("Error in configuration");
2117 }
2118 }
2119
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002120 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
2121 VlanId vlanId, boolean pushVlan) {
2122 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2123 if (grpHandler == null) {
2124 log.warn("Failed to retrieve group handler for device {}", deviceId);
2125 return;
2126 }
2127
2128 // Update filtering objective for a single port
2129 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
2130 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
2131
2132 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
2133 // Update L2IG bucket of the port
2134 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
2135 } else {
2136 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2137 }
2138 }
2139
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002140 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
2141 VlanId vlanId, boolean pushVlan, boolean install) {
2142 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2143 if (grpHandler == null) {
2144 log.warn("Failed to retrieve group handler for device {}", deviceId);
2145 return;
2146 }
2147
2148 // Update filtering objective for a single port
2149 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
2150
2151 // Update filtering objective for multicast ingress port
2152 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
2153
2154 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
2155
2156 if (nextId != -1 && !install) {
2157 // Update next objective for a single port as an output port
2158 // Remove a single port from L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002159 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002160 // Remove L2 Bridging rule and L3 Unicast rule to the host
Charles Chan94549652018-04-29 18:11:37 -07002161 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2162 vlanId, pushVlan, install));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002163 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
2164 // only if there is no port configured on that VLAN ID
2165 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
2166 // Remove broadcast forwarding rule for the VLAN
2167 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2168 // Remove L2FG for VLAN
2169 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
2170 } else {
2171 // Remove L2IG of the port
2172 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
2173 }
2174 } else if (install) {
2175 if (nextId != -1) {
2176 // Add a single port to L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002177 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002178 } else {
2179 // Create L2FG for VLAN
2180 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
2181 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2182 }
Charles Chan94549652018-04-29 18:11:37 -07002183 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2184 vlanId, pushVlan, install));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002185 } else {
2186 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2187 }
2188 }
2189
2190 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2191 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2192 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2193
2194 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2195 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2196 .filter(intf -> !intf.connectPoint().equals(cp))
2197 .flatMap(intf -> intf.ipAddressesList().stream())
2198 .collect(Collectors.toSet());
2199 // 1. Partial subnet population
2200 // Remove routing rules for removed subnet from previous configuration,
2201 // which does not also exist in other interfaces in the same device
2202 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2203 .map(InterfaceIpAddress::subnetAddress)
2204 .collect(Collectors.toSet());
2205
2206 defaultRoutingHandler.revokeSubnet(
2207 ipPrefixSet.stream()
2208 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2209 .collect(Collectors.toSet()));
2210
2211 // 2. Interface IP punts
2212 // Remove IP punts for old Intf address
2213 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2214 .map(InterfaceIpAddress::ipAddress)
2215 .collect(Collectors.toSet());
2216 ipAddressSet.stream()
2217 .map(InterfaceIpAddress::ipAddress)
2218 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2219 .forEach(interfaceIpAddress ->
2220 routingRulePopulator.revokeSingleIpPunts(
2221 cp.deviceId(), interfaceIpAddress));
2222
2223 // 3. Host unicast routing rule
2224 // Remove unicast routing rule
Charles Chan94549652018-04-29 18:11:37 -07002225 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002226 }
2227
2228 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2229 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2230 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2231
2232 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2233 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2234 .filter(intf -> !intf.connectPoint().equals(cp))
2235 .flatMap(intf -> intf.ipAddressesList().stream())
2236 .collect(Collectors.toSet());
2237 // 1. Partial subnet population
2238 // Add routing rules for newly added subnet, which does not also exist in
2239 // other interfaces in the same device
2240 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2241 .map(InterfaceIpAddress::subnetAddress)
2242 .collect(Collectors.toSet());
2243
2244 defaultRoutingHandler.populateSubnet(
2245 Collections.singleton(cp),
2246 ipPrefixSet.stream()
2247 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2248 .collect(Collectors.toSet()));
2249
2250 // 2. Interface IP punts
2251 // Add IP punts for new Intf address
2252 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2253 .map(InterfaceIpAddress::ipAddress)
2254 .collect(Collectors.toSet());
2255 ipAddressSet.stream()
2256 .map(InterfaceIpAddress::ipAddress)
2257 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2258 .forEach(interfaceIpAddress ->
2259 routingRulePopulator.populateSingleIpPunts(
2260 cp.deviceId(), interfaceIpAddress));
2261
2262 // 3. Host unicast routing rule
2263 // Add unicast routing rule
Charles Chan94549652018-04-29 18:11:37 -07002264 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002265 }
sanghob35a6192015-04-01 13:05:26 -07002266}