blob: c8c88c7fccd8fc320672b9a36b35b916ea3c6277 [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 Chan82f19972016-05-17 13:13:55 -070099import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chanc91c8782016-03-30 17:54:24 -0700100import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -0700101import org.onosproject.segmentrouting.grouphandler.DestinationSet;
102import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Pier Luigi69f774d2018-02-28 12:10:50 +0100103import org.onosproject.segmentrouting.mcast.McastHandler;
104import org.onosproject.segmentrouting.mcast.McastRole;
Piere99511d2018-04-19 16:47:06 +0200105import org.onosproject.segmentrouting.mcast.McastRoleStoreKey;
Ray Milkey2bd24a92018-08-17 14:54:17 -0700106import org.onosproject.segmentrouting.mcast.McastStoreKey;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700107import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700108import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800109import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700110import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800111import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkey2bd24a92018-08-17 14:54:17 -0700112import org.onosproject.segmentrouting.pwaas.L2TunnelDescription;
Ray Milkeyae0068a2017-08-15 11:02:29 -0700113import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800114import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Saurav Das261c3002017-06-13 15:35:54 -0700115import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700116import org.onosproject.segmentrouting.storekey.DummyVlanIdStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -0700117import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan10b0fb72017-02-02 16:20:42 -0800118import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000119import org.onosproject.segmentrouting.storekey.MacVlanNextObjectiveStoreKey;
Charles Chan82f19972016-05-17 13:13:55 -0700120import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Charles Chan8d316332018-06-19 20:31:57 -0700121import org.onosproject.segmentrouting.xconnect.api.XconnectService;
Jonathan Hart54541d12016-04-12 15:39:44 -0700122import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700123import org.onosproject.store.service.EventuallyConsistentMap;
124import org.onosproject.store.service.EventuallyConsistentMapBuilder;
125import org.onosproject.store.service.StorageService;
126import org.onosproject.store.service.WallClockTimestamp;
Charles Chan873661e2017-11-30 15:37:50 -0800127import org.osgi.service.component.ComponentContext;
Ray Milkey2bd24a92018-08-17 14:54:17 -0700128import org.osgi.service.component.annotations.Activate;
129import org.osgi.service.component.annotations.Component;
130import org.osgi.service.component.annotations.Deactivate;
131import org.osgi.service.component.annotations.Modified;
132import org.osgi.service.component.annotations.Reference;
133import org.osgi.service.component.annotations.ReferenceCardinality;
Charles Chanb4f9cd12019-02-27 11:46:32 -0800134import org.osgi.service.component.annotations.ReferencePolicy;
sangho80f11cb2015-04-01 13:05:26 -0700135import org.slf4j.Logger;
136import org.slf4j.LoggerFactory;
137
Saurav Das00e553b2018-04-21 17:19:48 -0700138import java.time.Instant;
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700139import java.util.ArrayList;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800140import java.util.Collections;
Charles Chan873661e2017-11-30 15:37:50 -0800141import java.util.Dictionary;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800142import java.util.HashSet;
143import java.util.List;
144import java.util.Map;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800145import java.util.Optional;
146import java.util.Set;
Andrea Campanella060cad82018-04-17 12:09:34 +0200147import java.util.concurrent.CompletableFuture;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800148import java.util.concurrent.ConcurrentHashMap;
Andrea Campanella060cad82018-04-17 12:09:34 +0200149import java.util.concurrent.CopyOnWriteArrayList;
Charles Chan77cdde42018-05-08 21:35:50 -0700150import java.util.concurrent.ExecutorService;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800151import java.util.concurrent.Executors;
152import java.util.concurrent.ScheduledExecutorService;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800153import java.util.concurrent.TimeUnit;
154import java.util.concurrent.atomic.AtomicBoolean;
155import java.util.stream.Collectors;
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700156import java.util.stream.IntStream;
sangho80f11cb2015-04-01 13:05:26 -0700157
Charles Chand6d25332016-02-26 22:19:52 -0800158import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800159import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700160import static org.onlab.util.Tools.groupedThreads;
Saurav Dase321cff2018-02-09 17:26:45 -0800161import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
162import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700163import static org.onosproject.segmentrouting.OsgiPropertyConstants.ACTIVE_PROBING_DEFAULT;
164import static org.onosproject.segmentrouting.OsgiPropertyConstants.DEFAULT_INTERNAL_VLAN_DEFAULT;
165import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ACTIVE_PROBING;
166import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_DEFAULT_INTERNAL_VLAN;
167import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_PW_TRANSPORT_VLAN;
168import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_RESPOND_TO_UNKNOWN_HOSTS;
169import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ROUTE_DOUBLE_TAGGED_HOSTS;
psneha86e60d32019-03-26 06:31:41 -0400170import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ROUTE_SIMPLIFICATION;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700171import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SINGLE_HOMED_DOWN;
Ray Milkeye96d0de2018-11-02 17:12:21 -0700172import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SYMMETRIC_PROBING;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700173import static org.onosproject.segmentrouting.OsgiPropertyConstants.PW_TRANSPORT_VLAN_DEFAULT;
174import static org.onosproject.segmentrouting.OsgiPropertyConstants.RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
175import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
psneha86e60d32019-03-26 06:31:41 -0400176import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_SIMPLIFICATION_DEFAULT;
Ray Milkey05edbfc2018-10-23 14:23:16 -0700177import static org.onosproject.segmentrouting.OsgiPropertyConstants.SINGLE_HOMED_DOWN_DEFAULT;
Ray Milkeye96d0de2018-11-02 17:12:21 -0700178import static org.onosproject.segmentrouting.OsgiPropertyConstants.SYMMETRIC_PROBING_DEFAULT;
Charles Chand6d25332016-02-26 22:19:52 -0800179
Charles Chanb7f75ac2016-01-11 18:28:54 -0800180/**
181 * Segment routing manager.
182 */
Ray Milkey05edbfc2018-10-23 14:23:16 -0700183@Component(
184 immediate = true,
185 service = SegmentRoutingService.class,
186 property = {
187 PROP_ACTIVE_PROBING + ":Boolean=" + ACTIVE_PROBING_DEFAULT,
188 PROP_SINGLE_HOMED_DOWN + ":Boolean=" + SINGLE_HOMED_DOWN_DEFAULT,
189 PROP_RESPOND_TO_UNKNOWN_HOSTS + ":Boolean=" + RESPOND_TO_UNKNOWN_HOSTS_DEFAULT,
190 PROP_ROUTE_DOUBLE_TAGGED_HOSTS + ":Boolean=" + ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT,
191 PROP_DEFAULT_INTERNAL_VLAN + ":Integer=" + DEFAULT_INTERNAL_VLAN_DEFAULT,
192 PROP_PW_TRANSPORT_VLAN + ":Integer=" + PW_TRANSPORT_VLAN_DEFAULT,
psneha86e60d32019-03-26 06:31:41 -0400193 PROP_SYMMETRIC_PROBING + ":Boolean=" + SYMMETRIC_PROBING_DEFAULT,
194 PROP_ROUTE_SIMPLIFICATION + ":Boolean=" + ROUTE_SIMPLIFICATION_DEFAULT
Ray Milkey05edbfc2018-10-23 14:23:16 -0700195 }
196)
sangho27462c62015-05-14 00:39:53 -0700197public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700198
Charles Chan46fdfaf2016-11-09 20:51:44 -0800199 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan910be6a2017-08-23 14:46:43 -0700200 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sangho80f11cb2015-04-01 13:05:26 -0700201
Ray Milkey2bd24a92018-08-17 14:54:17 -0700202 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700203 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700204
Ray Milkey2bd24a92018-08-17 14:54:17 -0700205 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chancf789822019-03-22 10:04:27 -0700206 public NeighbourResolutionService neighbourResolutionService;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800207
Ray Milkey2bd24a92018-08-17 14:54:17 -0700208 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100209 public CoreService coreService;
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 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700213
Ray Milkey2bd24a92018-08-17 14:54:17 -0700214 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chandebfea32016-10-24 14:52:01 -0700215 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700216
Ray Milkey2bd24a92018-08-17 14:54:17 -0700217 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chanc6bcdf92018-06-01 16:33:48 -0700218 HostProbingService probingService;
Charles Chan873661e2017-11-30 15:37:50 -0800219
Ray Milkey2bd24a92018-08-17 14:54:17 -0700220 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100221 public DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700222
Ray Milkey2bd24a92018-08-17 14:54:17 -0700223 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Saurav Dase6c448a2018-01-18 12:07:33 -0800224 DeviceAdminService deviceAdminService;
225
Ray Milkey2bd24a92018-08-17 14:54:17 -0700226 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800227 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700228
Ray Milkey2bd24a92018-08-17 14:54:17 -0700229 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100230 public LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700231
Ray Milkey2bd24a92018-08-17 14:54:17 -0700232 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800233 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700234
Ray Milkey2bd24a92018-08-17 14:54:17 -0700235 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800236 public StorageService storageService;
Charles Chandebfea32016-10-24 14:52:01 -0700237
Ray Milkey2bd24a92018-08-17 14:54:17 -0700238 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100239 public MulticastRouteService multicastRouteService;
Charles Chandebfea32016-10-24 14:52:01 -0700240
Ray Milkey2bd24a92018-08-17 14:54:17 -0700241 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan83163812018-01-09 13:45:07 -0800242 public TopologyService topologyService;
Charles Chandebfea32016-10-24 14:52:01 -0700243
Ray Milkey2bd24a92018-08-17 14:54:17 -0700244 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan19b70032019-04-17 14:20:26 -0700245 public RouteService routeService;
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 NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800249
Ray Milkey2bd24a92018-08-17 14:54:17 -0700250 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800251 public InterfaceService interfaceService;
252
Ray Milkey2bd24a92018-08-17 14:54:17 -0700253 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pier Luigi580fd8a2018-01-16 10:47:50 +0100254 public ClusterService clusterService;
255
Ray Milkey2bd24a92018-08-17 14:54:17 -0700256 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Pierdb27b8d2018-04-17 16:29:56 +0200257 public WorkPartitionService workPartitionService;
Pier Luigi580fd8a2018-01-16 10:47:50 +0100258
Ray Milkey2bd24a92018-08-17 14:54:17 -0700259 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700260 public LeadershipService leadershipService;
261
Charles Chanb4f9cd12019-02-27 11:46:32 -0800262 @Reference(cardinality = ReferenceCardinality.OPTIONAL,
263 bind = "bindXconnectService",
264 unbind = "unbindXconnectService",
265 policy = ReferencePolicy.DYNAMIC)
Charles Chan8d316332018-06-19 20:31:57 -0700266 public XconnectService xconnectService;
267
Ray Milkey05edbfc2018-10-23 14:23:16 -0700268 /** Enable active probing to discover dual-homed hosts. */
269 boolean activeProbing = ACTIVE_PROBING_DEFAULT;
Charles Chan873661e2017-11-30 15:37:50 -0800270
Ray Milkeye96d0de2018-11-02 17:12:21 -0700271 /** Enable only send probe on the same port number of the pair device. */
272 boolean symmetricProbing = SYMMETRIC_PROBING_DEFAULT;
Mayank Tiwarif7942402018-10-29 18:27:35 -0400273
Ray Milkey05edbfc2018-10-23 14:23:16 -0700274 /** Enable administratively taking down single-homed hosts. */
275 boolean singleHomedDown = SINGLE_HOMED_DOWN_DEFAULT;
Saurav Dasec683dc2018-04-27 18:42:30 -0700276
Ray Milkey05edbfc2018-10-23 14:23:16 -0700277 /** Enable this to respond to ARP/NDP requests from unknown hosts. */
278 boolean respondToUnknownHosts = RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700279
Ray Milkey05edbfc2018-10-23 14:23:16 -0700280 /** Program flows and groups to pop and route double tagged hosts. */
281 boolean routeDoubleTaggedHosts = ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
Charles Chan4eb73bb2018-07-19 14:55:31 -0700282
Ray Milkey05edbfc2018-10-23 14:23:16 -0700283 /** internal vlan assigned by default to unconfigured ports. */
284 private int defaultInternalVlan = DEFAULT_INTERNAL_VLAN_DEFAULT;
Saurav Das9bf49582018-08-13 15:34:26 -0700285
Ray Milkey05edbfc2018-10-23 14:23:16 -0700286 /** vlan used for transport of pseudowires between switches. */
287 private int pwTransportVlan = PW_TRANSPORT_VLAN_DEFAULT;
Saurav Das9bf49582018-08-13 15:34:26 -0700288
psneha86e60d32019-03-26 06:31:41 -0400289 /** Enabling route simplification. */
290 boolean routeSimplification = ROUTE_SIMPLIFICATION_DEFAULT;
291
Charles Chandebfea32016-10-24 14:52:01 -0700292 ArpHandler arpHandler = null;
293 IcmpHandler icmpHandler = null;
294 IpHandler ipHandler = null;
295 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700296 ApplicationId appId;
297 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700298
Charles Chandebfea32016-10-24 14:52:01 -0700299 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700300 private TunnelHandler tunnelHandler = null;
301 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700302 private InternalPacketProcessor processor = null;
303 private InternalLinkListener linkListener = null;
304 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700305 private AppConfigHandler appCfgHandler = null;
Pier Luigi69f774d2018-02-28 12:10:50 +0100306 public XConnectHandler xConnectHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800307 McastHandler mcastHandler = null;
308 HostHandler hostHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800309 private RouteHandler routeHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800310 LinkHandler linkHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800311 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800312 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigid8a15162018-02-15 16:33:08 +0100313 private TopologyHandler topologyHandler = null;
Charles Chan82ab1932016-01-30 23:22:37 -0800314 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700315 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
316 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chandebfea32016-10-24 14:52:01 -0700317 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigid8a15162018-02-15 16:33:08 +0100318 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
Charles Chanfbcb8812018-04-18 18:41:05 -0700319 private final InternalMastershipListener mastershipListener = new InternalMastershipListener();
Saurav Das00e553b2018-04-21 17:19:48 -0700320 final InternalClusterListener clusterListener = new InternalClusterListener();
Andrea Campanella060cad82018-04-17 12:09:34 +0200321 //Completable future for network configuration process to buffer config events handling during activation
322 private CompletableFuture<Boolean> networkConfigCompletion = null;
Ray Milkey521e4392018-11-16 15:15:14 -0800323 private final Object networkConfigCompletionLock = new Object();
Charles Chan39b75522018-04-21 00:44:29 -0700324 private List<Event> queuedEvents = new CopyOnWriteArrayList<>();
sangho80f11cb2015-04-01 13:05:26 -0700325
Charles Chan52003922018-04-05 16:31:15 -0700326 // Handles device, link, topology and network config events
Charles Chanfbcb8812018-04-18 18:41:05 -0700327 private ScheduledExecutorService mainEventExecutor;
sangho80f11cb2015-04-01 13:05:26 -0700328
Charles Chanfbcb8812018-04-18 18:41:05 -0700329 // Handles host, route and mcast events respectively
330 private ScheduledExecutorService hostEventExecutor;
331 private ScheduledExecutorService routeEventExecutor;
332 private ScheduledExecutorService mcastEventExecutor;
Charles Chan77cdde42018-05-08 21:35:50 -0700333 private ExecutorService packetExecutor;
Charles Chana7601c92019-01-23 15:03:17 -0800334 ExecutorService neighborExecutor;
Charles Chan52003922018-04-05 16:31:15 -0700335
336 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chanb7f75ac2016-01-11 18:28:54 -0800337 /**
Saurav Das261c3002017-06-13 15:35:54 -0700338 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700339 * Used to keep track on MPLS group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800340 */
Charles Chan873661e2017-11-30 15:37:50 -0800341 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das261c3002017-06-13 15:35:54 -0700342 dsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800343 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700344 * Per device next objective ID store with (device id + vlanid) as key.
345 * Used to keep track on L2 flood group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800346 */
Charles Chan873661e2017-11-30 15:37:50 -0800347 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800348 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800349 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700350 * Per device next objective ID store with (device id + port + treatment + meta) as key.
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000351 * Used to keep track on L2 interface group and L3 unicast group information for direct hosts.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800352 */
Charles Chan873661e2017-11-30 15:37:50 -0800353 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800354 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800355
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700356 /**
357 * Per port dummy VLAN ID store with (connect point + ip address) as key.
358 * Used to keep track on dummy VLAN ID allocation.
359 */
360 private EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId>
361 dummyVlanIdStore = null;
362
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000363 /**
364 * Per device next objective ID store with (device id + MAC address + vlan) as key.
365 * Used to keep track of L3 unicast group for indirect hosts.
366 */
367 private EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer>
368 macVlanNextObjStore = null;
369
Saurav Das2d94d312015-11-24 23:21:05 -0800370 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
371 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700372
Saurav Das261c3002017-06-13 15:35:54 -0700373 private AtomicBoolean programmingScheduled = new AtomicBoolean();
374
Charles Chanc91c8782016-03-30 17:54:24 -0700375 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700376 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
377 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700378 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700379 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800380 public SegmentRoutingDeviceConfig createConfig() {
381 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700382 }
383 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800384
Charles Chanc91c8782016-03-30 17:54:24 -0700385 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700386 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
387 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700388 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800389 @Override
390 public SegmentRoutingAppConfig createConfig() {
391 return new SegmentRoutingAppConfig();
392 }
393 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800394
Charles Chan82f19972016-05-17 13:13:55 -0700395 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
396 new ConfigFactory<ApplicationId, XConnectConfig>(
397 SubjectFactories.APP_SUBJECT_FACTORY,
398 XConnectConfig.class, "xconnect") {
399 @Override
400 public XConnectConfig createConfig() {
401 return new XConnectConfig();
402 }
403 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800404
Charles Chanc91c8782016-03-30 17:54:24 -0700405 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700406 new ConfigFactory<ApplicationId, McastConfig>(
407 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700408 McastConfig.class, "multicast") {
409 @Override
410 public McastConfig createConfig() {
411 return new McastConfig();
412 }
413 };
414
Charles Chan1963f4f2016-02-18 14:22:42 -0800415 /**
416 * Segment Routing App ID.
417 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800418 public static final String APP_NAME = "org.onosproject.segmentrouting";
Saurav Das7c305372015-10-28 12:39:42 -0700419
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700420 /**
421 * Minumum and maximum value of dummy VLAN ID to be allocated.
422 */
423 public static final int MIN_DUMMY_VLAN_ID = 2;
424 public static final int MAX_DUMMY_VLAN_ID = 4093;
425
Charles Chana7601c92019-01-23 15:03:17 -0800426 private static final int DEFAULT_POOL_SIZE = 32;
427
Saurav Dasec683dc2018-04-27 18:42:30 -0700428 Instant lastEdgePortEvent = Instant.EPOCH;
429
Charles Chanb4f9cd12019-02-27 11:46:32 -0800430 protected void bindXconnectService(XconnectService xconnectService) {
431 if (this.xconnectService == null) {
432 log.info("Binding XconnectService");
433 this.xconnectService = xconnectService;
434 } else {
435 log.warn("Trying to bind XconnectService but it is already bound");
436 }
437 }
438
439 protected void unbindXconnectService(XconnectService xconnectService) {
440 if (this.xconnectService == xconnectService) {
441 log.info("Unbinding XconnectService");
442 this.xconnectService = null;
443 } else {
444 log.warn("Trying to unbind XconnectService but it is already unbound");
445 }
446 }
447
sangho80f11cb2015-04-01 13:05:26 -0700448 @Activate
Charles Chan873661e2017-11-30 15:37:50 -0800449 protected void activate(ComponentContext context) {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800450 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700451
Charles Chana7601c92019-01-23 15:03:17 -0800452 mainEventExecutor = Executors.newSingleThreadScheduledExecutor(
453 groupedThreads("onos/sr", "event-main-%d", log));
454 hostEventExecutor = Executors.newSingleThreadScheduledExecutor(
455 groupedThreads("onos/sr", "event-host-%d", log));
456 routeEventExecutor = Executors.newSingleThreadScheduledExecutor(
457 groupedThreads("onos/sr", "event-route-%d", log));
458 mcastEventExecutor = Executors.newSingleThreadScheduledExecutor(
459 groupedThreads("onos/sr", "event-mcast-%d", log));
460 packetExecutor = Executors.newSingleThreadExecutor(groupedThreads("onos/sr", "packet-%d", log));
461 neighborExecutor = Executors.newFixedThreadPool(DEFAULT_POOL_SIZE,
462 groupedThreads("onos/sr", "neighbor-%d", log));
Charles Chanfbcb8812018-04-18 18:41:05 -0700463
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700464 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das261c3002017-06-13 15:35:54 -0700465 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700466 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das261c3002017-06-13 15:35:54 -0700467 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700468 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700469 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700470 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700471 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700472 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700473
Charles Chan10b0fb72017-02-02 16:20:42 -0800474 log.debug("Creating EC map vlannextobjectivestore");
475 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
476 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
477 vlanNextObjStore = vlanNextObjMapBuilder
478 .withName("vlannextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700479 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700480 .withTimestampProvider((k, v) -> new WallClockTimestamp())
481 .build();
482
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000483 log.debug("Creating EC map macvlannextobjectivestore");
484 EventuallyConsistentMapBuilder<MacVlanNextObjectiveStoreKey, Integer>
485 macVlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
486 macVlanNextObjStore = macVlanNextObjMapBuilder
487 .withName("macvlannextobjectivestore")
488 .withSerializer(createSerializer())
489 .withTimestampProvider((k, v) -> new WallClockTimestamp())
490 .build();
491
Saurav Das2d94d312015-11-24 23:21:05 -0800492 log.debug("Creating EC map subnetnextobjectivestore");
493 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
494 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
495 portNextObjStore = portNextObjMapBuilder
496 .withName("portnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700497 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800498 .withTimestampProvider((k, v) -> new WallClockTimestamp())
499 .build();
500
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700501 EventuallyConsistentMapBuilder<DummyVlanIdStoreKey, VlanId>
502 dummyVlanIdMapBuilder = storageService.eventuallyConsistentMapBuilder();
503 dummyVlanIdStore = dummyVlanIdMapBuilder
504 .withName("dummyvlanidstore")
505 .withSerializer(createSerializer())
506 .withTimestampProvider((k, v) -> new WallClockTimestamp())
507 .build();
508
sangho4a5c42a2015-05-20 22:16:38 -0700509 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
510 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700511 tunnelStore = tunnelMapBuilder
512 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700513 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700514 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700515 .build();
516
517 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
518 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700519 policyStore = policyMapBuilder
520 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700521 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700522 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700523 .build();
524
Saurav Dasc3604f12016-03-23 11:22:49 -0700525 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700526 "purgeOnDisconnection", "true", false);
Saurav Dasc3604f12016-03-23 11:22:49 -0700527 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700528 "purgeOnDisconnection", "true", false);
Pier Luigib9632ba2017-01-12 18:14:58 -0800529 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
Charles Chan0fa51db2019-04-10 17:12:20 -0700530 "requestInterceptsEnabled", "false", false);
Charles Chanc1909e12017-08-08 15:13:37 -0700531 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700532 "requestInterceptsEnabled", "false", false);
Charles Chan9597f8d2017-07-24 15:56:10 -0700533 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700534 "arpEnabled", "false", false);
Pier Luigi0ebeb312017-02-02 22:31:34 -0800535 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700536 "greedyLearningIpv6", "true", false);
Charles Chancf8ea472017-02-28 15:15:17 -0800537 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700538 "forceUnprovision", "true", false);
Charles Chanc7b73c72017-08-10 16:57:28 -0700539 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan0fa51db2019-04-10 17:12:20 -0700540 "distributed", "true", false);
Charles Chan804772f2017-08-14 11:42:11 -0700541 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
Charles Chan0fa51db2019-04-10 17:12:20 -0700542 "multihomingEnabled", "true", false);
Charles Chan0c9c19f2017-11-29 19:54:20 -0800543 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
Charles Chan0fa51db2019-04-10 17:12:20 -0700544 "staleLinkAge", "15000", false);
Charles Chan0c9c19f2017-11-29 19:54:20 -0800545 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700546 "allowDuplicateIps", "false", false);
Yi Tsenga7da8d82018-08-19 03:09:54 +0800547 // For P4 switches
548 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700549 "fallbackFlowPollFrequency", "4", false);
Yi Tsenga7da8d82018-08-19 03:09:54 +0800550 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Charles Chan0fa51db2019-04-10 17:12:20 -0700551 "fallbackGroupPollFrequency", "3", false);
Charles Chan873661e2017-11-30 15:37:50 -0800552 compCfgService.registerProperties(getClass());
553 modified(context);
Saurav Dasc3604f12016-03-23 11:22:49 -0700554
Charles Chan2b078ae2015-10-14 11:24:40 -0700555 processor = new InternalPacketProcessor();
556 linkListener = new InternalLinkListener();
557 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700558 appCfgHandler = new AppConfigHandler(this);
559 xConnectHandler = new XConnectHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700560 mcastHandler = new McastHandler(this);
561 hostHandler = new HostHandler(this);
Saurav Dase6c448a2018-01-18 12:07:33 -0800562 linkHandler = new LinkHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700563 routeHandler = new RouteHandler(this);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800564 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800565 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Pier Luigid8a15162018-02-15 16:33:08 +0100566 topologyHandler = new TopologyHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700567
Charles Chand6d25332016-02-26 22:19:52 -0800568 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700569 cfgService.registerConfigFactory(deviceConfigFactory);
570 cfgService.registerConfigFactory(appConfigFactory);
Charles Chan82f19972016-05-17 13:13:55 -0700571 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700572 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase321cff2018-02-09 17:26:45 -0800573 log.info("Configuring network before adding listeners");
Andrea Campanella060cad82018-04-17 12:09:34 +0200574
Charles Chan6961f222018-01-04 14:26:07 -0800575 cfgListener.configureNetwork();
576
Charles Chan82ab1932016-01-30 23:22:37 -0800577 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700578 packetService.addProcessor(processor, PacketProcessor.director(2));
579 linkService.addListener(linkListener);
580 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700581 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700582 routeService.addListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100583 topologyService.addListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700584 mastershipService.addListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700585 clusterService.addListener(clusterListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700586
Charles Chanc12c3d02018-03-09 15:53:44 -0800587 linkHandler.init();
Andreas Pantelopoulos75eef062018-01-11 07:53:48 -0800588 l2TunnelHandler.init();
589
Ray Milkey521e4392018-11-16 15:15:14 -0800590 synchronized (networkConfigCompletionLock) {
591 networkConfigCompletion.whenComplete((value, ex) -> {
592 //setting to null for easier fall through
593 networkConfigCompletion = null;
594 //process all queued events
595 queuedEvents.forEach(event -> {
596 mainEventExecutor.execute(new InternalEventHandler(event));
597 });
Andrea Campanella060cad82018-04-17 12:09:34 +0200598 });
Ray Milkey521e4392018-11-16 15:15:14 -0800599 }
Andrea Campanella060cad82018-04-17 12:09:34 +0200600
sangho80f11cb2015-04-01 13:05:26 -0700601 log.info("Started");
602 }
603
Saurav Dase6c448a2018-01-18 12:07:33 -0800604 KryoNamespace.Builder createSerializer() {
Jonathan Hart54541d12016-04-12 15:39:44 -0700605 return new KryoNamespace.Builder()
606 .register(KryoNamespaces.API)
Saurav Das261c3002017-06-13 15:35:54 -0700607 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800608 VlanNextObjectiveStoreKey.class,
609 DestinationSet.class,
Andreas Pantelopoulos92998c92018-05-29 13:11:14 -0700610 DestinationSet.DestinationSetType.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800611 NextNeighbors.class,
612 Tunnel.class,
613 DefaultTunnel.class,
614 Policy.class,
615 TunnelPolicy.class,
616 Policy.Type.class,
617 PortNextObjectiveStoreKey.class,
618 XConnectStoreKey.class,
619 L2Tunnel.class,
620 L2TunnelPolicy.class,
621 DefaultL2Tunnel.class,
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700622 DefaultL2TunnelPolicy.class,
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000623 DummyVlanIdStoreKey.class,
624 MacVlanNextObjectiveStoreKey.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700625 );
626 }
627
sangho80f11cb2015-04-01 13:05:26 -0700628 @Deactivate
629 protected void deactivate() {
Charles Chanfbcb8812018-04-18 18:41:05 -0700630 mainEventExecutor.shutdown();
631 hostEventExecutor.shutdown();
632 routeEventExecutor.shutdown();
633 mcastEventExecutor.shutdown();
Charles Chan77cdde42018-05-08 21:35:50 -0700634 packetExecutor.shutdown();
Charles Chana7601c92019-01-23 15:03:17 -0800635 neighborExecutor.shutdown();
Charles Chanfbcb8812018-04-18 18:41:05 -0700636
Ray Milkeyaee4d3e2018-05-11 09:59:19 -0700637 mainEventExecutor = null;
638 hostEventExecutor = null;
639 routeEventExecutor = null;
640 mcastEventExecutor = null;
641 packetExecutor = null;
Charles Chana7601c92019-01-23 15:03:17 -0800642 neighborExecutor = null;
Ray Milkeyaee4d3e2018-05-11 09:59:19 -0700643
Charles Chan72f556a2015-10-05 17:50:33 -0700644 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700645 cfgService.unregisterConfigFactory(deviceConfigFactory);
646 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chandebfea32016-10-24 14:52:01 -0700647 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700648 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan873661e2017-11-30 15:37:50 -0800649 compCfgService.unregisterProperties(getClass(), false);
Charles Chan72f556a2015-10-05 17:50:33 -0700650
Charles Chanfd48c222017-06-19 00:43:31 -0700651 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700652 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700653 linkService.removeListener(linkListener);
654 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700655 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700656 routeService.removeListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100657 topologyService.removeListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700658 mastershipService.removeListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700659 clusterService.removeListener(clusterListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700660
Charles Chan2e71ef32017-02-23 15:44:08 -0800661 neighbourResolutionService.unregisterNeighbourHandlers(appId);
662
sangho80f11cb2015-04-01 13:05:26 -0700663 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700664 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700665 deviceListener = null;
Charles Chan05bb1702018-04-19 13:10:01 -0700666 groupHandlerMap.forEach((k, v) -> v.shutdown());
Charles Chan2b078ae2015-10-14 11:24:40 -0700667 groupHandlerMap.clear();
Saurav Das49368392018-04-23 18:42:12 -0700668 defaultRoutingHandler.shutdown();
Charles Chan2b078ae2015-10-14 11:24:40 -0700669
Saurav Das261c3002017-06-13 15:35:54 -0700670 dsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800671 vlanNextObjStore.destroy();
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000672 macVlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700673 portNextObjStore.destroy();
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700674 dummyVlanIdStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700675 tunnelStore.destroy();
676 policyStore.destroy();
Pier Luigi35dab3f2018-01-25 16:16:02 +0100677
678 mcastHandler.terminate();
sangho80f11cb2015-04-01 13:05:26 -0700679 log.info("Stopped");
680 }
681
Charles Chan873661e2017-11-30 15:37:50 -0800682 @Modified
683 private void modified(ComponentContext context) {
684 Dictionary<?, ?> properties = context.getProperties();
685 if (properties == null) {
686 return;
687 }
688
Ray Milkey48d10c02018-10-24 10:04:03 -0700689 String strActiveProbing = Tools.get(properties, PROP_ACTIVE_PROBING);
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700690 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProbing);
Charles Chan873661e2017-11-30 15:37:50 -0800691 if (expectActiveProbing != activeProbing) {
692 activeProbing = expectActiveProbing;
693 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
694 }
Saurav Dasec683dc2018-04-27 18:42:30 -0700695
Ray Milkeye96d0de2018-11-02 17:12:21 -0700696
697 String strSymmetricProbing = Tools.get(properties, PROP_SYMMETRIC_PROBING);
698 boolean expectSymmetricProbing = Boolean.parseBoolean(strSymmetricProbing);
Mayank Tiwarif7942402018-10-29 18:27:35 -0400699 if (expectSymmetricProbing != symmetricProbing) {
700 symmetricProbing = expectSymmetricProbing;
701 log.info("{} symmetric probing", symmetricProbing ? "Enabling" : "Disabling");
702 }
703
Ray Milkey48d10c02018-10-24 10:04:03 -0700704 String strSingleHomedDown = Tools.get(properties, PROP_SINGLE_HOMED_DOWN);
Saurav Dasec683dc2018-04-27 18:42:30 -0700705 boolean expectSingleHomedDown = Boolean.parseBoolean(strSingleHomedDown);
706 if (expectSingleHomedDown != singleHomedDown) {
707 singleHomedDown = expectSingleHomedDown;
708 log.info("{} downing of single homed hosts for lost uplinks",
709 singleHomedDown ? "Enabling" : "Disabling");
710 if (singleHomedDown && linkHandler != null) {
711 hostService.getHosts().forEach(host -> host.locations()
712 .forEach(loc -> {
713 if (interfaceService.isConfigured(loc)) {
714 linkHandler.checkUplinksForHost(loc);
715 }
716 }));
717 } else {
718 log.warn("Disabling singleHomedDown does not re-enable already "
719 + "downed ports for single-homed hosts");
720 }
721 }
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700722
Ray Milkey48d10c02018-10-24 10:04:03 -0700723 String strRespondToUnknownHosts = Tools.get(properties, PROP_RESPOND_TO_UNKNOWN_HOSTS);
Andreas Pantelopoulos6464d862018-06-18 12:50:35 -0700724 boolean expectRespondToUnknownHosts = Boolean.parseBoolean(strRespondToUnknownHosts);
725 if (expectRespondToUnknownHosts != respondToUnknownHosts) {
726 respondToUnknownHosts = expectRespondToUnknownHosts;
727 log.info("{} responding to ARPs/NDPs from unknown hosts", respondToUnknownHosts ? "Enabling" : "Disabling");
728 }
Charles Chan4eb73bb2018-07-19 14:55:31 -0700729
Ray Milkey48d10c02018-10-24 10:04:03 -0700730 String strRouteDoubleTaggedHosts = Tools.get(properties, PROP_ROUTE_DOUBLE_TAGGED_HOSTS);
Charles Chanfbca55e2018-07-24 16:40:35 -0700731 boolean expectRouteDoubleTaggedHosts = Boolean.parseBoolean(strRouteDoubleTaggedHosts);
732 if (expectRouteDoubleTaggedHosts != routeDoubleTaggedHosts) {
733 routeDoubleTaggedHosts = expectRouteDoubleTaggedHosts;
734 log.info("{} routing for double tagged hosts", routeDoubleTaggedHosts ? "Enabling" : "Disabling");
Charles Chan4eb73bb2018-07-19 14:55:31 -0700735
Charles Chanfbca55e2018-07-24 16:40:35 -0700736 if (routeDoubleTaggedHosts) {
Charles Chan4eb73bb2018-07-19 14:55:31 -0700737 hostHandler.populateAllDoubleTaggedHost();
738 } else {
739 hostHandler.revokeAllDoubleTaggedHost();
740 }
741 }
Saurav Das9bf49582018-08-13 15:34:26 -0700742
Ray Milkey48d10c02018-10-24 10:04:03 -0700743 String strDefaultInternalVlan = Tools.get(properties, PROP_DEFAULT_INTERNAL_VLAN);
Saurav Das9bf49582018-08-13 15:34:26 -0700744 int defIntVlan = Integer.parseInt(strDefaultInternalVlan);
745 if (defIntVlan != defaultInternalVlan) {
746 if (canUseVlanId(defIntVlan)) {
747 log.warn("Default internal vlan value changed from {} to {}.. "
748 + "re-programming filtering rules, but NOT any groups already "
749 + "created with the former value", defaultInternalVlan, defIntVlan);
750 VlanId oldDefIntVlan = VlanId.vlanId((short) defaultInternalVlan);
751 defaultInternalVlan = defIntVlan;
752 routingRulePopulator
753 .updateSpecialVlanFilteringRules(true, oldDefIntVlan,
754 VlanId.vlanId((short) defIntVlan));
755 } else {
756 log.warn("Cannot change default internal vlan to unusable "
757 + "value {}", defIntVlan);
758 }
759 }
760
Ray Milkey48d10c02018-10-24 10:04:03 -0700761 String strPwTxpVlan = Tools.get(properties, PROP_PW_TRANSPORT_VLAN);
Saurav Das9bf49582018-08-13 15:34:26 -0700762 int pwTxpVlan = Integer.parseInt(strPwTxpVlan);
763 if (pwTxpVlan != pwTransportVlan) {
764 if (canUseVlanId(pwTxpVlan)) {
765 log.warn("Pseudowire transport vlan value changed from {} to {}.. "
766 + "re-programming filtering rules, but NOT any groups already "
767 + "created with the former value", pwTransportVlan,
768 pwTxpVlan);
769 VlanId oldPwTxpVlan = VlanId.vlanId((short) pwTransportVlan);
770 pwTransportVlan = pwTxpVlan;
771 routingRulePopulator
772 .updateSpecialVlanFilteringRules(false, oldPwTxpVlan,
773 VlanId.vlanId((short) pwTxpVlan));
774 } else {
775 log.warn("Cannot change pseudowire transport vlan to unusable "
776 + "value {}", pwTxpVlan);
777 }
778 }
779
psneha86e60d32019-03-26 06:31:41 -0400780 String strRouteSimplification = Tools.get(properties, PROP_ROUTE_SIMPLIFICATION);
781 boolean expectRouteSimplification = Boolean.parseBoolean(strRouteSimplification);
782 if (expectRouteSimplification != routeSimplification) {
783 routeSimplification = expectRouteSimplification;
784 log.info("{} route simplification", routeSimplification ? "Enabling" : "Disabling");
785 }
786
Saurav Das9bf49582018-08-13 15:34:26 -0700787 }
788
789 /**
790 * Returns true if given vlan id is not being used in the system currently,
791 * either as one of the default system wide vlans or as one of the
792 * configured interface vlans.
793 *
794 * @param vlanId given vlan id
795 * @return true if vlan is not currently in use
796 */
797 public boolean canUseVlanId(int vlanId) {
798 if (vlanId >= 4095 || vlanId <= 1) {
799 log.error("Vlan id {} value is not in valid range 2 <--> 4094",
800 vlanId);
801 return false;
802 }
803
804 VlanId vid = VlanId.vlanId((short) vlanId);
805 if (getDefaultInternalVlan().equals(vid) || getPwTransportVlan().equals(vid)) {
806 log.warn("Vlan id {} value is already in use system-wide. "
807 + "DefaultInternalVlan:{} PwTransportVlan:{} ", vlanId,
808 getDefaultInternalVlan(), getPwTransportVlan());
809 return false;
810 }
811
812 if (interfaceService.inUse(vid)) {
813 log.warn("Vlan id {} value is already in use on a configured "
814 + "interface in the system", vlanId);
815 return false;
816 }
817 return true;
818 }
819
820 /**
821 * Returns the VlanId assigned internally by default to unconfigured ports.
822 *
823 * @return the default internal vlan id
824 */
825 public VlanId getDefaultInternalVlan() {
826 return VlanId.vlanId((short) defaultInternalVlan);
827 }
828
829 /**
830 * Returns the Vlan id used to transport pseudowire traffic across the
831 * network.
832 *
833 * @return the pseudowire transport vlan id
834 */
835 public VlanId getPwTransportVlan() {
836 return VlanId.vlanId((short) pwTransportVlan);
Charles Chan873661e2017-11-30 15:37:50 -0800837 }
838
sangho27462c62015-05-14 00:39:53 -0700839 @Override
840 public List<Tunnel> getTunnels() {
841 return tunnelHandler.getTunnels();
842 }
843
844 @Override
sanghobd812f82015-06-29 14:58:47 -0700845 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
846 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700847 }
848
849 @Override
sanghobd812f82015-06-29 14:58:47 -0700850 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700851 for (Policy policy: policyHandler.getPolicies()) {
852 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
853 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
854 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
855 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700856 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700857 }
858 }
859 }
sanghobd812f82015-06-29 14:58:47 -0700860 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700861 }
862
863 @Override
sanghobd812f82015-06-29 14:58:47 -0700864 public PolicyHandler.Result removePolicy(Policy policy) {
865 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700866 }
867
868 @Override
sanghobd812f82015-06-29 14:58:47 -0700869 public PolicyHandler.Result createPolicy(Policy policy) {
870 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700871 }
872
873 @Override
874 public List<Policy> getPolicies() {
875 return policyHandler.getPolicies();
876 }
877
Saurav Das07c74602016-04-27 18:35:50 -0700878 @Override
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700879 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
880 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700881 }
882
883 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800884 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700885 return l2TunnelHandler.getL2Tunnels();
886 }
887
888 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800889 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700890 return l2TunnelHandler.getL2Policies();
891 }
892
Saurav Dasdebcf882018-04-06 20:16:01 -0700893 @Override
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700894 @Deprecated
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700895 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
896
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700897 // get both added and pending pseudowires
898 List<L2TunnelDescription> pseudowires = new ArrayList<>();
899 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
900 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700901 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700902
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700903 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
904
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700905 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
906 L2TunnelHandler.Result res;
907 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
908 res = addPseudowire(pw);
909 if (res != L2TunnelHandler.Result.SUCCESS) {
910 log.error("Pseudowire with id {} can not be instantiated !", res);
911 retRes = res;
912 }
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700913 }
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700914
915 return retRes;
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700916 }
917
918 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800919 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700920 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700921 }
922
923 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800924 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700925 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700926 }
927
928 @Override
Saurav Das07c74602016-04-27 18:35:50 -0700929 public void rerouteNetwork() {
930 cfgListener.configureNetwork();
Saurav Das07c74602016-04-27 18:35:50 -0700931 }
932
Charles Chand7844e52016-10-20 17:02:44 -0700933 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800934 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
935 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart61e24e12017-11-30 18:23:42 -0800936 deviceConfiguration.getRouters().forEach(device ->
937 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chand7844e52016-10-20 17:02:44 -0700938 return deviceSubnetMap;
939 }
940
Saurav Das62ae6792017-05-15 15:34:25 -0700941
942 @Override
943 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
944 if (defaultRoutingHandler != null) {
945 return defaultRoutingHandler.getCurrentEmcpSpgMap();
946 } else {
947 return null;
948 }
949 }
950
951 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700952 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDstNextObjStore() {
Saurav Das261c3002017-06-13 15:35:54 -0700953 if (dsNextObjStore != null) {
954 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Das62ae6792017-05-15 15:34:25 -0700955 } else {
956 return ImmutableMap.of();
957 }
958 }
959
Saurav Dasfbe74572017-08-03 18:30:35 -0700960 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700961 public ImmutableMap<VlanNextObjectiveStoreKey, Integer> getVlanNextObjStore() {
962 if (vlanNextObjStore != null) {
963 return ImmutableMap.copyOf(vlanNextObjStore.entrySet());
964 } else {
965 return ImmutableMap.of();
966 }
967 }
968
969 @Override
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +0000970 public ImmutableMap<MacVlanNextObjectiveStoreKey, Integer> getMacVlanNextObjStore() {
971 if (macVlanNextObjStore != null) {
972 return ImmutableMap.copyOf(macVlanNextObjStore.entrySet());
973 } else {
974 return ImmutableMap.of();
975 }
976 }
977
978 @Override
Charles Chan0b1dd7e2018-08-19 19:21:46 -0700979 public ImmutableMap<PortNextObjectiveStoreKey, Integer> getPortNextObjStore() {
980 if (portNextObjStore != null) {
981 return ImmutableMap.copyOf(portNextObjStore.entrySet());
982 } else {
983 return ImmutableMap.of();
984 }
985 }
986
987 @Override
988 public ImmutableMap<String, NextObjective> getPwInitNext() {
989 if (l2TunnelHandler != null) {
990 return l2TunnelHandler.getInitNext();
991 } else {
992 return ImmutableMap.of();
993 }
994 }
995
996 @Override
997 public ImmutableMap<String, NextObjective> getPwTermNext() {
998 if (l2TunnelHandler != null) {
999 return l2TunnelHandler.getTermNext();
1000 } else {
1001 return ImmutableMap.of();
1002 }
1003 }
1004
1005 @Override
1006 public void invalidateNextObj(int nextId) {
1007 if (dsNextObjStore != null) {
1008 dsNextObjStore.entrySet().forEach(e -> {
1009 if (e.getValue().nextId() == nextId) {
1010 dsNextObjStore.remove(e.getKey());
1011 }
1012 });
1013 }
1014 if (vlanNextObjStore != null) {
1015 vlanNextObjStore.entrySet().forEach(e -> {
1016 if (e.getValue() == nextId) {
1017 vlanNextObjStore.remove(e.getKey());
1018 }
1019 });
1020 }
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001021 if (macVlanNextObjStore != null) {
1022 macVlanNextObjStore.entrySet().forEach(e -> {
1023 if (e.getValue() == nextId) {
1024 macVlanNextObjStore.remove(e.getKey());
1025 }
1026 });
1027 }
Charles Chan0b1dd7e2018-08-19 19:21:46 -07001028 if (portNextObjStore != null) {
1029 portNextObjStore.entrySet().forEach(e -> {
1030 if (e.getValue() == nextId) {
1031 portNextObjStore.remove(e.getKey());
1032 }
1033 });
1034 }
1035 if (mcastHandler != null) {
1036 mcastHandler.removeNextId(nextId);
1037 }
1038 if (l2TunnelHandler != null) {
1039 l2TunnelHandler.removeNextId(nextId);
1040 }
1041 if (xconnectService != null) {
1042 xconnectService.removeNextId(nextId);
1043 }
1044 }
1045
1046 @Override
Saurav Dasfbe74572017-08-03 18:30:35 -07001047 public void verifyGroups(DeviceId id) {
1048 DefaultGroupHandler gh = groupHandlerMap.get(id);
1049 if (gh != null) {
1050 gh.triggerBucketCorrector();
1051 }
1052 }
1053
Saurav Das6430f412018-01-25 09:49:01 -08001054 @Override
1055 public ImmutableMap<Link, Boolean> getSeenLinks() {
1056 return linkHandler.getSeenLinks();
1057 }
1058
1059 @Override
1060 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
1061 return linkHandler.getDownedPorts();
1062 }
1063
Pier Luigi0f9635b2018-01-15 18:06:43 +01001064 @Override
1065 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
Charles Chan0b1dd7e2018-08-19 19:21:46 -07001066 return mcastHandler.getNextIds(mcastIp);
Pier Luigi0f9635b2018-01-15 18:06:43 +01001067 }
1068
1069 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +01001070 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigi0f9635b2018-01-15 18:06:43 +01001071 return mcastHandler.getMcastRoles(mcastIp);
1072 }
1073
1074 @Override
Piere99511d2018-04-19 16:47:06 +02001075 public Map<McastRoleStoreKey, McastRole> getMcastRoles(IpAddress mcastIp, ConnectPoint sourcecp) {
1076 return mcastHandler.getMcastRoles(mcastIp, sourcecp);
1077 }
1078
1079 @Override
Pier Luigi0f9635b2018-01-15 18:06:43 +01001080 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
1081 return mcastHandler.getMcastPaths(mcastIp);
1082 }
1083
Pierdb27b8d2018-04-17 16:29:56 +02001084 @Override
Pier71c55772018-04-17 17:25:22 +02001085 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
1086 ConnectPoint sourcecp) {
1087 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
1088 }
1089
1090 @Override
Pierdb27b8d2018-04-17 16:29:56 +02001091 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
1092 return mcastHandler.getMcastLeaders(mcastIp);
1093 }
1094
Charles Chanb13e0702018-04-17 18:56:53 -07001095 @Override
1096 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
1097 return defaultRoutingHandler == null ? ImmutableMap.of() :
1098 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
1099 }
1100
1101 @Override
1102 public Map<DeviceId, Boolean> getShouldProgramCache() {
1103 return defaultRoutingHandler == null ? ImmutableMap.of() :
1104 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
1105 }
1106
Charles Chan8d316332018-06-19 20:31:57 -07001107 @Override
jayakumarthazhath66c9ec12018-10-01 00:51:54 +05301108 public boolean shouldProgram(DeviceId deviceId) {
1109 return defaultRoutingHandler.shouldProgram(deviceId);
1110 }
1111
1112 @Override
Ray Milkeyb85de082017-04-05 09:42:04 -07001113 public ApplicationId appId() {
1114 return appId;
1115 }
1116
1117 /**
1118 * Returns the device configuration.
1119 *
1120 * @return device configuration
1121 */
1122 public DeviceConfiguration deviceConfiguration() {
1123 return deviceConfiguration;
1124 }
1125
1126 /**
Saurav Das261c3002017-06-13 15:35:54 -07001127 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -07001128 * Used to keep track on MPLS group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001129 *
1130 * @return next objective ID store
1131 */
Saurav Das261c3002017-06-13 15:35:54 -07001132 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
1133 dsNextObjStore() {
1134 return dsNextObjStore;
Ray Milkeyb85de082017-04-05 09:42:04 -07001135 }
1136
1137 /**
Charles Chan53de91f2017-08-22 15:07:34 -07001138 * Per device next objective ID store with (device id + vlanid) as key.
1139 * Used to keep track on L2 flood group information.
Ray Milkeyb85de082017-04-05 09:42:04 -07001140 *
1141 * @return vlan next object store
1142 */
1143 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
1144 return vlanNextObjStore;
1145 }
1146
1147 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001148 * Per device next objective ID store with (device id + MAC address + vlan) as key.
1149 * Used to keep track on L3 Unicast group information for indirect hosts.
1150 *
1151 * @return mac vlan next object store
1152 */
1153 public EventuallyConsistentMap<MacVlanNextObjectiveStoreKey, Integer> macVlanNextObjStore() {
1154 return macVlanNextObjStore;
1155 }
1156
1157 /**
Charles Chan53de91f2017-08-22 15:07:34 -07001158 * Per device next objective ID store with (device id + port + treatment + meta) as key.
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001159 * Used to keep track on L2 interface group and L3 unicast group information for direct hosts.
Ray Milkeyb85de082017-04-05 09:42:04 -07001160 *
1161 * @return port next object store.
1162 */
1163 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
1164 return portNextObjStore;
1165 }
1166
1167 /**
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001168 * Per port dummy VLAN ID store with (connect point + ip address) as key.
1169 * Used to keep track on dummy VLAN ID allocation.
1170 *
1171 * @return dummy vlan id store.
1172 */
1173 public EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId> dummyVlanIdStore() {
1174 return dummyVlanIdStore;
1175 }
1176
1177 /**
Saurav Das261c3002017-06-13 15:35:54 -07001178 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
1179 * labeled packets should be programmed or not.
Pier Ventre7a78de22016-10-31 15:00:01 -07001180 *
1181 * @return MPLS-ECMP value
1182 */
1183 public boolean getMplsEcmp() {
1184 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
1185 .getConfig(this.appId, SegmentRoutingAppConfig.class);
1186 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
1187 }
1188
1189 /**
sangho80f1f892015-05-19 11:57:42 -07001190 * Returns the tunnel object with the tunnel ID.
1191 *
1192 * @param tunnelId Tunnel ID
1193 * @return Tunnel reference
1194 */
sangho27462c62015-05-14 00:39:53 -07001195 public Tunnel getTunnel(String tunnelId) {
1196 return tunnelHandler.getTunnel(tunnelId);
1197 }
1198
Charles Chan8d316332018-06-19 20:31:57 -07001199 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +01001200 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chan098ca202018-05-01 11:50:20 -07001201 VlanId untaggedVlanId = interfaceService.getUntaggedVlanId(connectPoint);
1202 VlanId nativeVlanId = interfaceService.getNativeVlanId(connectPoint);
Charles Chand9265a32017-06-16 15:19:24 -07001203 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
1204 }
1205
Charles Chan8d316332018-06-19 20:31:57 -07001206 @Override
1207 public Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001208 SegmentRoutingDeviceConfig deviceConfig =
1209 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1210 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
1211 }
Charles Chan8d316332018-06-19 20:31:57 -07001212
1213 @Override
Saurav Dasec683dc2018-04-27 18:42:30 -07001214 public Optional<PortNumber> getPairLocalPort(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001215 SegmentRoutingDeviceConfig deviceConfig =
1216 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
1217 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
1218 }
1219
1220 /**
Charles Chanf0ae41e2017-08-23 13:55:39 -07001221 * Returns locations of given resolved route.
1222 *
1223 * @param resolvedRoute resolved route
1224 * @return locations of nexthop. Might be empty if next hop is not found
1225 */
Charles Chan19b70032019-04-17 14:20:26 -07001226 public Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
Charles Chanf0ae41e2017-08-23 13:55:39 -07001227 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
1228 return Optional.ofNullable(hostService.getHost(hostId))
1229 .map(Host::locations).orElse(Sets.newHashSet())
1230 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
1231 }
1232
1233 /**
Charles Chan90772a72017-02-08 15:52:08 -08001234 * Returns vlan port map of given device.
1235 *
1236 * @param deviceId device id
1237 * @return vlan-port multimap
1238 */
1239 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
1240 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
1241
1242 interfaceService.getInterfaces().stream()
1243 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
1244 .forEach(intf -> {
1245 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan3ed34d82017-06-22 18:03:14 -07001246 intf.vlanTagged().forEach(vlanTagged ->
1247 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
1248 );
Charles Chan90772a72017-02-08 15:52:08 -08001249 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
1250 });
1251 vlanPortMap.removeAll(VlanId.NONE);
1252
1253 return vlanPortMap;
1254 }
1255
1256 /**
Charles Chan10b0fb72017-02-02 16:20:42 -08001257 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -08001258 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -07001259 *
1260 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -08001261 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -08001262 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -07001263 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001264 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -07001265 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -08001266 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
1267 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -07001268 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -08001269 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -08001270 + "device {} not found", deviceId);
1271 return -1;
1272 }
1273 }
1274
1275 /**
1276 * Returns the next objective ID for the given portNumber, given the treatment.
1277 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -07001278 * would result in different objectives. If the next object does not exist,
1279 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -08001280 *
1281 * @param deviceId Device ID
1282 * @param portNum port number on device for which NextObjective is queried
1283 * @param treatment the actions to apply on the packets (should include outport)
1284 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -07001285 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -07001286 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -08001287 */
1288 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
1289 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -07001290 TrafficSelector meta,
1291 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -08001292 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1293 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -07001294 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -08001295 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -08001296 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
1297 + " not found", deviceId);
1298 return -1;
1299 }
1300 }
1301
Saurav Das62ae6792017-05-15 15:34:25 -07001302 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001303 * Returns the next Objective ID for the given mac and vlan, given the treatment.
1304 * There could be multiple different treatments to the same outport, which
1305 * would result in different objectives. If the next object does not exist,
1306 * and should be created, a new one is created and its id is returned.
1307 *
1308 * @param deviceId Device ID
1309 * @param macAddr mac of host for which Next ID is required.
1310 * @param vlanId vlan of host for which Next ID is required.
Ruchi Sahota07869322019-05-09 17:26:14 -04001311 * @param port port with which to create the Next Obj.
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001312 * @param createIfMissing true if a next object should be created if not found
1313 * @return next objective ID or -1 if an error occurred during retrieval or creation
1314 */
1315 public int getMacVlanNextObjectiveId(DeviceId deviceId, MacAddress macAddr, VlanId vlanId,
Ruchi Sahota07869322019-05-09 17:26:14 -04001316 PortNumber port, boolean createIfMissing) {
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001317 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1318 if (ghdlr != null) {
Ruchi Sahota07869322019-05-09 17:26:14 -04001319 return ghdlr.getMacVlanNextObjectiveId(macAddr, vlanId, port, createIfMissing);
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001320 } else {
1321 log.warn("getMacVlanNextObjectiveId query - groupHandler for device {}"
1322 + " not found", deviceId);
1323 return -1;
1324 }
1325 }
1326
1327 /**
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001328 * Updates the next objective for the given nextId .
1329 *
1330 * @param deviceId Device ID
1331 * @param hostMac mac of host for which Next obj is to be updated.
1332 * @param hostVlanId vlan of host for which Next obj is to be updated.
1333 * @param port port with which to update the Next Obj.
1334 * @param nextId of Next Obj which needs to be updated.
1335 */
1336 public void updateMacVlanTreatment(DeviceId deviceId, MacAddress hostMac,
1337 VlanId hostVlanId, PortNumber port, int nextId) {
1338 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
1339 if (ghdlr != null) {
1340 ghdlr.updateL3UcastGroupBucket(hostMac, hostVlanId, port, nextId);
1341 } else {
1342 log.warn("updateL3UcastGroupBucket query - groupHandler for device {}"
1343 + " not found", deviceId);
1344 }
1345 }
1346
1347
1348 /**
Saurav Das62ae6792017-05-15 15:34:25 -07001349 * Returns the group handler object for the specified device id.
1350 *
1351 * @param devId the device identifier
1352 * @return the groupHandler object for the device id, or null if not found
1353 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001354 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Das62ae6792017-05-15 15:34:25 -07001355 return groupHandlerMap.get(devId);
1356 }
1357
1358 /**
Saurav Dasfbe74572017-08-03 18:30:35 -07001359 * Returns the default routing handler object.
1360 *
1361 * @return the default routing handler object
1362 */
1363 public DefaultRoutingHandler getRoutingHandler() {
1364 return defaultRoutingHandler;
1365 }
1366
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001367 /**
1368 * Returns new dummy VLAN ID.
1369 * Dummy VLAN ID should be unique in each connect point.
1370 *
1371 * @param cp connect point
1372 * @param ipAddress IP address
1373 * @return new dummy VLAN ID. Returns VlanId.NONE if no VLAN ID is available.
1374 */
1375 public synchronized VlanId allocateDummyVlanId(ConnectPoint cp, IpAddress ipAddress) {
1376 Set<VlanId> usedVlanId = Sets.union(getVlanPortMap(cp.deviceId()).keySet(),
1377 dummyVlanIdStore.entrySet().stream().filter(entry ->
1378 (entry.getKey()).connectPoint().equals(cp))
1379 .map(Map.Entry::getValue)
1380 .collect(Collectors.toSet()));
1381
1382 VlanId dummyVlanId = IntStream.range(MIN_DUMMY_VLAN_ID, MAX_DUMMY_VLAN_ID).mapToObj(
1383 i -> VlanId.vlanId((short) (i & 0xFFFF))
1384 ).filter(vlanId -> !usedVlanId.contains(vlanId)).findFirst().orElse(VlanId.NONE);
1385
1386 if (!dummyVlanId.equals(VlanId.NONE)) {
1387 this.dummyVlanIdStore.put(new DummyVlanIdStoreKey(cp, ipAddress), dummyVlanId);
1388 log.debug("Dummy VLAN ID {} is allocated to {}, {}", dummyVlanId, cp, ipAddress);
1389 } else {
1390 log.error("Failed to allocate dummy VLAN ID for {}, {}", cp, ipAddress);
1391 }
1392 return dummyVlanId;
1393 }
1394
1395
sangho80f11cb2015-04-01 13:05:26 -07001396 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -07001397 @Override
1398 public void process(PacketContext context) {
Charles Chan77cdde42018-05-08 21:35:50 -07001399 packetExecutor.execute(() -> processPacketInternal(context));
1400 }
sangho80f11cb2015-04-01 13:05:26 -07001401
Charles Chan77cdde42018-05-08 21:35:50 -07001402 private void processPacketInternal(PacketContext context) {
sangho80f11cb2015-04-01 13:05:26 -07001403 if (context.isHandled()) {
1404 return;
1405 }
1406
1407 InboundPacket pkt = context.inPacket();
1408 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -08001409
1410 if (ethernet == null) {
1411 return;
1412 }
1413
Saurav Das261c3002017-06-13 15:35:54 -07001414 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1415 ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -08001416 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -07001417 log.warn("Received unexpected ARP packet on {}",
1418 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -07001419 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001420 return;
sangho80f11cb2015-04-01 13:05:26 -07001421 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -08001422 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1423 //ipHandler.addToPacketBuffer(ipv4Packet);
1424 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1425 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001426 } else {
Charles Chand041ad82017-01-13 17:20:44 -08001427 // NOTE: We don't support IP learning at this moment so this
1428 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001429 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001430 }
Pier Ventreb6a7f342016-11-26 21:05:22 -08001431 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1432 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -08001433 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -08001434 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -08001435 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1436 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1437 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1438 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1439 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1440 } else {
Saurav Das62ae6792017-05-15 15:34:25 -07001441 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -07001442 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -08001443 }
1444 } else {
1445 // NOTE: We don't support IP learning at this moment so this
1446 // is not necessary. Also it causes duplication of DHCPv6 packets.
1447 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1448 }
sangho80f11cb2015-04-01 13:05:26 -07001449 }
1450 }
1451 }
1452
sangho80f11cb2015-04-01 13:05:26 -07001453 private class InternalEventHandler implements Runnable {
Charles Chan52003922018-04-05 16:31:15 -07001454 private Event event;
1455
1456 InternalEventHandler(Event event) {
1457 this.event = event;
1458 }
1459
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001460 @Override
sangho80f11cb2015-04-01 13:05:26 -07001461 public void run() {
Charles Chan52003922018-04-05 16:31:15 -07001462 try {
1463 // TODO We should also change SR routing and PW to listen to TopologyEvents
1464 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1465 event.type() == LinkEvent.Type.LINK_UPDATED) {
1466 linkHandler.processLinkAdded((Link) event.subject());
1467 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1468 linkHandler.processLinkRemoved((Link) event.subject());
1469 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1470 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1471 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1472 DeviceId deviceId = ((Device) event.subject()).id();
1473 if (deviceService.isAvailable(deviceId)) {
1474 log.info("** DEVICE UP Processing device event {} "
1475 + "for available device {}",
1476 event.type(), ((Device) event.subject()).id());
1477 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001478 } else {
Charles Chanb8f3af52018-08-29 21:21:51 -07001479 if (event.type() == DeviceEvent.Type.DEVICE_ADDED) {
1480 // Note: For p4 devices, the device will be added but unavailable at the beginning.
1481 // The device will later on being marked as available once the pipeline is pushed
1482 // to the device.
1483 log.info("** DEVICE ADDED but unavailable. Ignore");
1484 return;
1485 }
Charles Chan52003922018-04-05 16:31:15 -07001486 log.info(" ** DEVICE DOWN Processing device event {}"
1487 + " for unavailable device {}",
1488 event.type(), ((Device) event.subject()).id());
1489 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001490 }
Charles Chan52003922018-04-05 16:31:15 -07001491 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1492 // typically these calls come when device is added first time
1493 // so port filtering rules are handled at the device_added event.
1494 // port added calls represent all ports on the device,
1495 // enabled or not.
1496 log.trace("** PORT ADDED {}/{} -> {}",
1497 ((DeviceEvent) event).subject().id(),
1498 ((DeviceEvent) event).port().number(),
1499 event.type());
1500 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1501 // these calls happen for every subsequent event
1502 // ports enabled, disabled, switch goes away, comes back
1503 log.info("** PORT UPDATED {}/{} -> {}",
1504 event.subject(),
1505 ((DeviceEvent) event).port(),
1506 event.type());
Saurav Dasec683dc2018-04-27 18:42:30 -07001507 processPortUpdatedInternal(((Device) event.subject()),
Charles Chan52003922018-04-05 16:31:15 -07001508 ((DeviceEvent) event).port());
1509 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1510 // Process topology event, needed for all modules relying on
1511 // topology service for path computation
1512 TopologyEvent topologyEvent = (TopologyEvent) event;
1513 log.info("Processing topology event {}, topology age {}, reasons {}",
1514 event.type(), topologyEvent.subject().time(),
1515 topologyEvent.reasons().size());
1516 topologyHandler.processTopologyChange(topologyEvent.reasons());
1517 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1518 hostHandler.processHostAddedEvent((HostEvent) event);
1519 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1520 hostHandler.processHostMovedEvent((HostEvent) event);
1521 routeHandler.processHostMovedEvent((HostEvent) event);
1522 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1523 hostHandler.processHostRemovedEvent((HostEvent) event);
1524 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1525 hostHandler.processHostUpdatedEvent((HostEvent) event);
1526 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1527 routeHandler.processRouteAdded((RouteEvent) event);
1528 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1529 routeHandler.processRouteUpdated((RouteEvent) event);
1530 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1531 routeHandler.processRouteRemoved((RouteEvent) event);
1532 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1533 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1534 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1535 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1536 event.type() == McastEvent.Type.SINKS_ADDED ||
1537 event.type() == McastEvent.Type.SINKS_REMOVED ||
1538 event.type() == McastEvent.Type.ROUTE_ADDED ||
1539 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1540 mcastHandler.processMcastEvent((McastEvent) event);
1541 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1542 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1543 Class configClass = netcfgEvent.configClass();
1544 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1545 appCfgHandler.processAppConfigAdded(netcfgEvent);
1546 log.info("App config event .. configuring network");
1547 cfgListener.configureNetwork();
1548 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1549 log.info("Segment Routing Device Config added for {}", event.subject());
1550 cfgListener.configureNetwork();
1551 } else if (configClass.equals(XConnectConfig.class)) {
1552 xConnectHandler.processXConnectConfigAdded(netcfgEvent);
1553 } else if (configClass.equals(InterfaceConfig.class)) {
1554 log.info("Interface Config added for {}", event.subject());
1555 cfgListener.configureNetwork();
1556 } else {
1557 log.error("Unhandled config class: {}", configClass);
1558 }
1559 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1560 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1561 Class configClass = netcfgEvent.configClass();
1562 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1563 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1564 log.info("App config event .. configuring network");
1565 cfgListener.configureNetwork();
1566 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1567 log.info("Segment Routing Device Config updated for {}", event.subject());
1568 createOrUpdateDeviceConfiguration();
1569 } else if (configClass.equals(XConnectConfig.class)) {
1570 xConnectHandler.processXConnectConfigUpdated(netcfgEvent);
1571 } else if (configClass.equals(InterfaceConfig.class)) {
1572 log.info("Interface Config updated for {}", event.subject());
1573 createOrUpdateDeviceConfiguration();
1574 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1575 (InterfaceConfig) netcfgEvent.prevConfig().get());
1576 } else {
1577 log.error("Unhandled config class: {}", configClass);
1578 }
1579 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1580 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1581 Class configClass = netcfgEvent.configClass();
1582 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1583 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1584 log.info("App config event .. configuring network");
1585 cfgListener.configureNetwork();
1586 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1587 // TODO Handle sr device config removal
1588 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
1589 } else if (configClass.equals(XConnectConfig.class)) {
1590 xConnectHandler.processXConnectConfigRemoved(netcfgEvent);
1591 } else if (configClass.equals(InterfaceConfig.class)) {
1592 // TODO Handle interface removal
1593 log.info("InterfaceConfig removal is not handled in current implementation");
1594 } else {
1595 log.error("Unhandled config class: {}", configClass);
1596 }
Saurav Das00e553b2018-04-21 17:19:48 -07001597 } else if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
1598 MastershipEvent me = (MastershipEvent) event;
1599 DeviceId deviceId = me.subject();
1600 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1601 log.info(" ** MASTERSHIP CHANGED Invalidating shouldProgram cache"
1602 + " for {}/pair={} due to change", deviceId, pairDeviceId);
1603 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1604 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1605 defaultRoutingHandler.checkFullRerouteForMasterChange(deviceId, me);
Charles Chan52003922018-04-05 16:31:15 -07001606 } else {
1607 log.warn("Unhandled event type: {}", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001608 }
Charles Chan52003922018-04-05 16:31:15 -07001609 } catch (Exception e) {
1610 log.error("SegmentRouting event handler thread thrown an exception: {}",
1611 e.getMessage(), e);
sangho80f11cb2015-04-01 13:05:26 -07001612 }
sangho80f11cb2015-04-01 13:05:26 -07001613 }
1614 }
1615
Saurav Dase6c448a2018-01-18 12:07:33 -08001616 void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001617 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan9bd6aea2017-06-27 18:48:32 -07001618
1619 // NOTE: Punt ARP/NDP even when the device is not configured.
1620 // Host learning without network config is required for CORD config generator.
1621 routingRulePopulator.populateIpPunts(device.id());
1622 routingRulePopulator.populateArpNdpPunts(device.id());
1623
Charles Chan319d1a22015-11-03 10:42:14 -08001624 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das261c3002017-06-13 15:35:54 -07001625 log.warn("Device configuration unavailable. Device {} will be "
1626 + "processed after configuration.", device.id());
Saurav Das8ec0ec42015-11-03 14:39:27 -08001627 return;
1628 }
Charles Chan72779502016-04-23 17:36:10 -07001629 processDeviceAddedInternal(device.id());
1630 }
1631
1632 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001633 // Irrespective of whether the local is a MASTER or not for this device,
1634 // we need to create a SR-group-handler instance. This is because in a
1635 // multi-instance setup, any instance can initiate forwarding/next-objectives
1636 // for any switch (even if this instance is a SLAVE or not even connected
1637 // to the switch). To handle this, a default-group-handler instance is necessary
1638 // per switch.
Charles Chan72779502016-04-23 17:36:10 -07001639 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1640 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001641 DefaultGroupHandler groupHandler;
1642 try {
1643 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001644 createGroupHandler(deviceId,
1645 appId,
1646 deviceConfiguration,
1647 linkService,
1648 flowObjectiveService,
1649 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001650 } catch (DeviceConfigNotFoundException e) {
1651 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1652 return;
1653 }
Saurav Das2b6a00f2017-12-05 15:00:23 -08001654 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan72779502016-04-23 17:36:10 -07001655 deviceId);
1656 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001657 }
Saurav Dasb149be12016-06-07 10:08:06 -07001658
Charles Chan72779502016-04-23 17:36:10 -07001659 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001660 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan82f19972016-05-17 13:13:55 -07001661 xConnectHandler.init(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001662 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001663 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001664 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001665 }
Charles Chan82ab1932016-01-30 23:22:37 -08001666
Charles Chandebfea32016-10-24 14:52:01 -07001667 appCfgHandler.init(deviceId);
Charles Chan074fae62018-04-29 18:11:37 -07001668 hostEventExecutor.execute(() -> hostHandler.init(deviceId));
1669 routeEventExecutor.execute(() -> routeHandler.init(deviceId));
sangho80f11cb2015-04-01 13:05:26 -07001670 }
1671
Saurav Dasc3604f12016-03-23 11:22:49 -07001672 private void processDeviceRemoved(Device device) {
Saurav Das261c3002017-06-13 15:35:54 -07001673 dsNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001674 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan873661e2017-11-30 15:37:50 -08001675 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan10b0fb72017-02-02 16:20:42 -08001676 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001677 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001678 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Ruchi Sahota71bcb4e2019-01-28 01:08:18 +00001679 macVlanNextObjStore.entrySet().stream()
1680 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1681 .forEach(entry -> macVlanNextObjStore.remove(entry.getKey()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001682 portNextObjStore.entrySet().stream()
1683 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001684 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Dase6c448a2018-01-18 12:07:33 -08001685 linkHandler.processDeviceRemoved(device);
Charles Chan17d75d82017-06-15 00:44:51 -07001686
Saurav Dasfbe74572017-08-03 18:30:35 -07001687 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1688 if (gh != null) {
1689 gh.shutdown();
1690 }
Saurav Das62ae6792017-05-15 15:34:25 -07001691 // Note that a switch going down is associated with all of its links
1692 // going down as well, but it is treated as a single switch down event
Saurav Dasdebcf882018-04-06 20:16:01 -07001693 // while the link-downs are ignored. We cannot rely on the ordering of
1694 // events - i.e we cannot expect all link-downs to come before the
1695 // switch down - so we purge all seen-links for the switch before
1696 // handling route-path changes for the switch-down
Saurav Das62ae6792017-05-15 15:34:25 -07001697 defaultRoutingHandler
Saurav Dasdc7f2752018-03-18 21:28:15 -07001698 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Das6430f412018-01-25 09:49:01 -08001699 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan82f19972016-05-17 13:13:55 -07001700 xConnectHandler.removeDevice(device.id());
Saurav Das97241862018-02-14 14:14:54 -08001701
1702 // Cleanup all internal groupHandler stores for this device. Should be
1703 // done after all rerouting or rehashing has been completed
1704 groupHandlerMap.entrySet()
1705 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001706 }
1707
Saurav Das49368392018-04-23 18:42:12 -07001708 /**
1709 * Purge the destinationSet nextObjective store of entries with this device
1710 * as key. Erases app-level knowledge of hashed groups in this device.
1711 *
1712 * @param devId the device identifier
1713 */
Saurav Das00e553b2018-04-21 17:19:48 -07001714 void purgeHashedNextObjectiveStore(DeviceId devId) {
Saurav Das49368392018-04-23 18:42:12 -07001715 log.debug("Purging hashed next-obj store for dev:{}", devId);
Saurav Das00e553b2018-04-21 17:19:48 -07001716 dsNextObjStore.entrySet().stream()
1717 .filter(entry -> entry.getKey().deviceId().equals(devId))
1718 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
1719 }
1720
Saurav Dasec683dc2018-04-27 18:42:30 -07001721 private void processPortUpdatedInternal(Device device, Port port) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001722 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1723 log.warn("Device configuration uploading. Not handling port event for"
1724 + "dev: {} port: {}", device.id(), port.number());
1725 return;
1726 }
Saurav Dasf9332192017-02-18 14:05:44 -08001727
Saurav Dasec683dc2018-04-27 18:42:30 -07001728 if (interfaceService.isConfigured(new ConnectPoint(device.id(), port.number()))) {
1729 lastEdgePortEvent = Instant.now();
1730 }
1731
Saurav Dasf9332192017-02-18 14:05:44 -08001732 if (!mastershipService.isLocalMaster(device.id())) {
1733 log.debug("Not master for dev:{} .. not handling port updated event"
1734 + "for port {}", device.id(), port.number());
1735 return;
1736 }
Saurav Dasec683dc2018-04-27 18:42:30 -07001737 processPortUpdated(device.id(), port);
1738 }
Saurav Dasf9332192017-02-18 14:05:44 -08001739
Saurav Dasec683dc2018-04-27 18:42:30 -07001740 /**
1741 * Adds or remove filtering rules for the given switchport. If switchport is
1742 * an edge facing port, additionally handles host probing and broadcast
1743 * rules. Must be called by local master of device.
1744 *
1745 * @param deviceId the device identifier
1746 * @param port the port to update
1747 */
1748 void processPortUpdated(DeviceId deviceId, Port port) {
Saurav Dasf9332192017-02-18 14:05:44 -08001749 // first we handle filtering rules associated with the port
1750 if (port.isEnabled()) {
1751 log.info("Switchport {}/{} enabled..programming filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001752 deviceId, port.number());
1753 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001754 } else {
1755 log.info("Switchport {}/{} disabled..removing filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001756 deviceId, port.number());
1757 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001758 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001759
1760 // portUpdated calls are for ports that have gone down or up. For switch
1761 // to switch ports, link-events should take care of any re-routing or
1762 // group editing necessary for port up/down. Here we only process edge ports
1763 // that are already configured.
Saurav Dasec683dc2018-04-27 18:42:30 -07001764 ConnectPoint cp = new ConnectPoint(deviceId, port.number());
Charles Chan098ca202018-05-01 11:50:20 -07001765 VlanId untaggedVlan = interfaceService.getUntaggedVlanId(cp);
1766 VlanId nativeVlan = interfaceService.getNativeVlanId(cp);
1767 Set<VlanId> taggedVlans = interfaceService.getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001768
Saurav Das3fb28272017-03-04 16:08:47 -08001769 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001770 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasec683dc2018-04-27 18:42:30 -07001771 + "dev/port: {}/{}", deviceId, port.number());
Saurav Dasf0f592d2016-11-18 15:21:57 -08001772 return;
1773 }
Saurav Das3fb28272017-03-04 16:08:47 -08001774 if (untaggedVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001775 processEdgePort(deviceId, port, untaggedVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001776 }
1777 if (nativeVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001778 processEdgePort(deviceId, port, nativeVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001779 }
1780 if (!taggedVlans.isEmpty()) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001781 taggedVlans.forEach(tag -> processEdgePort(deviceId, port, tag, false));
Saurav Das3fb28272017-03-04 16:08:47 -08001782 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001783 }
1784
Saurav Dasec683dc2018-04-27 18:42:30 -07001785 private void processEdgePort(DeviceId deviceId, Port port, VlanId vlanId,
Saurav Das3fb28272017-03-04 16:08:47 -08001786 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001787 boolean portUp = port.isEnabled();
1788 if (portUp) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001789 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001790 port.number(), vlanId);
Charles Chan074fae62018-04-29 18:11:37 -07001791 hostEventExecutor.execute(() -> hostHandler.processPortUp(new ConnectPoint(deviceId, port.number())));
Saurav Dasf0f592d2016-11-18 15:21:57 -08001792 } else {
Saurav Dasec683dc2018-04-27 18:42:30 -07001793 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001794 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001795 }
1796
Saurav Dasec683dc2018-04-27 18:42:30 -07001797 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001798 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001799 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001800 } else {
1801 log.warn("Group handler not found for dev:{}. Not handling edge port"
Saurav Dasec683dc2018-04-27 18:42:30 -07001802 + " {} event for port:{}", deviceId,
Saurav Dasf0f592d2016-11-18 15:21:57 -08001803 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001804 }
1805 }
sangho27462c62015-05-14 00:39:53 -07001806
Charles Chan8ca5a122017-10-20 16:06:55 -07001807 private void createOrUpdateDeviceConfiguration() {
1808 if (deviceConfiguration == null) {
Saurav Dase321cff2018-02-09 17:26:45 -08001809 log.info("Creating new DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001810 deviceConfiguration = new DeviceConfiguration(this);
1811 } else {
Saurav Dase321cff2018-02-09 17:26:45 -08001812 log.info("Updating DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001813 deviceConfiguration.updateConfig();
1814 }
1815 }
1816
Charles Chanfbcb8812018-04-18 18:41:05 -07001817 private void createOrUpdateDefaultRoutingHandler() {
1818 if (defaultRoutingHandler == null) {
1819 log.info("Creating new DefaultRoutingHandler");
1820 defaultRoutingHandler = new DefaultRoutingHandler(this);
1821 } else {
1822 log.info("Updating DefaultRoutingHandler");
1823 defaultRoutingHandler.update(this);
1824 }
1825 }
1826
Pier Ventreb6a7f342016-11-26 21:05:22 -08001827 /**
1828 * Registers the given connect point with the NRS, this is necessary
1829 * to receive the NDP and ARP packets from the NRS.
1830 *
1831 * @param portToRegister connect point to register
1832 */
1833 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001834 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001835 portToRegister,
1836 neighbourHandler,
1837 appId
1838 );
1839 }
1840
Charles Chan72f556a2015-10-05 17:50:33 -07001841 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das261c3002017-06-13 15:35:54 -07001842 private static final long PROGRAM_DELAY = 2;
Charles Chan46fdfaf2016-11-09 20:51:44 -08001843 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001844
Charles Chanb7f75ac2016-01-11 18:28:54 -08001845 /**
1846 * Constructs the internal network config listener.
1847 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001848 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001849 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001850 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001851 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001852 }
1853
Charles Chanb7f75ac2016-01-11 18:28:54 -08001854 /**
1855 * Reads network config and initializes related data structure accordingly.
1856 */
Charles Chan873661e2017-11-30 15:37:50 -08001857 void configureNetwork() {
Saurav Dase321cff2018-02-09 17:26:45 -08001858 log.info("Configuring network ...");
Andrea Campanella060cad82018-04-17 12:09:34 +02001859
1860 // Setting handling of network configuration events completable future
1861 // The completable future is needed because of the async behaviour of the configureNetwork,
1862 // listener registration and event arrival
1863 // Enables us to buffer the events and execute them when the configure network is done.
Ray Milkey521e4392018-11-16 15:15:14 -08001864 synchronized (networkConfigCompletionLock) {
1865 networkConfigCompletion = new CompletableFuture<>();
Andrea Campanella060cad82018-04-17 12:09:34 +02001866
Ray Milkey521e4392018-11-16 15:15:14 -08001867 // add a small delay to absorb multiple network config added notifications
1868 if (!programmingScheduled.get()) {
1869 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
1870 programmingScheduled.set(true);
1871 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
1872 }
1873
1874 createOrUpdateDeviceConfiguration();
1875
1876 arpHandler = new ArpHandler(srManager);
1877 icmpHandler = new IcmpHandler(srManager);
1878 ipHandler = new IpHandler(srManager);
1879 routingRulePopulator = new RoutingRulePopulator(srManager);
1880 createOrUpdateDefaultRoutingHandler();
1881
1882 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1883 groupHandlerMap, tunnelStore);
1884 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1885 flowObjectiveService,
1886 tunnelHandler, policyStore);
1887 networkConfigCompletion.complete(true);
Andrea Campanella060cad82018-04-17 12:09:34 +02001888 }
1889
Charles Chan72779502016-04-23 17:36:10 -07001890 mcastHandler.init();
Andrea Campanella060cad82018-04-17 12:09:34 +02001891
Charles Chane7c61022015-10-07 14:21:45 -07001892 }
1893
Charles Chan72f556a2015-10-05 17:50:33 -07001894 @Override
1895 public void event(NetworkConfigEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001896 if (mainEventExecutor == null) {
1897 return;
1898 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001899 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
1900 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1901 switch (event.type()) {
1902 case CONFIG_ADDED:
1903 case CONFIG_UPDATED:
1904 case CONFIG_REMOVED:
1905 log.trace("Schedule Network Config event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001906 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1907 mainEventExecutor.execute(new InternalEventHandler(event));
1908 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001909 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001910 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001911 break;
1912 default:
1913 break;
Charles Chan72f556a2015-10-05 17:50:33 -07001914 }
1915 }
Saurav Das261c3002017-06-13 15:35:54 -07001916
Charles Chanc4a68c32018-01-03 16:26:32 -08001917 @Override
1918 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase321cff2018-02-09 17:26:45 -08001919 if (event.type() == CONFIG_REGISTERED ||
1920 event.type() == CONFIG_UNREGISTERED) {
1921 log.debug("Ignore event {} due to type mismatch", event);
1922 return false;
Charles Chanc4a68c32018-01-03 16:26:32 -08001923 }
Saurav Dase321cff2018-02-09 17:26:45 -08001924
1925 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1926 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1927 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -08001928 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase321cff2018-02-09 17:26:45 -08001929 log.debug("Ignore event {} due to class mismatch", event);
1930 return false;
1931 }
1932
1933 return true;
Charles Chanc4a68c32018-01-03 16:26:32 -08001934 }
1935
Saurav Das261c3002017-06-13 15:35:54 -07001936 private final class ConfigChange implements Runnable {
1937 @Override
1938 public void run() {
1939 programmingScheduled.set(false);
Saurav Dase321cff2018-02-09 17:26:45 -08001940 log.info("Reacting to config changes after buffer delay");
Saurav Das261c3002017-06-13 15:35:54 -07001941 for (Device device : deviceService.getDevices()) {
1942 processDeviceAdded(device);
1943 }
1944 defaultRoutingHandler.startPopulationProcess();
1945 }
1946 }
Charles Chan72f556a2015-10-05 17:50:33 -07001947 }
Charles Chanf4586112015-11-09 16:37:23 -08001948
Charles Chan3f4aca62018-03-21 16:57:47 -07001949 private class InternalLinkListener implements LinkListener {
1950 @Override
1951 public void event(LinkEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001952 if (mainEventExecutor == null) {
1953 return;
1954 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001955 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1956 event.type() == LinkEvent.Type.LINK_UPDATED ||
1957 event.type() == LinkEvent.Type.LINK_REMOVED) {
1958 log.trace("Schedule Link event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001959 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1960 mainEventExecutor.execute(new InternalEventHandler(event));
1961 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001962 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001963 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001964 }
1965 }
1966 }
1967
1968 private class InternalDeviceListener implements DeviceListener {
1969 @Override
1970 public void event(DeviceEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001971 if (mainEventExecutor == null) {
1972 return;
1973 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001974 switch (event.type()) {
1975 case DEVICE_ADDED:
1976 case PORT_UPDATED:
1977 case PORT_ADDED:
1978 case DEVICE_UPDATED:
1979 case DEVICE_AVAILABILITY_CHANGED:
1980 log.trace("Schedule Device event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001981 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1982 mainEventExecutor.execute(new InternalEventHandler(event));
1983 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001984 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001985 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001986 break;
1987 default:
1988 }
1989 }
1990 }
1991
1992 private class InternalTopologyListener implements TopologyListener {
1993 @Override
1994 public void event(TopologyEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001995 if (mainEventExecutor == null) {
1996 return;
1997 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001998 switch (event.type()) {
1999 case TOPOLOGY_CHANGED:
2000 log.trace("Schedule Topology event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02002001 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
2002 mainEventExecutor.execute(new InternalEventHandler(event));
2003 } else {
Charles Chan39b75522018-04-21 00:44:29 -07002004 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02002005 }
Charles Chan3f4aca62018-03-21 16:57:47 -07002006 break;
2007 default:
2008 }
2009 }
2010 }
2011
Charles Chanf4586112015-11-09 16:37:23 -08002012 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08002013 @Override
2014 public void event(HostEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07002015 if (hostEventExecutor == null) {
2016 return;
2017 }
Charles Chanf4586112015-11-09 16:37:23 -08002018 switch (event.type()) {
2019 case HOST_ADDED:
Charles Chanf4586112015-11-09 16:37:23 -08002020 case HOST_MOVED:
Charles Chanf4586112015-11-09 16:37:23 -08002021 case HOST_REMOVED:
Charles Chanf4586112015-11-09 16:37:23 -08002022 case HOST_UPDATED:
Charles Chan3f4aca62018-03-21 16:57:47 -07002023 log.trace("Schedule Host event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07002024 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chanf4586112015-11-09 16:37:23 -08002025 break;
2026 default:
2027 log.warn("Unsupported host event type: {}", event.type());
2028 break;
2029 }
2030 }
2031 }
2032
Charles Chanc91c8782016-03-30 17:54:24 -07002033 private class InternalMcastListener implements McastListener {
2034 @Override
2035 public void event(McastEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07002036 if (mcastEventExecutor == null) {
2037 return;
2038 }
Charles Chanc91c8782016-03-30 17:54:24 -07002039 switch (event.type()) {
Pier1f87aca2018-03-14 16:47:32 -07002040 case SOURCES_ADDED:
2041 case SOURCES_REMOVED:
2042 case SINKS_ADDED:
2043 case SINKS_REMOVED:
Charles Chanc91c8782016-03-30 17:54:24 -07002044 case ROUTE_REMOVED:
Pierdb27b8d2018-04-17 16:29:56 +02002045 case ROUTE_ADDED:
Charles Chan3f4aca62018-03-21 16:57:47 -07002046 log.trace("Schedule Mcast event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07002047 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi6786b922018-02-02 16:19:11 +01002048 break;
Charles Chanc91c8782016-03-30 17:54:24 -07002049 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07002050 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chanc91c8782016-03-30 17:54:24 -07002051 break;
2052 }
2053 }
2054 }
Charles Chan41f5ec02016-06-13 18:54:31 -07002055
Charles Chandebfea32016-10-24 14:52:01 -07002056 private class InternalRouteEventListener implements RouteListener {
2057 @Override
2058 public void event(RouteEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07002059 if (routeEventExecutor == null) {
2060 return;
2061 }
Charles Chandebfea32016-10-24 14:52:01 -07002062 switch (event.type()) {
2063 case ROUTE_ADDED:
Charles Chandebfea32016-10-24 14:52:01 -07002064 case ROUTE_UPDATED:
Charles Chandebfea32016-10-24 14:52:01 -07002065 case ROUTE_REMOVED:
Charles Chan910be6a2017-08-23 14:46:43 -07002066 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan3f4aca62018-03-21 16:57:47 -07002067 log.trace("Schedule Route event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07002068 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan910be6a2017-08-23 14:46:43 -07002069 break;
Charles Chandebfea32016-10-24 14:52:01 -07002070 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07002071 log.warn("Unsupported route event type: {}", event.type());
Charles Chandebfea32016-10-24 14:52:01 -07002072 break;
2073 }
2074 }
2075 }
Saurav Das62ae6792017-05-15 15:34:25 -07002076
Charles Chanfbcb8812018-04-18 18:41:05 -07002077 private class InternalMastershipListener implements MastershipListener {
2078 @Override
2079 public void event(MastershipEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07002080 if (mainEventExecutor == null) {
2081 return;
2082 }
Charles Chanfbcb8812018-04-18 18:41:05 -07002083 switch (event.type()) {
Saurav Das00e553b2018-04-21 17:19:48 -07002084 case MASTER_CHANGED:
2085 log.debug("Mastership event: {}/{}", event.subject(),
2086 event.roleInfo());
2087 mainEventExecutor.execute(new InternalEventHandler(event));
2088 break;
2089 case BACKUPS_CHANGED:
2090 case SUSPENDED:
2091 default:
2092 log.debug("Mastership event type {} not handled", event.type());
2093 break;
Charles Chanfbcb8812018-04-18 18:41:05 -07002094 }
2095 }
2096 }
2097
Saurav Das00e553b2018-04-21 17:19:48 -07002098 class InternalClusterListener implements ClusterEventListener {
2099 private Instant lastClusterEvent = Instant.EPOCH;
2100
2101 long timeSinceLastClusterEvent() {
2102 return Instant.now().toEpochMilli() - lastClusterEvent.toEpochMilli();
2103 }
2104
2105 @Override
2106 public void event(ClusterEvent event) {
2107 switch (event.type()) {
2108 case INSTANCE_ACTIVATED:
2109 case INSTANCE_ADDED:
2110 case INSTANCE_READY:
2111 log.debug("Cluster event {} ignored", event.type());
2112 break;
2113 case INSTANCE_DEACTIVATED:
2114 case INSTANCE_REMOVED:
2115 log.info("** Cluster event {}", event.type());
2116 lastClusterEvent = Instant.now();
2117 break;
2118 default:
2119 break;
2120 }
2121
2122 }
2123
2124 }
2125
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002126 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
2127 try {
2128 Set<Interface> intfs = conf.getInterfaces();
2129 Set<Interface> prevIntfs = prevConf.getInterfaces();
2130
2131 // Now we only handle one interface config at each port.
2132 if (intfs.size() != 1 || prevIntfs.size() != 1) {
2133 log.warn("Interface update aborted - one at a time is allowed, " +
2134 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
2135 return;
2136 }
2137
Andrea Campanella060cad82018-04-17 12:09:34 +02002138 //The system is in an incoherent state, abort
2139 if (defaultRoutingHandler == null) {
2140 log.warn("Interface update aborted, defaultRoutingHandler is null");
2141 return;
2142 }
2143
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002144 Interface intf = intfs.stream().findFirst().get();
2145 Interface prevIntf = prevIntfs.stream().findFirst().get();
2146
2147 DeviceId deviceId = intf.connectPoint().deviceId();
2148 PortNumber portNum = intf.connectPoint().port();
2149
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002150 removeSubnetConfig(prevIntf.connectPoint(),
2151 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
2152 new HashSet<>(intf.ipAddressesList())));
2153
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002154 if (!prevIntf.vlanNative().equals(VlanId.NONE)
2155 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
2156 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
2157 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
2158 // Update filtering objective and L2IG group bucket
2159 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
2160 } else {
2161 // RemoveVlanNative
2162 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
2163 }
2164 }
2165
2166 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
2167 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2168 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2169 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
2170 // Update filtering objective and L2IG group bucket
2171 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
2172 } else {
2173 // RemoveVlanUntagged
2174 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
2175 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002176 }
2177
2178 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2179 // RemoveVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002180 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
2181 .filter(i -> !intf.vlanUntagged().equals(i))
2182 .filter(i -> !intf.vlanNative().equals(i))
2183 .forEach(vlanId -> updateVlanConfigInternal(
2184 deviceId, portNum, vlanId, false, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002185 }
2186
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002187 if (!intf.vlanNative().equals(VlanId.NONE)
2188 && !prevIntf.vlanNative().equals(intf.vlanNative())
2189 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
2190 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
2191 // Update filtering objective and L2IG group bucket
2192 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
2193 } else {
2194 // AddVlanNative
2195 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
2196 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002197 }
2198
2199 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
2200 // AddVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002201 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
2202 .filter(i -> !prevIntf.vlanUntagged().equals(i))
2203 .filter(i -> !prevIntf.vlanNative().equals(i))
2204 .forEach(vlanId -> updateVlanConfigInternal(
2205 deviceId, portNum, vlanId, false, true)
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002206 );
2207 }
2208
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002209 if (!intf.vlanUntagged().equals(VlanId.NONE)
2210 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
2211 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
2212 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
2213 // Update filtering objective and L2IG group bucket
2214 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
2215 } else {
2216 // AddVlanUntagged
2217 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
2218 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002219 }
2220 addSubnetConfig(prevIntf.connectPoint(),
2221 Sets.difference(new HashSet<>(intf.ipAddressesList()),
2222 new HashSet<>(prevIntf.ipAddressesList())));
2223 } catch (ConfigException e) {
2224 log.error("Error in configuration");
2225 }
2226 }
2227
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002228 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
2229 VlanId vlanId, boolean pushVlan) {
2230 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2231 if (grpHandler == null) {
2232 log.warn("Failed to retrieve group handler for device {}", deviceId);
2233 return;
2234 }
2235
2236 // Update filtering objective for a single port
2237 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
2238 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
2239
2240 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
2241 // Update L2IG bucket of the port
2242 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
2243 } else {
2244 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2245 }
2246 }
2247
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002248 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
2249 VlanId vlanId, boolean pushVlan, boolean install) {
2250 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
2251 if (grpHandler == null) {
2252 log.warn("Failed to retrieve group handler for device {}", deviceId);
2253 return;
2254 }
2255
2256 // Update filtering objective for a single port
2257 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
2258
2259 // Update filtering objective for multicast ingress port
2260 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
2261
2262 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
2263
2264 if (nextId != -1 && !install) {
2265 // Update next objective for a single port as an output port
2266 // Remove a single port from L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002267 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002268 // Remove L2 Bridging rule and L3 Unicast rule to the host
Charles Chan074fae62018-04-29 18:11:37 -07002269 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2270 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002271 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
2272 // only if there is no port configured on that VLAN ID
2273 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
2274 // Remove broadcast forwarding rule for the VLAN
2275 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2276 // Remove L2FG for VLAN
2277 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
2278 } else {
2279 // Remove L2IG of the port
2280 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
2281 }
2282 } else if (install) {
2283 if (nextId != -1) {
2284 // Add a single port to L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09002285 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002286 } else {
2287 // Create L2FG for VLAN
2288 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
2289 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
2290 }
Charles Chan074fae62018-04-29 18:11:37 -07002291 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
2292 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002293 } else {
2294 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
2295 }
2296 }
2297
2298 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2299 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2300 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2301
2302 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2303 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2304 .filter(intf -> !intf.connectPoint().equals(cp))
2305 .flatMap(intf -> intf.ipAddressesList().stream())
2306 .collect(Collectors.toSet());
2307 // 1. Partial subnet population
2308 // Remove routing rules for removed subnet from previous configuration,
2309 // which does not also exist in other interfaces in the same device
2310 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2311 .map(InterfaceIpAddress::subnetAddress)
2312 .collect(Collectors.toSet());
2313
2314 defaultRoutingHandler.revokeSubnet(
2315 ipPrefixSet.stream()
2316 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2317 .collect(Collectors.toSet()));
2318
2319 // 2. Interface IP punts
2320 // Remove IP punts for old Intf address
2321 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2322 .map(InterfaceIpAddress::ipAddress)
2323 .collect(Collectors.toSet());
2324 ipAddressSet.stream()
2325 .map(InterfaceIpAddress::ipAddress)
2326 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2327 .forEach(interfaceIpAddress ->
2328 routingRulePopulator.revokeSingleIpPunts(
2329 cp.deviceId(), interfaceIpAddress));
2330
2331 // 3. Host unicast routing rule
2332 // Remove unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002333 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002334 }
2335
2336 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
2337 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
2338 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
2339
2340 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
2341 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
2342 .filter(intf -> !intf.connectPoint().equals(cp))
2343 .flatMap(intf -> intf.ipAddressesList().stream())
2344 .collect(Collectors.toSet());
2345 // 1. Partial subnet population
2346 // Add routing rules for newly added subnet, which does not also exist in
2347 // other interfaces in the same device
2348 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
2349 .map(InterfaceIpAddress::subnetAddress)
2350 .collect(Collectors.toSet());
2351
2352 defaultRoutingHandler.populateSubnet(
2353 Collections.singleton(cp),
2354 ipPrefixSet.stream()
2355 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
2356 .collect(Collectors.toSet()));
2357
2358 // 2. Interface IP punts
2359 // Add IP punts for new Intf address
2360 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2361 .map(InterfaceIpAddress::ipAddress)
2362 .collect(Collectors.toSet());
2363 ipAddressSet.stream()
2364 .map(InterfaceIpAddress::ipAddress)
2365 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2366 .forEach(interfaceIpAddress ->
2367 routingRulePopulator.populateSingleIpPunts(
2368 cp.deviceId(), interfaceIpAddress));
2369
2370 // 3. Host unicast routing rule
2371 // Add unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002372 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002373 }
sangho80f11cb2015-04-01 13:05:26 -07002374}