blob: e78b1521e48e76459028f4b262e48fcc61c6aa47 [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;
Ruchi Sahotaef0761c2019-01-28 01:08:18 +000030import org.onlab.packet.MacAddress;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070031import org.onlab.util.KryoNamespace;
Charles Chan47933752017-11-30 15:37:50 -080032import org.onlab.util.Tools;
Saurav Das80980c72016-03-23 11:22:49 -070033import org.onosproject.cfg.ComponentConfigService;
Saurav Das201762d2018-04-21 17:19:48 -070034import org.onosproject.cluster.ClusterEvent;
35import org.onosproject.cluster.ClusterEventListener;
Pier Luigieba73a02018-01-16 10:47:50 +010036import org.onosproject.cluster.ClusterService;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -070037import org.onosproject.cluster.LeadershipService;
Pier96f63cb2018-04-17 16:29:56 +020038import org.onosproject.cluster.NodeId;
sanghob35a6192015-04-01 13:05:26 -070039import org.onosproject.core.ApplicationId;
40import org.onosproject.core.CoreService;
41import org.onosproject.event.Event;
Charles Chan50bb6ef2018-04-18 18:41:05 -070042import org.onosproject.mastership.MastershipEvent;
43import org.onosproject.mastership.MastershipListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070044import org.onosproject.mastership.MastershipService;
Pier3ee24552018-03-14 16:47:32 -070045import org.onosproject.mcast.api.McastEvent;
46import org.onosproject.mcast.api.McastListener;
47import org.onosproject.mcast.api.MulticastRouteService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080048import org.onosproject.net.ConnectPoint;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070049import org.onosproject.net.Device;
50import org.onosproject.net.DeviceId;
Charles Chan9640c812017-08-23 13:55:39 -070051import org.onosproject.net.Host;
52import org.onosproject.net.HostId;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070053import org.onosproject.net.Link;
54import org.onosproject.net.Port;
Charles Chan68aa62d2015-11-09 16:37:23 -080055import org.onosproject.net.PortNumber;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070056import org.onosproject.net.config.ConfigException;
Charles Chand6832882015-10-05 17:50:33 -070057import org.onosproject.net.config.ConfigFactory;
58import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070059import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070060import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070061import org.onosproject.net.config.basics.InterfaceConfig;
62import org.onosproject.net.config.basics.McastConfig;
Charles Chand6832882015-10-05 17:50:33 -070063import org.onosproject.net.config.basics.SubjectFactories;
Saurav Das45f48152018-01-18 12:07:33 -080064import org.onosproject.net.device.DeviceAdminService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070065import org.onosproject.net.device.DeviceEvent;
66import org.onosproject.net.device.DeviceListener;
67import org.onosproject.net.device.DeviceService;
Charles Chan68aa62d2015-11-09 16:37:23 -080068import org.onosproject.net.flow.TrafficSelector;
69import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070070import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chand5814aa2018-08-19 19:21:46 -070071import org.onosproject.net.flowobjective.NextObjective;
Charles Chan68aa62d2015-11-09 16:37:23 -080072import org.onosproject.net.host.HostEvent;
73import org.onosproject.net.host.HostListener;
Charles Chanff79dd92018-06-01 16:33:48 -070074import org.onosproject.net.host.HostProbingService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080075import org.onosproject.net.host.HostService;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070076import org.onosproject.net.host.InterfaceIpAddress;
Pier96f63cb2018-04-17 16:29:56 +020077import org.onosproject.net.intent.WorkPartitionService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070078import org.onosproject.net.intf.Interface;
79import org.onosproject.net.intf.InterfaceService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080080import org.onosproject.net.link.LinkEvent;
81import org.onosproject.net.link.LinkListener;
82import org.onosproject.net.link.LinkService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070083import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080084import org.onosproject.net.packet.InboundPacket;
85import org.onosproject.net.packet.PacketContext;
86import org.onosproject.net.packet.PacketProcessor;
87import org.onosproject.net.packet.PacketService;
Pier Luigi83f919b2018-02-15 16:33:08 +010088import org.onosproject.net.topology.TopologyEvent;
89import org.onosproject.net.topology.TopologyListener;
Charles Chand55e84d2016-03-30 17:54:24 -070090import org.onosproject.net.topology.TopologyService;
Charles Chan9640c812017-08-23 13:55:39 -070091import org.onosproject.routeservice.ResolvedRoute;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070092import org.onosproject.routeservice.RouteEvent;
93import org.onosproject.routeservice.RouteListener;
94import org.onosproject.routeservice.RouteService;
Charles Chand55e84d2016-03-30 17:54:24 -070095import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
96import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventref34966c2016-11-07 16:21:04 -080097import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070098import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -070099import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -0700100import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700101import org.onosproject.segmentrouting.grouphandler.DestinationSet;
102import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Pier Luigi96fe0772018-02-28 12:10:50 +0100103import org.onosproject.segmentrouting.mcast.McastHandler;
104import org.onosproject.segmentrouting.mcast.McastRole;
Pier3e793752018-04-19 16:47:06 +0200105import org.onosproject.segmentrouting.mcast.McastRoleStoreKey;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700106import org.onosproject.segmentrouting.mcast.McastStoreKey;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700107import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700108import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800109import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700110import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800111import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700112import org.onosproject.segmentrouting.pwaas.L2TunnelDescription;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700113import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800114import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Saurav Das7bcbe702017-06-13 15:35:54 -0700115import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700116import org.onosproject.segmentrouting.storekey.DummyVlanIdStoreKey;
Charles Chand2990362016-04-18 13:44:03 -0700117import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -0800118import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000119import org.onosproject.segmentrouting.storekey.MacVlanNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -0700120import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Charles Chanc7b3c452018-06-19 20:31:57 -0700121import org.onosproject.segmentrouting.xconnect.api.XconnectService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700122import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700123import org.onosproject.store.service.EventuallyConsistentMap;
124import org.onosproject.store.service.EventuallyConsistentMapBuilder;
125import org.onosproject.store.service.StorageService;
126import org.onosproject.store.service.WallClockTimestamp;
Charles Chan47933752017-11-30 15:37:50 -0800127import org.osgi.service.component.ComponentContext;
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700128import org.osgi.service.component.annotations.Activate;
129import org.osgi.service.component.annotations.Component;
130import org.osgi.service.component.annotations.Deactivate;
131import org.osgi.service.component.annotations.Modified;
132import org.osgi.service.component.annotations.Reference;
133import org.osgi.service.component.annotations.ReferenceCardinality;
Charles Chan8bdb2252019-02-27 11:46:32 -0800134import org.osgi.service.component.annotations.ReferencePolicy;
sanghob35a6192015-04-01 13:05:26 -0700135import org.slf4j.Logger;
136import org.slf4j.LoggerFactory;
137
Saurav Das201762d2018-04-21 17:19:48 -0700138import java.time.Instant;
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700139import java.util.ArrayList;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800140import java.util.Collections;
Charles Chan47933752017-11-30 15:37:50 -0800141import java.util.Dictionary;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800142import java.util.HashSet;
143import java.util.List;
144import java.util.Map;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800145import java.util.Optional;
146import java.util.Set;
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200147import java.util.concurrent.CompletableFuture;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800148import java.util.concurrent.ConcurrentHashMap;
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200149import java.util.concurrent.CopyOnWriteArrayList;
Charles Chan07f15f22018-05-08 21:35:50 -0700150import java.util.concurrent.ExecutorService;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800151import java.util.concurrent.Executors;
152import java.util.concurrent.ScheduledExecutorService;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800153import java.util.concurrent.TimeUnit;
154import java.util.concurrent.atomic.AtomicBoolean;
155import java.util.stream.Collectors;
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700156import java.util.stream.IntStream;
sanghob35a6192015-04-01 13:05:26 -0700157
Charles Chan3e783d02016-02-26 22:19:52 -0800158import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800159import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700160import static org.onlab.util.Tools.groupedThreads;
Saurav Dase7f51012018-02-09 17:26:45 -0800161import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
162import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Ray Milkey3101df62018-10-23 14:23:16 -0700163import static org.onosproject.segmentrouting.OsgiPropertyConstants.ACTIVE_PROBING_DEFAULT;
164import static org.onosproject.segmentrouting.OsgiPropertyConstants.DEFAULT_INTERNAL_VLAN_DEFAULT;
165import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ACTIVE_PROBING;
166import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_DEFAULT_INTERNAL_VLAN;
167import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_PW_TRANSPORT_VLAN;
168import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_RESPOND_TO_UNKNOWN_HOSTS;
169import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ROUTE_DOUBLE_TAGGED_HOSTS;
psneha1c0d5f82019-03-26 06:31:41 -0400170import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ROUTE_SIMPLIFICATION;
Ray Milkey3101df62018-10-23 14:23:16 -0700171import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SINGLE_HOMED_DOWN;
Ray Milkeye52652c2018-11-02 17:12:21 -0700172import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SYMMETRIC_PROBING;
Ray Milkey3101df62018-10-23 14:23:16 -0700173import static org.onosproject.segmentrouting.OsgiPropertyConstants.PW_TRANSPORT_VLAN_DEFAULT;
174import static org.onosproject.segmentrouting.OsgiPropertyConstants.RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
175import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
psneha1c0d5f82019-03-26 06:31:41 -0400176import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_SIMPLIFICATION_DEFAULT;
Ray Milkey3101df62018-10-23 14:23:16 -0700177import static org.onosproject.segmentrouting.OsgiPropertyConstants.SINGLE_HOMED_DOWN_DEFAULT;
Ray Milkeye52652c2018-11-02 17:12:21 -0700178import static org.onosproject.segmentrouting.OsgiPropertyConstants.SYMMETRIC_PROBING_DEFAULT;
Charles Chan3e783d02016-02-26 22:19:52 -0800179
Charles Chane849c192016-01-11 18:28:54 -0800180/**
181 * Segment routing manager.
182 */
Ray Milkey3101df62018-10-23 14:23:16 -0700183@Component(
184 immediate = true,
185 service = SegmentRoutingService.class,
186 property = {
187 PROP_ACTIVE_PROBING + ":Boolean=" + ACTIVE_PROBING_DEFAULT,
188 PROP_SINGLE_HOMED_DOWN + ":Boolean=" + SINGLE_HOMED_DOWN_DEFAULT,
189 PROP_RESPOND_TO_UNKNOWN_HOSTS + ":Boolean=" + RESPOND_TO_UNKNOWN_HOSTS_DEFAULT,
190 PROP_ROUTE_DOUBLE_TAGGED_HOSTS + ":Boolean=" + ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT,
191 PROP_DEFAULT_INTERNAL_VLAN + ":Integer=" + DEFAULT_INTERNAL_VLAN_DEFAULT,
192 PROP_PW_TRANSPORT_VLAN + ":Integer=" + PW_TRANSPORT_VLAN_DEFAULT,
psneha1c0d5f82019-03-26 06:31:41 -0400193 PROP_SYMMETRIC_PROBING + ":Boolean=" + SYMMETRIC_PROBING_DEFAULT,
194 PROP_ROUTE_SIMPLIFICATION + ":Boolean=" + ROUTE_SIMPLIFICATION_DEFAULT
Ray Milkey3101df62018-10-23 14:23:16 -0700195 }
196)
sangho1e575652015-05-14 00:39:53 -0700197public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700198
Charles Chan2c15aca2016-11-09 20:51:44 -0800199 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan2fde6d42017-08-23 14:46:43 -0700200 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sanghob35a6192015-04-01 13:05:26 -0700201
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700202 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan03a73e02016-10-24 14:52:01 -0700203 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700204
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700205 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan61eb99e2019-03-22 10:04:27 -0700206 public NeighbourResolutionService neighbourResolutionService;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800207
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700208 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100209 public CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700210
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700211 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan03a73e02016-10-24 14:52:01 -0700212 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700213
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700214 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan03a73e02016-10-24 14:52:01 -0700215 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700216
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700217 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chanff79dd92018-06-01 16:33:48 -0700218 HostProbingService probingService;
Charles Chan47933752017-11-30 15:37:50 -0800219
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700220 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100221 public DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700222
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700223 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Saurav Das45f48152018-01-18 12:07:33 -0800224 DeviceAdminService deviceAdminService;
225
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700226 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventre42287df2016-11-09 14:17:26 -0800227 public FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700228
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700229 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100230 public LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700231
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700232 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventre42287df2016-11-09 14:17:26 -0800233 public MastershipService mastershipService;
Charles Chan03a73e02016-10-24 14:52:01 -0700234
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700235 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventre42287df2016-11-09 14:17:26 -0800236 public StorageService storageService;
Charles Chan03a73e02016-10-24 14:52:01 -0700237
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700238 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi96fe0772018-02-28 12:10:50 +0100239 public MulticastRouteService multicastRouteService;
Charles Chan03a73e02016-10-24 14:52:01 -0700240
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700241 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chanf237e1b2018-01-09 13:45:07 -0800242 public TopologyService topologyService;
Charles Chan03a73e02016-10-24 14:52:01 -0700243
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700244 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan03a73e02016-10-24 14:52:01 -0700245 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800246
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700247 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800248 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800249
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700250 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800251 public InterfaceService interfaceService;
252
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700253 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigieba73a02018-01-16 10:47:50 +0100254 public ClusterService clusterService;
255
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700256 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier96f63cb2018-04-17 16:29:56 +0200257 public WorkPartitionService workPartitionService;
Pier Luigieba73a02018-01-16 10:47:50 +0100258
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700259 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700260 public LeadershipService leadershipService;
261
Charles Chan8bdb2252019-02-27 11:46:32 -0800262 @Reference(cardinality = ReferenceCardinality.OPTIONAL,
263 bind = "bindXconnectService",
264 unbind = "unbindXconnectService",
265 policy = ReferencePolicy.DYNAMIC)
Charles Chanc7b3c452018-06-19 20:31:57 -0700266 public XconnectService xconnectService;
267
Ray Milkey3101df62018-10-23 14:23:16 -0700268 /** Enable active probing to discover dual-homed hosts. */
269 boolean activeProbing = ACTIVE_PROBING_DEFAULT;
Charles Chan47933752017-11-30 15:37:50 -0800270
Ray Milkeye52652c2018-11-02 17:12:21 -0700271 /** Enable only send probe on the same port number of the pair device. */
272 boolean symmetricProbing = SYMMETRIC_PROBING_DEFAULT;
Mayank Tiwaria61b0a82018-10-29 18:27:35 -0400273
Ray Milkey3101df62018-10-23 14:23:16 -0700274 /** Enable administratively taking down single-homed hosts. */
275 boolean singleHomedDown = SINGLE_HOMED_DOWN_DEFAULT;
Saurav Das9a554292018-04-27 18:42:30 -0700276
Ray Milkey3101df62018-10-23 14:23:16 -0700277 /** Enable this to respond to ARP/NDP requests from unknown hosts. */
278 boolean respondToUnknownHosts = RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
Andreas Pantelopoulosb2518012018-06-18 12:50:35 -0700279
Ray Milkey3101df62018-10-23 14:23:16 -0700280 /** Program flows and groups to pop and route double tagged hosts. */
281 boolean routeDoubleTaggedHosts = ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
Charles Chan57465d32018-07-19 14:55:31 -0700282
Ray Milkey3101df62018-10-23 14:23:16 -0700283 /** internal vlan assigned by default to unconfigured ports. */
284 private int defaultInternalVlan = DEFAULT_INTERNAL_VLAN_DEFAULT;
Saurav Das09c2c4d2018-08-13 15:34:26 -0700285
Ray Milkey3101df62018-10-23 14:23:16 -0700286 /** vlan used for transport of pseudowires between switches. */
287 private int pwTransportVlan = PW_TRANSPORT_VLAN_DEFAULT;
Saurav Das09c2c4d2018-08-13 15:34:26 -0700288
psneha1c0d5f82019-03-26 06:31:41 -0400289 /** Enabling route simplification. */
290 boolean routeSimplification = ROUTE_SIMPLIFICATION_DEFAULT;
291
Charles Chan03a73e02016-10-24 14:52:01 -0700292 ArpHandler arpHandler = null;
293 IcmpHandler icmpHandler = null;
294 IpHandler ipHandler = null;
295 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700296 ApplicationId appId;
297 DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700298
Charles Chan03a73e02016-10-24 14:52:01 -0700299 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700300 private TunnelHandler tunnelHandler = null;
301 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700302 private InternalPacketProcessor processor = null;
303 private InternalLinkListener linkListener = null;
304 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700305 private AppConfigHandler appCfgHandler = null;
Pier Luigi96fe0772018-02-28 12:10:50 +0100306 public XConnectHandler xConnectHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800307 McastHandler mcastHandler = null;
308 HostHandler hostHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800309 private RouteHandler routeHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800310 LinkHandler linkHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800311 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800312 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigi83f919b2018-02-15 16:33:08 +0100313 private TopologyHandler topologyHandler = null;
Charles Chan5270ed02016-01-30 23:22:37 -0800314 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700315 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
316 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700317 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigi83f919b2018-02-15 16:33:08 +0100318 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
Charles Chan50bb6ef2018-04-18 18:41:05 -0700319 private final InternalMastershipListener mastershipListener = new InternalMastershipListener();
Saurav Das201762d2018-04-21 17:19:48 -0700320 final InternalClusterListener clusterListener = new InternalClusterListener();
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200321 //Completable future for network configuration process to buffer config events handling during activation
322 private CompletableFuture<Boolean> networkConfigCompletion = null;
Ray Milkeyec9e4de2018-11-16 15:15:14 -0800323 private final Object networkConfigCompletionLock = new Object();
Charles Chan10b2fee2018-04-21 00:44:29 -0700324 private List<Event> queuedEvents = new CopyOnWriteArrayList<>();
sanghob35a6192015-04-01 13:05:26 -0700325
Charles Chan9b7217c2018-04-05 16:31:15 -0700326 // Handles device, link, topology and network config events
Charles Chan50bb6ef2018-04-18 18:41:05 -0700327 private ScheduledExecutorService mainEventExecutor;
sanghob35a6192015-04-01 13:05:26 -0700328
Charles Chan50bb6ef2018-04-18 18:41:05 -0700329 // Handles host, route and mcast events respectively
330 private ScheduledExecutorService hostEventExecutor;
331 private ScheduledExecutorService routeEventExecutor;
332 private ScheduledExecutorService mcastEventExecutor;
Charles Chan07f15f22018-05-08 21:35:50 -0700333 private ExecutorService packetExecutor;
Charles Chan64a1c8e2019-01-23 15:03:17 -0800334 ExecutorService neighborExecutor;
Charles Chan9b7217c2018-04-05 16:31:15 -0700335
336 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chane849c192016-01-11 18:28:54 -0800337 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700338 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700339 * Used to keep track on MPLS group information.
Charles Chane849c192016-01-11 18:28:54 -0800340 */
Charles Chan47933752017-11-30 15:37:50 -0800341 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das7bcbe702017-06-13 15:35:54 -0700342 dsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800343 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700344 * Per device next objective ID store with (device id + vlanid) as key.
345 * Used to keep track on L2 flood group information.
Charles Chane849c192016-01-11 18:28:54 -0800346 */
Charles Chan47933752017-11-30 15:37:50 -0800347 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan59cc16d2017-02-02 16:20:42 -0800348 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800349 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700350 * Per device next objective ID store with (device id + port + treatment + meta) as key.
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000351 * Used to keep track on L2 interface group and L3 unicast group information for direct hosts.
Charles Chane849c192016-01-11 18:28:54 -0800352 */
Charles Chan47933752017-11-30 15:37:50 -0800353 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800354 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800355
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700356 /**
357 * Per port dummy VLAN ID store with (connect point + ip address) as key.
358 * Used to keep track on dummy VLAN ID allocation.
359 */
360 private EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId>
361 dummyVlanIdStore = null;
362
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000363 /**
364 * Per device next objective ID store with (device id + MAC address + vlan) as key.
365 * Used to keep track of L3 unicast group for indirect hosts.
366 */
367 private EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer>
368 macVlanNextObjStore = null;
369
Saurav Das4ce45962015-11-24 23:21:05 -0800370 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
371 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700372
Saurav Das7bcbe702017-06-13 15:35:54 -0700373 private AtomicBoolean programmingScheduled = new AtomicBoolean();
374
Charles Chand55e84d2016-03-30 17:54:24 -0700375 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700376 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
377 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700378 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700379 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800380 public SegmentRoutingDeviceConfig createConfig() {
381 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700382 }
383 };
Pier Ventref34966c2016-11-07 16:21:04 -0800384
Charles Chand55e84d2016-03-30 17:54:24 -0700385 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700386 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
387 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700388 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800389 @Override
390 public SegmentRoutingAppConfig createConfig() {
391 return new SegmentRoutingAppConfig();
392 }
393 };
Pier Ventref34966c2016-11-07 16:21:04 -0800394
Charles Chanfc5c7802016-05-17 13:13:55 -0700395 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
396 new ConfigFactory<ApplicationId, XConnectConfig>(
397 SubjectFactories.APP_SUBJECT_FACTORY,
398 XConnectConfig.class, "xconnect") {
399 @Override
400 public XConnectConfig createConfig() {
401 return new XConnectConfig();
402 }
403 };
Pier Ventref34966c2016-11-07 16:21:04 -0800404
Charles Chand55e84d2016-03-30 17:54:24 -0700405 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700406 new ConfigFactory<ApplicationId, McastConfig>(
407 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700408 McastConfig.class, "multicast") {
409 @Override
410 public McastConfig createConfig() {
411 return new McastConfig();
412 }
413 };
414
Charles Chan116188d2016-02-18 14:22:42 -0800415 /**
416 * Segment Routing App ID.
417 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800418 public static final String APP_NAME = "org.onosproject.segmentrouting";
Saurav Das0e99e2b2015-10-28 12:39:42 -0700419
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700420 /**
421 * Minumum and maximum value of dummy VLAN ID to be allocated.
422 */
423 public static final int MIN_DUMMY_VLAN_ID = 2;
424 public static final int MAX_DUMMY_VLAN_ID = 4093;
425
Charles Chan64a1c8e2019-01-23 15:03:17 -0800426 private static final int DEFAULT_POOL_SIZE = 32;
427
Saurav Das9a554292018-04-27 18:42:30 -0700428 Instant lastEdgePortEvent = Instant.EPOCH;
429
Charles Chan8bdb2252019-02-27 11:46:32 -0800430 protected void bindXconnectService(XconnectService xconnectService) {
431 if (this.xconnectService == null) {
432 log.info("Binding XconnectService");
433 this.xconnectService = xconnectService;
434 } else {
435 log.warn("Trying to bind XconnectService but it is already bound");
436 }
437 }
438
439 protected void unbindXconnectService(XconnectService xconnectService) {
440 if (this.xconnectService == xconnectService) {
441 log.info("Unbinding XconnectService");
442 this.xconnectService = null;
443 } else {
444 log.warn("Trying to unbind XconnectService but it is already unbound");
445 }
446 }
447
sanghob35a6192015-04-01 13:05:26 -0700448 @Activate
Charles Chan47933752017-11-30 15:37:50 -0800449 protected void activate(ComponentContext context) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800450 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700451
Charles Chan64a1c8e2019-01-23 15:03:17 -0800452 mainEventExecutor = Executors.newSingleThreadScheduledExecutor(
453 groupedThreads("onos/sr", "event-main-%d", log));
454 hostEventExecutor = Executors.newSingleThreadScheduledExecutor(
455 groupedThreads("onos/sr", "event-host-%d", log));
456 routeEventExecutor = Executors.newSingleThreadScheduledExecutor(
457 groupedThreads("onos/sr", "event-route-%d", log));
458 mcastEventExecutor = Executors.newSingleThreadScheduledExecutor(
459 groupedThreads("onos/sr", "event-mcast-%d", log));
460 packetExecutor = Executors.newSingleThreadExecutor(groupedThreads("onos/sr", "packet-%d", log));
461 neighborExecutor = Executors.newFixedThreadPool(DEFAULT_POOL_SIZE,
462 groupedThreads("onos/sr", "neighbor-%d", log));
Charles Chan50bb6ef2018-04-18 18:41:05 -0700463
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700464 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das7bcbe702017-06-13 15:35:54 -0700465 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700466 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700467 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700468 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700469 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700470 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700471 .build();
Saurav Das7bcbe702017-06-13 15:35:54 -0700472 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700473
Charles Chan59cc16d2017-02-02 16:20:42 -0800474 log.debug("Creating EC map vlannextobjectivestore");
475 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
476 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
477 vlanNextObjStore = vlanNextObjMapBuilder
478 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700479 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700480 .withTimestampProvider((k, v) -> new WallClockTimestamp())
481 .build();
482
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000483 log.debug("Creating EC map macvlannextobjectivestore");
484 EventuallyConsistentMapBuilder<MacVlanNextObjectiveStoreKey, Integer>
485 macVlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
486 macVlanNextObjStore = macVlanNextObjMapBuilder
487 .withName("macvlannextobjectivestore")
488 .withSerializer(createSerializer())
489 .withTimestampProvider((k, v) -> new WallClockTimestamp())
490 .build();
491
Saurav Das4ce45962015-11-24 23:21:05 -0800492 log.debug("Creating EC map subnetnextobjectivestore");
493 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
494 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
495 portNextObjStore = portNextObjMapBuilder
496 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700497 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800498 .withTimestampProvider((k, v) -> new WallClockTimestamp())
499 .build();
500
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700501 EventuallyConsistentMapBuilder<DummyVlanIdStoreKey, VlanId>
502 dummyVlanIdMapBuilder = storageService.eventuallyConsistentMapBuilder();
503 dummyVlanIdStore = dummyVlanIdMapBuilder
504 .withName("dummyvlanidstore")
505 .withSerializer(createSerializer())
506 .withTimestampProvider((k, v) -> new WallClockTimestamp())
507 .build();
508
sangho0b2b6d12015-05-20 22:16:38 -0700509 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
510 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700511 tunnelStore = tunnelMapBuilder
512 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700513 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700514 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700515 .build();
516
517 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
518 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700519 policyStore = policyMapBuilder
520 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700521 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700522 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700523 .build();
524
Saurav Das80980c72016-03-23 11:22:49 -0700525 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800526 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700527 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800528 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800529 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
530 "requestInterceptsEnabled", "false");
Charles Chand3baaba2017-08-08 15:13:37 -0700531 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800532 "requestInterceptsEnabled", "false");
Charles Chanc760f382017-07-24 15:56:10 -0700533 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800534 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800535 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
536 "greedyLearningIpv6", "true");
Charles Chanc6d227e2017-02-28 15:15:17 -0800537 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
538 "forceUnprovision", "true");
Charles Chanef624ed2017-08-10 16:57:28 -0700539 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan73316522017-07-20 16:16:25 -0700540 "distributed", "true");
Charles Chan35a32322017-08-14 11:42:11 -0700541 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
542 "multihomingEnabled", "true");
Charles Chan807d87a2017-11-29 19:54:20 -0800543 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
544 "staleLinkAge", "15000");
545 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
546 "allowDuplicateIps", "false");
Yi Tsengfe13f3e2018-08-19 03:09:54 +0800547 // For P4 switches
548 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Charles Chanc76685f2018-08-30 17:30:45 -0700549 "fallbackFlowPollFrequency", "4");
Yi Tsengfe13f3e2018-08-19 03:09:54 +0800550 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Charles Chanc76685f2018-08-30 17:30:45 -0700551 "fallbackGroupPollFrequency", "3");
Charles Chan47933752017-11-30 15:37:50 -0800552 compCfgService.registerProperties(getClass());
553 modified(context);
Saurav Das80980c72016-03-23 11:22:49 -0700554
Charles Chanb8e10c82015-10-14 11:24:40 -0700555 processor = new InternalPacketProcessor();
556 linkListener = new InternalLinkListener();
557 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700558 appCfgHandler = new AppConfigHandler(this);
559 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700560 mcastHandler = new McastHandler(this);
561 hostHandler = new HostHandler(this);
Saurav Das45f48152018-01-18 12:07:33 -0800562 linkHandler = new LinkHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700563 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800564 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800565 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Pier Luigi83f919b2018-02-15 16:33:08 +0100566 topologyHandler = new TopologyHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700567
Charles Chan3e783d02016-02-26 22:19:52 -0800568 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700569 cfgService.registerConfigFactory(deviceConfigFactory);
570 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700571 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700572 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase7f51012018-02-09 17:26:45 -0800573 log.info("Configuring network before adding listeners");
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200574
Charles Chan132393a2018-01-04 14:26:07 -0800575 cfgListener.configureNetwork();
576
Charles Chan5270ed02016-01-30 23:22:37 -0800577 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700578 packetService.addProcessor(processor, PacketProcessor.director(2));
579 linkService.addListener(linkListener);
580 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700581 multicastRouteService.addListener(mcastListener);
Charles Chanc7a8a682017-06-19 00:43:31 -0700582 routeService.addListener(routeListener);
Pier Luigi83f919b2018-02-15 16:33:08 +0100583 topologyService.addListener(topologyListener);
Charles Chan50bb6ef2018-04-18 18:41:05 -0700584 mastershipService.addListener(mastershipListener);
Saurav Das201762d2018-04-21 17:19:48 -0700585 clusterService.addListener(clusterListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700586
Charles Chanb39777c2018-03-09 15:53:44 -0800587 linkHandler.init();
Andreas Pantelopoulos4a768c02018-01-11 07:53:48 -0800588 l2TunnelHandler.init();
589
Ray Milkeyec9e4de2018-11-16 15:15:14 -0800590 synchronized (networkConfigCompletionLock) {
591 networkConfigCompletion.whenComplete((value, ex) -> {
592 //setting to null for easier fall through
593 networkConfigCompletion = null;
594 //process all queued events
595 queuedEvents.forEach(event -> {
596 mainEventExecutor.execute(new InternalEventHandler(event));
597 });
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200598 });
Ray Milkeyec9e4de2018-11-16 15:15:14 -0800599 }
Andrea Campanella6468e1f2018-04-17 12:09:34 +0200600
sanghob35a6192015-04-01 13:05:26 -0700601 log.info("Started");
602 }
603
Saurav Das45f48152018-01-18 12:07:33 -0800604 KryoNamespace.Builder createSerializer() {
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700605 return new KryoNamespace.Builder()
606 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700607 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800608 VlanNextObjectiveStoreKey.class,
609 DestinationSet.class,
Andreas Pantelopoulos67e09ae2018-05-29 13:11:14 -0700610 DestinationSet.DestinationSetType.class,
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800611 NextNeighbors.class,
612 Tunnel.class,
613 DefaultTunnel.class,
614 Policy.class,
615 TunnelPolicy.class,
616 Policy.Type.class,
617 PortNextObjectiveStoreKey.class,
618 XConnectStoreKey.class,
619 L2Tunnel.class,
620 L2TunnelPolicy.class,
621 DefaultL2Tunnel.class,
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700622 DefaultL2TunnelPolicy.class,
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000623 DummyVlanIdStoreKey.class,
624 MacVlanNextObjectiveStoreKey.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700625 );
626 }
627
sanghob35a6192015-04-01 13:05:26 -0700628 @Deactivate
629 protected void deactivate() {
Charles Chan50bb6ef2018-04-18 18:41:05 -0700630 mainEventExecutor.shutdown();
631 hostEventExecutor.shutdown();
632 routeEventExecutor.shutdown();
633 mcastEventExecutor.shutdown();
Charles Chan07f15f22018-05-08 21:35:50 -0700634 packetExecutor.shutdown();
Charles Chan64a1c8e2019-01-23 15:03:17 -0800635 neighborExecutor.shutdown();
Charles Chan50bb6ef2018-04-18 18:41:05 -0700636
Ray Milkey749996b2018-05-11 09:59:19 -0700637 mainEventExecutor = null;
638 hostEventExecutor = null;
639 routeEventExecutor = null;
640 mcastEventExecutor = null;
641 packetExecutor = null;
Charles Chan64a1c8e2019-01-23 15:03:17 -0800642 neighborExecutor = null;
Ray Milkey749996b2018-05-11 09:59:19 -0700643
Charles Chand6832882015-10-05 17:50:33 -0700644 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700645 cfgService.unregisterConfigFactory(deviceConfigFactory);
646 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700647 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700648 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan47933752017-11-30 15:37:50 -0800649 compCfgService.unregisterProperties(getClass(), false);
Charles Chand6832882015-10-05 17:50:33 -0700650
Charles Chanc7a8a682017-06-19 00:43:31 -0700651 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700652 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700653 linkService.removeListener(linkListener);
654 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700655 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700656 routeService.removeListener(routeListener);
Pier Luigi83f919b2018-02-15 16:33:08 +0100657 topologyService.removeListener(topologyListener);
Charles Chan50bb6ef2018-04-18 18:41:05 -0700658 mastershipService.removeListener(mastershipListener);
Saurav Das201762d2018-04-21 17:19:48 -0700659 clusterService.removeListener(clusterListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700660
Charles Chan0aa674e2017-02-23 15:44:08 -0800661 neighbourResolutionService.unregisterNeighbourHandlers(appId);
662
sanghob35a6192015-04-01 13:05:26 -0700663 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700664 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700665 deviceListener = null;
Charles Chan6577f612018-04-19 13:10:01 -0700666 groupHandlerMap.forEach((k, v) -> v.shutdown());
Charles Chanb8e10c82015-10-14 11:24:40 -0700667 groupHandlerMap.clear();
Saurav Dasc6ff8f02018-04-23 18:42:12 -0700668 defaultRoutingHandler.shutdown();
Charles Chanb8e10c82015-10-14 11:24:40 -0700669
Saurav Das7bcbe702017-06-13 15:35:54 -0700670 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800671 vlanNextObjStore.destroy();
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000672 macVlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700673 portNextObjStore.destroy();
Jonghwan Hyun800d9d02018-04-09 09:40:50 -0700674 dummyVlanIdStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700675 tunnelStore.destroy();
676 policyStore.destroy();
Pier Luigib72201b2018-01-25 16:16:02 +0100677
678 mcastHandler.terminate();
sanghob35a6192015-04-01 13:05:26 -0700679 log.info("Stopped");
680 }
681
Charles Chan47933752017-11-30 15:37:50 -0800682 @Modified
683 private void modified(ComponentContext context) {
684 Dictionary<?, ?> properties = context.getProperties();
685 if (properties == null) {
686 return;
687 }
688
Ray Milkey88dd7e22018-10-24 10:04:03 -0700689 String strActiveProbing = Tools.get(properties, PROP_ACTIVE_PROBING);
Andreas Pantelopoulosb2518012018-06-18 12:50:35 -0700690 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProbing);
Charles Chan47933752017-11-30 15:37:50 -0800691 if (expectActiveProbing != activeProbing) {
692 activeProbing = expectActiveProbing;
693 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
694 }
Saurav Das9a554292018-04-27 18:42:30 -0700695
Ray Milkeye52652c2018-11-02 17:12:21 -0700696
697 String strSymmetricProbing = Tools.get(properties, PROP_SYMMETRIC_PROBING);
698 boolean expectSymmetricProbing = Boolean.parseBoolean(strSymmetricProbing);
Mayank Tiwaria61b0a82018-10-29 18:27:35 -0400699 if (expectSymmetricProbing != symmetricProbing) {
700 symmetricProbing = expectSymmetricProbing;
701 log.info("{} symmetric probing", symmetricProbing ? "Enabling" : "Disabling");
702 }
703
Ray Milkey88dd7e22018-10-24 10:04:03 -0700704 String strSingleHomedDown = Tools.get(properties, PROP_SINGLE_HOMED_DOWN);
Saurav Das9a554292018-04-27 18:42:30 -0700705 boolean expectSingleHomedDown = Boolean.parseBoolean(strSingleHomedDown);
706 if (expectSingleHomedDown != singleHomedDown) {
707 singleHomedDown = expectSingleHomedDown;
708 log.info("{} downing of single homed hosts for lost uplinks",
709 singleHomedDown ? "Enabling" : "Disabling");
710 if (singleHomedDown && linkHandler != null) {
711 hostService.getHosts().forEach(host -> host.locations()
712 .forEach(loc -> {
713 if (interfaceService.isConfigured(loc)) {
714 linkHandler.checkUplinksForHost(loc);
715 }
716 }));
717 } else {
718 log.warn("Disabling singleHomedDown does not re-enable already "
719 + "downed ports for single-homed hosts");
720 }
721 }
Andreas Pantelopoulosb2518012018-06-18 12:50:35 -0700722
Ray Milkey88dd7e22018-10-24 10:04:03 -0700723 String strRespondToUnknownHosts = Tools.get(properties, PROP_RESPOND_TO_UNKNOWN_HOSTS);
Andreas Pantelopoulosb2518012018-06-18 12:50:35 -0700724 boolean expectRespondToUnknownHosts = Boolean.parseBoolean(strRespondToUnknownHosts);
725 if (expectRespondToUnknownHosts != respondToUnknownHosts) {
726 respondToUnknownHosts = expectRespondToUnknownHosts;
727 log.info("{} responding to ARPs/NDPs from unknown hosts", respondToUnknownHosts ? "Enabling" : "Disabling");
728 }
Charles Chan57465d32018-07-19 14:55:31 -0700729
Ray Milkey88dd7e22018-10-24 10:04:03 -0700730 String strRouteDoubleTaggedHosts = Tools.get(properties, PROP_ROUTE_DOUBLE_TAGGED_HOSTS);
Charles Chanb67dfdd2018-07-24 16:40:35 -0700731 boolean expectRouteDoubleTaggedHosts = Boolean.parseBoolean(strRouteDoubleTaggedHosts);
732 if (expectRouteDoubleTaggedHosts != routeDoubleTaggedHosts) {
733 routeDoubleTaggedHosts = expectRouteDoubleTaggedHosts;
734 log.info("{} routing for double tagged hosts", routeDoubleTaggedHosts ? "Enabling" : "Disabling");
Charles Chan57465d32018-07-19 14:55:31 -0700735
Charles Chanb67dfdd2018-07-24 16:40:35 -0700736 if (routeDoubleTaggedHosts) {
Charles Chan57465d32018-07-19 14:55:31 -0700737 hostHandler.populateAllDoubleTaggedHost();
738 } else {
739 hostHandler.revokeAllDoubleTaggedHost();
740 }
741 }
Saurav Das09c2c4d2018-08-13 15:34:26 -0700742
Ray Milkey88dd7e22018-10-24 10:04:03 -0700743 String strDefaultInternalVlan = Tools.get(properties, PROP_DEFAULT_INTERNAL_VLAN);
Saurav Das09c2c4d2018-08-13 15:34:26 -0700744 int defIntVlan = Integer.parseInt(strDefaultInternalVlan);
745 if (defIntVlan != defaultInternalVlan) {
746 if (canUseVlanId(defIntVlan)) {
747 log.warn("Default internal vlan value changed from {} to {}.. "
748 + "re-programming filtering rules, but NOT any groups already "
749 + "created with the former value", defaultInternalVlan, defIntVlan);
750 VlanId oldDefIntVlan = VlanId.vlanId((short) defaultInternalVlan);
751 defaultInternalVlan = defIntVlan;
752 routingRulePopulator
753 .updateSpecialVlanFilteringRules(true, oldDefIntVlan,
754 VlanId.vlanId((short) defIntVlan));
755 } else {
756 log.warn("Cannot change default internal vlan to unusable "
757 + "value {}", defIntVlan);
758 }
759 }
760
Ray Milkey88dd7e22018-10-24 10:04:03 -0700761 String strPwTxpVlan = Tools.get(properties, PROP_PW_TRANSPORT_VLAN);
Saurav Das09c2c4d2018-08-13 15:34:26 -0700762 int pwTxpVlan = Integer.parseInt(strPwTxpVlan);
763 if (pwTxpVlan != pwTransportVlan) {
764 if (canUseVlanId(pwTxpVlan)) {
765 log.warn("Pseudowire transport vlan value changed from {} to {}.. "
766 + "re-programming filtering rules, but NOT any groups already "
767 + "created with the former value", pwTransportVlan,
768 pwTxpVlan);
769 VlanId oldPwTxpVlan = VlanId.vlanId((short) pwTransportVlan);
770 pwTransportVlan = pwTxpVlan;
771 routingRulePopulator
772 .updateSpecialVlanFilteringRules(false, oldPwTxpVlan,
773 VlanId.vlanId((short) pwTxpVlan));
774 } else {
775 log.warn("Cannot change pseudowire transport vlan to unusable "
776 + "value {}", pwTxpVlan);
777 }
778 }
779
psneha1c0d5f82019-03-26 06:31:41 -0400780 String strRouteSimplification = Tools.get(properties, PROP_ROUTE_SIMPLIFICATION);
781 boolean expectRouteSimplification = Boolean.parseBoolean(strRouteSimplification);
782 if (expectRouteSimplification != routeSimplification) {
783 routeSimplification = expectRouteSimplification;
784 log.info("{} route simplification", routeSimplification ? "Enabling" : "Disabling");
785 }
786
Saurav Das09c2c4d2018-08-13 15:34:26 -0700787 }
788
789 /**
790 * Returns true if given vlan id is not being used in the system currently,
791 * either as one of the default system wide vlans or as one of the
792 * configured interface vlans.
793 *
794 * @param vlanId given vlan id
795 * @return true if vlan is not currently in use
796 */
797 public boolean canUseVlanId(int vlanId) {
798 if (vlanId >= 4095 || vlanId <= 1) {
799 log.error("Vlan id {} value is not in valid range 2 <--> 4094",
800 vlanId);
801 return false;
802 }
803
804 VlanId vid = VlanId.vlanId((short) vlanId);
805 if (getDefaultInternalVlan().equals(vid) || getPwTransportVlan().equals(vid)) {
806 log.warn("Vlan id {} value is already in use system-wide. "
807 + "DefaultInternalVlan:{} PwTransportVlan:{} ", vlanId,
808 getDefaultInternalVlan(), getPwTransportVlan());
809 return false;
810 }
811
812 if (interfaceService.inUse(vid)) {
813 log.warn("Vlan id {} value is already in use on a configured "
814 + "interface in the system", vlanId);
815 return false;
816 }
817 return true;
818 }
819
820 /**
821 * Returns the VlanId assigned internally by default to unconfigured ports.
822 *
823 * @return the default internal vlan id
824 */
825 public VlanId getDefaultInternalVlan() {
826 return VlanId.vlanId((short) defaultInternalVlan);
827 }
828
829 /**
830 * Returns the Vlan id used to transport pseudowire traffic across the
831 * network.
832 *
833 * @return the pseudowire transport vlan id
834 */
835 public VlanId getPwTransportVlan() {
836 return VlanId.vlanId((short) pwTransportVlan);
Charles Chan47933752017-11-30 15:37:50 -0800837 }
838
sangho1e575652015-05-14 00:39:53 -0700839 @Override
840 public List<Tunnel> getTunnels() {
841 return tunnelHandler.getTunnels();
842 }
843
844 @Override
sangho71abe1b2015-06-29 14:58:47 -0700845 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
846 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700847 }
848
849 @Override
sangho71abe1b2015-06-29 14:58:47 -0700850 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700851 for (Policy policy: policyHandler.getPolicies()) {
852 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
853 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
854 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
855 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700856 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700857 }
858 }
859 }
sangho71abe1b2015-06-29 14:58:47 -0700860 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700861 }
862
863 @Override
sangho71abe1b2015-06-29 14:58:47 -0700864 public PolicyHandler.Result removePolicy(Policy policy) {
865 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700866 }
867
868 @Override
sangho71abe1b2015-06-29 14:58:47 -0700869 public PolicyHandler.Result createPolicy(Policy policy) {
870 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700871 }
872
873 @Override
874 public List<Policy> getPolicies() {
875 return policyHandler.getPolicies();
876 }
877
Saurav Das59232cf2016-04-27 18:35:50 -0700878 @Override
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700879 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
880 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700881 }
882
883 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800884 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700885 return l2TunnelHandler.getL2Tunnels();
886 }
887
888 @Override
Andreas Pantelopoulosb21547d2018-02-22 12:32:42 -0800889 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700890 return l2TunnelHandler.getL2Policies();
891 }
892
Saurav Das5a356042018-04-06 20:16:01 -0700893 @Override
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700894 @Deprecated
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700895 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
896
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700897 // get both added and pending pseudowires
898 List<L2TunnelDescription> pseudowires = new ArrayList<>();
899 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
900 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700901 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulos811bbae2018-03-15 16:56:09 -0700902
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700903 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
904
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700905 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
906 L2TunnelHandler.Result res;
907 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
908 res = addPseudowire(pw);
909 if (res != L2TunnelHandler.Result.SUCCESS) {
910 log.error("Pseudowire with id {} can not be instantiated !", res);
911 retRes = res;
912 }
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700913 }
Andreas Pantelopoulosffe69742018-03-20 13:58:49 -0700914
915 return retRes;
Andreas Pantelopoulosff691b72018-03-12 16:30:20 -0700916 }
917
918 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800919 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700920 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700921 }
922
923 @Override
Andreas Pantelopouloscd339592018-02-23 14:18:00 -0800924 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos935d59d2018-03-21 16:44:18 -0700925 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700926 }
927
928 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700929 public void rerouteNetwork() {
930 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700931 }
932
Charles Chanc81c45b2016-10-20 17:02:44 -0700933 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800934 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
935 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800936 deviceConfiguration.getRouters().forEach(device ->
937 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chanc81c45b2016-10-20 17:02:44 -0700938 return deviceSubnetMap;
939 }
940
Saurav Dasc88d4662017-05-15 15:34:25 -0700941
942 @Override
943 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
944 if (defaultRoutingHandler != null) {
945 return defaultRoutingHandler.getCurrentEmcpSpgMap();
946 } else {
947 return null;
948 }
949 }
950
951 @Override
Charles Chand5814aa2018-08-19 19:21:46 -0700952 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDstNextObjStore() {
Saurav Das7bcbe702017-06-13 15:35:54 -0700953 if (dsNextObjStore != null) {
954 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700955 } else {
956 return ImmutableMap.of();
957 }
958 }
959
Saurav Dasceccf242017-08-03 18:30:35 -0700960 @Override
Charles Chand5814aa2018-08-19 19:21:46 -0700961 public ImmutableMap<VlanNextObjectiveStoreKey, Integer> getVlanNextObjStore() {
962 if (vlanNextObjStore != null) {
963 return ImmutableMap.copyOf(vlanNextObjStore.entrySet());
964 } else {
965 return ImmutableMap.of();
966 }
967 }
968
969 @Override
Ruchi Sahotaef0761c2019-01-28 01:08:18 +0000970 public ImmutableMap<MacVlanNextObjectiveStoreKey, Integer> getMacVlanNextObjStore() {
971 if (macVlanNextObjStore != null) {
972 return ImmutableMap.copyOf(macVlanNextObjStore.entrySet());
973 } else {
974 return ImmutableMap.of();
975 }
976 }
977
978 @Override
Charles Chand5814aa2018-08-19 19:21:46 -0700979 public ImmutableMap<PortNextObjectiveStoreKey, Integer> getPortNextObjStore() {
980 if (portNextObjStore != null) {
981 return ImmutableMap.copyOf(portNextObjStore.entrySet());
982 } else {
983 return ImmutableMap.of();
984 }
985 }
986
987 @Override
988 public ImmutableMap<String, NextObjective> getPwInitNext() {
989 if (l2TunnelHandler != null) {
990 return l2TunnelHandler.getInitNext();
991 } else {
992 return ImmutableMap.of();
993 }
994 }
995
996 @Override
997 public ImmutableMap<String, NextObjective> getPwTermNext() {
998 if (l2TunnelHandler != null) {
999 return l2TunnelHandler.getTermNext();
1000 } else {
1001 return ImmutableMap.of();
1002 }
1003 }
1004
1005 @Override
1006 public void invalidateNextObj(int nextId) {
1007 if (dsNextObjStore != null) {
1008 dsNextObjStore.entrySet().forEach(e -> {
1009 if (e.getValue().nextId() == nextId) {
1010 dsNextObjStore.remove(e.getKey());
1011 }
1012 });
1013 }
1014 if (vlanNextObjStore != null) {
1015 vlanNextObjStore.entrySet().forEach(e -> {
1016 if (e.getValue() == nextId) {
1017 vlanNextObjStore.remove(e.getKey());
1018 }
1019 });
1020 }
Ruchi Sahotaef0761c2019-01-28 01:08:18 +00001021 if (macVlanNextObjStore != null) {
1022 macVlanNextObjStore.entrySet().forEach(e -> {
1023 if (e.getValue() == nextId) {
1024 macVlanNextObjStore.remove(e.getKey());
1025 }
1026 });
1027 }
Charles Chand5814aa2018-08-19 19:21:46 -07001028 if (portNextObjStore != null) {
1029 portNextObjStore.entrySet().forEach(e -> {
1030 if (e.getValue() == nextId) {
1031 portNextObjStore.remove(e.getKey());
1032 }
1033 });
1034 }
1035 if (mcastHandler != null) {
1036 mcastHandler.removeNextId(nextId);
1037 }
1038 if (l2TunnelHandler != null) {
1039 l2TunnelHandler.removeNextId(nextId);
1040 }
1041 if (xconnectService != null) {
1042 xconnectService.removeNextId(nextId);
1043 }
1044 }
1045
1046 @Override
Saurav Dasceccf242017-08-03 18:30:35 -07001047 public void verifyGroups(DeviceId id) {
1048 DefaultGroupHandler gh = groupHandlerMap.get(id);
1049 if (gh != null) {
1050 gh.triggerBucketCorrector();
1051 }
1052 }
1053
Saurav Dasc568c342018-01-25 09:49:01 -08001054 @Override
1055 public ImmutableMap<Link, Boolean> getSeenLinks() {
1056 return linkHandler.getSeenLinks();
1057 }
1058
1059 @Override
1060 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
1061 return linkHandler.getDownedPorts();
1062 }
1063
Pier Luigib29144d2018-01-15 18:06:43 +01001064 @Override
1065 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
Charles Chand5814aa2018-08-19 19:21:46 -07001066 return mcastHandler.getNextIds(mcastIp);
Pier Luigib29144d2018-01-15 18:06:43 +01001067 }
1068
1069 @Override
Pier Luigi96fe0772018-02-28 12:10:50 +01001070 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigib29144d2018-01-15 18:06:43 +01001071 return mcastHandler.getMcastRoles(mcastIp);
1072 }
1073
1074 @Override
Pier3e793752018-04-19 16:47:06 +02001075 public Map<McastRoleStoreKey, McastRole> getMcastRoles(IpAddress mcastIp, ConnectPoint sourcecp) {
1076 return mcastHandler.getMcastRoles(mcastIp, sourcecp);
1077 }
1078
1079 @Override
Pier Luigib29144d2018-01-15 18:06:43 +01001080 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
1081 return mcastHandler.getMcastPaths(mcastIp);
1082 }
1083
Pier96f63cb2018-04-17 16:29:56 +02001084 @Override
Pierb1fe7382018-04-17 17:25:22 +02001085 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
1086 ConnectPoint sourcecp) {
1087 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
1088 }
1089
1090 @Override
Pier96f63cb2018-04-17 16:29:56 +02001091 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
1092 return mcastHandler.getMcastLeaders(mcastIp);
1093 }
1094
Charles Chan8bc75ee2018-04-17 18:56:53 -07001095 @Override
1096 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
1097 return defaultRoutingHandler == null ? ImmutableMap.of() :
1098 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
1099 }
1100
1101 @Override
1102 public Map<DeviceId, Boolean> getShouldProgramCache() {
1103 return defaultRoutingHandler == null ? ImmutableMap.of() :
1104 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
1105 }
1106
Charles Chanc7b3c452018-06-19 20:31:57 -07001107 @Override
jayakumarthazhath655b9a82018-10-01 00:51:54 +05301108 public boolean shouldProgram(DeviceId deviceId) {
1109 return defaultRoutingHandler.shouldProgram(deviceId);
1110 }
1111
1112 @Override
Ray Milkeye4afdb52017-04-05 09:42:04 -07001113 public ApplicationId appId() {
1114 return appId;
1115 }
1116
1117 /**
1118 * Returns the device configuration.
1119 *
1120 * @return device configuration
1121 */
1122 public DeviceConfiguration deviceConfiguration() {
1123 return deviceConfiguration;
1124 }
1125
1126 /**
Saurav Das7bcbe702017-06-13 15:35:54 -07001127 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -07001128 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -07001129 *
1130 * @return next objective ID store
1131 */
Saurav Das7bcbe702017-06-13 15:35:54 -07001132 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
1133 dsNextObjStore() {
1134 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -07001135 }
1136
1137 /**
Charles Chan5bc32632017-08-22 15:07:34 -07001138 * Per device next objective ID store with (device id + vlanid) as key.
1139 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -07001140 *
1141 * @return vlan next object store
1142 */
1143 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
1144 return vlanNextObjStore;
1145 }
1146
1147 /**
Ruchi Sahotaef0761c2019-01-28 01:08:18 +00001148 * Per device next objective ID store with (device id + MAC address + vlan) as key.
1149 * Used to keep track on L3 Unicast group information for indirect hosts.
1150 *
1151 * @return mac vlan next object store
1152 */
1153 public EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer> macVlanNextObjStore() {
1154 return macVlanNextObjStore;
1155 }
1156
1157 /**
Charles Chan5bc32632017-08-22 15:07:34 -07001158 * Per device next objective ID store with (device id + port + treatment + meta) as key.
Ruchi Sahotaef0761c2019-01-28 01:08:18 +00001159 * Used to keep track on L2 interface group and L3 unicast group information for direct hosts.
Ray Milkeye4afdb52017-04-05 09:42:04 -07001160 *
1161 * @return port next object store.
1162 */
1163 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
1164 return portNextObjStore;
1165 }
1166
1167 /**
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001168 * Per port dummy VLAN ID store with (connect point + ip address) as key.
1169 * Used to keep track on dummy VLAN ID allocation.
1170 *
1171 * @return dummy vlan id store.
1172 */
1173 public EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId> dummyVlanIdStore() {
1174 return dummyVlanIdStore;
1175 }
1176
1177 /**
Saurav Das7bcbe702017-06-13 15:35:54 -07001178 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
1179 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -07001180 *
1181 * @return MPLS-ECMP value
1182 */
1183 public boolean getMplsEcmp() {
1184 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
1185 .getConfig(this.appId, SegmentRoutingAppConfig.class);
1186 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
1187 }
1188
1189 /**
sanghof9d0bf12015-05-19 11:57:42 -07001190 * Returns the tunnel object with the tunnel ID.
1191 *
1192 * @param tunnelId Tunnel ID
1193 * @return Tunnel reference
1194 */
sangho1e575652015-05-14 00:39:53 -07001195 public Tunnel getTunnel(String tunnelId) {
1196 return tunnelHandler.getTunnel(tunnelId);
1197 }
1198
Charles Chanc7b3c452018-06-19 20:31:57 -07001199 @Override
Pier Luigi96fe0772018-02-28 12:10:50 +01001200 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chan971d7ba2018-05-01 11:50:20 -07001201 VlanId untaggedVlanId = interfaceService.getUntaggedVlanId(connectPoint);
1202 VlanId nativeVlanId = interfaceService.getNativeVlanId(connectPoint);
Charles Chanf9a52702017-06-16 15:19:24 -07001203 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
1204 }
1205
Charles Chanc7b3c452018-06-19 20:31:57 -07001206 @Override
1207 public Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
Charles Chan65238242017-06-22 18:03:14 -07001208 SegmentRoutingDeviceConfig deviceConfig =
1209 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1210 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
1211 }
Charles Chanc7b3c452018-06-19 20:31:57 -07001212
1213 @Override
Saurav Das9a554292018-04-27 18:42:30 -07001214 public Optional<PortNumber> getPairLocalPort(DeviceId deviceId) {
Charles Chan65238242017-06-22 18:03:14 -07001215 SegmentRoutingDeviceConfig deviceConfig =
1216 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1217 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
1218 }
1219
1220 /**
Charles Chan9640c812017-08-23 13:55:39 -07001221 * Returns locations of given resolved route.
1222 *
1223 * @param resolvedRoute resolved route
1224 * @return locations of nexthop. Might be empty if next hop is not found
1225 */
1226 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
1227 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
1228 return Optional.ofNullable(hostService.getHost(hostId))
1229 .map(Host::locations).orElse(Sets.newHashSet())
1230 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
1231 }
1232
1233 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -08001234 * Returns vlan port map of given device.
1235 *
1236 * @param deviceId device id
1237 * @return vlan-port multimap
1238 */
1239 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
1240 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
1241
1242 interfaceService.getInterfaces().stream()
1243 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
1244 .forEach(intf -> {
1245 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -07001246 intf.vlanTagged().forEach(vlanTagged ->
1247 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
1248 );
Charles Chan7ffd81f2017-02-08 15:52:08 -08001249 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
1250 });
1251 vlanPortMap.removeAll(VlanId.NONE);
1252
1253 return vlanPortMap;
1254 }
1255
1256 /**
Charles Chan59cc16d2017-02-02 16:20:42 -08001257 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -08001258 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -07001259 *
1260 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -08001261 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -08001262 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -07001263 */
Charles Chan65238242017-06-22 18:03:14 -07001264 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -07001265 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -08001266 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
1267 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001268 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -08001269 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -08001270 + "device {} not found", deviceId);
1271 return -1;
1272 }
1273 }
1274
1275 /**
1276 * Returns the next objective ID for the given portNumber, given the treatment.
1277 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -07001278 * would result in different objectives. If the next object does not exist,
1279 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -08001280 *
1281 * @param deviceId Device ID
1282 * @param portNum port number on device for which NextObjective is queried
1283 * @param treatment the actions to apply on the packets (should include outport)
1284 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -07001285 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -07001286 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -08001287 */
1288 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
1289 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -07001290 TrafficSelector meta,
1291 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -08001292 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1293 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -07001294 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -08001295 } else {
Charles Chane849c192016-01-11 18:28:54 -08001296 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
1297 + " not found", deviceId);
1298 return -1;
1299 }
1300 }
1301
Saurav Dasc88d4662017-05-15 15:34:25 -07001302 /**
Ruchi Sahotaef0761c2019-01-28 01:08:18 +00001303 * Returns the next Objective ID for the given mac and vlan, given the treatment.
1304 * There could be multiple different treatments to the same outport, which
1305 * would result in different objectives. If the next object does not exist,
1306 * and should be created, a new one is created and its id is returned.
1307 *
1308 * @param deviceId Device ID
1309 * @param macAddr mac of host for which Next ID is required.
1310 * @param vlanId vlan of host for which Next ID is required.
1311 * @param treatment the actions to apply on the packets (should include outport)
1312 * @param meta metadata passed into the creation of a Next Objective if necessary
1313 * @param createIfMissing true if a next object should be created if not found
1314 * @return next objective ID or -1 if an error occurred during retrieval or creation
1315 */
1316 public int getMacVlanNextObjectiveId(DeviceId deviceId, MacAddress macAddr, VlanId vlanId,
1317 TrafficTreatment treatment,
1318 TrafficSelector meta,
1319 boolean createIfMissing) {
1320 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1321 if (ghdlr != null) {
1322 return ghdlr.getMacVlanNextObjectiveId(macAddr, vlanId, treatment, meta, createIfMissing);
1323 } else {
1324 log.warn("getMacVlanNextObjectiveId query - groupHandler for device {}"
1325 + " not found", deviceId);
1326 return -1;
1327 }
1328 }
1329
1330 /**
1331 * Returns the next ID for the given host port from the store.
1332 *
1333 * @param deviceId Device ID
1334 * @param hostMac mac of host for which Next ID is required.
1335 * @param hostVlanId vlan of host for which Next ID is required.
1336 * @param port port of device for which next ID is required.
1337 * @return next objective ID or -1 if an error occurred during retrieval
1338 */
1339 public int getNextIdForHostPort(DeviceId deviceId, MacAddress hostMac,
1340 VlanId hostVlanId, PortNumber port) {
1341 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1342 if (ghdlr != null) {
1343 return ghdlr.getNextIdForHostPort(hostMac, hostVlanId, port);
1344 } else {
1345 log.warn("getNextIdForHostPort query - groupHandler for device {}"
1346 + " not found", deviceId);
1347 return -1;
1348 }
1349 }
1350
1351 /**
1352 * Updates the next objective for the given nextId .
1353 *
1354 * @param deviceId Device ID
1355 * @param hostMac mac of host for which Next obj is to be updated.
1356 * @param hostVlanId vlan of host for which Next obj is to be updated.
1357 * @param port port with which to update the Next Obj.
1358 * @param nextId of Next Obj which needs to be updated.
1359 */
1360 public void updateMacVlanTreatment(DeviceId deviceId, MacAddress hostMac,
1361 VlanId hostVlanId, PortNumber port, int nextId) {
1362 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1363 if (ghdlr != null) {
1364 ghdlr.updateL3UcastGroupBucket(hostMac, hostVlanId, port, nextId);
1365 } else {
1366 log.warn("updateL3UcastGroupBucket query - groupHandler for device {}"
1367 + " not found", deviceId);
1368 }
1369 }
1370
1371
1372 /**
Saurav Dasc88d4662017-05-15 15:34:25 -07001373 * Returns the group handler object for the specified device id.
1374 *
1375 * @param devId the device identifier
1376 * @return the groupHandler object for the device id, or null if not found
1377 */
Charles Chan65238242017-06-22 18:03:14 -07001378 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001379 return groupHandlerMap.get(devId);
1380 }
1381
1382 /**
Saurav Dasceccf242017-08-03 18:30:35 -07001383 * Returns the default routing handler object.
1384 *
1385 * @return the default routing handler object
1386 */
1387 public DefaultRoutingHandler getRoutingHandler() {
1388 return defaultRoutingHandler;
1389 }
1390
Jonghwan Hyun800d9d02018-04-09 09:40:50 -07001391 /**
1392 * Returns new dummy VLAN ID.
1393 * Dummy VLAN ID should be unique in each connect point.
1394 *
1395 * @param cp connect point
1396 * @param ipAddress IP address
1397 * @return new dummy VLAN ID. Returns VlanId.NONE if no VLAN ID is available.
1398 */
1399 public synchronized VlanId allocateDummyVlanId(ConnectPoint cp, IpAddress ipAddress) {
1400 Set<VlanId> usedVlanId = Sets.union(getVlanPortMap(cp.deviceId()).keySet(),
1401 dummyVlanIdStore.entrySet().stream().filter(entry ->
1402 (entry.getKey()).connectPoint().equals(cp))
1403 .map(Map.Entry::getValue)
1404 .collect(Collectors.toSet()));
1405
1406 VlanId dummyVlanId = IntStream.range(MIN_DUMMY_VLAN_ID, MAX_DUMMY_VLAN_ID).mapToObj(
1407 i -> VlanId.vlanId((short) (i & 0xFFFF))
1408 ).filter(vlanId -> !usedVlanId.contains(vlanId)).findFirst().orElse(VlanId.NONE);
1409
1410 if (!dummyVlanId.equals(VlanId.NONE)) {
1411 this.dummyVlanIdStore.put(new DummyVlanIdStoreKey(cp, ipAddress), dummyVlanId);
1412 log.debug("Dummy VLAN ID {} is allocated to {}, {}", dummyVlanId, cp, ipAddress);
1413 } else {
1414 log.error("Failed to allocate dummy VLAN ID for {}, {}", cp, ipAddress);
1415 }
1416 return dummyVlanId;
1417 }
1418
1419
sanghob35a6192015-04-01 13:05:26 -07001420 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -07001421 @Override
1422 public void process(PacketContext context) {
Charles Chan07f15f22018-05-08 21:35:50 -07001423 packetExecutor.execute(() -> processPacketInternal(context));
1424 }
sanghob35a6192015-04-01 13:05:26 -07001425
Charles Chan07f15f22018-05-08 21:35:50 -07001426 private void processPacketInternal(PacketContext context) {
sanghob35a6192015-04-01 13:05:26 -07001427 if (context.isHandled()) {
1428 return;
1429 }
1430
1431 InboundPacket pkt = context.inPacket();
1432 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -08001433
1434 if (ethernet == null) {
1435 return;
1436 }
1437
Saurav Das7bcbe702017-06-13 15:35:54 -07001438 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1439 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001440 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001441 log.warn("Received unexpected ARP packet on {}",
1442 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -07001443 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -08001444 return;
sanghob35a6192015-04-01 13:05:26 -07001445 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -08001446 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1447 //ipHandler.addToPacketBuffer(ipv4Packet);
1448 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1449 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001450 } else {
Charles Chan50035632017-01-13 17:20:44 -08001451 // NOTE: We don't support IP learning at this moment so this
1452 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -08001453 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001454 }
Pier Ventre10bd8d12016-11-26 21:05:22 -08001455 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1456 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -08001457 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -08001458 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -08001459 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1460 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1461 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1462 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1463 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1464 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -07001465 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -07001466 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -08001467 }
1468 } else {
1469 // NOTE: We don't support IP learning at this moment so this
1470 // is not necessary. Also it causes duplication of DHCPv6 packets.
1471 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1472 }
sanghob35a6192015-04-01 13:05:26 -07001473 }
1474 }
1475 }
1476
sanghob35a6192015-04-01 13:05:26 -07001477 private class InternalEventHandler implements Runnable {
Charles Chan9b7217c2018-04-05 16:31:15 -07001478 private Event event;
1479
1480 InternalEventHandler(Event event) {
1481 this.event = event;
1482 }
1483
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -07001484 @Override
sanghob35a6192015-04-01 13:05:26 -07001485 public void run() {
Charles Chan9b7217c2018-04-05 16:31:15 -07001486 try {
1487 // TODO We should also change SR routing and PW to listen to TopologyEvents
1488 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1489 event.type() == LinkEvent.Type.LINK_UPDATED) {
1490 linkHandler.processLinkAdded((Link) event.subject());
1491 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1492 linkHandler.processLinkRemoved((Link) event.subject());
1493 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1494 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1495 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1496 DeviceId deviceId = ((Device) event.subject()).id();
1497 if (deviceService.isAvailable(deviceId)) {
1498 log.info("** DEVICE UP Processing device event {} "
1499 + "for available device {}",
1500 event.type(), ((Device) event.subject()).id());
1501 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001502 } else {
Charles Chan7040b692018-08-29 21:21:51 -07001503 if (event.type() == DeviceEvent.Type.DEVICE_ADDED) {
1504 // Note: For p4 devices, the device will be added but unavailable at the beginning.
1505 // The device will later on being marked as available once the pipeline is pushed
1506 // to the device.
1507 log.info("** DEVICE ADDED but unavailable. Ignore");
1508 return;
1509 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001510 log.info(" ** DEVICE DOWN Processing device event {}"
1511 + " for unavailable device {}",
1512 event.type(), ((Device) event.subject()).id());
1513 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001514 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001515 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1516 // typically these calls come when device is added first time
1517 // so port filtering rules are handled at the device_added event.
1518 // port added calls represent all ports on the device,
1519 // enabled or not.
1520 log.trace("** PORT ADDED {}/{} -> {}",
1521 ((DeviceEvent) event).subject().id(),
1522 ((DeviceEvent) event).port().number(),
1523 event.type());
1524 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1525 // these calls happen for every subsequent event
1526 // ports enabled, disabled, switch goes away, comes back
1527 log.info("** PORT UPDATED {}/{} -> {}",
1528 event.subject(),
1529 ((DeviceEvent) event).port(),
1530 event.type());
Saurav Das9a554292018-04-27 18:42:30 -07001531 processPortUpdatedInternal(((Device) event.subject()),
Charles Chan9b7217c2018-04-05 16:31:15 -07001532 ((DeviceEvent) event).port());
1533 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1534 // Process topology event, needed for all modules relying on
1535 // topology service for path computation
1536 TopologyEvent topologyEvent = (TopologyEvent) event;
1537 log.info("Processing topology event {}, topology age {}, reasons {}",
1538 event.type(), topologyEvent.subject().time(),
1539 topologyEvent.reasons().size());
1540 topologyHandler.processTopologyChange(topologyEvent.reasons());
1541 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1542 hostHandler.processHostAddedEvent((HostEvent) event);
1543 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1544 hostHandler.processHostMovedEvent((HostEvent) event);
1545 routeHandler.processHostMovedEvent((HostEvent) event);
1546 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1547 hostHandler.processHostRemovedEvent((HostEvent) event);
1548 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1549 hostHandler.processHostUpdatedEvent((HostEvent) event);
1550 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1551 routeHandler.processRouteAdded((RouteEvent) event);
1552 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1553 routeHandler.processRouteUpdated((RouteEvent) event);
1554 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1555 routeHandler.processRouteRemoved((RouteEvent) event);
1556 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1557 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1558 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1559 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1560 event.type() == McastEvent.Type.SINKS_ADDED ||
1561 event.type() == McastEvent.Type.SINKS_REMOVED ||
1562 event.type() == McastEvent.Type.ROUTE_ADDED ||
1563 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1564 mcastHandler.processMcastEvent((McastEvent) event);
1565 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1566 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1567 Class configClass = netcfgEvent.configClass();
1568 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1569 appCfgHandler.processAppConfigAdded(netcfgEvent);
1570 log.info("App config event .. configuring network");
1571 cfgListener.configureNetwork();
1572 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1573 log.info("Segment Routing Device Config added for {}", event.subject());
1574 cfgListener.configureNetwork();
1575 } else if (configClass.equals(XConnectConfig.class)) {
1576 xConnectHandler.processXConnectConfigAdded(netcfgEvent);
1577 } else if (configClass.equals(InterfaceConfig.class)) {
1578 log.info("Interface Config added for {}", event.subject());
1579 cfgListener.configureNetwork();
1580 } else {
1581 log.error("Unhandled config class: {}", configClass);
1582 }
1583 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1584 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1585 Class configClass = netcfgEvent.configClass();
1586 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1587 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1588 log.info("App config event .. configuring network");
1589 cfgListener.configureNetwork();
1590 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1591 log.info("Segment Routing Device Config updated for {}", event.subject());
1592 createOrUpdateDeviceConfiguration();
1593 } else if (configClass.equals(XConnectConfig.class)) {
1594 xConnectHandler.processXConnectConfigUpdated(netcfgEvent);
1595 } else if (configClass.equals(InterfaceConfig.class)) {
1596 log.info("Interface Config updated for {}", event.subject());
1597 createOrUpdateDeviceConfiguration();
1598 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1599 (InterfaceConfig) netcfgEvent.prevConfig().get());
1600 } else {
1601 log.error("Unhandled config class: {}", configClass);
1602 }
1603 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1604 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1605 Class configClass = netcfgEvent.configClass();
1606 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1607 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1608 log.info("App config event .. configuring network");
1609 cfgListener.configureNetwork();
1610 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1611 // TODO Handle sr device config removal
1612 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
1613 } else if (configClass.equals(XConnectConfig.class)) {
1614 xConnectHandler.processXConnectConfigRemoved(netcfgEvent);
1615 } else if (configClass.equals(InterfaceConfig.class)) {
1616 // TODO Handle interface removal
1617 log.info("InterfaceConfig removal is not handled in current implementation");
1618 } else {
1619 log.error("Unhandled config class: {}", configClass);
1620 }
Saurav Das201762d2018-04-21 17:19:48 -07001621 } else if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
1622 MastershipEvent me = (MastershipEvent) event;
1623 DeviceId deviceId = me.subject();
1624 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1625 log.info(" ** MASTERSHIP CHANGED Invalidating shouldProgram cache"
1626 + " for {}/pair={} due to change", deviceId, pairDeviceId);
1627 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1628 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1629 defaultRoutingHandler.checkFullRerouteForMasterChange(deviceId, me);
Charles Chan9b7217c2018-04-05 16:31:15 -07001630 } else {
1631 log.warn("Unhandled event type: {}", event.type());
sanghob35a6192015-04-01 13:05:26 -07001632 }
Charles Chan9b7217c2018-04-05 16:31:15 -07001633 } catch (Exception e) {
1634 log.error("SegmentRouting event handler thread thrown an exception: {}",
1635 e.getMessage(), e);
sanghob35a6192015-04-01 13:05:26 -07001636 }
sanghob35a6192015-04-01 13:05:26 -07001637 }
1638 }
1639
Saurav Das45f48152018-01-18 12:07:33 -08001640 void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001641 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001642
1643 // NOTE: Punt ARP/NDP even when the device is not configured.
1644 // Host learning without network config is required for CORD config generator.
1645 routingRulePopulator.populateIpPunts(device.id());
1646 routingRulePopulator.populateArpNdpPunts(device.id());
1647
Charles Chan0b4e6182015-11-03 10:42:14 -08001648 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001649 log.warn("Device configuration unavailable. Device {} will be "
1650 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001651 return;
1652 }
Charles Chan2199c302016-04-23 17:36:10 -07001653 processDeviceAddedInternal(device.id());
1654 }
1655
1656 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001657 // Irrespective of whether the local is a MASTER or not for this device,
1658 // we need to create a SR-group-handler instance. This is because in a
1659 // multi-instance setup, any instance can initiate forwarding/next-objectives
1660 // for any switch (even if this instance is a SLAVE or not even connected
1661 // to the switch). To handle this, a default-group-handler instance is necessary
1662 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001663 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1664 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001665 DefaultGroupHandler groupHandler;
1666 try {
1667 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001668 createGroupHandler(deviceId,
1669 appId,
1670 deviceConfiguration,
1671 linkService,
1672 flowObjectiveService,
1673 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001674 } catch (DeviceConfigNotFoundException e) {
1675 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1676 return;
1677 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001678 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001679 deviceId);
1680 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001681 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001682
Charles Chan2199c302016-04-23 17:36:10 -07001683 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001684 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001685 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001686 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001687 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001688 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001689 }
Charles Chan5270ed02016-01-30 23:22:37 -08001690
Charles Chan03a73e02016-10-24 14:52:01 -07001691 appCfgHandler.init(deviceId);
Charles Chan94549652018-04-29 18:11:37 -07001692 hostEventExecutor.execute(() -> hostHandler.init(deviceId));
1693 routeEventExecutor.execute(() -> routeHandler.init(deviceId));
sanghob35a6192015-04-01 13:05:26 -07001694 }
1695
Saurav Das80980c72016-03-23 11:22:49 -07001696 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001697 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001698 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan47933752017-11-30 15:37:50 -08001699 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001700 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001701 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001702 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Ruchi Sahotaef0761c2019-01-28 01:08:18 +00001703 macVlanNextObjStore.entrySet().stream()
1704 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1705 .forEach(entry -> macVlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001706 portNextObjStore.entrySet().stream()
1707 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001708 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Das45f48152018-01-18 12:07:33 -08001709 linkHandler.processDeviceRemoved(device);
Charles Chaned3742352017-06-15 00:44:51 -07001710
Saurav Dasceccf242017-08-03 18:30:35 -07001711 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1712 if (gh != null) {
1713 gh.shutdown();
1714 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001715 // Note that a switch going down is associated with all of its links
1716 // going down as well, but it is treated as a single switch down event
Saurav Das5a356042018-04-06 20:16:01 -07001717 // while the link-downs are ignored. We cannot rely on the ordering of
1718 // events - i.e we cannot expect all link-downs to come before the
1719 // switch down - so we purge all seen-links for the switch before
1720 // handling route-path changes for the switch-down
Saurav Dasc88d4662017-05-15 15:34:25 -07001721 defaultRoutingHandler
Saurav Das604ab3a2018-03-18 21:28:15 -07001722 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Dasc568c342018-01-25 09:49:01 -08001723 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001724 xConnectHandler.removeDevice(device.id());
Saurav Dasa4020382018-02-14 14:14:54 -08001725
1726 // Cleanup all internal groupHandler stores for this device. Should be
1727 // done after all rerouting or rehashing has been completed
1728 groupHandlerMap.entrySet()
1729 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Das80980c72016-03-23 11:22:49 -07001730 }
1731
Saurav Dasc6ff8f02018-04-23 18:42:12 -07001732 /**
1733 * Purge the destinationSet nextObjective store of entries with this device
1734 * as key. Erases app-level knowledge of hashed groups in this device.
1735 *
1736 * @param devId the device identifier
1737 */
Saurav Das201762d2018-04-21 17:19:48 -07001738 void purgeHashedNextObjectiveStore(DeviceId devId) {
Saurav Dasc6ff8f02018-04-23 18:42:12 -07001739 log.debug("Purging hashed next-obj store for dev:{}", devId);
Saurav Das201762d2018-04-21 17:19:48 -07001740 dsNextObjStore.entrySet().stream()
1741 .filter(entry -> entry.getKey().deviceId().equals(devId))
1742 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
1743 }
1744
Saurav Das9a554292018-04-27 18:42:30 -07001745 private void processPortUpdatedInternal(Device device, Port port) {
Saurav Das1a129a02016-11-18 15:21:57 -08001746 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1747 log.warn("Device configuration uploading. Not handling port event for"
1748 + "dev: {} port: {}", device.id(), port.number());
1749 return;
1750 }
Saurav Das018605f2017-02-18 14:05:44 -08001751
Saurav Das9a554292018-04-27 18:42:30 -07001752 if (interfaceService.isConfigured(new ConnectPoint(device.id(), port.number()))) {
1753 lastEdgePortEvent = Instant.now();
1754 }
1755
Saurav Das018605f2017-02-18 14:05:44 -08001756 if (!mastershipService.isLocalMaster(device.id())) {
1757 log.debug("Not master for dev:{} .. not handling port updated event"
1758 + "for port {}", device.id(), port.number());
1759 return;
1760 }
Saurav Das9a554292018-04-27 18:42:30 -07001761 processPortUpdated(device.id(), port);
1762 }
Saurav Das018605f2017-02-18 14:05:44 -08001763
Saurav Das9a554292018-04-27 18:42:30 -07001764 /**
1765 * Adds or remove filtering rules for the given switchport. If switchport is
1766 * an edge facing port, additionally handles host probing and broadcast
1767 * rules. Must be called by local master of device.
1768 *
1769 * @param deviceId the device identifier
1770 * @param port the port to update
1771 */
1772 void processPortUpdated(DeviceId deviceId, Port port) {
Saurav Das018605f2017-02-18 14:05:44 -08001773 // first we handle filtering rules associated with the port
1774 if (port.isEnabled()) {
1775 log.info("Switchport {}/{} enabled..programming filters",
Saurav Das9a554292018-04-27 18:42:30 -07001776 deviceId, port.number());
1777 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001778 } else {
1779 log.info("Switchport {}/{} disabled..removing filters",
Saurav Das9a554292018-04-27 18:42:30 -07001780 deviceId, port.number());
1781 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001782 }
Saurav Das1a129a02016-11-18 15:21:57 -08001783
1784 // portUpdated calls are for ports that have gone down or up. For switch
1785 // to switch ports, link-events should take care of any re-routing or
1786 // group editing necessary for port up/down. Here we only process edge ports
1787 // that are already configured.
Saurav Das9a554292018-04-27 18:42:30 -07001788 ConnectPoint cp = new ConnectPoint(deviceId, port.number());
Charles Chan971d7ba2018-05-01 11:50:20 -07001789 VlanId untaggedVlan = interfaceService.getUntaggedVlanId(cp);
1790 VlanId nativeVlan = interfaceService.getNativeVlanId(cp);
1791 Set<VlanId> taggedVlans = interfaceService.getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001792
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001793 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001794 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das9a554292018-04-27 18:42:30 -07001795 + "dev/port: {}/{}", deviceId, port.number());
Saurav Das1a129a02016-11-18 15:21:57 -08001796 return;
1797 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001798 if (untaggedVlan != null) {
Saurav Das9a554292018-04-27 18:42:30 -07001799 processEdgePort(deviceId, port, untaggedVlan, true);
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001800 }
1801 if (nativeVlan != null) {
Saurav Das9a554292018-04-27 18:42:30 -07001802 processEdgePort(deviceId, port, nativeVlan, true);
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001803 }
1804 if (!taggedVlans.isEmpty()) {
Saurav Das9a554292018-04-27 18:42:30 -07001805 taggedVlans.forEach(tag -> processEdgePort(deviceId, port, tag, false));
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001806 }
Saurav Das1a129a02016-11-18 15:21:57 -08001807 }
1808
Saurav Das9a554292018-04-27 18:42:30 -07001809 private void processEdgePort(DeviceId deviceId, Port port, VlanId vlanId,
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001810 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001811 boolean portUp = port.isEnabled();
1812 if (portUp) {
Saurav Das9a554292018-04-27 18:42:30 -07001813 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", deviceId,
Charles Chan59cc16d2017-02-02 16:20:42 -08001814 port.number(), vlanId);
Charles Chan94549652018-04-29 18:11:37 -07001815 hostEventExecutor.execute(() -> hostHandler.processPortUp(new ConnectPoint(deviceId, port.number())));
Saurav Das1a129a02016-11-18 15:21:57 -08001816 } else {
Saurav Das9a554292018-04-27 18:42:30 -07001817 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", deviceId,
Charles Chan59cc16d2017-02-02 16:20:42 -08001818 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001819 }
1820
Saurav Das9a554292018-04-27 18:42:30 -07001821 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001822 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001823 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001824 } else {
1825 log.warn("Group handler not found for dev:{}. Not handling edge port"
Saurav Das9a554292018-04-27 18:42:30 -07001826 + " {} event for port:{}", deviceId,
Saurav Das1a129a02016-11-18 15:21:57 -08001827 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001828 }
1829 }
sangho1e575652015-05-14 00:39:53 -07001830
Charles Chan27fe1a52017-10-20 16:06:55 -07001831 private void createOrUpdateDeviceConfiguration() {
1832 if (deviceConfiguration == null) {
Saurav Dase7f51012018-02-09 17:26:45 -08001833 log.info("Creating new DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001834 deviceConfiguration = new DeviceConfiguration(this);
1835 } else {
Saurav Dase7f51012018-02-09 17:26:45 -08001836 log.info("Updating DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001837 deviceConfiguration.updateConfig();
1838 }
1839 }
1840
Charles Chan50bb6ef2018-04-18 18:41:05 -07001841 private void createOrUpdateDefaultRoutingHandler() {
1842 if (defaultRoutingHandler == null) {
1843 log.info("Creating new DefaultRoutingHandler");
1844 defaultRoutingHandler = new DefaultRoutingHandler(this);
1845 } else {
1846 log.info("Updating DefaultRoutingHandler");
1847 defaultRoutingHandler.update(this);
1848 }
1849 }
1850
Pier Ventre10bd8d12016-11-26 21:05:22 -08001851 /**
1852 * Registers the given connect point with the NRS, this is necessary
1853 * to receive the NDP and ARP packets from the NRS.
1854 *
1855 * @param portToRegister connect point to register
1856 */
1857 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001858 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001859 portToRegister,
1860 neighbourHandler,
1861 appId
1862 );
1863 }
1864
Charles Chand6832882015-10-05 17:50:33 -07001865 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001866 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001867 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001868
Charles Chane849c192016-01-11 18:28:54 -08001869 /**
1870 * Constructs the internal network config listener.
1871 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001872 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001873 */
Charles Chan65238242017-06-22 18:03:14 -07001874 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001875 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001876 }
1877
Charles Chane849c192016-01-11 18:28:54 -08001878 /**
1879 * Reads network config and initializes related data structure accordingly.
1880 */
Charles Chan47933752017-11-30 15:37:50 -08001881 void configureNetwork() {
Saurav Dase7f51012018-02-09 17:26:45 -08001882 log.info("Configuring network ...");
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001883
1884 // Setting handling of network configuration events completable future
1885 // The completable future is needed because of the async behaviour of the configureNetwork,
1886 // listener registration and event arrival
1887 // Enables us to buffer the events and execute them when the configure network is done.
Ray Milkeyec9e4de2018-11-16 15:15:14 -08001888 synchronized (networkConfigCompletionLock) {
1889 networkConfigCompletion = new CompletableFuture<>();
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001890
Ray Milkeyec9e4de2018-11-16 15:15:14 -08001891 // add a small delay to absorb multiple network config added notifications
1892 if (!programmingScheduled.get()) {
1893 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
1894 programmingScheduled.set(true);
1895 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
1896 }
1897
1898 createOrUpdateDeviceConfiguration();
1899
1900 arpHandler = new ArpHandler(srManager);
1901 icmpHandler = new IcmpHandler(srManager);
1902 ipHandler = new IpHandler(srManager);
1903 routingRulePopulator = new RoutingRulePopulator(srManager);
1904 createOrUpdateDefaultRoutingHandler();
1905
1906 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1907 groupHandlerMap, tunnelStore);
1908 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1909 flowObjectiveService,
1910 tunnelHandler, policyStore);
1911 networkConfigCompletion.complete(true);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001912 }
1913
Charles Chan2199c302016-04-23 17:36:10 -07001914 mcastHandler.init();
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001915
Charles Chan4636be02015-10-07 14:21:45 -07001916 }
1917
Charles Chand6832882015-10-05 17:50:33 -07001918 @Override
1919 public void event(NetworkConfigEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001920 if (mainEventExecutor == null) {
1921 return;
1922 }
Charles Chan7a888e82018-03-21 16:57:47 -07001923 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
1924 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1925 switch (event.type()) {
1926 case CONFIG_ADDED:
1927 case CONFIG_UPDATED:
1928 case CONFIG_REMOVED:
1929 log.trace("Schedule Network Config event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001930 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1931 mainEventExecutor.execute(new InternalEventHandler(event));
1932 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001933 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001934 }
Charles Chan7a888e82018-03-21 16:57:47 -07001935 break;
1936 default:
1937 break;
Charles Chand6832882015-10-05 17:50:33 -07001938 }
1939 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001940
Charles Chan50443e82018-01-03 16:26:32 -08001941 @Override
1942 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase7f51012018-02-09 17:26:45 -08001943 if (event.type() == CONFIG_REGISTERED ||
1944 event.type() == CONFIG_UNREGISTERED) {
1945 log.debug("Ignore event {} due to type mismatch", event);
1946 return false;
Charles Chan50443e82018-01-03 16:26:32 -08001947 }
Saurav Dase7f51012018-02-09 17:26:45 -08001948
1949 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1950 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1951 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscd339592018-02-23 14:18:00 -08001952 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase7f51012018-02-09 17:26:45 -08001953 log.debug("Ignore event {} due to class mismatch", event);
1954 return false;
1955 }
1956
1957 return true;
Charles Chan50443e82018-01-03 16:26:32 -08001958 }
1959
Saurav Das7bcbe702017-06-13 15:35:54 -07001960 private final class ConfigChange implements Runnable {
1961 @Override
1962 public void run() {
1963 programmingScheduled.set(false);
Saurav Dase7f51012018-02-09 17:26:45 -08001964 log.info("Reacting to config changes after buffer delay");
Saurav Das7bcbe702017-06-13 15:35:54 -07001965 for (Device device : deviceService.getDevices()) {
1966 processDeviceAdded(device);
1967 }
1968 defaultRoutingHandler.startPopulationProcess();
1969 }
1970 }
Charles Chand6832882015-10-05 17:50:33 -07001971 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001972
Charles Chan7a888e82018-03-21 16:57:47 -07001973 private class InternalLinkListener implements LinkListener {
1974 @Override
1975 public void event(LinkEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001976 if (mainEventExecutor == null) {
1977 return;
1978 }
Charles Chan7a888e82018-03-21 16:57:47 -07001979 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1980 event.type() == LinkEvent.Type.LINK_UPDATED ||
1981 event.type() == LinkEvent.Type.LINK_REMOVED) {
1982 log.trace("Schedule Link event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001983 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1984 mainEventExecutor.execute(new InternalEventHandler(event));
1985 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07001986 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02001987 }
Charles Chan7a888e82018-03-21 16:57:47 -07001988 }
1989 }
1990 }
1991
1992 private class InternalDeviceListener implements DeviceListener {
1993 @Override
1994 public void event(DeviceEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07001995 if (mainEventExecutor == null) {
1996 return;
1997 }
Charles Chan7a888e82018-03-21 16:57:47 -07001998 switch (event.type()) {
1999 case DEVICE_ADDED:
2000 case PORT_UPDATED:
2001 case PORT_ADDED:
2002 case DEVICE_UPDATED:
2003 case DEVICE_AVAILABILITY_CHANGED:
2004 log.trace("Schedule Device event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02002005 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
2006 mainEventExecutor.execute(new InternalEventHandler(event));
2007 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07002008 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02002009 }
Charles Chan7a888e82018-03-21 16:57:47 -07002010 break;
2011 default:
2012 }
2013 }
2014 }
2015
2016 private class InternalTopologyListener implements TopologyListener {
2017 @Override
2018 public void event(TopologyEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07002019 if (mainEventExecutor == null) {
2020 return;
2021 }
Charles Chan7a888e82018-03-21 16:57:47 -07002022 switch (event.type()) {
2023 case TOPOLOGY_CHANGED:
2024 log.trace("Schedule Topology event {}", event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02002025 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
2026 mainEventExecutor.execute(new InternalEventHandler(event));
2027 } else {
Charles Chan10b2fee2018-04-21 00:44:29 -07002028 queuedEvents.add(event);
Andrea Campanella6468e1f2018-04-17 12:09:34 +02002029 }
Charles Chan7a888e82018-03-21 16:57:47 -07002030 break;
2031 default:
2032 }
2033 }
2034 }
2035
Charles Chan68aa62d2015-11-09 16:37:23 -08002036 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08002037 @Override
2038 public void event(HostEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07002039 if (hostEventExecutor == null) {
2040 return;
2041 }
Charles Chan68aa62d2015-11-09 16:37:23 -08002042 switch (event.type()) {
2043 case HOST_ADDED:
Charles Chan68aa62d2015-11-09 16:37:23 -08002044 case HOST_MOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08002045 case HOST_REMOVED:
Charles Chan68aa62d2015-11-09 16:37:23 -08002046 case HOST_UPDATED:
Charles Chan7a888e82018-03-21 16:57:47 -07002047 log.trace("Schedule Host event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07002048 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chan68aa62d2015-11-09 16:37:23 -08002049 break;
2050 default:
2051 log.warn("Unsupported host event type: {}", event.type());
2052 break;
2053 }
2054 }
2055 }
2056
Charles Chand55e84d2016-03-30 17:54:24 -07002057 private class InternalMcastListener implements McastListener {
2058 @Override
2059 public void event(McastEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07002060 if (mcastEventExecutor == null) {
2061 return;
2062 }
Charles Chand55e84d2016-03-30 17:54:24 -07002063 switch (event.type()) {
Pier3ee24552018-03-14 16:47:32 -07002064 case SOURCES_ADDED:
2065 case SOURCES_REMOVED:
2066 case SINKS_ADDED:
2067 case SINKS_REMOVED:
Charles Chand55e84d2016-03-30 17:54:24 -07002068 case ROUTE_REMOVED:
Pier96f63cb2018-04-17 16:29:56 +02002069 case ROUTE_ADDED:
Charles Chan7a888e82018-03-21 16:57:47 -07002070 log.trace("Schedule Mcast event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07002071 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi9930da52018-02-02 16:19:11 +01002072 break;
Charles Chand55e84d2016-03-30 17:54:24 -07002073 default:
Charles Chan7a888e82018-03-21 16:57:47 -07002074 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chand55e84d2016-03-30 17:54:24 -07002075 break;
2076 }
2077 }
2078 }
Charles Chan35fd1a72016-06-13 18:54:31 -07002079
Charles Chan03a73e02016-10-24 14:52:01 -07002080 private class InternalRouteEventListener implements RouteListener {
2081 @Override
2082 public void event(RouteEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07002083 if (routeEventExecutor == null) {
2084 return;
2085 }
Charles Chan03a73e02016-10-24 14:52:01 -07002086 switch (event.type()) {
2087 case ROUTE_ADDED:
Charles Chan03a73e02016-10-24 14:52:01 -07002088 case ROUTE_UPDATED:
Charles Chan03a73e02016-10-24 14:52:01 -07002089 case ROUTE_REMOVED:
Charles Chan2fde6d42017-08-23 14:46:43 -07002090 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan7a888e82018-03-21 16:57:47 -07002091 log.trace("Schedule Route event {}", event);
Charles Chan4e49df82018-04-07 11:35:08 -07002092 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan2fde6d42017-08-23 14:46:43 -07002093 break;
Charles Chan03a73e02016-10-24 14:52:01 -07002094 default:
Charles Chan7a888e82018-03-21 16:57:47 -07002095 log.warn("Unsupported route event type: {}", event.type());
Charles Chan03a73e02016-10-24 14:52:01 -07002096 break;
2097 }
2098 }
2099 }
Saurav Dasc88d4662017-05-15 15:34:25 -07002100
Charles Chan50bb6ef2018-04-18 18:41:05 -07002101 private class InternalMastershipListener implements MastershipListener {
2102 @Override
2103 public void event(MastershipEvent event) {
Ray Milkey749996b2018-05-11 09:59:19 -07002104 if (mainEventExecutor == null) {
2105 return;
2106 }
Charles Chan50bb6ef2018-04-18 18:41:05 -07002107 switch (event.type()) {
Saurav Das201762d2018-04-21 17:19:48 -07002108 case MASTER_CHANGED:
2109 log.debug("Mastership event: {}/{}", event.subject(),
2110 event.roleInfo());
2111 mainEventExecutor.execute(new InternalEventHandler(event));
2112 break;
2113 case BACKUPS_CHANGED:
2114 case SUSPENDED:
2115 default:
2116 log.debug("Mastership event type {} not handled", event.type());
2117 break;
Charles Chan50bb6ef2018-04-18 18:41:05 -07002118 }
2119 }
2120 }
2121
Saurav Das201762d2018-04-21 17:19:48 -07002122 class InternalClusterListener implements ClusterEventListener {
2123 private Instant lastClusterEvent = Instant.EPOCH;
2124
2125 long timeSinceLastClusterEvent() {
2126 return Instant.now().toEpochMilli() - lastClusterEvent.toEpochMilli();
2127 }
2128
2129 @Override
2130 public void event(ClusterEvent event) {
2131 switch (event.type()) {
2132 case INSTANCE_ACTIVATED:
2133 case INSTANCE_ADDED:
2134 case INSTANCE_READY:
2135 log.debug("Cluster event {} ignored", event.type());
2136 break;
2137 case INSTANCE_DEACTIVATED:
2138 case INSTANCE_REMOVED:
2139 log.info("** Cluster event {}", event.type());
2140 lastClusterEvent = Instant.now();
2141 break;
2142 default:
2143 break;
2144 }
2145
2146 }
2147
2148 }
2149
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002150 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
2151 try {
2152 Set<Interface> intfs = conf.getInterfaces();
2153 Set<Interface> prevIntfs = prevConf.getInterfaces();
2154
2155 // Now we only handle one interface config at each port.
2156 if (intfs.size() != 1 || prevIntfs.size() != 1) {
2157 log.warn("Interface update aborted - one at a time is allowed, " +
2158 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
2159 return;
2160 }
2161
Andrea Campanella6468e1f2018-04-17 12:09:34 +02002162 //The system is in an incoherent state, abort
2163 if (defaultRoutingHandler == null) {
2164 log.warn("Interface update aborted, defaultRoutingHandler is null");
2165 return;
2166 }
2167
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002168 Interface intf = intfs.stream().findFirst().get();
2169 Interface prevIntf = prevIntfs.stream().findFirst().get();
2170
2171 DeviceId deviceId = intf.connectPoint().deviceId();
2172 PortNumber portNum = intf.connectPoint().port();
2173
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002174 removeSubnetConfig(prevIntf.connectPoint(),
2175 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
2176 new HashSet<>(intf.ipAddressesList())));
2177
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002178 if (!prevIntf.vlanNative().equals(VlanId.NONE)
2179 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
2180 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
2181 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
2182 // Update filtering objective and L2IG group bucket
2183 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
2184 } else {
2185 // RemoveVlanNative
2186 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
2187 }
2188 }
2189
2190 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
2191 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2192 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2193 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
2194 // Update filtering objective and L2IG group bucket
2195 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
2196 } else {
2197 // RemoveVlanUntagged
2198 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
2199 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002200 }
2201
2202 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2203 // RemoveVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002204 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
2205 .filter(i -> !intf.vlanUntagged().equals(i))
2206 .filter(i -> !intf.vlanNative().equals(i))
2207 .forEach(vlanId -> updateVlanConfigInternal(
2208 deviceId, portNum, vlanId, false, false));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002209 }
2210
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002211 if (!intf.vlanNative().equals(VlanId.NONE)
2212 && !prevIntf.vlanNative().equals(intf.vlanNative())
2213 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2214 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
2215 // Update filtering objective and L2IG group bucket
2216 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
2217 } else {
2218 // AddVlanNative
2219 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
2220 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002221 }
2222
2223 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2224 // AddVlanTagged
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002225 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
2226 .filter(i -> !prevIntf.vlanUntagged().equals(i))
2227 .filter(i -> !prevIntf.vlanNative().equals(i))
2228 .forEach(vlanId -> updateVlanConfigInternal(
2229 deviceId, portNum, vlanId, false, true)
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002230 );
2231 }
2232
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002233 if (!intf.vlanUntagged().equals(VlanId.NONE)
2234 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2235 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
2236 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
2237 // Update filtering objective and L2IG group bucket
2238 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
2239 } else {
2240 // AddVlanUntagged
2241 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
2242 }
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002243 }
2244 addSubnetConfig(prevIntf.connectPoint(),
2245 Sets.difference(new HashSet<>(intf.ipAddressesList()),
2246 new HashSet<>(prevIntf.ipAddressesList())));
2247 } catch (ConfigException e) {
2248 log.error("Error in configuration");
2249 }
2250 }
2251
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002252 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
2253 VlanId vlanId, boolean pushVlan) {
2254 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2255 if (grpHandler == null) {
2256 log.warn("Failed to retrieve group handler for device {}", deviceId);
2257 return;
2258 }
2259
2260 // Update filtering objective for a single port
2261 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
2262 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
2263
2264 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
2265 // Update L2IG bucket of the port
2266 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
2267 } else {
2268 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2269 }
2270 }
2271
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002272 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
2273 VlanId vlanId, boolean pushVlan, boolean install) {
2274 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2275 if (grpHandler == null) {
2276 log.warn("Failed to retrieve group handler for device {}", deviceId);
2277 return;
2278 }
2279
2280 // Update filtering objective for a single port
2281 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
2282
2283 // Update filtering objective for multicast ingress port
2284 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
2285
2286 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
2287
2288 if (nextId != -1 && !install) {
2289 // Update next objective for a single port as an output port
2290 // Remove a single port from L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002291 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002292 // Remove L2 Bridging rule and L3 Unicast rule to the host
Charles Chan94549652018-04-29 18:11:37 -07002293 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2294 vlanId, pushVlan, install));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002295 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
2296 // only if there is no port configured on that VLAN ID
2297 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
2298 // Remove broadcast forwarding rule for the VLAN
2299 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2300 // Remove L2FG for VLAN
2301 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
2302 } else {
2303 // Remove L2IG of the port
2304 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
2305 }
2306 } else if (install) {
2307 if (nextId != -1) {
2308 // Add a single port to L2FG
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +09002309 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002310 } else {
2311 // Create L2FG for VLAN
2312 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
2313 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2314 }
Charles Chan94549652018-04-29 18:11:37 -07002315 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2316 vlanId, pushVlan, install));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002317 } else {
2318 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2319 }
2320 }
2321
2322 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2323 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2324 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2325
2326 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2327 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2328 .filter(intf -> !intf.connectPoint().equals(cp))
2329 .flatMap(intf -> intf.ipAddressesList().stream())
2330 .collect(Collectors.toSet());
2331 // 1. Partial subnet population
2332 // Remove routing rules for removed subnet from previous configuration,
2333 // which does not also exist in other interfaces in the same device
2334 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2335 .map(InterfaceIpAddress::subnetAddress)
2336 .collect(Collectors.toSet());
2337
2338 defaultRoutingHandler.revokeSubnet(
2339 ipPrefixSet.stream()
2340 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2341 .collect(Collectors.toSet()));
2342
2343 // 2. Interface IP punts
2344 // Remove IP punts for old Intf address
2345 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2346 .map(InterfaceIpAddress::ipAddress)
2347 .collect(Collectors.toSet());
2348 ipAddressSet.stream()
2349 .map(InterfaceIpAddress::ipAddress)
2350 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2351 .forEach(interfaceIpAddress ->
2352 routingRulePopulator.revokeSingleIpPunts(
2353 cp.deviceId(), interfaceIpAddress));
2354
2355 // 3. Host unicast routing rule
2356 // Remove unicast routing rule
Charles Chan94549652018-04-29 18:11:37 -07002357 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002358 }
2359
2360 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2361 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2362 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2363
2364 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2365 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2366 .filter(intf -> !intf.connectPoint().equals(cp))
2367 .flatMap(intf -> intf.ipAddressesList().stream())
2368 .collect(Collectors.toSet());
2369 // 1. Partial subnet population
2370 // Add routing rules for newly added subnet, which does not also exist in
2371 // other interfaces in the same device
2372 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2373 .map(InterfaceIpAddress::subnetAddress)
2374 .collect(Collectors.toSet());
2375
2376 defaultRoutingHandler.populateSubnet(
2377 Collections.singleton(cp),
2378 ipPrefixSet.stream()
2379 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2380 .collect(Collectors.toSet()));
2381
2382 // 2. Interface IP punts
2383 // Add IP punts for new Intf address
2384 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2385 .map(InterfaceIpAddress::ipAddress)
2386 .collect(Collectors.toSet());
2387 ipAddressSet.stream()
2388 .map(InterfaceIpAddress::ipAddress)
2389 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2390 .forEach(interfaceIpAddress ->
2391 routingRulePopulator.populateSingleIpPunts(
2392 cp.deviceId(), interfaceIpAddress));
2393
2394 // 3. Host unicast routing rule
2395 // Add unicast routing rule
Charles Chan94549652018-04-29 18:11:37 -07002396 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true));
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07002397 }
sanghob35a6192015-04-01 13:05:26 -07002398}