blob: 4c66dd0b64496743223cfde4460ab4c2c185f90d [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sangho80f11cb2015-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 Hart61e24e12017-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 Chanf0ae41e2017-08-23 13:55:39 -070022import com.google.common.collect.Sets;
sangho80f11cb2015-04-01 13:05:26 -070023import org.onlab.packet.Ethernet;
Pier Ventreb6b81d52016-12-02 08:16:05 -080024import org.onlab.packet.ICMP6;
Charles Chan77277672015-10-20 16:24:19 -070025import org.onlab.packet.IPv4;
Pier Ventreb6a7f342016-11-26 21:05:22 -080026import org.onlab.packet.IPv6;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070027import org.onlab.packet.IpAddress;
Charles Chan77277672015-10-20 16:24:19 -070028import org.onlab.packet.IpPrefix;
Jonathan Hart54541d12016-04-12 15:39:44 -070029import org.onlab.packet.VlanId;
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +000030import org.onlab.packet.MacAddress;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070031import org.onlab.util.KryoNamespace;
Charles Chan873661e2017-11-30 15:37:50 -080032import org.onlab.util.Tools;
Saurav Dasc3604f12016-03-23 11:22:49 -070033import org.onosproject.cfg.ComponentConfigService;
Saurav Das00e553b2018-04-21 17:19:48 -070034import org.onosproject.cluster.ClusterEvent;
35import org.onosproject.cluster.ClusterEventListener;
Pier Luigi580fd8a2018-01-16 10:47:50 +010036import org.onosproject.cluster.ClusterService;
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -070037import org.onosproject.cluster.LeadershipService;
Pierdb27b8d2018-04-17 16:29:56 +020038import org.onosproject.cluster.NodeId;
sangho80f11cb2015-04-01 13:05:26 -070039import org.onosproject.core.ApplicationId;
40import org.onosproject.core.CoreService;
41import org.onosproject.event.Event;
Charles Chanfbcb8812018-04-18 18:41:05 -070042import org.onosproject.mastership.MastershipEvent;
43import org.onosproject.mastership.MastershipListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070044import org.onosproject.mastership.MastershipService;
Pier1f87aca2018-03-14 16:47:32 -070045import org.onosproject.mcast.api.McastEvent;
46import org.onosproject.mcast.api.McastListener;
47import org.onosproject.mcast.api.MulticastRouteService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080048import org.onosproject.net.ConnectPoint;
Jonathan Hart54541d12016-04-12 15:39:44 -070049import org.onosproject.net.Device;
50import org.onosproject.net.DeviceId;
Charles Chanf0ae41e2017-08-23 13:55:39 -070051import org.onosproject.net.Host;
52import org.onosproject.net.HostId;
Jonathan Hart54541d12016-04-12 15:39:44 -070053import org.onosproject.net.Link;
54import org.onosproject.net.Port;
Charles Chanf4586112015-11-09 16:37:23 -080055import org.onosproject.net.PortNumber;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070056import org.onosproject.net.config.ConfigException;
Charles Chan72f556a2015-10-05 17:50:33 -070057import org.onosproject.net.config.ConfigFactory;
58import org.onosproject.net.config.NetworkConfigEvent;
Charles Chan72f556a2015-10-05 17:50:33 -070059import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070060import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkeyae0068a2017-08-15 11:02:29 -070061import org.onosproject.net.config.basics.InterfaceConfig;
62import org.onosproject.net.config.basics.McastConfig;
Charles Chan72f556a2015-10-05 17:50:33 -070063import org.onosproject.net.config.basics.SubjectFactories;
Saurav Dase6c448a2018-01-18 12:07:33 -080064import org.onosproject.net.device.DeviceAdminService;
Jonathan Hart54541d12016-04-12 15:39:44 -070065import org.onosproject.net.device.DeviceEvent;
66import org.onosproject.net.device.DeviceListener;
67import org.onosproject.net.device.DeviceService;
Charles Chanf4586112015-11-09 16:37:23 -080068import org.onosproject.net.flow.TrafficSelector;
69import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hart54541d12016-04-12 15:39:44 -070070import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan0b1dd7e2018-08-19 19:21:46 -070071import org.onosproject.net.flowobjective.NextObjective;
Charles Chanf4586112015-11-09 16:37:23 -080072import org.onosproject.net.host.HostEvent;
73import org.onosproject.net.host.HostListener;
Charles Chanc6bcdf92018-06-01 16:33:48 -070074import org.onosproject.net.host.HostProbingService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080075import org.onosproject.net.host.HostService;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070076import org.onosproject.net.host.InterfaceIpAddress;
Pierdb27b8d2018-04-17 16:29:56 +020077import org.onosproject.net.intent.WorkPartitionService;
Ray Milkeyae0068a2017-08-15 11:02:29 -070078import org.onosproject.net.intf.Interface;
79import org.onosproject.net.intf.InterfaceService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080080import org.onosproject.net.link.LinkEvent;
81import org.onosproject.net.link.LinkListener;
82import org.onosproject.net.link.LinkService;
Ray Milkeyae0068a2017-08-15 11:02:29 -070083import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventreb6a7f342016-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 Luigid8a15162018-02-15 16:33:08 +010088import org.onosproject.net.topology.TopologyEvent;
89import org.onosproject.net.topology.TopologyListener;
Charles Chanc91c8782016-03-30 17:54:24 -070090import org.onosproject.net.topology.TopologyService;
Charles Chanf0ae41e2017-08-23 13:55:39 -070091import org.onosproject.routeservice.ResolvedRoute;
Ray Milkeyae0068a2017-08-15 11:02:29 -070092import org.onosproject.routeservice.RouteEvent;
93import org.onosproject.routeservice.RouteListener;
94import org.onosproject.routeservice.RouteService;
Charles Chanc91c8782016-03-30 17:54:24 -070095import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
96import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventre6b19e482016-11-07 16:21:04 -080097import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkeyae0068a2017-08-15 11:02:29 -070098import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanc91c8782016-03-30 17:54:24 -070099import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -0700100import org.onosproject.segmentrouting.grouphandler.DestinationSet;
101import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Pier Luigi69f774d2018-02-28 12:10:50 +0100102import org.onosproject.segmentrouting.mcast.McastHandler;
103import org.onosproject.segmentrouting.mcast.McastRole;
Piere99511d2018-04-19 16:47:06 +0200104import org.onosproject.segmentrouting.mcast.McastRoleStoreKey;
Ray Milkey2bd24a92018-08-17 14:54:17 -0700105import org.onosproject.segmentrouting.mcast.McastStoreKey;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700106import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700107import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800108import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700109import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800110import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkey2bd24a92018-08-17 14:54:17 -0700111import org.onosproject.segmentrouting.pwaas.L2TunnelDescription;
Ray Milkeyae0068a2017-08-15 11:02:29 -0700112import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800113import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Saurav Das261c3002017-06-13 15:35:54 -0700114import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -0700115import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan10b0fb72017-02-02 16:20:42 -0800116import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000117import org.onosproject.segmentrouting.storekey.MacVlanNextObjectiveStoreKey;
Charles Chan82f19972016-05-17 13:13:55 -0700118import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Charles Chan8d316332018-06-19 20:31:57 -0700119import org.onosproject.segmentrouting.xconnect.api.XconnectService;
Jonathan Hart54541d12016-04-12 15:39:44 -0700120import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700121import org.onosproject.store.service.EventuallyConsistentMap;
122import org.onosproject.store.service.EventuallyConsistentMapBuilder;
123import org.onosproject.store.service.StorageService;
124import org.onosproject.store.service.WallClockTimestamp;
Charles Chan873661e2017-11-30 15:37:50 -0800125import org.osgi.service.component.ComponentContext;
Ray Milkey2bd24a92018-08-17 14:54:17 -0700126import org.osgi.service.component.annotations.Activate;
127import org.osgi.service.component.annotations.Component;
128import org.osgi.service.component.annotations.Deactivate;
129import org.osgi.service.component.annotations.Modified;
130import org.osgi.service.component.annotations.Reference;
131import org.osgi.service.component.annotations.ReferenceCardinality;
Charles Chanb4f9cd12019-02-27 11:46:32 -0800132import org.osgi.service.component.annotations.ReferencePolicy;
sangho80f11cb2015-04-01 13:05:26 -0700133import org.slf4j.Logger;
134import org.slf4j.LoggerFactory;
135
Saurav Das00e553b2018-04-21 17:19:48 -0700136import java.time.Instant;
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700137import java.util.ArrayList;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800138import java.util.Collections;
Charles Chan873661e2017-11-30 15:37:50 -0800139import java.util.Dictionary;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800140import java.util.HashSet;
141import java.util.List;
142import java.util.Map;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800143import java.util.Optional;
144import java.util.Set;
Andrea Campanella060cad82018-04-17 12:09:34 +0200145import java.util.concurrent.CompletableFuture;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800146import java.util.concurrent.ConcurrentHashMap;
Andrea Campanella060cad82018-04-17 12:09:34 +0200147import java.util.concurrent.CopyOnWriteArrayList;
Charles Chan77cdde42018-05-08 21:35:50 -0700148import java.util.concurrent.ExecutorService;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800149import java.util.concurrent.Executors;
150import java.util.concurrent.ScheduledExecutorService;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800151import java.util.concurrent.TimeUnit;
152import java.util.concurrent.atomic.AtomicBoolean;
153import java.util.stream.Collectors;
sangho80f11cb2015-04-01 13:05:26 -0700154
Charles Chand6d25332016-02-26 22:19:52 -0800155import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800156import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700157import static org.onlab.util.Tools.groupedThreads;
Saurav Dase321cff2018-02-09 17:26:45 -0800158import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
159import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700160import static org.onosproject.segmentrouting.OsgiPropertyConstants.ACTIVE_PROBING_DEFAULT;
161import static org.onosproject.segmentrouting.OsgiPropertyConstants.DEFAULT_INTERNAL_VLAN_DEFAULT;
162import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ACTIVE_PROBING;
163import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_DEFAULT_INTERNAL_VLAN;
164import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_PW_TRANSPORT_VLAN;
165import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_RESPOND_TO_UNKNOWN_HOSTS;
166import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ROUTE_DOUBLE_TAGGED_HOSTS;
psneha86e60d32019-03-26 06:31:41 -0400167import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ROUTE_SIMPLIFICATION;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700168import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SINGLE_HOMED_DOWN;
Ray Milkeye96d0de2018-11-02 17:12:21 -0700169import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SYMMETRIC_PROBING;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700170import static org.onosproject.segmentrouting.OsgiPropertyConstants.PW_TRANSPORT_VLAN_DEFAULT;
171import static org.onosproject.segmentrouting.OsgiPropertyConstants.RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
172import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
psneha86e60d32019-03-26 06:31:41 -0400173import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_SIMPLIFICATION_DEFAULT;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700174import static org.onosproject.segmentrouting.OsgiPropertyConstants.SINGLE_HOMED_DOWN_DEFAULT;
Ray Milkeye96d0de2018-11-02 17:12:21 -0700175import static org.onosproject.segmentrouting.OsgiPropertyConstants.SYMMETRIC_PROBING_DEFAULT;
Charles Chand6d25332016-02-26 22:19:52 -0800176
Charles Chanb7f75ac2016-01-11 18:28:54 -0800177/**
178 * Segment routing manager.
179 */
Ray Milkey05edbfc2018-10-23 14:23:16 -0700180@Component(
181 immediate = true,
182 service = SegmentRoutingService.class,
183 property = {
184 PROP_ACTIVE_PROBING + ":Boolean=" + ACTIVE_PROBING_DEFAULT,
185 PROP_SINGLE_HOMED_DOWN + ":Boolean=" + SINGLE_HOMED_DOWN_DEFAULT,
186 PROP_RESPOND_TO_UNKNOWN_HOSTS + ":Boolean=" + RESPOND_TO_UNKNOWN_HOSTS_DEFAULT,
187 PROP_ROUTE_DOUBLE_TAGGED_HOSTS + ":Boolean=" + ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT,
188 PROP_DEFAULT_INTERNAL_VLAN + ":Integer=" + DEFAULT_INTERNAL_VLAN_DEFAULT,
189 PROP_PW_TRANSPORT_VLAN + ":Integer=" + PW_TRANSPORT_VLAN_DEFAULT,
psneha86e60d32019-03-26 06:31:41 -0400190 PROP_SYMMETRIC_PROBING + ":Boolean=" + SYMMETRIC_PROBING_DEFAULT,
191 PROP_ROUTE_SIMPLIFICATION + ":Boolean=" + ROUTE_SIMPLIFICATION_DEFAULT
Ray Milkey05edbfc2018-10-23 14:23:16 -0700192 }
193)
sangho27462c62015-05-14 00:39:53 -0700194public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700195
Charles Chan46fdfaf2016-11-09 20:51:44 -0800196 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan910be6a2017-08-23 14:46:43 -0700197 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sangho80f11cb2015-04-01 13:05:26 -0700198
Ray Milkey2bd24a92018-08-17 14:54:17 -0700199 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700200 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700201
Ray Milkey2bd24a92018-08-17 14:54:17 -0700202 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chancf789822019-03-22 10:04:27 -0700203 public NeighbourResolutionService neighbourResolutionService;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800204
Ray Milkey2bd24a92018-08-17 14:54:17 -0700205 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100206 public CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700207
Ray Milkey2bd24a92018-08-17 14:54:17 -0700208 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700209 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700210
Ray Milkey2bd24a92018-08-17 14:54:17 -0700211 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700212 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700213
Ray Milkey2bd24a92018-08-17 14:54:17 -0700214 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chanc6bcdf92018-06-01 16:33:48 -0700215 HostProbingService probingService;
Charles Chan873661e2017-11-30 15:37:50 -0800216
Ray Milkey2bd24a92018-08-17 14:54:17 -0700217 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100218 public DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700219
Ray Milkey2bd24a92018-08-17 14:54:17 -0700220 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Saurav Dase6c448a2018-01-18 12:07:33 -0800221 DeviceAdminService deviceAdminService;
222
Ray Milkey2bd24a92018-08-17 14:54:17 -0700223 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800224 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700225
Ray Milkey2bd24a92018-08-17 14:54:17 -0700226 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100227 public LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700228
Ray Milkey2bd24a92018-08-17 14:54:17 -0700229 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800230 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700231
Ray Milkey2bd24a92018-08-17 14:54:17 -0700232 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800233 public StorageService storageService;
Charles Chandebfea32016-10-24 14:52:01 -0700234
Ray Milkey2bd24a92018-08-17 14:54:17 -0700235 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100236 public MulticastRouteService multicastRouteService;
Charles Chandebfea32016-10-24 14:52:01 -0700237
Ray Milkey2bd24a92018-08-17 14:54:17 -0700238 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan83163812018-01-09 13:45:07 -0800239 public TopologyService topologyService;
Charles Chandebfea32016-10-24 14:52:01 -0700240
Ray Milkey2bd24a92018-08-17 14:54:17 -0700241 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan19b70032019-04-17 14:20:26 -0700242 public RouteService routeService;
Charles Chan82ab1932016-01-30 23:22:37 -0800243
Ray Milkey2bd24a92018-08-17 14:54:17 -0700244 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800245 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800246
Ray Milkey2bd24a92018-08-17 14:54:17 -0700247 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800248 public InterfaceService interfaceService;
249
Ray Milkey2bd24a92018-08-17 14:54:17 -0700250 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi580fd8a2018-01-16 10:47:50 +0100251 public ClusterService clusterService;
252
Ray Milkey2bd24a92018-08-17 14:54:17 -0700253 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pierdb27b8d2018-04-17 16:29:56 +0200254 public WorkPartitionService workPartitionService;
Pier Luigi580fd8a2018-01-16 10:47:50 +0100255
Ray Milkey2bd24a92018-08-17 14:54:17 -0700256 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700257 public LeadershipService leadershipService;
258
Charles Chanb4f9cd12019-02-27 11:46:32 -0800259 @Reference(cardinality = ReferenceCardinality.OPTIONAL,
260 bind = "bindXconnectService",
261 unbind = "unbindXconnectService",
262 policy = ReferencePolicy.DYNAMIC)
Charles Chan8d316332018-06-19 20:31:57 -0700263 public XconnectService xconnectService;
264
Ray Milkey05edbfc2018-10-23 14:23:16 -0700265 /** Enable active probing to discover dual-homed hosts. */
266 boolean activeProbing = ACTIVE_PROBING_DEFAULT;
Charles Chan873661e2017-11-30 15:37:50 -0800267
Ray Milkeye96d0de2018-11-02 17:12:21 -0700268 /** Enable only send probe on the same port number of the pair device. */
269 boolean symmetricProbing = SYMMETRIC_PROBING_DEFAULT;
Mayank Tiwarif7942402018-10-29 18:27:35 -0400270
Ray Milkey05edbfc2018-10-23 14:23:16 -0700271 /** Enable administratively taking down single-homed hosts. */
272 boolean singleHomedDown = SINGLE_HOMED_DOWN_DEFAULT;
Saurav Dasec683dc2018-04-27 18:42:30 -0700273
Ray Milkey05edbfc2018-10-23 14:23:16 -0700274 /** Enable this to respond to ARP/NDP requests from unknown hosts. */
275 boolean respondToUnknownHosts = RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700276
Ray Milkey05edbfc2018-10-23 14:23:16 -0700277 /** Program flows and groups to pop and route double tagged hosts. */
278 boolean routeDoubleTaggedHosts = ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
Charles Chan4eb73bb2018-07-19 14:55:31 -0700279
Ray Milkey05edbfc2018-10-23 14:23:16 -0700280 /** internal vlan assigned by default to unconfigured ports. */
281 private int defaultInternalVlan = DEFAULT_INTERNAL_VLAN_DEFAULT;
Saurav Das9bf49582018-08-13 15:34:26 -0700282
Ray Milkey05edbfc2018-10-23 14:23:16 -0700283 /** vlan used for transport of pseudowires between switches. */
284 private int pwTransportVlan = PW_TRANSPORT_VLAN_DEFAULT;
Saurav Das9bf49582018-08-13 15:34:26 -0700285
psneha86e60d32019-03-26 06:31:41 -0400286 /** Enabling route simplification. */
287 boolean routeSimplification = ROUTE_SIMPLIFICATION_DEFAULT;
288
Charles Chandebfea32016-10-24 14:52:01 -0700289 ArpHandler arpHandler = null;
290 IcmpHandler icmpHandler = null;
291 IpHandler ipHandler = null;
292 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700293 ApplicationId appId;
294 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700295
Charles Chandebfea32016-10-24 14:52:01 -0700296 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700297 private TunnelHandler tunnelHandler = null;
298 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700299 private InternalPacketProcessor processor = null;
300 private InternalLinkListener linkListener = null;
301 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700302 private AppConfigHandler appCfgHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800303 McastHandler mcastHandler = null;
304 HostHandler hostHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800305 private RouteHandler routeHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800306 LinkHandler linkHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800307 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800308 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigid8a15162018-02-15 16:33:08 +0100309 private TopologyHandler topologyHandler = null;
Charles Chan82ab1932016-01-30 23:22:37 -0800310 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700311 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
312 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chandebfea32016-10-24 14:52:01 -0700313 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigid8a15162018-02-15 16:33:08 +0100314 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
Charles Chanfbcb8812018-04-18 18:41:05 -0700315 private final InternalMastershipListener mastershipListener = new InternalMastershipListener();
Saurav Das00e553b2018-04-21 17:19:48 -0700316 final InternalClusterListener clusterListener = new InternalClusterListener();
Andrea Campanella060cad82018-04-17 12:09:34 +0200317 //Completable future for network configuration process to buffer config events handling during activation
318 private CompletableFuture<Boolean> networkConfigCompletion = null;
Ray Milkey521e4392018-11-16 15:15:14 -0800319 private final Object networkConfigCompletionLock = new Object();
Charles Chan39b75522018-04-21 00:44:29 -0700320 private List<Event> queuedEvents = new CopyOnWriteArrayList<>();
sangho80f11cb2015-04-01 13:05:26 -0700321
Charles Chan52003922018-04-05 16:31:15 -0700322 // Handles device, link, topology and network config events
Charles Chanfbcb8812018-04-18 18:41:05 -0700323 private ScheduledExecutorService mainEventExecutor;
sangho80f11cb2015-04-01 13:05:26 -0700324
Charles Chanfbcb8812018-04-18 18:41:05 -0700325 // Handles host, route and mcast events respectively
326 private ScheduledExecutorService hostEventExecutor;
327 private ScheduledExecutorService routeEventExecutor;
328 private ScheduledExecutorService mcastEventExecutor;
Charles Chan77cdde42018-05-08 21:35:50 -0700329 private ExecutorService packetExecutor;
Charles Chana7601c92019-01-23 15:03:17 -0800330 ExecutorService neighborExecutor;
Charles Chan52003922018-04-05 16:31:15 -0700331
332 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chanb7f75ac2016-01-11 18:28:54 -0800333 /**
Saurav Das261c3002017-06-13 15:35:54 -0700334 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700335 * Used to keep track on MPLS group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800336 */
Charles Chan873661e2017-11-30 15:37:50 -0800337 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das261c3002017-06-13 15:35:54 -0700338 dsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800339 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700340 * Per device next objective ID store with (device id + vlanid) as key.
341 * Used to keep track on L2 flood group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800342 */
Charles Chan873661e2017-11-30 15:37:50 -0800343 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800344 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800345 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700346 * Per device next objective ID store with (device id + port + treatment + meta) as key.
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000347 * Used to keep track on L2 interface group and L3 unicast group information for direct hosts.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800348 */
Charles Chan873661e2017-11-30 15:37:50 -0800349 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800350 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800351
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700352 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000353 * Per device next objective ID store with (device id + MAC address + vlan) as key.
354 * Used to keep track of L3 unicast group for indirect hosts.
355 */
356 private EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer>
357 macVlanNextObjStore = null;
358
Saurav Das2d94d312015-11-24 23:21:05 -0800359 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
360 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700361
Saurav Das261c3002017-06-13 15:35:54 -0700362 private AtomicBoolean programmingScheduled = new AtomicBoolean();
363
Charles Chanc91c8782016-03-30 17:54:24 -0700364 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700365 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
366 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700367 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700368 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800369 public SegmentRoutingDeviceConfig createConfig() {
370 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700371 }
372 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800373
Charles Chanc91c8782016-03-30 17:54:24 -0700374 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700375 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
376 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700377 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800378 @Override
379 public SegmentRoutingAppConfig createConfig() {
380 return new SegmentRoutingAppConfig();
381 }
382 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800383
Charles Chanc91c8782016-03-30 17:54:24 -0700384 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700385 new ConfigFactory<ApplicationId, McastConfig>(
386 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700387 McastConfig.class, "multicast") {
388 @Override
389 public McastConfig createConfig() {
390 return new McastConfig();
391 }
392 };
393
Charles Chan1963f4f2016-02-18 14:22:42 -0800394 /**
395 * Segment Routing App ID.
396 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800397 public static final String APP_NAME = "org.onosproject.segmentrouting";
Saurav Das7c305372015-10-28 12:39:42 -0700398
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700399 /**
400 * Minumum and maximum value of dummy VLAN ID to be allocated.
401 */
402 public static final int MIN_DUMMY_VLAN_ID = 2;
403 public static final int MAX_DUMMY_VLAN_ID = 4093;
404
Charles Chana7601c92019-01-23 15:03:17 -0800405 private static final int DEFAULT_POOL_SIZE = 32;
406
Saurav Dasec683dc2018-04-27 18:42:30 -0700407 Instant lastEdgePortEvent = Instant.EPOCH;
408
Charles Chanb4f9cd12019-02-27 11:46:32 -0800409 protected void bindXconnectService(XconnectService xconnectService) {
410 if (this.xconnectService == null) {
411 log.info("Binding XconnectService");
412 this.xconnectService = xconnectService;
413 } else {
414 log.warn("Trying to bind XconnectService but it is already bound");
415 }
416 }
417
418 protected void unbindXconnectService(XconnectService xconnectService) {
419 if (this.xconnectService == xconnectService) {
420 log.info("Unbinding XconnectService");
421 this.xconnectService = null;
422 } else {
423 log.warn("Trying to unbind XconnectService but it is already unbound");
424 }
425 }
426
sangho80f11cb2015-04-01 13:05:26 -0700427 @Activate
Charles Chan873661e2017-11-30 15:37:50 -0800428 protected void activate(ComponentContext context) {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800429 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700430
Charles Chana7601c92019-01-23 15:03:17 -0800431 mainEventExecutor = Executors.newSingleThreadScheduledExecutor(
432 groupedThreads("onos/sr", "event-main-%d", log));
433 hostEventExecutor = Executors.newSingleThreadScheduledExecutor(
434 groupedThreads("onos/sr", "event-host-%d", log));
435 routeEventExecutor = Executors.newSingleThreadScheduledExecutor(
436 groupedThreads("onos/sr", "event-route-%d", log));
437 mcastEventExecutor = Executors.newSingleThreadScheduledExecutor(
438 groupedThreads("onos/sr", "event-mcast-%d", log));
439 packetExecutor = Executors.newSingleThreadExecutor(groupedThreads("onos/sr", "packet-%d", log));
440 neighborExecutor = Executors.newFixedThreadPool(DEFAULT_POOL_SIZE,
441 groupedThreads("onos/sr", "neighbor-%d", log));
Charles Chanfbcb8812018-04-18 18:41:05 -0700442
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700443 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das261c3002017-06-13 15:35:54 -0700444 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700445 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das261c3002017-06-13 15:35:54 -0700446 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700447 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700448 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700449 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700450 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700451 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700452
Charles Chan10b0fb72017-02-02 16:20:42 -0800453 log.debug("Creating EC map vlannextobjectivestore");
454 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
455 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
456 vlanNextObjStore = vlanNextObjMapBuilder
457 .withName("vlannextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700458 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700459 .withTimestampProvider((k, v) -> new WallClockTimestamp())
460 .build();
461
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000462 log.debug("Creating EC map macvlannextobjectivestore");
463 EventuallyConsistentMapBuilder<MacVlanNextObjectiveStoreKey, Integer>
464 macVlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
465 macVlanNextObjStore = macVlanNextObjMapBuilder
466 .withName("macvlannextobjectivestore")
467 .withSerializer(createSerializer())
468 .withTimestampProvider((k, v) -> new WallClockTimestamp())
469 .build();
470
Saurav Das2d94d312015-11-24 23:21:05 -0800471 log.debug("Creating EC map subnetnextobjectivestore");
472 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
473 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
474 portNextObjStore = portNextObjMapBuilder
475 .withName("portnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700476 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800477 .withTimestampProvider((k, v) -> new WallClockTimestamp())
478 .build();
479
sangho4a5c42a2015-05-20 22:16:38 -0700480 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
481 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700482 tunnelStore = tunnelMapBuilder
483 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700484 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700485 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700486 .build();
487
488 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
489 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700490 policyStore = policyMapBuilder
491 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700492 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700493 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700494 .build();
495
Saurav Dasc3604f12016-03-23 11:22:49 -0700496 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700497 "purgeOnDisconnection", "true", false);
Saurav Dasc3604f12016-03-23 11:22:49 -0700498 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700499 "purgeOnDisconnection", "true", false);
Pier Luigib9632ba2017-01-12 18:14:58 -0800500 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
Charles Chan0fa51db2019-04-10 17:12:20 -0700501 "requestInterceptsEnabled", "false", false);
Charles Chanc1909e12017-08-08 15:13:37 -0700502 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700503 "requestInterceptsEnabled", "false", false);
Charles Chan9597f8d2017-07-24 15:56:10 -0700504 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700505 "arpEnabled", "false", false);
Pier Luigi0ebeb312017-02-02 22:31:34 -0800506 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700507 "greedyLearningIpv6", "true", false);
Charles Chancf8ea472017-02-28 15:15:17 -0800508 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700509 "forceUnprovision", "true", false);
Charles Chanc7b73c72017-08-10 16:57:28 -0700510 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan0fa51db2019-04-10 17:12:20 -0700511 "distributed", "true", false);
Charles Chan804772f2017-08-14 11:42:11 -0700512 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
Charles Chan0fa51db2019-04-10 17:12:20 -0700513 "multihomingEnabled", "true", false);
Charles Chan0c9c19f2017-11-29 19:54:20 -0800514 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
Charles Chan0fa51db2019-04-10 17:12:20 -0700515 "staleLinkAge", "15000", false);
Charles Chan0c9c19f2017-11-29 19:54:20 -0800516 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700517 "allowDuplicateIps", "false", false);
Yi Tsenga7da8d82018-08-19 03:09:54 +0800518 // For P4 switches
519 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700520 "fallbackFlowPollFrequency", "4", false);
Yi Tsenga7da8d82018-08-19 03:09:54 +0800521 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700522 "fallbackGroupPollFrequency", "3", false);
Charles Chan873661e2017-11-30 15:37:50 -0800523 compCfgService.registerProperties(getClass());
524 modified(context);
Saurav Dasc3604f12016-03-23 11:22:49 -0700525
Charles Chan2b078ae2015-10-14 11:24:40 -0700526 processor = new InternalPacketProcessor();
527 linkListener = new InternalLinkListener();
528 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700529 appCfgHandler = new AppConfigHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700530 mcastHandler = new McastHandler(this);
531 hostHandler = new HostHandler(this);
Saurav Dase6c448a2018-01-18 12:07:33 -0800532 linkHandler = new LinkHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700533 routeHandler = new RouteHandler(this);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800534 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800535 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Pier Luigid8a15162018-02-15 16:33:08 +0100536 topologyHandler = new TopologyHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700537
Charles Chand6d25332016-02-26 22:19:52 -0800538 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700539 cfgService.registerConfigFactory(deviceConfigFactory);
540 cfgService.registerConfigFactory(appConfigFactory);
541 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase321cff2018-02-09 17:26:45 -0800542 log.info("Configuring network before adding listeners");
Andrea Campanella060cad82018-04-17 12:09:34 +0200543
Charles Chan6961f222018-01-04 14:26:07 -0800544 cfgListener.configureNetwork();
545
Charles Chan82ab1932016-01-30 23:22:37 -0800546 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700547 packetService.addProcessor(processor, PacketProcessor.director(2));
548 linkService.addListener(linkListener);
549 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700550 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700551 routeService.addListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100552 topologyService.addListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700553 mastershipService.addListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700554 clusterService.addListener(clusterListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700555
Charles Chanc12c3d02018-03-09 15:53:44 -0800556 linkHandler.init();
Andreas Pantelopoulos75eef062018-01-11 07:53:48 -0800557 l2TunnelHandler.init();
558
Ray Milkey521e4392018-11-16 15:15:14 -0800559 synchronized (networkConfigCompletionLock) {
560 networkConfigCompletion.whenComplete((value, ex) -> {
561 //setting to null for easier fall through
562 networkConfigCompletion = null;
563 //process all queued events
564 queuedEvents.forEach(event -> {
565 mainEventExecutor.execute(new InternalEventHandler(event));
566 });
Andrea Campanella060cad82018-04-17 12:09:34 +0200567 });
Ray Milkey521e4392018-11-16 15:15:14 -0800568 }
Andrea Campanella060cad82018-04-17 12:09:34 +0200569
sangho80f11cb2015-04-01 13:05:26 -0700570 log.info("Started");
571 }
572
Saurav Dase6c448a2018-01-18 12:07:33 -0800573 KryoNamespace.Builder createSerializer() {
Jonathan Hart54541d12016-04-12 15:39:44 -0700574 return new KryoNamespace.Builder()
575 .register(KryoNamespaces.API)
Saurav Das261c3002017-06-13 15:35:54 -0700576 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800577 VlanNextObjectiveStoreKey.class,
578 DestinationSet.class,
Andreas Pantelopoulos92998c92018-05-29 13:11:14 -0700579 DestinationSet.DestinationSetType.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800580 NextNeighbors.class,
581 Tunnel.class,
582 DefaultTunnel.class,
583 Policy.class,
584 TunnelPolicy.class,
585 Policy.Type.class,
586 PortNextObjectiveStoreKey.class,
587 XConnectStoreKey.class,
588 L2Tunnel.class,
589 L2TunnelPolicy.class,
590 DefaultL2Tunnel.class,
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700591 DefaultL2TunnelPolicy.class,
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000592 MacVlanNextObjectiveStoreKey.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700593 );
594 }
595
sangho80f11cb2015-04-01 13:05:26 -0700596 @Deactivate
597 protected void deactivate() {
Charles Chanfbcb8812018-04-18 18:41:05 -0700598 mainEventExecutor.shutdown();
599 hostEventExecutor.shutdown();
600 routeEventExecutor.shutdown();
601 mcastEventExecutor.shutdown();
Charles Chan77cdde42018-05-08 21:35:50 -0700602 packetExecutor.shutdown();
Charles Chana7601c92019-01-23 15:03:17 -0800603 neighborExecutor.shutdown();
Charles Chanfbcb8812018-04-18 18:41:05 -0700604
Ray Milkeyaee4d3e2018-05-11 09:59:19 -0700605 mainEventExecutor = null;
606 hostEventExecutor = null;
607 routeEventExecutor = null;
608 mcastEventExecutor = null;
609 packetExecutor = null;
Charles Chana7601c92019-01-23 15:03:17 -0800610 neighborExecutor = null;
Ray Milkeyaee4d3e2018-05-11 09:59:19 -0700611
Charles Chan72f556a2015-10-05 17:50:33 -0700612 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700613 cfgService.unregisterConfigFactory(deviceConfigFactory);
614 cfgService.unregisterConfigFactory(appConfigFactory);
615 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan873661e2017-11-30 15:37:50 -0800616 compCfgService.unregisterProperties(getClass(), false);
Charles Chan72f556a2015-10-05 17:50:33 -0700617
Charles Chanfd48c222017-06-19 00:43:31 -0700618 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700619 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700620 linkService.removeListener(linkListener);
621 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700622 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700623 routeService.removeListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100624 topologyService.removeListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700625 mastershipService.removeListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700626 clusterService.removeListener(clusterListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700627
Charles Chan2e71ef32017-02-23 15:44:08 -0800628 neighbourResolutionService.unregisterNeighbourHandlers(appId);
629
sangho80f11cb2015-04-01 13:05:26 -0700630 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700631 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700632 deviceListener = null;
Charles Chan05bb1702018-04-19 13:10:01 -0700633 groupHandlerMap.forEach((k, v) -> v.shutdown());
Charles Chan2b078ae2015-10-14 11:24:40 -0700634 groupHandlerMap.clear();
Saurav Das49368392018-04-23 18:42:12 -0700635 defaultRoutingHandler.shutdown();
Charles Chan2b078ae2015-10-14 11:24:40 -0700636
Saurav Das261c3002017-06-13 15:35:54 -0700637 dsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800638 vlanNextObjStore.destroy();
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000639 macVlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700640 portNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700641 tunnelStore.destroy();
642 policyStore.destroy();
Pier Luigi35dab3f2018-01-25 16:16:02 +0100643
644 mcastHandler.terminate();
piercf557922019-05-17 20:47:06 +0200645 hostHandler.terminate();
sangho80f11cb2015-04-01 13:05:26 -0700646 log.info("Stopped");
647 }
648
Charles Chan873661e2017-11-30 15:37:50 -0800649 @Modified
650 private void modified(ComponentContext context) {
651 Dictionary<?, ?> properties = context.getProperties();
652 if (properties == null) {
653 return;
654 }
655
Ray Milkey48d10c02018-10-24 10:04:03 -0700656 String strActiveProbing = Tools.get(properties, PROP_ACTIVE_PROBING);
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700657 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProbing);
Charles Chan873661e2017-11-30 15:37:50 -0800658 if (expectActiveProbing != activeProbing) {
659 activeProbing = expectActiveProbing;
660 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
661 }
Saurav Dasec683dc2018-04-27 18:42:30 -0700662
Ray Milkeye96d0de2018-11-02 17:12:21 -0700663
664 String strSymmetricProbing = Tools.get(properties, PROP_SYMMETRIC_PROBING);
665 boolean expectSymmetricProbing = Boolean.parseBoolean(strSymmetricProbing);
Mayank Tiwarif7942402018-10-29 18:27:35 -0400666 if (expectSymmetricProbing != symmetricProbing) {
667 symmetricProbing = expectSymmetricProbing;
668 log.info("{} symmetric probing", symmetricProbing ? "Enabling" : "Disabling");
669 }
670
Ray Milkey48d10c02018-10-24 10:04:03 -0700671 String strSingleHomedDown = Tools.get(properties, PROP_SINGLE_HOMED_DOWN);
Saurav Dasec683dc2018-04-27 18:42:30 -0700672 boolean expectSingleHomedDown = Boolean.parseBoolean(strSingleHomedDown);
673 if (expectSingleHomedDown != singleHomedDown) {
674 singleHomedDown = expectSingleHomedDown;
675 log.info("{} downing of single homed hosts for lost uplinks",
676 singleHomedDown ? "Enabling" : "Disabling");
677 if (singleHomedDown && linkHandler != null) {
678 hostService.getHosts().forEach(host -> host.locations()
679 .forEach(loc -> {
680 if (interfaceService.isConfigured(loc)) {
681 linkHandler.checkUplinksForHost(loc);
682 }
683 }));
684 } else {
685 log.warn("Disabling singleHomedDown does not re-enable already "
686 + "downed ports for single-homed hosts");
687 }
688 }
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700689
Ray Milkey48d10c02018-10-24 10:04:03 -0700690 String strRespondToUnknownHosts = Tools.get(properties, PROP_RESPOND_TO_UNKNOWN_HOSTS);
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700691 boolean expectRespondToUnknownHosts = Boolean.parseBoolean(strRespondToUnknownHosts);
692 if (expectRespondToUnknownHosts != respondToUnknownHosts) {
693 respondToUnknownHosts = expectRespondToUnknownHosts;
694 log.info("{} responding to ARPs/NDPs from unknown hosts", respondToUnknownHosts ? "Enabling" : "Disabling");
695 }
Charles Chan4eb73bb2018-07-19 14:55:31 -0700696
Ray Milkey48d10c02018-10-24 10:04:03 -0700697 String strRouteDoubleTaggedHosts = Tools.get(properties, PROP_ROUTE_DOUBLE_TAGGED_HOSTS);
Charles Chanfbca55e2018-07-24 16:40:35 -0700698 boolean expectRouteDoubleTaggedHosts = Boolean.parseBoolean(strRouteDoubleTaggedHosts);
699 if (expectRouteDoubleTaggedHosts != routeDoubleTaggedHosts) {
700 routeDoubleTaggedHosts = expectRouteDoubleTaggedHosts;
701 log.info("{} routing for double tagged hosts", routeDoubleTaggedHosts ? "Enabling" : "Disabling");
Charles Chan4eb73bb2018-07-19 14:55:31 -0700702
Charles Chanfbca55e2018-07-24 16:40:35 -0700703 if (routeDoubleTaggedHosts) {
Charles Chan4eb73bb2018-07-19 14:55:31 -0700704 hostHandler.populateAllDoubleTaggedHost();
705 } else {
706 hostHandler.revokeAllDoubleTaggedHost();
707 }
708 }
Saurav Das9bf49582018-08-13 15:34:26 -0700709
Ray Milkey48d10c02018-10-24 10:04:03 -0700710 String strDefaultInternalVlan = Tools.get(properties, PROP_DEFAULT_INTERNAL_VLAN);
Saurav Das9bf49582018-08-13 15:34:26 -0700711 int defIntVlan = Integer.parseInt(strDefaultInternalVlan);
712 if (defIntVlan != defaultInternalVlan) {
713 if (canUseVlanId(defIntVlan)) {
714 log.warn("Default internal vlan value changed from {} to {}.. "
715 + "re-programming filtering rules, but NOT any groups already "
716 + "created with the former value", defaultInternalVlan, defIntVlan);
717 VlanId oldDefIntVlan = VlanId.vlanId((short) defaultInternalVlan);
718 defaultInternalVlan = defIntVlan;
719 routingRulePopulator
720 .updateSpecialVlanFilteringRules(true, oldDefIntVlan,
721 VlanId.vlanId((short) defIntVlan));
722 } else {
723 log.warn("Cannot change default internal vlan to unusable "
724 + "value {}", defIntVlan);
725 }
726 }
727
Ray Milkey48d10c02018-10-24 10:04:03 -0700728 String strPwTxpVlan = Tools.get(properties, PROP_PW_TRANSPORT_VLAN);
Saurav Das9bf49582018-08-13 15:34:26 -0700729 int pwTxpVlan = Integer.parseInt(strPwTxpVlan);
730 if (pwTxpVlan != pwTransportVlan) {
731 if (canUseVlanId(pwTxpVlan)) {
732 log.warn("Pseudowire transport vlan value changed from {} to {}.. "
733 + "re-programming filtering rules, but NOT any groups already "
734 + "created with the former value", pwTransportVlan,
735 pwTxpVlan);
736 VlanId oldPwTxpVlan = VlanId.vlanId((short) pwTransportVlan);
737 pwTransportVlan = pwTxpVlan;
738 routingRulePopulator
739 .updateSpecialVlanFilteringRules(false, oldPwTxpVlan,
740 VlanId.vlanId((short) pwTxpVlan));
741 } else {
742 log.warn("Cannot change pseudowire transport vlan to unusable "
743 + "value {}", pwTxpVlan);
744 }
745 }
746
psneha86e60d32019-03-26 06:31:41 -0400747 String strRouteSimplification = Tools.get(properties, PROP_ROUTE_SIMPLIFICATION);
748 boolean expectRouteSimplification = Boolean.parseBoolean(strRouteSimplification);
749 if (expectRouteSimplification != routeSimplification) {
750 routeSimplification = expectRouteSimplification;
751 log.info("{} route simplification", routeSimplification ? "Enabling" : "Disabling");
752 }
753
Saurav Das9bf49582018-08-13 15:34:26 -0700754 }
755
756 /**
757 * Returns true if given vlan id is not being used in the system currently,
758 * either as one of the default system wide vlans or as one of the
759 * configured interface vlans.
760 *
761 * @param vlanId given vlan id
762 * @return true if vlan is not currently in use
763 */
764 public boolean canUseVlanId(int vlanId) {
765 if (vlanId >= 4095 || vlanId <= 1) {
766 log.error("Vlan id {} value is not in valid range 2 <--> 4094",
767 vlanId);
768 return false;
769 }
770
771 VlanId vid = VlanId.vlanId((short) vlanId);
772 if (getDefaultInternalVlan().equals(vid) || getPwTransportVlan().equals(vid)) {
773 log.warn("Vlan id {} value is already in use system-wide. "
774 + "DefaultInternalVlan:{} PwTransportVlan:{} ", vlanId,
775 getDefaultInternalVlan(), getPwTransportVlan());
776 return false;
777 }
778
779 if (interfaceService.inUse(vid)) {
780 log.warn("Vlan id {} value is already in use on a configured "
781 + "interface in the system", vlanId);
782 return false;
783 }
784 return true;
785 }
786
787 /**
788 * Returns the VlanId assigned internally by default to unconfigured ports.
789 *
790 * @return the default internal vlan id
791 */
792 public VlanId getDefaultInternalVlan() {
793 return VlanId.vlanId((short) defaultInternalVlan);
794 }
795
796 /**
797 * Returns the Vlan id used to transport pseudowire traffic across the
798 * network.
799 *
800 * @return the pseudowire transport vlan id
801 */
802 public VlanId getPwTransportVlan() {
803 return VlanId.vlanId((short) pwTransportVlan);
Charles Chan873661e2017-11-30 15:37:50 -0800804 }
805
sangho27462c62015-05-14 00:39:53 -0700806 @Override
807 public List<Tunnel> getTunnels() {
808 return tunnelHandler.getTunnels();
809 }
810
811 @Override
sanghobd812f82015-06-29 14:58:47 -0700812 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
813 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700814 }
815
816 @Override
sanghobd812f82015-06-29 14:58:47 -0700817 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700818 for (Policy policy: policyHandler.getPolicies()) {
819 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
820 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
821 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
822 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700823 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700824 }
825 }
826 }
sanghobd812f82015-06-29 14:58:47 -0700827 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700828 }
829
830 @Override
sanghobd812f82015-06-29 14:58:47 -0700831 public PolicyHandler.Result removePolicy(Policy policy) {
832 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700833 }
834
835 @Override
sanghobd812f82015-06-29 14:58:47 -0700836 public PolicyHandler.Result createPolicy(Policy policy) {
837 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700838 }
839
840 @Override
841 public List<Policy> getPolicies() {
842 return policyHandler.getPolicies();
843 }
844
Saurav Das07c74602016-04-27 18:35:50 -0700845 @Override
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700846 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
847 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700848 }
849
850 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800851 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700852 return l2TunnelHandler.getL2Tunnels();
853 }
854
855 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800856 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700857 return l2TunnelHandler.getL2Policies();
858 }
859
Saurav Dasdebcf882018-04-06 20:16:01 -0700860 @Override
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700861 @Deprecated
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700862 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
863
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700864 // get both added and pending pseudowires
865 List<L2TunnelDescription> pseudowires = new ArrayList<>();
866 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
867 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700868 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700869
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700870 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
871
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700872 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
873 L2TunnelHandler.Result res;
874 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
875 res = addPseudowire(pw);
876 if (res != L2TunnelHandler.Result.SUCCESS) {
877 log.error("Pseudowire with id {} can not be instantiated !", res);
878 retRes = res;
879 }
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700880 }
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700881
882 return retRes;
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700883 }
884
885 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800886 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700887 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700888 }
889
890 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800891 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700892 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700893 }
894
895 @Override
Saurav Das07c74602016-04-27 18:35:50 -0700896 public void rerouteNetwork() {
897 cfgListener.configureNetwork();
Saurav Das07c74602016-04-27 18:35:50 -0700898 }
899
Charles Chand7844e52016-10-20 17:02:44 -0700900 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800901 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
902 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart61e24e12017-11-30 18:23:42 -0800903 deviceConfiguration.getRouters().forEach(device ->
904 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chand7844e52016-10-20 17:02:44 -0700905 return deviceSubnetMap;
906 }
907
Saurav Das62ae6792017-05-15 15:34:25 -0700908
909 @Override
910 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
911 if (defaultRoutingHandler != null) {
912 return defaultRoutingHandler.getCurrentEmcpSpgMap();
913 } else {
914 return null;
915 }
916 }
917
918 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700919 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDstNextObjStore() {
Saurav Das261c3002017-06-13 15:35:54 -0700920 if (dsNextObjStore != null) {
921 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Das62ae6792017-05-15 15:34:25 -0700922 } else {
923 return ImmutableMap.of();
924 }
925 }
926
Saurav Dasfbe74572017-08-03 18:30:35 -0700927 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700928 public ImmutableMap<VlanNextObjectiveStoreKey, Integer> getVlanNextObjStore() {
929 if (vlanNextObjStore != null) {
930 return ImmutableMap.copyOf(vlanNextObjStore.entrySet());
931 } else {
932 return ImmutableMap.of();
933 }
934 }
935
936 @Override
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000937 public ImmutableMap<MacVlanNextObjectiveStoreKey, Integer> getMacVlanNextObjStore() {
938 if (macVlanNextObjStore != null) {
939 return ImmutableMap.copyOf(macVlanNextObjStore.entrySet());
940 } else {
941 return ImmutableMap.of();
942 }
943 }
944
945 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700946 public ImmutableMap<PortNextObjectiveStoreKey, Integer> getPortNextObjStore() {
947 if (portNextObjStore != null) {
948 return ImmutableMap.copyOf(portNextObjStore.entrySet());
949 } else {
950 return ImmutableMap.of();
951 }
952 }
953
954 @Override
955 public ImmutableMap<String, NextObjective> getPwInitNext() {
956 if (l2TunnelHandler != null) {
957 return l2TunnelHandler.getInitNext();
958 } else {
959 return ImmutableMap.of();
960 }
961 }
962
963 @Override
964 public ImmutableMap<String, NextObjective> getPwTermNext() {
965 if (l2TunnelHandler != null) {
966 return l2TunnelHandler.getTermNext();
967 } else {
968 return ImmutableMap.of();
969 }
970 }
971
972 @Override
973 public void invalidateNextObj(int nextId) {
974 if (dsNextObjStore != null) {
975 dsNextObjStore.entrySet().forEach(e -> {
976 if (e.getValue().nextId() == nextId) {
977 dsNextObjStore.remove(e.getKey());
978 }
979 });
980 }
981 if (vlanNextObjStore != null) {
982 vlanNextObjStore.entrySet().forEach(e -> {
983 if (e.getValue() == nextId) {
984 vlanNextObjStore.remove(e.getKey());
985 }
986 });
987 }
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000988 if (macVlanNextObjStore != null) {
989 macVlanNextObjStore.entrySet().forEach(e -> {
990 if (e.getValue() == nextId) {
991 macVlanNextObjStore.remove(e.getKey());
992 }
993 });
994 }
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700995 if (portNextObjStore != null) {
996 portNextObjStore.entrySet().forEach(e -> {
997 if (e.getValue() == nextId) {
998 portNextObjStore.remove(e.getKey());
999 }
1000 });
1001 }
1002 if (mcastHandler != null) {
1003 mcastHandler.removeNextId(nextId);
1004 }
1005 if (l2TunnelHandler != null) {
1006 l2TunnelHandler.removeNextId(nextId);
1007 }
1008 if (xconnectService != null) {
1009 xconnectService.removeNextId(nextId);
1010 }
1011 }
1012
1013 @Override
Saurav Dasfbe74572017-08-03 18:30:35 -07001014 public void verifyGroups(DeviceId id) {
1015 DefaultGroupHandler gh = groupHandlerMap.get(id);
1016 if (gh != null) {
1017 gh.triggerBucketCorrector();
1018 }
1019 }
1020
Saurav Das6430f412018-01-25 09:49:01 -08001021 @Override
1022 public ImmutableMap<Link, Boolean> getSeenLinks() {
1023 return linkHandler.getSeenLinks();
1024 }
1025
1026 @Override
1027 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
1028 return linkHandler.getDownedPorts();
1029 }
1030
Pier Luigi0f9635b2018-01-15 18:06:43 +01001031 @Override
1032 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
Charles Chan0b1dd7e2018-08-19 19:21:46 -07001033 return mcastHandler.getNextIds(mcastIp);
Pier Luigi0f9635b2018-01-15 18:06:43 +01001034 }
1035
1036 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +01001037 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigi0f9635b2018-01-15 18:06:43 +01001038 return mcastHandler.getMcastRoles(mcastIp);
1039 }
1040
1041 @Override
Piere99511d2018-04-19 16:47:06 +02001042 public Map<McastRoleStoreKey, McastRole> getMcastRoles(IpAddress mcastIp, ConnectPoint sourcecp) {
1043 return mcastHandler.getMcastRoles(mcastIp, sourcecp);
1044 }
1045
1046 @Override
Pier Luigi0f9635b2018-01-15 18:06:43 +01001047 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
1048 return mcastHandler.getMcastPaths(mcastIp);
1049 }
1050
Pierdb27b8d2018-04-17 16:29:56 +02001051 @Override
Pier71c55772018-04-17 17:25:22 +02001052 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
1053 ConnectPoint sourcecp) {
1054 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
1055 }
1056
1057 @Override
Pierdb27b8d2018-04-17 16:29:56 +02001058 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
1059 return mcastHandler.getMcastLeaders(mcastIp);
1060 }
1061
Charles Chanb13e0702018-04-17 18:56:53 -07001062 @Override
1063 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
1064 return defaultRoutingHandler == null ? ImmutableMap.of() :
1065 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
1066 }
1067
1068 @Override
1069 public Map<DeviceId, Boolean> getShouldProgramCache() {
1070 return defaultRoutingHandler == null ? ImmutableMap.of() :
1071 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
1072 }
1073
Charles Chan8d316332018-06-19 20:31:57 -07001074 @Override
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301075 public boolean shouldProgram(DeviceId deviceId) {
1076 return defaultRoutingHandler.shouldProgram(deviceId);
1077 }
1078
1079 @Override
Ray Milkeyb85de082017-04-05 09:42:04 -07001080 public ApplicationId appId() {
1081 return appId;
1082 }
1083
1084 /**
1085 * Returns the device configuration.
1086 *
1087 * @return device configuration
1088 */
1089 public DeviceConfiguration deviceConfiguration() {
1090 return deviceConfiguration;
1091 }
1092
1093 /**
Saurav Das261c3002017-06-13 15:35:54 -07001094 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -07001095 * Used to keep track on MPLS group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001096 *
1097 * @return next objective ID store
1098 */
Saurav Das261c3002017-06-13 15:35:54 -07001099 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
1100 dsNextObjStore() {
1101 return dsNextObjStore;
Ray Milkeyb85de082017-04-05 09:42:04 -07001102 }
1103
1104 /**
Charles Chan53de91f2017-08-22 15:07:34 -07001105 * Per device next objective ID store with (device id + vlanid) as key.
1106 * Used to keep track on L2 flood group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001107 *
1108 * @return vlan next object store
1109 */
1110 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
1111 return vlanNextObjStore;
1112 }
1113
1114 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001115 * Per device next objective ID store with (device id + MAC address + vlan) as key.
1116 * Used to keep track on L3 Unicast group information for indirect hosts.
1117 *
1118 * @return mac vlan next object store
1119 */
1120 public EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer> macVlanNextObjStore() {
1121 return macVlanNextObjStore;
1122 }
1123
1124 /**
Charles Chan53de91f2017-08-22 15:07:34 -07001125 * Per device next objective ID store with (device id + port + treatment + meta) as key.
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001126 * Used to keep track on L2 interface group and L3 unicast group information for direct hosts.
Ray Milkeyb85de082017-04-05 09:42:04 -07001127 *
1128 * @return port next object store.
1129 */
1130 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
1131 return portNextObjStore;
1132 }
1133
1134 /**
Saurav Das261c3002017-06-13 15:35:54 -07001135 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
1136 * labeled packets should be programmed or not.
Pier Ventre7a78de22016-10-31 15:00:01 -07001137 *
1138 * @return MPLS-ECMP value
1139 */
1140 public boolean getMplsEcmp() {
1141 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
1142 .getConfig(this.appId, SegmentRoutingAppConfig.class);
1143 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
1144 }
1145
1146 /**
sangho80f1f892015-05-19 11:57:42 -07001147 * Returns the tunnel object with the tunnel ID.
1148 *
1149 * @param tunnelId Tunnel ID
1150 * @return Tunnel reference
1151 */
sangho27462c62015-05-14 00:39:53 -07001152 public Tunnel getTunnel(String tunnelId) {
1153 return tunnelHandler.getTunnel(tunnelId);
1154 }
1155
Charles Chan8d316332018-06-19 20:31:57 -07001156 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +01001157 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chan098ca202018-05-01 11:50:20 -07001158 VlanId untaggedVlanId = interfaceService.getUntaggedVlanId(connectPoint);
1159 VlanId nativeVlanId = interfaceService.getNativeVlanId(connectPoint);
Charles Chand9265a32017-06-16 15:19:24 -07001160 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
1161 }
1162
Charles Chan8d316332018-06-19 20:31:57 -07001163 @Override
1164 public Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001165 SegmentRoutingDeviceConfig deviceConfig =
1166 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1167 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
1168 }
Charles Chan8d316332018-06-19 20:31:57 -07001169
1170 @Override
Saurav Dasec683dc2018-04-27 18:42:30 -07001171 public Optional<PortNumber> getPairLocalPort(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001172 SegmentRoutingDeviceConfig deviceConfig =
1173 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1174 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
1175 }
1176
1177 /**
Charles Chanf0ae41e2017-08-23 13:55:39 -07001178 * Returns locations of given resolved route.
1179 *
1180 * @param resolvedRoute resolved route
1181 * @return locations of nexthop. Might be empty if next hop is not found
1182 */
Charles Chan19b70032019-04-17 14:20:26 -07001183 public Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
Charles Chanf0ae41e2017-08-23 13:55:39 -07001184 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
1185 return Optional.ofNullable(hostService.getHost(hostId))
1186 .map(Host::locations).orElse(Sets.newHashSet())
1187 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
1188 }
1189
1190 /**
Charles Chan90772a72017-02-08 15:52:08 -08001191 * Returns vlan port map of given device.
1192 *
1193 * @param deviceId device id
1194 * @return vlan-port multimap
1195 */
1196 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
1197 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
1198
1199 interfaceService.getInterfaces().stream()
1200 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
1201 .forEach(intf -> {
1202 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan3ed34d82017-06-22 18:03:14 -07001203 intf.vlanTagged().forEach(vlanTagged ->
1204 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
1205 );
Charles Chan90772a72017-02-08 15:52:08 -08001206 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
1207 });
1208 vlanPortMap.removeAll(VlanId.NONE);
1209
1210 return vlanPortMap;
1211 }
1212
1213 /**
Charles Chan10b0fb72017-02-02 16:20:42 -08001214 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -08001215 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -07001216 *
1217 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -08001218 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -08001219 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -07001220 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001221 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -07001222 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -08001223 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
1224 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -07001225 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -08001226 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -08001227 + "device {} not found", deviceId);
1228 return -1;
1229 }
1230 }
1231
1232 /**
1233 * Returns the next objective ID for the given portNumber, given the treatment.
1234 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -07001235 * would result in different objectives. If the next object does not exist,
1236 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -08001237 *
1238 * @param deviceId Device ID
1239 * @param portNum port number on device for which NextObjective is queried
1240 * @param treatment the actions to apply on the packets (should include outport)
1241 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -07001242 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -07001243 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -08001244 */
1245 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
1246 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -07001247 TrafficSelector meta,
1248 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -08001249 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1250 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -07001251 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -08001252 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -08001253 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
1254 + " not found", deviceId);
1255 return -1;
1256 }
1257 }
1258
Saurav Das62ae6792017-05-15 15:34:25 -07001259 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001260 * Returns the next Objective ID for the given mac and vlan, given the treatment.
1261 * There could be multiple different treatments to the same outport, which
1262 * would result in different objectives. If the next object does not exist,
1263 * and should be created, a new one is created and its id is returned.
1264 *
1265 * @param deviceId Device ID
1266 * @param macAddr mac of host for which Next ID is required.
1267 * @param vlanId vlan of host for which Next ID is required.
Ruchi Sahota07869322019-05-09 17:26:14 -04001268 * @param port port with which to create the Next Obj.
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001269 * @param createIfMissing true if a next object should be created if not found
1270 * @return next objective ID or -1 if an error occurred during retrieval or creation
1271 */
1272 public int getMacVlanNextObjectiveId(DeviceId deviceId, MacAddress macAddr, VlanId vlanId,
Ruchi Sahota07869322019-05-09 17:26:14 -04001273 PortNumber port, boolean createIfMissing) {
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001274 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1275 if (ghdlr != null) {
Ruchi Sahota07869322019-05-09 17:26:14 -04001276 return ghdlr.getMacVlanNextObjectiveId(macAddr, vlanId, port, createIfMissing);
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001277 } else {
1278 log.warn("getMacVlanNextObjectiveId query - groupHandler for device {}"
1279 + " not found", deviceId);
1280 return -1;
1281 }
1282 }
1283
1284 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001285 * Updates the next objective for the given nextId .
1286 *
1287 * @param deviceId Device ID
1288 * @param hostMac mac of host for which Next obj is to be updated.
1289 * @param hostVlanId vlan of host for which Next obj is to be updated.
1290 * @param port port with which to update the Next Obj.
1291 * @param nextId of Next Obj which needs to be updated.
1292 */
1293 public void updateMacVlanTreatment(DeviceId deviceId, MacAddress hostMac,
1294 VlanId hostVlanId, PortNumber port, int nextId) {
1295 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1296 if (ghdlr != null) {
1297 ghdlr.updateL3UcastGroupBucket(hostMac, hostVlanId, port, nextId);
1298 } else {
1299 log.warn("updateL3UcastGroupBucket query - groupHandler for device {}"
1300 + " not found", deviceId);
1301 }
1302 }
1303
1304
1305 /**
Saurav Das62ae6792017-05-15 15:34:25 -07001306 * Returns the group handler object for the specified device id.
1307 *
1308 * @param devId the device identifier
1309 * @return the groupHandler object for the device id, or null if not found
1310 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001311 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Das62ae6792017-05-15 15:34:25 -07001312 return groupHandlerMap.get(devId);
1313 }
1314
1315 /**
Saurav Dasfbe74572017-08-03 18:30:35 -07001316 * Returns the default routing handler object.
1317 *
1318 * @return the default routing handler object
1319 */
1320 public DefaultRoutingHandler getRoutingHandler() {
1321 return defaultRoutingHandler;
1322 }
1323
sangho80f11cb2015-04-01 13:05:26 -07001324 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -07001325 @Override
1326 public void process(PacketContext context) {
Charles Chan77cdde42018-05-08 21:35:50 -07001327 packetExecutor.execute(() -> processPacketInternal(context));
1328 }
sangho80f11cb2015-04-01 13:05:26 -07001329
Charles Chan77cdde42018-05-08 21:35:50 -07001330 private void processPacketInternal(PacketContext context) {
sangho80f11cb2015-04-01 13:05:26 -07001331 if (context.isHandled()) {
1332 return;
1333 }
1334
1335 InboundPacket pkt = context.inPacket();
1336 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -08001337
1338 if (ethernet == null) {
1339 return;
1340 }
1341
Saurav Das261c3002017-06-13 15:35:54 -07001342 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1343 ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -08001344 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -07001345 log.warn("Received unexpected ARP packet on {}",
1346 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -07001347 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001348 return;
sangho80f11cb2015-04-01 13:05:26 -07001349 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -08001350 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1351 //ipHandler.addToPacketBuffer(ipv4Packet);
1352 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1353 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001354 } else {
Charles Chand041ad82017-01-13 17:20:44 -08001355 // NOTE: We don't support IP learning at this moment so this
1356 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001357 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001358 }
Pier Ventreb6a7f342016-11-26 21:05:22 -08001359 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1360 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -08001361 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -08001362 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -08001363 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1364 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1365 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1366 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1367 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1368 } else {
Saurav Das62ae6792017-05-15 15:34:25 -07001369 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -07001370 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -08001371 }
1372 } else {
1373 // NOTE: We don't support IP learning at this moment so this
1374 // is not necessary. Also it causes duplication of DHCPv6 packets.
1375 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1376 }
sangho80f11cb2015-04-01 13:05:26 -07001377 }
1378 }
1379 }
1380
sangho80f11cb2015-04-01 13:05:26 -07001381 private class InternalEventHandler implements Runnable {
Charles Chan52003922018-04-05 16:31:15 -07001382 private Event event;
1383
1384 InternalEventHandler(Event event) {
1385 this.event = event;
1386 }
1387
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001388 @Override
sangho80f11cb2015-04-01 13:05:26 -07001389 public void run() {
Charles Chan52003922018-04-05 16:31:15 -07001390 try {
1391 // TODO We should also change SR routing and PW to listen to TopologyEvents
1392 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1393 event.type() == LinkEvent.Type.LINK_UPDATED) {
1394 linkHandler.processLinkAdded((Link) event.subject());
1395 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1396 linkHandler.processLinkRemoved((Link) event.subject());
1397 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1398 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1399 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1400 DeviceId deviceId = ((Device) event.subject()).id();
1401 if (deviceService.isAvailable(deviceId)) {
1402 log.info("** DEVICE UP Processing device event {} "
1403 + "for available device {}",
1404 event.type(), ((Device) event.subject()).id());
1405 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001406 } else {
Charles Chanb8f3af52018-08-29 21:21:51 -07001407 if (event.type() == DeviceEvent.Type.DEVICE_ADDED) {
1408 // Note: For p4 devices, the device will be added but unavailable at the beginning.
1409 // The device will later on being marked as available once the pipeline is pushed
1410 // to the device.
1411 log.info("** DEVICE ADDED but unavailable. Ignore");
1412 return;
1413 }
Charles Chan52003922018-04-05 16:31:15 -07001414 log.info(" ** DEVICE DOWN Processing device event {}"
1415 + " for unavailable device {}",
1416 event.type(), ((Device) event.subject()).id());
1417 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001418 }
Charles Chan52003922018-04-05 16:31:15 -07001419 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1420 // typically these calls come when device is added first time
1421 // so port filtering rules are handled at the device_added event.
1422 // port added calls represent all ports on the device,
1423 // enabled or not.
1424 log.trace("** PORT ADDED {}/{} -> {}",
1425 ((DeviceEvent) event).subject().id(),
1426 ((DeviceEvent) event).port().number(),
1427 event.type());
1428 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1429 // these calls happen for every subsequent event
1430 // ports enabled, disabled, switch goes away, comes back
1431 log.info("** PORT UPDATED {}/{} -> {}",
1432 event.subject(),
1433 ((DeviceEvent) event).port(),
1434 event.type());
Saurav Dasec683dc2018-04-27 18:42:30 -07001435 processPortUpdatedInternal(((Device) event.subject()),
Charles Chan52003922018-04-05 16:31:15 -07001436 ((DeviceEvent) event).port());
1437 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1438 // Process topology event, needed for all modules relying on
1439 // topology service for path computation
1440 TopologyEvent topologyEvent = (TopologyEvent) event;
1441 log.info("Processing topology event {}, topology age {}, reasons {}",
1442 event.type(), topologyEvent.subject().time(),
1443 topologyEvent.reasons().size());
1444 topologyHandler.processTopologyChange(topologyEvent.reasons());
1445 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1446 hostHandler.processHostAddedEvent((HostEvent) event);
1447 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1448 hostHandler.processHostMovedEvent((HostEvent) event);
1449 routeHandler.processHostMovedEvent((HostEvent) event);
1450 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1451 hostHandler.processHostRemovedEvent((HostEvent) event);
1452 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1453 hostHandler.processHostUpdatedEvent((HostEvent) event);
1454 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1455 routeHandler.processRouteAdded((RouteEvent) event);
1456 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1457 routeHandler.processRouteUpdated((RouteEvent) event);
1458 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1459 routeHandler.processRouteRemoved((RouteEvent) event);
1460 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1461 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1462 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1463 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1464 event.type() == McastEvent.Type.SINKS_ADDED ||
1465 event.type() == McastEvent.Type.SINKS_REMOVED ||
1466 event.type() == McastEvent.Type.ROUTE_ADDED ||
1467 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1468 mcastHandler.processMcastEvent((McastEvent) event);
1469 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1470 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1471 Class configClass = netcfgEvent.configClass();
1472 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1473 appCfgHandler.processAppConfigAdded(netcfgEvent);
1474 log.info("App config event .. configuring network");
1475 cfgListener.configureNetwork();
1476 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1477 log.info("Segment Routing Device Config added for {}", event.subject());
1478 cfgListener.configureNetwork();
Charles Chan52003922018-04-05 16:31:15 -07001479 } else if (configClass.equals(InterfaceConfig.class)) {
1480 log.info("Interface Config added for {}", event.subject());
1481 cfgListener.configureNetwork();
1482 } else {
1483 log.error("Unhandled config class: {}", configClass);
1484 }
1485 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1486 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1487 Class configClass = netcfgEvent.configClass();
1488 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1489 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1490 log.info("App config event .. configuring network");
1491 cfgListener.configureNetwork();
1492 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1493 log.info("Segment Routing Device Config updated for {}", event.subject());
1494 createOrUpdateDeviceConfiguration();
Charles Chan52003922018-04-05 16:31:15 -07001495 } else if (configClass.equals(InterfaceConfig.class)) {
1496 log.info("Interface Config updated for {}", event.subject());
1497 createOrUpdateDeviceConfiguration();
1498 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1499 (InterfaceConfig) netcfgEvent.prevConfig().get());
1500 } else {
1501 log.error("Unhandled config class: {}", configClass);
1502 }
1503 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1504 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1505 Class configClass = netcfgEvent.configClass();
1506 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1507 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1508 log.info("App config event .. configuring network");
1509 cfgListener.configureNetwork();
1510 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1511 // TODO Handle sr device config removal
1512 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
Charles Chan52003922018-04-05 16:31:15 -07001513 } else if (configClass.equals(InterfaceConfig.class)) {
1514 // TODO Handle interface removal
1515 log.info("InterfaceConfig removal is not handled in current implementation");
1516 } else {
1517 log.error("Unhandled config class: {}", configClass);
1518 }
Saurav Das00e553b2018-04-21 17:19:48 -07001519 } else if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
1520 MastershipEvent me = (MastershipEvent) event;
1521 DeviceId deviceId = me.subject();
1522 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1523 log.info(" ** MASTERSHIP CHANGED Invalidating shouldProgram cache"
1524 + " for {}/pair={} due to change", deviceId, pairDeviceId);
1525 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1526 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1527 defaultRoutingHandler.checkFullRerouteForMasterChange(deviceId, me);
Charles Chan52003922018-04-05 16:31:15 -07001528 } else {
1529 log.warn("Unhandled event type: {}", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001530 }
Charles Chan52003922018-04-05 16:31:15 -07001531 } catch (Exception e) {
1532 log.error("SegmentRouting event handler thread thrown an exception: {}",
1533 e.getMessage(), e);
sangho80f11cb2015-04-01 13:05:26 -07001534 }
sangho80f11cb2015-04-01 13:05:26 -07001535 }
1536 }
1537
Saurav Dase6c448a2018-01-18 12:07:33 -08001538 void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001539 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan9bd6aea2017-06-27 18:48:32 -07001540
1541 // NOTE: Punt ARP/NDP even when the device is not configured.
1542 // Host learning without network config is required for CORD config generator.
1543 routingRulePopulator.populateIpPunts(device.id());
1544 routingRulePopulator.populateArpNdpPunts(device.id());
1545
Charles Chan319d1a22015-11-03 10:42:14 -08001546 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das261c3002017-06-13 15:35:54 -07001547 log.warn("Device configuration unavailable. Device {} will be "
1548 + "processed after configuration.", device.id());
Saurav Das8ec0ec42015-11-03 14:39:27 -08001549 return;
1550 }
Charles Chan72779502016-04-23 17:36:10 -07001551 processDeviceAddedInternal(device.id());
1552 }
1553
1554 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001555 // Irrespective of whether the local is a MASTER or not for this device,
1556 // we need to create a SR-group-handler instance. This is because in a
1557 // multi-instance setup, any instance can initiate forwarding/next-objectives
1558 // for any switch (even if this instance is a SLAVE or not even connected
1559 // to the switch). To handle this, a default-group-handler instance is necessary
1560 // per switch.
Charles Chan72779502016-04-23 17:36:10 -07001561 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1562 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001563 DefaultGroupHandler groupHandler;
1564 try {
1565 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001566 createGroupHandler(deviceId,
1567 appId,
1568 deviceConfiguration,
1569 linkService,
1570 flowObjectiveService,
1571 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001572 } catch (DeviceConfigNotFoundException e) {
1573 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1574 return;
1575 }
Saurav Das2b6a00f2017-12-05 15:00:23 -08001576 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan72779502016-04-23 17:36:10 -07001577 deviceId);
1578 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001579 }
Saurav Dasb149be12016-06-07 10:08:06 -07001580
Charles Chan72779502016-04-23 17:36:10 -07001581 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001582 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001583 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001584 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001585 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001586 }
Charles Chan82ab1932016-01-30 23:22:37 -08001587
Charles Chandebfea32016-10-24 14:52:01 -07001588 appCfgHandler.init(deviceId);
Charles Chan074fae62018-04-29 18:11:37 -07001589 hostEventExecutor.execute(() -> hostHandler.init(deviceId));
1590 routeEventExecutor.execute(() -> routeHandler.init(deviceId));
sangho80f11cb2015-04-01 13:05:26 -07001591 }
1592
Saurav Dasc3604f12016-03-23 11:22:49 -07001593 private void processDeviceRemoved(Device device) {
Saurav Das261c3002017-06-13 15:35:54 -07001594 dsNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001595 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan873661e2017-11-30 15:37:50 -08001596 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan10b0fb72017-02-02 16:20:42 -08001597 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001598 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001599 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001600 macVlanNextObjStore.entrySet().stream()
1601 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1602 .forEach(entry -> macVlanNextObjStore.remove(entry.getKey()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001603 portNextObjStore.entrySet().stream()
1604 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001605 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Dase6c448a2018-01-18 12:07:33 -08001606 linkHandler.processDeviceRemoved(device);
Charles Chan17d75d82017-06-15 00:44:51 -07001607
Saurav Dasfbe74572017-08-03 18:30:35 -07001608 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1609 if (gh != null) {
1610 gh.shutdown();
1611 }
Saurav Das62ae6792017-05-15 15:34:25 -07001612 // Note that a switch going down is associated with all of its links
1613 // going down as well, but it is treated as a single switch down event
Saurav Dasdebcf882018-04-06 20:16:01 -07001614 // while the link-downs are ignored. We cannot rely on the ordering of
1615 // events - i.e we cannot expect all link-downs to come before the
1616 // switch down - so we purge all seen-links for the switch before
1617 // handling route-path changes for the switch-down
Saurav Das62ae6792017-05-15 15:34:25 -07001618 defaultRoutingHandler
Saurav Dasdc7f2752018-03-18 21:28:15 -07001619 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Das6430f412018-01-25 09:49:01 -08001620 defaultRoutingHandler.purgeEcmpGraph(device.id());
Saurav Das97241862018-02-14 14:14:54 -08001621
1622 // Cleanup all internal groupHandler stores for this device. Should be
1623 // done after all rerouting or rehashing has been completed
1624 groupHandlerMap.entrySet()
1625 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001626 }
1627
Saurav Das49368392018-04-23 18:42:12 -07001628 /**
1629 * Purge the destinationSet nextObjective store of entries with this device
1630 * as key. Erases app-level knowledge of hashed groups in this device.
1631 *
1632 * @param devId the device identifier
1633 */
Saurav Das00e553b2018-04-21 17:19:48 -07001634 void purgeHashedNextObjectiveStore(DeviceId devId) {
Saurav Das49368392018-04-23 18:42:12 -07001635 log.debug("Purging hashed next-obj store for dev:{}", devId);
Saurav Das00e553b2018-04-21 17:19:48 -07001636 dsNextObjStore.entrySet().stream()
1637 .filter(entry -> entry.getKey().deviceId().equals(devId))
1638 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
1639 }
1640
Saurav Dasec683dc2018-04-27 18:42:30 -07001641 private void processPortUpdatedInternal(Device device, Port port) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001642 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1643 log.warn("Device configuration uploading. Not handling port event for"
1644 + "dev: {} port: {}", device.id(), port.number());
1645 return;
1646 }
Saurav Dasf9332192017-02-18 14:05:44 -08001647
Saurav Dasec683dc2018-04-27 18:42:30 -07001648 if (interfaceService.isConfigured(new ConnectPoint(device.id(), port.number()))) {
1649 lastEdgePortEvent = Instant.now();
1650 }
1651
Saurav Dasf9332192017-02-18 14:05:44 -08001652 if (!mastershipService.isLocalMaster(device.id())) {
1653 log.debug("Not master for dev:{} .. not handling port updated event"
1654 + "for port {}", device.id(), port.number());
1655 return;
1656 }
Saurav Dasec683dc2018-04-27 18:42:30 -07001657 processPortUpdated(device.id(), port);
1658 }
Saurav Dasf9332192017-02-18 14:05:44 -08001659
Saurav Dasec683dc2018-04-27 18:42:30 -07001660 /**
1661 * Adds or remove filtering rules for the given switchport. If switchport is
1662 * an edge facing port, additionally handles host probing and broadcast
1663 * rules. Must be called by local master of device.
1664 *
1665 * @param deviceId the device identifier
1666 * @param port the port to update
1667 */
1668 void processPortUpdated(DeviceId deviceId, Port port) {
Saurav Dasf9332192017-02-18 14:05:44 -08001669 // first we handle filtering rules associated with the port
1670 if (port.isEnabled()) {
1671 log.info("Switchport {}/{} enabled..programming filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001672 deviceId, port.number());
1673 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001674 } else {
1675 log.info("Switchport {}/{} disabled..removing filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001676 deviceId, port.number());
1677 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001678 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001679
1680 // portUpdated calls are for ports that have gone down or up. For switch
1681 // to switch ports, link-events should take care of any re-routing or
1682 // group editing necessary for port up/down. Here we only process edge ports
1683 // that are already configured.
Saurav Dasec683dc2018-04-27 18:42:30 -07001684 ConnectPoint cp = new ConnectPoint(deviceId, port.number());
Charles Chan098ca202018-05-01 11:50:20 -07001685 VlanId untaggedVlan = interfaceService.getUntaggedVlanId(cp);
1686 VlanId nativeVlan = interfaceService.getNativeVlanId(cp);
1687 Set<VlanId> taggedVlans = interfaceService.getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001688
Saurav Das3fb28272017-03-04 16:08:47 -08001689 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001690 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasec683dc2018-04-27 18:42:30 -07001691 + "dev/port: {}/{}", deviceId, port.number());
Saurav Dasf0f592d2016-11-18 15:21:57 -08001692 return;
1693 }
Saurav Das3fb28272017-03-04 16:08:47 -08001694 if (untaggedVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001695 processEdgePort(deviceId, port, untaggedVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001696 }
1697 if (nativeVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001698 processEdgePort(deviceId, port, nativeVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001699 }
1700 if (!taggedVlans.isEmpty()) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001701 taggedVlans.forEach(tag -> processEdgePort(deviceId, port, tag, false));
Saurav Das3fb28272017-03-04 16:08:47 -08001702 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001703 }
1704
Saurav Dasec683dc2018-04-27 18:42:30 -07001705 private void processEdgePort(DeviceId deviceId, Port port, VlanId vlanId,
Saurav Das3fb28272017-03-04 16:08:47 -08001706 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001707 boolean portUp = port.isEnabled();
1708 if (portUp) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001709 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001710 port.number(), vlanId);
Charles Chan074fae62018-04-29 18:11:37 -07001711 hostEventExecutor.execute(() -> hostHandler.processPortUp(new ConnectPoint(deviceId, port.number())));
Saurav Dasf0f592d2016-11-18 15:21:57 -08001712 } else {
Saurav Dasec683dc2018-04-27 18:42:30 -07001713 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001714 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001715 }
1716
Saurav Dasec683dc2018-04-27 18:42:30 -07001717 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001718 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001719 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001720 } else {
1721 log.warn("Group handler not found for dev:{}. Not handling edge port"
Saurav Dasec683dc2018-04-27 18:42:30 -07001722 + " {} event for port:{}", deviceId,
Saurav Dasf0f592d2016-11-18 15:21:57 -08001723 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001724 }
1725 }
sangho27462c62015-05-14 00:39:53 -07001726
Charles Chan8ca5a122017-10-20 16:06:55 -07001727 private void createOrUpdateDeviceConfiguration() {
1728 if (deviceConfiguration == null) {
Saurav Dase321cff2018-02-09 17:26:45 -08001729 log.info("Creating new DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001730 deviceConfiguration = new DeviceConfiguration(this);
1731 } else {
Saurav Dase321cff2018-02-09 17:26:45 -08001732 log.info("Updating DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001733 deviceConfiguration.updateConfig();
1734 }
1735 }
1736
Charles Chanfbcb8812018-04-18 18:41:05 -07001737 private void createOrUpdateDefaultRoutingHandler() {
1738 if (defaultRoutingHandler == null) {
1739 log.info("Creating new DefaultRoutingHandler");
1740 defaultRoutingHandler = new DefaultRoutingHandler(this);
1741 } else {
1742 log.info("Updating DefaultRoutingHandler");
1743 defaultRoutingHandler.update(this);
1744 }
1745 }
1746
Pier Ventreb6a7f342016-11-26 21:05:22 -08001747 /**
1748 * Registers the given connect point with the NRS, this is necessary
1749 * to receive the NDP and ARP packets from the NRS.
1750 *
1751 * @param portToRegister connect point to register
1752 */
1753 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001754 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001755 portToRegister,
1756 neighbourHandler,
1757 appId
1758 );
1759 }
1760
Charles Chan72f556a2015-10-05 17:50:33 -07001761 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das261c3002017-06-13 15:35:54 -07001762 private static final long PROGRAM_DELAY = 2;
Charles Chan46fdfaf2016-11-09 20:51:44 -08001763 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001764
Charles Chanb7f75ac2016-01-11 18:28:54 -08001765 /**
1766 * Constructs the internal network config listener.
1767 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001768 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001769 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001770 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001771 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001772 }
1773
Charles Chanb7f75ac2016-01-11 18:28:54 -08001774 /**
1775 * Reads network config and initializes related data structure accordingly.
1776 */
Charles Chan873661e2017-11-30 15:37:50 -08001777 void configureNetwork() {
Saurav Dase321cff2018-02-09 17:26:45 -08001778 log.info("Configuring network ...");
Andrea Campanella060cad82018-04-17 12:09:34 +02001779
1780 // Setting handling of network configuration events completable future
1781 // The completable future is needed because of the async behaviour of the configureNetwork,
1782 // listener registration and event arrival
1783 // Enables us to buffer the events and execute them when the configure network is done.
Ray Milkey521e4392018-11-16 15:15:14 -08001784 synchronized (networkConfigCompletionLock) {
1785 networkConfigCompletion = new CompletableFuture<>();
Andrea Campanella060cad82018-04-17 12:09:34 +02001786
Ray Milkey521e4392018-11-16 15:15:14 -08001787 // add a small delay to absorb multiple network config added notifications
1788 if (!programmingScheduled.get()) {
1789 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
1790 programmingScheduled.set(true);
1791 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
1792 }
1793
1794 createOrUpdateDeviceConfiguration();
1795
1796 arpHandler = new ArpHandler(srManager);
1797 icmpHandler = new IcmpHandler(srManager);
1798 ipHandler = new IpHandler(srManager);
1799 routingRulePopulator = new RoutingRulePopulator(srManager);
1800 createOrUpdateDefaultRoutingHandler();
1801
1802 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1803 groupHandlerMap, tunnelStore);
1804 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1805 flowObjectiveService,
1806 tunnelHandler, policyStore);
1807 networkConfigCompletion.complete(true);
Andrea Campanella060cad82018-04-17 12:09:34 +02001808 }
1809
Charles Chan72779502016-04-23 17:36:10 -07001810 mcastHandler.init();
Andrea Campanella060cad82018-04-17 12:09:34 +02001811
Charles Chane7c61022015-10-07 14:21:45 -07001812 }
1813
Charles Chan72f556a2015-10-05 17:50:33 -07001814 @Override
1815 public void event(NetworkConfigEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001816 if (mainEventExecutor == null) {
1817 return;
1818 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001819 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan3f4aca62018-03-21 16:57:47 -07001820 switch (event.type()) {
1821 case CONFIG_ADDED:
1822 case CONFIG_UPDATED:
1823 case CONFIG_REMOVED:
1824 log.trace("Schedule Network Config event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001825 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1826 mainEventExecutor.execute(new InternalEventHandler(event));
1827 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001828 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001829 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001830 break;
1831 default:
1832 break;
Charles Chan72f556a2015-10-05 17:50:33 -07001833 }
1834 }
Saurav Das261c3002017-06-13 15:35:54 -07001835
Charles Chanc4a68c32018-01-03 16:26:32 -08001836 @Override
1837 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase321cff2018-02-09 17:26:45 -08001838 if (event.type() == CONFIG_REGISTERED ||
1839 event.type() == CONFIG_UNREGISTERED) {
1840 log.debug("Ignore event {} due to type mismatch", event);
1841 return false;
Charles Chanc4a68c32018-01-03 16:26:32 -08001842 }
Saurav Dase321cff2018-02-09 17:26:45 -08001843
1844 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1845 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
Charles Chana01d57c2019-07-19 16:25:38 -07001846 !event.configClass().equals(InterfaceConfig.class)) {
Saurav Dase321cff2018-02-09 17:26:45 -08001847 log.debug("Ignore event {} due to class mismatch", event);
1848 return false;
1849 }
1850
1851 return true;
Charles Chanc4a68c32018-01-03 16:26:32 -08001852 }
1853
Saurav Das261c3002017-06-13 15:35:54 -07001854 private final class ConfigChange implements Runnable {
1855 @Override
1856 public void run() {
1857 programmingScheduled.set(false);
Saurav Dase321cff2018-02-09 17:26:45 -08001858 log.info("Reacting to config changes after buffer delay");
Saurav Das261c3002017-06-13 15:35:54 -07001859 for (Device device : deviceService.getDevices()) {
1860 processDeviceAdded(device);
1861 }
1862 defaultRoutingHandler.startPopulationProcess();
1863 }
1864 }
Charles Chan72f556a2015-10-05 17:50:33 -07001865 }
Charles Chanf4586112015-11-09 16:37:23 -08001866
Charles Chan3f4aca62018-03-21 16:57:47 -07001867 private class InternalLinkListener implements LinkListener {
1868 @Override
1869 public void event(LinkEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001870 if (mainEventExecutor == null) {
1871 return;
1872 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001873 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1874 event.type() == LinkEvent.Type.LINK_UPDATED ||
1875 event.type() == LinkEvent.Type.LINK_REMOVED) {
1876 log.trace("Schedule Link event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001877 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1878 mainEventExecutor.execute(new InternalEventHandler(event));
1879 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001880 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001881 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001882 }
1883 }
1884 }
1885
1886 private class InternalDeviceListener implements DeviceListener {
1887 @Override
1888 public void event(DeviceEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001889 if (mainEventExecutor == null) {
1890 return;
1891 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001892 switch (event.type()) {
1893 case DEVICE_ADDED:
1894 case PORT_UPDATED:
1895 case PORT_ADDED:
1896 case DEVICE_UPDATED:
1897 case DEVICE_AVAILABILITY_CHANGED:
1898 log.trace("Schedule Device event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001899 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1900 mainEventExecutor.execute(new InternalEventHandler(event));
1901 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001902 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001903 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001904 break;
1905 default:
1906 }
1907 }
1908 }
1909
1910 private class InternalTopologyListener implements TopologyListener {
1911 @Override
1912 public void event(TopologyEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001913 if (mainEventExecutor == null) {
1914 return;
1915 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001916 switch (event.type()) {
1917 case TOPOLOGY_CHANGED:
1918 log.trace("Schedule Topology event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001919 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1920 mainEventExecutor.execute(new InternalEventHandler(event));
1921 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001922 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001923 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001924 break;
1925 default:
1926 }
1927 }
1928 }
1929
Charles Chanf4586112015-11-09 16:37:23 -08001930 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001931 @Override
1932 public void event(HostEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001933 if (hostEventExecutor == null) {
1934 return;
1935 }
Charles Chanf4586112015-11-09 16:37:23 -08001936 switch (event.type()) {
1937 case HOST_ADDED:
Charles Chanf4586112015-11-09 16:37:23 -08001938 case HOST_MOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001939 case HOST_REMOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001940 case HOST_UPDATED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001941 log.trace("Schedule Host event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001942 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chanf4586112015-11-09 16:37:23 -08001943 break;
1944 default:
1945 log.warn("Unsupported host event type: {}", event.type());
1946 break;
1947 }
1948 }
1949 }
1950
Charles Chanc91c8782016-03-30 17:54:24 -07001951 private class InternalMcastListener implements McastListener {
1952 @Override
1953 public void event(McastEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001954 if (mcastEventExecutor == null) {
1955 return;
1956 }
Charles Chanc91c8782016-03-30 17:54:24 -07001957 switch (event.type()) {
Pier1f87aca2018-03-14 16:47:32 -07001958 case SOURCES_ADDED:
1959 case SOURCES_REMOVED:
1960 case SINKS_ADDED:
1961 case SINKS_REMOVED:
Charles Chanc91c8782016-03-30 17:54:24 -07001962 case ROUTE_REMOVED:
Pierdb27b8d2018-04-17 16:29:56 +02001963 case ROUTE_ADDED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001964 log.trace("Schedule Mcast event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001965 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi6786b922018-02-02 16:19:11 +01001966 break;
Charles Chanc91c8782016-03-30 17:54:24 -07001967 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07001968 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chanc91c8782016-03-30 17:54:24 -07001969 break;
1970 }
1971 }
1972 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001973
Charles Chandebfea32016-10-24 14:52:01 -07001974 private class InternalRouteEventListener implements RouteListener {
1975 @Override
1976 public void event(RouteEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001977 if (routeEventExecutor == null) {
1978 return;
1979 }
Charles Chandebfea32016-10-24 14:52:01 -07001980 switch (event.type()) {
1981 case ROUTE_ADDED:
Charles Chandebfea32016-10-24 14:52:01 -07001982 case ROUTE_UPDATED:
Charles Chandebfea32016-10-24 14:52:01 -07001983 case ROUTE_REMOVED:
Charles Chan910be6a2017-08-23 14:46:43 -07001984 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001985 log.trace("Schedule Route event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001986 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan910be6a2017-08-23 14:46:43 -07001987 break;
Charles Chandebfea32016-10-24 14:52:01 -07001988 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07001989 log.warn("Unsupported route event type: {}", event.type());
Charles Chandebfea32016-10-24 14:52:01 -07001990 break;
1991 }
1992 }
1993 }
Saurav Das62ae6792017-05-15 15:34:25 -07001994
Charles Chanfbcb8812018-04-18 18:41:05 -07001995 private class InternalMastershipListener implements MastershipListener {
1996 @Override
1997 public void event(MastershipEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001998 if (mainEventExecutor == null) {
1999 return;
2000 }
Charles Chanfbcb8812018-04-18 18:41:05 -07002001 switch (event.type()) {
Saurav Das00e553b2018-04-21 17:19:48 -07002002 case MASTER_CHANGED:
2003 log.debug("Mastership event: {}/{}", event.subject(),
2004 event.roleInfo());
2005 mainEventExecutor.execute(new InternalEventHandler(event));
2006 break;
2007 case BACKUPS_CHANGED:
2008 case SUSPENDED:
2009 default:
2010 log.debug("Mastership event type {} not handled", event.type());
2011 break;
Charles Chanfbcb8812018-04-18 18:41:05 -07002012 }
2013 }
2014 }
2015
Saurav Das00e553b2018-04-21 17:19:48 -07002016 class InternalClusterListener implements ClusterEventListener {
2017 private Instant lastClusterEvent = Instant.EPOCH;
2018
2019 long timeSinceLastClusterEvent() {
2020 return Instant.now().toEpochMilli() - lastClusterEvent.toEpochMilli();
2021 }
2022
2023 @Override
2024 public void event(ClusterEvent event) {
2025 switch (event.type()) {
2026 case INSTANCE_ACTIVATED:
2027 case INSTANCE_ADDED:
2028 case INSTANCE_READY:
2029 log.debug("Cluster event {} ignored", event.type());
2030 break;
2031 case INSTANCE_DEACTIVATED:
2032 case INSTANCE_REMOVED:
2033 log.info("** Cluster event {}", event.type());
2034 lastClusterEvent = Instant.now();
2035 break;
2036 default:
2037 break;
2038 }
2039
2040 }
2041
2042 }
2043
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002044 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
2045 try {
2046 Set<Interface> intfs = conf.getInterfaces();
2047 Set<Interface> prevIntfs = prevConf.getInterfaces();
2048
2049 // Now we only handle one interface config at each port.
2050 if (intfs.size() != 1 || prevIntfs.size() != 1) {
2051 log.warn("Interface update aborted - one at a time is allowed, " +
2052 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
2053 return;
2054 }
2055
Andrea Campanella060cad82018-04-17 12:09:34 +02002056 //The system is in an incoherent state, abort
2057 if (defaultRoutingHandler == null) {
2058 log.warn("Interface update aborted, defaultRoutingHandler is null");
2059 return;
2060 }
2061
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002062 Interface intf = intfs.stream().findFirst().get();
2063 Interface prevIntf = prevIntfs.stream().findFirst().get();
2064
2065 DeviceId deviceId = intf.connectPoint().deviceId();
2066 PortNumber portNum = intf.connectPoint().port();
2067
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002068 removeSubnetConfig(prevIntf.connectPoint(),
2069 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
2070 new HashSet<>(intf.ipAddressesList())));
2071
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002072 if (!prevIntf.vlanNative().equals(VlanId.NONE)
2073 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
2074 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
2075 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
2076 // Update filtering objective and L2IG group bucket
2077 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
2078 } else {
2079 // RemoveVlanNative
2080 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
2081 }
2082 }
2083
2084 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
2085 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2086 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2087 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
2088 // Update filtering objective and L2IG group bucket
2089 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
2090 } else {
2091 // RemoveVlanUntagged
2092 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
2093 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002094 }
2095
2096 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2097 // RemoveVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002098 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
2099 .filter(i -> !intf.vlanUntagged().equals(i))
2100 .filter(i -> !intf.vlanNative().equals(i))
2101 .forEach(vlanId -> updateVlanConfigInternal(
2102 deviceId, portNum, vlanId, false, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002103 }
2104
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002105 if (!intf.vlanNative().equals(VlanId.NONE)
2106 && !prevIntf.vlanNative().equals(intf.vlanNative())
2107 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2108 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
2109 // Update filtering objective and L2IG group bucket
2110 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
2111 } else {
2112 // AddVlanNative
2113 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
2114 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002115 }
2116
2117 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2118 // AddVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002119 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
2120 .filter(i -> !prevIntf.vlanUntagged().equals(i))
2121 .filter(i -> !prevIntf.vlanNative().equals(i))
2122 .forEach(vlanId -> updateVlanConfigInternal(
2123 deviceId, portNum, vlanId, false, true)
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002124 );
2125 }
2126
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002127 if (!intf.vlanUntagged().equals(VlanId.NONE)
2128 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2129 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
2130 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
2131 // Update filtering objective and L2IG group bucket
2132 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
2133 } else {
2134 // AddVlanUntagged
2135 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
2136 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002137 }
2138 addSubnetConfig(prevIntf.connectPoint(),
2139 Sets.difference(new HashSet<>(intf.ipAddressesList()),
2140 new HashSet<>(prevIntf.ipAddressesList())));
2141 } catch (ConfigException e) {
2142 log.error("Error in configuration");
2143 }
2144 }
2145
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002146 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
2147 VlanId vlanId, boolean pushVlan) {
2148 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2149 if (grpHandler == null) {
2150 log.warn("Failed to retrieve group handler for device {}", deviceId);
2151 return;
2152 }
2153
2154 // Update filtering objective for a single port
2155 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
2156 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
2157
2158 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
2159 // Update L2IG bucket of the port
2160 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
2161 } else {
2162 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2163 }
2164 }
2165
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002166 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
2167 VlanId vlanId, boolean pushVlan, boolean install) {
2168 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2169 if (grpHandler == null) {
2170 log.warn("Failed to retrieve group handler for device {}", deviceId);
2171 return;
2172 }
2173
2174 // Update filtering objective for a single port
2175 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
2176
2177 // Update filtering objective for multicast ingress port
2178 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
2179
2180 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
2181
2182 if (nextId != -1 && !install) {
2183 // Update next objective for a single port as an output port
2184 // Remove a single port from L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002185 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002186 // Remove L2 Bridging rule and L3 Unicast rule to the host
Charles Chan074fae62018-04-29 18:11:37 -07002187 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2188 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002189 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
2190 // only if there is no port configured on that VLAN ID
2191 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
2192 // Remove broadcast forwarding rule for the VLAN
2193 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2194 // Remove L2FG for VLAN
2195 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
2196 } else {
2197 // Remove L2IG of the port
2198 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
2199 }
2200 } else if (install) {
2201 if (nextId != -1) {
2202 // Add a single port to L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002203 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002204 } else {
2205 // Create L2FG for VLAN
2206 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
2207 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2208 }
Charles Chan074fae62018-04-29 18:11:37 -07002209 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2210 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002211 } else {
2212 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2213 }
2214 }
2215
2216 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2217 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2218 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2219
2220 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2221 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2222 .filter(intf -> !intf.connectPoint().equals(cp))
2223 .flatMap(intf -> intf.ipAddressesList().stream())
2224 .collect(Collectors.toSet());
2225 // 1. Partial subnet population
2226 // Remove routing rules for removed subnet from previous configuration,
2227 // which does not also exist in other interfaces in the same device
2228 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2229 .map(InterfaceIpAddress::subnetAddress)
2230 .collect(Collectors.toSet());
2231
2232 defaultRoutingHandler.revokeSubnet(
2233 ipPrefixSet.stream()
2234 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2235 .collect(Collectors.toSet()));
2236
2237 // 2. Interface IP punts
2238 // Remove IP punts for old Intf address
2239 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2240 .map(InterfaceIpAddress::ipAddress)
2241 .collect(Collectors.toSet());
2242 ipAddressSet.stream()
2243 .map(InterfaceIpAddress::ipAddress)
2244 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2245 .forEach(interfaceIpAddress ->
2246 routingRulePopulator.revokeSingleIpPunts(
2247 cp.deviceId(), interfaceIpAddress));
2248
2249 // 3. Host unicast routing rule
2250 // Remove unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002251 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002252 }
2253
2254 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2255 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2256 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2257
2258 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2259 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2260 .filter(intf -> !intf.connectPoint().equals(cp))
2261 .flatMap(intf -> intf.ipAddressesList().stream())
2262 .collect(Collectors.toSet());
2263 // 1. Partial subnet population
2264 // Add routing rules for newly added subnet, which does not also exist in
2265 // other interfaces in the same device
2266 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2267 .map(InterfaceIpAddress::subnetAddress)
2268 .collect(Collectors.toSet());
2269
2270 defaultRoutingHandler.populateSubnet(
2271 Collections.singleton(cp),
2272 ipPrefixSet.stream()
2273 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2274 .collect(Collectors.toSet()));
2275
2276 // 2. Interface IP punts
2277 // Add IP punts for new Intf address
2278 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2279 .map(InterfaceIpAddress::ipAddress)
2280 .collect(Collectors.toSet());
2281 ipAddressSet.stream()
2282 .map(InterfaceIpAddress::ipAddress)
2283 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2284 .forEach(interfaceIpAddress ->
2285 routingRulePopulator.populateSingleIpPunts(
2286 cp.deviceId(), interfaceIpAddress));
2287
2288 // 3. Host unicast routing rule
2289 // Add unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002290 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002291 }
sangho80f11cb2015-04-01 13:05:26 -07002292}