blob: 33c71f50b66741f5065a26ab4e794cf59f7e43a9 [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.apache.felix.scr.annotations.Activate;
24import org.apache.felix.scr.annotations.Component;
25import org.apache.felix.scr.annotations.Deactivate;
Charles Chan873661e2017-11-30 15:37:50 -080026import org.apache.felix.scr.annotations.Modified;
27import org.apache.felix.scr.annotations.Property;
sangho80f11cb2015-04-01 13:05:26 -070028import org.apache.felix.scr.annotations.Reference;
29import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho27462c62015-05-14 00:39:53 -070030import org.apache.felix.scr.annotations.Service;
sangho80f11cb2015-04-01 13:05:26 -070031import org.onlab.packet.Ethernet;
Pier Ventreb6b81d52016-12-02 08:16:05 -080032import org.onlab.packet.ICMP6;
Charles Chan77277672015-10-20 16:24:19 -070033import org.onlab.packet.IPv4;
Pier Ventreb6a7f342016-11-26 21:05:22 -080034import org.onlab.packet.IPv6;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070035import org.onlab.packet.IpAddress;
Charles Chan77277672015-10-20 16:24:19 -070036import org.onlab.packet.IpPrefix;
Jonathan Hart54541d12016-04-12 15:39:44 -070037import org.onlab.packet.VlanId;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070038import org.onlab.util.KryoNamespace;
Charles Chan873661e2017-11-30 15:37:50 -080039import org.onlab.util.Tools;
Saurav Dasc3604f12016-03-23 11:22:49 -070040import org.onosproject.cfg.ComponentConfigService;
Saurav Das00e553b2018-04-21 17:19:48 -070041import org.onosproject.cluster.ClusterEvent;
42import org.onosproject.cluster.ClusterEventListener;
Pier Luigi580fd8a2018-01-16 10:47:50 +010043import org.onosproject.cluster.ClusterService;
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -070044import org.onosproject.cluster.LeadershipService;
Pierdb27b8d2018-04-17 16:29:56 +020045import org.onosproject.cluster.NodeId;
sangho80f11cb2015-04-01 13:05:26 -070046import org.onosproject.core.ApplicationId;
47import org.onosproject.core.CoreService;
48import org.onosproject.event.Event;
Charles Chanfbcb8812018-04-18 18:41:05 -070049import org.onosproject.mastership.MastershipEvent;
50import org.onosproject.mastership.MastershipListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070051import org.onosproject.mastership.MastershipService;
Pier1f87aca2018-03-14 16:47:32 -070052import org.onosproject.mcast.api.McastEvent;
53import org.onosproject.mcast.api.McastListener;
54import org.onosproject.mcast.api.MulticastRouteService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080055import org.onosproject.net.ConnectPoint;
Jonathan Hart54541d12016-04-12 15:39:44 -070056import org.onosproject.net.Device;
57import org.onosproject.net.DeviceId;
Charles Chanf0ae41e2017-08-23 13:55:39 -070058import org.onosproject.net.Host;
59import org.onosproject.net.HostId;
Jonathan Hart54541d12016-04-12 15:39:44 -070060import org.onosproject.net.Link;
61import org.onosproject.net.Port;
Charles Chanf4586112015-11-09 16:37:23 -080062import org.onosproject.net.PortNumber;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070063import org.onosproject.net.config.ConfigException;
Charles Chan72f556a2015-10-05 17:50:33 -070064import org.onosproject.net.config.ConfigFactory;
65import org.onosproject.net.config.NetworkConfigEvent;
Charles Chan72f556a2015-10-05 17:50:33 -070066import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070067import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkeyae0068a2017-08-15 11:02:29 -070068import org.onosproject.net.config.basics.InterfaceConfig;
69import org.onosproject.net.config.basics.McastConfig;
Charles Chan72f556a2015-10-05 17:50:33 -070070import org.onosproject.net.config.basics.SubjectFactories;
Saurav Dase6c448a2018-01-18 12:07:33 -080071import org.onosproject.net.device.DeviceAdminService;
Jonathan Hart54541d12016-04-12 15:39:44 -070072import org.onosproject.net.device.DeviceEvent;
73import org.onosproject.net.device.DeviceListener;
74import org.onosproject.net.device.DeviceService;
Charles Chanf4586112015-11-09 16:37:23 -080075import org.onosproject.net.flow.TrafficSelector;
76import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hart54541d12016-04-12 15:39:44 -070077import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chanf4586112015-11-09 16:37:23 -080078import org.onosproject.net.host.HostEvent;
79import org.onosproject.net.host.HostListener;
Charles Chan873661e2017-11-30 15:37:50 -080080import org.onosproject.net.host.HostLocationProbingService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080081import org.onosproject.net.host.HostService;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070082import org.onosproject.net.host.InterfaceIpAddress;
Pierdb27b8d2018-04-17 16:29:56 +020083import org.onosproject.net.intent.WorkPartitionService;
Ray Milkeyae0068a2017-08-15 11:02:29 -070084import org.onosproject.net.intf.Interface;
85import org.onosproject.net.intf.InterfaceService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080086import org.onosproject.net.link.LinkEvent;
87import org.onosproject.net.link.LinkListener;
88import org.onosproject.net.link.LinkService;
Ray Milkeyae0068a2017-08-15 11:02:29 -070089import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080090import org.onosproject.net.packet.InboundPacket;
91import org.onosproject.net.packet.PacketContext;
92import org.onosproject.net.packet.PacketProcessor;
93import org.onosproject.net.packet.PacketService;
Pier Luigid8a15162018-02-15 16:33:08 +010094import org.onosproject.net.topology.TopologyEvent;
95import org.onosproject.net.topology.TopologyListener;
Charles Chanc91c8782016-03-30 17:54:24 -070096import org.onosproject.net.topology.TopologyService;
Charles Chanf0ae41e2017-08-23 13:55:39 -070097import org.onosproject.routeservice.ResolvedRoute;
Ray Milkeyae0068a2017-08-15 11:02:29 -070098import org.onosproject.routeservice.RouteEvent;
99import org.onosproject.routeservice.RouteListener;
100import org.onosproject.routeservice.RouteService;
Charles Chanc91c8782016-03-30 17:54:24 -0700101import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
102import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventre6b19e482016-11-07 16:21:04 -0800103import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkeyae0068a2017-08-15 11:02:29 -0700104import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chan82f19972016-05-17 13:13:55 -0700105import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chanc91c8782016-03-30 17:54:24 -0700106import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -0700107import org.onosproject.segmentrouting.grouphandler.DestinationSet;
108import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Pier Luigi69f774d2018-02-28 12:10:50 +0100109import org.onosproject.segmentrouting.mcast.McastHandler;
110import org.onosproject.segmentrouting.mcast.McastRole;
Piere99511d2018-04-19 16:47:06 +0200111import org.onosproject.segmentrouting.mcast.McastRoleStoreKey;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700112import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700113import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelDescription;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800114import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700115import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800116
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800117import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkeyae0068a2017-08-15 11:02:29 -0700118import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800119import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800120import org.onosproject.segmentrouting.pwaas.L2TunnelDescription;
121
Saurav Das261c3002017-06-13 15:35:54 -0700122import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700123import org.onosproject.segmentrouting.storekey.DummyVlanIdStoreKey;
Piere99511d2018-04-19 16:47:06 +0200124import org.onosproject.segmentrouting.mcast.McastStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -0700125import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan10b0fb72017-02-02 16:20:42 -0800126import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chan82f19972016-05-17 13:13:55 -0700127import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hart54541d12016-04-12 15:39:44 -0700128import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700129import org.onosproject.store.service.EventuallyConsistentMap;
130import org.onosproject.store.service.EventuallyConsistentMapBuilder;
131import org.onosproject.store.service.StorageService;
132import org.onosproject.store.service.WallClockTimestamp;
Charles Chan873661e2017-11-30 15:37:50 -0800133import org.osgi.service.component.ComponentContext;
sangho80f11cb2015-04-01 13:05:26 -0700134import org.slf4j.Logger;
135import org.slf4j.LoggerFactory;
136
Saurav Das00e553b2018-04-21 17:19:48 -0700137import java.time.Instant;
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700138import java.util.ArrayList;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800139import java.util.Collections;
Charles Chan873661e2017-11-30 15:37:50 -0800140import java.util.Dictionary;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800141import java.util.HashSet;
142import java.util.List;
143import java.util.Map;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800144import java.util.Optional;
145import java.util.Set;
Andrea Campanella060cad82018-04-17 12:09:34 +0200146import java.util.concurrent.CompletableFuture;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800147import java.util.concurrent.ConcurrentHashMap;
Andrea Campanella060cad82018-04-17 12:09:34 +0200148import java.util.concurrent.CopyOnWriteArrayList;
Charles Chan77cdde42018-05-08 21:35:50 -0700149import java.util.concurrent.ExecutorService;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800150import java.util.concurrent.Executors;
151import java.util.concurrent.ScheduledExecutorService;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800152import java.util.concurrent.TimeUnit;
153import java.util.concurrent.atomic.AtomicBoolean;
154import java.util.stream.Collectors;
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700155import java.util.stream.IntStream;
sangho80f11cb2015-04-01 13:05:26 -0700156
Charles Chand6d25332016-02-26 22:19:52 -0800157import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800158import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700159import static org.onlab.util.Tools.groupedThreads;
Saurav Dase321cff2018-02-09 17:26:45 -0800160import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
161import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Charles Chand6d25332016-02-26 22:19:52 -0800162
Charles Chanb7f75ac2016-01-11 18:28:54 -0800163/**
164 * Segment routing manager.
165 */
Jonathan Hart54541d12016-04-12 15:39:44 -0700166@Service
167@Component(immediate = true)
sangho27462c62015-05-14 00:39:53 -0700168public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700169
Charles Chan46fdfaf2016-11-09 20:51:44 -0800170 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan910be6a2017-08-23 14:46:43 -0700171 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sangho80f11cb2015-04-01 13:05:26 -0700172
173 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700174 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700175
176 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventreb6a7f342016-11-26 21:05:22 -0800177 private NeighbourResolutionService neighbourResolutionService;
178
179 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100180 public CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700181
182 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700183 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700184
185 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700186 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700187
188 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan873661e2017-11-30 15:37:50 -0800189 HostLocationProbingService probingService;
190
191 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100192 public DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700193
194 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Saurav Dase6c448a2018-01-18 12:07:33 -0800195 DeviceAdminService deviceAdminService;
196
197 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800198 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700199
200 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100201 public LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700202
Charles Chan82ab1932016-01-30 23:22:37 -0800203 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800204 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700205
206 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800207 public StorageService storageService;
Charles Chandebfea32016-10-24 14:52:01 -0700208
209 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Luigi69f774d2018-02-28 12:10:50 +0100210 public MulticastRouteService multicastRouteService;
Charles Chandebfea32016-10-24 14:52:01 -0700211
212 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan83163812018-01-09 13:45:07 -0800213 public TopologyService topologyService;
Charles Chandebfea32016-10-24 14:52:01 -0700214
215 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700216 RouteService routeService;
Charles Chan82ab1932016-01-30 23:22:37 -0800217
218 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800219 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800220
Saurav Dasc3604f12016-03-23 11:22:49 -0700221 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800222 public InterfaceService interfaceService;
223
Pier Luigi580fd8a2018-01-16 10:47:50 +0100224 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
225 public ClusterService clusterService;
226
227 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pierdb27b8d2018-04-17 16:29:56 +0200228 public WorkPartitionService workPartitionService;
Pier Luigi580fd8a2018-01-16 10:47:50 +0100229
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700230 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
231 public LeadershipService leadershipService;
232
Charles Chan873661e2017-11-30 15:37:50 -0800233 @Property(name = "activeProbing", boolValue = true,
234 label = "Enable active probing to discover dual-homed hosts.")
235 boolean activeProbing = true;
236
Saurav Dasec683dc2018-04-27 18:42:30 -0700237 @Property(name = "singleHomedDown", boolValue = false,
238 label = "Enable administratively taking down single-homed hosts "
239 + "when all uplinks are gone")
240 boolean singleHomedDown = false;
241
Charles Chandebfea32016-10-24 14:52:01 -0700242 ArpHandler arpHandler = null;
243 IcmpHandler icmpHandler = null;
244 IpHandler ipHandler = null;
245 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700246 ApplicationId appId;
247 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700248
Charles Chandebfea32016-10-24 14:52:01 -0700249 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700250 private TunnelHandler tunnelHandler = null;
251 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700252 private InternalPacketProcessor processor = null;
253 private InternalLinkListener linkListener = null;
254 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700255 private AppConfigHandler appCfgHandler = null;
Pier Luigi69f774d2018-02-28 12:10:50 +0100256 public XConnectHandler xConnectHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800257 McastHandler mcastHandler = null;
258 HostHandler hostHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800259 private RouteHandler routeHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800260 LinkHandler linkHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800261 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800262 private DefaultL2TunnelHandler l2TunnelHandler = null;
Pier Luigid8a15162018-02-15 16:33:08 +0100263 private TopologyHandler topologyHandler = null;
Charles Chan82ab1932016-01-30 23:22:37 -0800264 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700265 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
266 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chandebfea32016-10-24 14:52:01 -0700267 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
Pier Luigid8a15162018-02-15 16:33:08 +0100268 private final InternalTopologyListener topologyListener = new InternalTopologyListener();
Charles Chanfbcb8812018-04-18 18:41:05 -0700269 private final InternalMastershipListener mastershipListener = new InternalMastershipListener();
Saurav Das00e553b2018-04-21 17:19:48 -0700270 final InternalClusterListener clusterListener = new InternalClusterListener();
Andrea Campanella060cad82018-04-17 12:09:34 +0200271 //Completable future for network configuration process to buffer config events handling during activation
272 private CompletableFuture<Boolean> networkConfigCompletion = null;
Charles Chan39b75522018-04-21 00:44:29 -0700273 private List<Event> queuedEvents = new CopyOnWriteArrayList<>();
sangho80f11cb2015-04-01 13:05:26 -0700274
Charles Chan52003922018-04-05 16:31:15 -0700275 // Handles device, link, topology and network config events
Charles Chanfbcb8812018-04-18 18:41:05 -0700276 private ScheduledExecutorService mainEventExecutor;
sangho80f11cb2015-04-01 13:05:26 -0700277
Charles Chanfbcb8812018-04-18 18:41:05 -0700278 // Handles host, route and mcast events respectively
279 private ScheduledExecutorService hostEventExecutor;
280 private ScheduledExecutorService routeEventExecutor;
281 private ScheduledExecutorService mcastEventExecutor;
Charles Chan77cdde42018-05-08 21:35:50 -0700282 private ExecutorService packetExecutor;
Charles Chan52003922018-04-05 16:31:15 -0700283
284 Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<>();
Charles Chanb7f75ac2016-01-11 18:28:54 -0800285 /**
Saurav Das261c3002017-06-13 15:35:54 -0700286 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700287 * Used to keep track on MPLS group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800288 */
Charles Chan873661e2017-11-30 15:37:50 -0800289 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das261c3002017-06-13 15:35:54 -0700290 dsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800291 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700292 * Per device next objective ID store with (device id + vlanid) as key.
293 * Used to keep track on L2 flood group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800294 */
Charles Chan873661e2017-11-30 15:37:50 -0800295 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800296 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800297 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700298 * Per device next objective ID store with (device id + port + treatment + meta) as key.
299 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800300 */
Charles Chan873661e2017-11-30 15:37:50 -0800301 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800302 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800303
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700304 /**
305 * Per port dummy VLAN ID store with (connect point + ip address) as key.
306 * Used to keep track on dummy VLAN ID allocation.
307 */
308 private EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId>
309 dummyVlanIdStore = null;
310
Saurav Das2d94d312015-11-24 23:21:05 -0800311 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
312 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700313
Saurav Das261c3002017-06-13 15:35:54 -0700314 private AtomicBoolean programmingScheduled = new AtomicBoolean();
315
Charles Chanc91c8782016-03-30 17:54:24 -0700316 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700317 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
318 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700319 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700320 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800321 public SegmentRoutingDeviceConfig createConfig() {
322 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700323 }
324 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800325
Charles Chanc91c8782016-03-30 17:54:24 -0700326 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700327 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
328 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700329 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800330 @Override
331 public SegmentRoutingAppConfig createConfig() {
332 return new SegmentRoutingAppConfig();
333 }
334 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800335
Charles Chan82f19972016-05-17 13:13:55 -0700336 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
337 new ConfigFactory<ApplicationId, XConnectConfig>(
338 SubjectFactories.APP_SUBJECT_FACTORY,
339 XConnectConfig.class, "xconnect") {
340 @Override
341 public XConnectConfig createConfig() {
342 return new XConnectConfig();
343 }
344 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800345
Charles Chanc91c8782016-03-30 17:54:24 -0700346 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700347 new ConfigFactory<ApplicationId, McastConfig>(
348 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700349 McastConfig.class, "multicast") {
350 @Override
351 public McastConfig createConfig() {
352 return new McastConfig();
353 }
354 };
355
Charles Chan1963f4f2016-02-18 14:22:42 -0800356 /**
357 * Segment Routing App ID.
358 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800359 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chanb7f75ac2016-01-11 18:28:54 -0800360 /**
361 * The default VLAN ID assigned to the interfaces without subnet config.
362 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800363 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Andreas Pantelopoulosb281ae22018-05-01 14:56:05 -0700364 /**
365 * The Vlan id used to transport pseudowire traffic across the network.
366 */
367 public static final VlanId PSEUDOWIRE_VLAN = VlanId.vlanId((short) 4093);
Saurav Das7c305372015-10-28 12:39:42 -0700368
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700369 /**
370 * Minumum and maximum value of dummy VLAN ID to be allocated.
371 */
372 public static final int MIN_DUMMY_VLAN_ID = 2;
373 public static final int MAX_DUMMY_VLAN_ID = 4093;
374
Saurav Dasec683dc2018-04-27 18:42:30 -0700375 Instant lastEdgePortEvent = Instant.EPOCH;
376
sangho80f11cb2015-04-01 13:05:26 -0700377 @Activate
Charles Chan873661e2017-11-30 15:37:50 -0800378 protected void activate(ComponentContext context) {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800379 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700380
Charles Chanfbcb8812018-04-18 18:41:05 -0700381 mainEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-main", "%d", log));
382 hostEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-host", "%d", log));
383 routeEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-route", "%d", log));
384 mcastEventExecutor = Executors.newSingleThreadScheduledExecutor(groupedThreads("sr-event-mcast", "%d", log));
Charles Chan77cdde42018-05-08 21:35:50 -0700385 packetExecutor = Executors.newSingleThreadExecutor(groupedThreads("sr-packet", "%d", log));
Charles Chanfbcb8812018-04-18 18:41:05 -0700386
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700387 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das261c3002017-06-13 15:35:54 -0700388 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700389 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das261c3002017-06-13 15:35:54 -0700390 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700391 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700392 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700393 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700394 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700395 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700396
Charles Chan10b0fb72017-02-02 16:20:42 -0800397 log.debug("Creating EC map vlannextobjectivestore");
398 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
399 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
400 vlanNextObjStore = vlanNextObjMapBuilder
401 .withName("vlannextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700402 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700403 .withTimestampProvider((k, v) -> new WallClockTimestamp())
404 .build();
405
Saurav Das2d94d312015-11-24 23:21:05 -0800406 log.debug("Creating EC map subnetnextobjectivestore");
407 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
408 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
409 portNextObjStore = portNextObjMapBuilder
410 .withName("portnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700411 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800412 .withTimestampProvider((k, v) -> new WallClockTimestamp())
413 .build();
414
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700415 EventuallyConsistentMapBuilder<DummyVlanIdStoreKey, VlanId>
416 dummyVlanIdMapBuilder = storageService.eventuallyConsistentMapBuilder();
417 dummyVlanIdStore = dummyVlanIdMapBuilder
418 .withName("dummyvlanidstore")
419 .withSerializer(createSerializer())
420 .withTimestampProvider((k, v) -> new WallClockTimestamp())
421 .build();
422
sangho4a5c42a2015-05-20 22:16:38 -0700423 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
424 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700425 tunnelStore = tunnelMapBuilder
426 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700427 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700428 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700429 .build();
430
431 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
432 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700433 policyStore = policyMapBuilder
434 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700435 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700436 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700437 .build();
438
Saurav Dasc3604f12016-03-23 11:22:49 -0700439 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800440 "purgeOnDisconnection", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700441 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800442 "purgeOnDisconnection", "true");
Pier Luigib9632ba2017-01-12 18:14:58 -0800443 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
444 "requestInterceptsEnabled", "false");
Charles Chanc1909e12017-08-08 15:13:37 -0700445 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800446 "requestInterceptsEnabled", "false");
Charles Chan9597f8d2017-07-24 15:56:10 -0700447 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800448 "arpEnabled", "false");
Pier Luigi0ebeb312017-02-02 22:31:34 -0800449 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
450 "greedyLearningIpv6", "true");
Charles Chancf8ea472017-02-28 15:15:17 -0800451 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
452 "forceUnprovision", "true");
Charles Chanc7b73c72017-08-10 16:57:28 -0700453 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan4c95c0d2017-07-20 16:16:25 -0700454 "distributed", "true");
Charles Chan804772f2017-08-14 11:42:11 -0700455 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
456 "multihomingEnabled", "true");
Charles Chan0c9c19f2017-11-29 19:54:20 -0800457 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
458 "staleLinkAge", "15000");
459 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
460 "allowDuplicateIps", "false");
Charles Chan873661e2017-11-30 15:37:50 -0800461 compCfgService.registerProperties(getClass());
462 modified(context);
Saurav Dasc3604f12016-03-23 11:22:49 -0700463
Charles Chan2b078ae2015-10-14 11:24:40 -0700464 processor = new InternalPacketProcessor();
465 linkListener = new InternalLinkListener();
466 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700467 appCfgHandler = new AppConfigHandler(this);
468 xConnectHandler = new XConnectHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700469 mcastHandler = new McastHandler(this);
470 hostHandler = new HostHandler(this);
Saurav Dase6c448a2018-01-18 12:07:33 -0800471 linkHandler = new LinkHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700472 routeHandler = new RouteHandler(this);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800473 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800474 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Pier Luigid8a15162018-02-15 16:33:08 +0100475 topologyHandler = new TopologyHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700476
Charles Chand6d25332016-02-26 22:19:52 -0800477 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700478 cfgService.registerConfigFactory(deviceConfigFactory);
479 cfgService.registerConfigFactory(appConfigFactory);
Charles Chan82f19972016-05-17 13:13:55 -0700480 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700481 cfgService.registerConfigFactory(mcastConfigFactory);
Saurav Dase321cff2018-02-09 17:26:45 -0800482 log.info("Configuring network before adding listeners");
Andrea Campanella060cad82018-04-17 12:09:34 +0200483
Charles Chan6961f222018-01-04 14:26:07 -0800484 cfgListener.configureNetwork();
485
Charles Chan82ab1932016-01-30 23:22:37 -0800486 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700487 packetService.addProcessor(processor, PacketProcessor.director(2));
488 linkService.addListener(linkListener);
489 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700490 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700491 routeService.addListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100492 topologyService.addListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700493 mastershipService.addListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700494 clusterService.addListener(clusterListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700495
Charles Chanc12c3d02018-03-09 15:53:44 -0800496 linkHandler.init();
Andreas Pantelopoulos75eef062018-01-11 07:53:48 -0800497 l2TunnelHandler.init();
498
Andrea Campanella060cad82018-04-17 12:09:34 +0200499 networkConfigCompletion.whenComplete((value, ex) -> {
500 //setting to null for easier fall through
501 networkConfigCompletion = null;
502 //process all queued events
Charles Chan39b75522018-04-21 00:44:29 -0700503 queuedEvents.forEach(event -> {
Andrea Campanella060cad82018-04-17 12:09:34 +0200504 mainEventExecutor.execute(new InternalEventHandler(event));
505 });
506 });
507
sangho80f11cb2015-04-01 13:05:26 -0700508 log.info("Started");
509 }
510
Saurav Dase6c448a2018-01-18 12:07:33 -0800511 KryoNamespace.Builder createSerializer() {
Jonathan Hart54541d12016-04-12 15:39:44 -0700512 return new KryoNamespace.Builder()
513 .register(KryoNamespaces.API)
Saurav Das261c3002017-06-13 15:35:54 -0700514 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800515 VlanNextObjectiveStoreKey.class,
516 DestinationSet.class,
517 NextNeighbors.class,
518 Tunnel.class,
519 DefaultTunnel.class,
520 Policy.class,
521 TunnelPolicy.class,
522 Policy.Type.class,
523 PortNextObjectiveStoreKey.class,
524 XConnectStoreKey.class,
525 L2Tunnel.class,
526 L2TunnelPolicy.class,
527 DefaultL2Tunnel.class,
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700528 DefaultL2TunnelPolicy.class,
529 DummyVlanIdStoreKey.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700530 );
531 }
532
sangho80f11cb2015-04-01 13:05:26 -0700533 @Deactivate
534 protected void deactivate() {
Charles Chanfbcb8812018-04-18 18:41:05 -0700535 mainEventExecutor.shutdown();
536 hostEventExecutor.shutdown();
537 routeEventExecutor.shutdown();
538 mcastEventExecutor.shutdown();
Charles Chan77cdde42018-05-08 21:35:50 -0700539 packetExecutor.shutdown();
Charles Chanfbcb8812018-04-18 18:41:05 -0700540
Ray Milkeyaee4d3e2018-05-11 09:59:19 -0700541 mainEventExecutor = null;
542 hostEventExecutor = null;
543 routeEventExecutor = null;
544 mcastEventExecutor = null;
545 packetExecutor = null;
546
Charles Chan72f556a2015-10-05 17:50:33 -0700547 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700548 cfgService.unregisterConfigFactory(deviceConfigFactory);
549 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chandebfea32016-10-24 14:52:01 -0700550 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700551 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan873661e2017-11-30 15:37:50 -0800552 compCfgService.unregisterProperties(getClass(), false);
Charles Chan72f556a2015-10-05 17:50:33 -0700553
Charles Chanfd48c222017-06-19 00:43:31 -0700554 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700555 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700556 linkService.removeListener(linkListener);
557 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700558 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700559 routeService.removeListener(routeListener);
Pier Luigid8a15162018-02-15 16:33:08 +0100560 topologyService.removeListener(topologyListener);
Charles Chanfbcb8812018-04-18 18:41:05 -0700561 mastershipService.removeListener(mastershipListener);
Saurav Das00e553b2018-04-21 17:19:48 -0700562 clusterService.removeListener(clusterListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700563
Charles Chan2e71ef32017-02-23 15:44:08 -0800564 neighbourResolutionService.unregisterNeighbourHandlers(appId);
565
sangho80f11cb2015-04-01 13:05:26 -0700566 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700567 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700568 deviceListener = null;
Charles Chan05bb1702018-04-19 13:10:01 -0700569 groupHandlerMap.forEach((k, v) -> v.shutdown());
Charles Chan2b078ae2015-10-14 11:24:40 -0700570 groupHandlerMap.clear();
Saurav Das49368392018-04-23 18:42:12 -0700571 defaultRoutingHandler.shutdown();
Charles Chan2b078ae2015-10-14 11:24:40 -0700572
Saurav Das261c3002017-06-13 15:35:54 -0700573 dsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800574 vlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700575 portNextObjStore.destroy();
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700576 dummyVlanIdStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700577 tunnelStore.destroy();
578 policyStore.destroy();
Pier Luigi35dab3f2018-01-25 16:16:02 +0100579
580 mcastHandler.terminate();
sangho80f11cb2015-04-01 13:05:26 -0700581 log.info("Stopped");
582 }
583
Charles Chan873661e2017-11-30 15:37:50 -0800584 @Modified
585 private void modified(ComponentContext context) {
586 Dictionary<?, ?> properties = context.getProperties();
587 if (properties == null) {
588 return;
589 }
590
591 String strActiveProving = Tools.get(properties, "activeProbing");
592 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
Charles Chan873661e2017-11-30 15:37:50 -0800593 if (expectActiveProbing != activeProbing) {
594 activeProbing = expectActiveProbing;
595 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
596 }
Saurav Dasec683dc2018-04-27 18:42:30 -0700597
598 String strSingleHomedDown = Tools.get(properties, "singleHomedDown");
599 boolean expectSingleHomedDown = Boolean.parseBoolean(strSingleHomedDown);
600 if (expectSingleHomedDown != singleHomedDown) {
601 singleHomedDown = expectSingleHomedDown;
602 log.info("{} downing of single homed hosts for lost uplinks",
603 singleHomedDown ? "Enabling" : "Disabling");
604 if (singleHomedDown && linkHandler != null) {
605 hostService.getHosts().forEach(host -> host.locations()
606 .forEach(loc -> {
607 if (interfaceService.isConfigured(loc)) {
608 linkHandler.checkUplinksForHost(loc);
609 }
610 }));
611 } else {
612 log.warn("Disabling singleHomedDown does not re-enable already "
613 + "downed ports for single-homed hosts");
614 }
615 }
Charles Chan873661e2017-11-30 15:37:50 -0800616 }
617
sangho27462c62015-05-14 00:39:53 -0700618 @Override
619 public List<Tunnel> getTunnels() {
620 return tunnelHandler.getTunnels();
621 }
622
623 @Override
sanghobd812f82015-06-29 14:58:47 -0700624 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
625 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700626 }
627
628 @Override
sanghobd812f82015-06-29 14:58:47 -0700629 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700630 for (Policy policy: policyHandler.getPolicies()) {
631 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
632 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
633 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
634 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700635 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700636 }
637 }
638 }
sanghobd812f82015-06-29 14:58:47 -0700639 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700640 }
641
642 @Override
sanghobd812f82015-06-29 14:58:47 -0700643 public PolicyHandler.Result removePolicy(Policy policy) {
644 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700645 }
646
647 @Override
sanghobd812f82015-06-29 14:58:47 -0700648 public PolicyHandler.Result createPolicy(Policy policy) {
649 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700650 }
651
652 @Override
653 public List<Policy> getPolicies() {
654 return policyHandler.getPolicies();
655 }
656
Saurav Das07c74602016-04-27 18:35:50 -0700657 @Override
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700658 public Set<L2TunnelDescription> getL2TunnelDescriptions(boolean pending) {
659 return l2TunnelHandler.getL2Descriptions(pending);
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700660 }
661
662 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800663 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700664 return l2TunnelHandler.getL2Tunnels();
665 }
666
667 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800668 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700669 return l2TunnelHandler.getL2Policies();
670 }
671
Saurav Dasdebcf882018-04-06 20:16:01 -0700672 @Override
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700673 @Deprecated
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700674 public L2TunnelHandler.Result addPseudowiresBulk(List<DefaultL2TunnelDescription> bulkPseudowires) {
675
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700676 // get both added and pending pseudowires
677 List<L2TunnelDescription> pseudowires = new ArrayList<>();
678 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(false));
679 pseudowires.addAll(l2TunnelHandler.getL2Descriptions(true));
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700680 pseudowires.addAll(bulkPseudowires);
Andreas Pantelopoulosd988c1a2018-03-15 16:56:09 -0700681
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700682 Set<L2TunnelDescription> newPseudowires = new HashSet(bulkPseudowires);
683
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700684 L2TunnelHandler.Result retRes = L2TunnelHandler.Result.SUCCESS;
685 L2TunnelHandler.Result res;
686 for (DefaultL2TunnelDescription pw : bulkPseudowires) {
687 res = addPseudowire(pw);
688 if (res != L2TunnelHandler.Result.SUCCESS) {
689 log.error("Pseudowire with id {} can not be instantiated !", res);
690 retRes = res;
691 }
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700692 }
Andreas Pantelopoulos96851252018-03-20 13:58:49 -0700693
694 return retRes;
Andreas Pantelopoulosfc4bc2a2018-03-12 16:30:20 -0700695 }
696
697 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800698 public L2TunnelHandler.Result addPseudowire(L2TunnelDescription l2TunnelDescription) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700699 return l2TunnelHandler.deployPseudowire(l2TunnelDescription);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700700 }
701
702 @Override
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -0800703 public L2TunnelHandler.Result removePseudowire(Integer pwId) {
Andreas Pantelopoulos87a06102018-03-21 16:44:18 -0700704 return l2TunnelHandler.tearDownPseudowire(pwId);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700705 }
706
707 @Override
Saurav Das07c74602016-04-27 18:35:50 -0700708 public void rerouteNetwork() {
709 cfgListener.configureNetwork();
Saurav Das07c74602016-04-27 18:35:50 -0700710 }
711
Charles Chand7844e52016-10-20 17:02:44 -0700712 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800713 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
714 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart61e24e12017-11-30 18:23:42 -0800715 deviceConfiguration.getRouters().forEach(device ->
716 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chand7844e52016-10-20 17:02:44 -0700717 return deviceSubnetMap;
718 }
719
Saurav Das62ae6792017-05-15 15:34:25 -0700720
721 @Override
722 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
723 if (defaultRoutingHandler != null) {
724 return defaultRoutingHandler.getCurrentEmcpSpgMap();
725 } else {
726 return null;
727 }
728 }
729
730 @Override
Saurav Das261c3002017-06-13 15:35:54 -0700731 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
732 if (dsNextObjStore != null) {
733 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Das62ae6792017-05-15 15:34:25 -0700734 } else {
735 return ImmutableMap.of();
736 }
737 }
738
Saurav Dasfbe74572017-08-03 18:30:35 -0700739 @Override
740 public void verifyGroups(DeviceId id) {
741 DefaultGroupHandler gh = groupHandlerMap.get(id);
742 if (gh != null) {
743 gh.triggerBucketCorrector();
744 }
745 }
746
Saurav Das6430f412018-01-25 09:49:01 -0800747 @Override
748 public ImmutableMap<Link, Boolean> getSeenLinks() {
749 return linkHandler.getSeenLinks();
750 }
751
752 @Override
753 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
754 return linkHandler.getDownedPorts();
755 }
756
Pier Luigi0f9635b2018-01-15 18:06:43 +0100757 @Override
758 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
759 return mcastHandler.getMcastNextIds(mcastIp);
760 }
761
762 @Override
Pier Luigi69f774d2018-02-28 12:10:50 +0100763 public Map<McastStoreKey, McastRole> getMcastRoles(IpAddress mcastIp) {
Pier Luigi0f9635b2018-01-15 18:06:43 +0100764 return mcastHandler.getMcastRoles(mcastIp);
765 }
766
767 @Override
Piere99511d2018-04-19 16:47:06 +0200768 public Map<McastRoleStoreKey, McastRole> getMcastRoles(IpAddress mcastIp, ConnectPoint sourcecp) {
769 return mcastHandler.getMcastRoles(mcastIp, sourcecp);
770 }
771
772 @Override
Pier Luigi0f9635b2018-01-15 18:06:43 +0100773 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
774 return mcastHandler.getMcastPaths(mcastIp);
775 }
776
Pierdb27b8d2018-04-17 16:29:56 +0200777 @Override
Pier71c55772018-04-17 17:25:22 +0200778 public Multimap<ConnectPoint, List<ConnectPoint>> getMcastTrees(IpAddress mcastIp,
779 ConnectPoint sourcecp) {
780 return mcastHandler.getMcastTrees(mcastIp, sourcecp);
781 }
782
783 @Override
Pierdb27b8d2018-04-17 16:29:56 +0200784 public Map<IpAddress, NodeId> getMcastLeaders(IpAddress mcastIp) {
785 return mcastHandler.getMcastLeaders(mcastIp);
786 }
787
Charles Chanb13e0702018-04-17 18:56:53 -0700788 @Override
789 public Map<Set<DeviceId>, NodeId> getShouldProgram() {
790 return defaultRoutingHandler == null ? ImmutableMap.of() :
791 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgram);
792 }
793
794 @Override
795 public Map<DeviceId, Boolean> getShouldProgramCache() {
796 return defaultRoutingHandler == null ? ImmutableMap.of() :
797 ImmutableMap.copyOf(defaultRoutingHandler.shouldProgramCache);
798 }
799
sangho80f1f892015-05-19 11:57:42 -0700800 /**
Ray Milkeyb85de082017-04-05 09:42:04 -0700801 * Extracts the application ID from the manager.
802 *
803 * @return application ID
804 */
805 public ApplicationId appId() {
806 return appId;
807 }
808
809 /**
810 * Returns the device configuration.
811 *
812 * @return device configuration
813 */
814 public DeviceConfiguration deviceConfiguration() {
815 return deviceConfiguration;
816 }
817
818 /**
Saurav Das261c3002017-06-13 15:35:54 -0700819 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700820 * Used to keep track on MPLS group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700821 *
822 * @return next objective ID store
823 */
Saurav Das261c3002017-06-13 15:35:54 -0700824 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
825 dsNextObjStore() {
826 return dsNextObjStore;
Ray Milkeyb85de082017-04-05 09:42:04 -0700827 }
828
829 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700830 * Per device next objective ID store with (device id + vlanid) as key.
831 * Used to keep track on L2 flood group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700832 *
833 * @return vlan next object store
834 */
835 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
836 return vlanNextObjStore;
837 }
838
839 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700840 * Per device next objective ID store with (device id + port + treatment + meta) as key.
841 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700842 *
843 * @return port next object store.
844 */
845 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
846 return portNextObjStore;
847 }
848
849 /**
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -0700850 * Per port dummy VLAN ID store with (connect point + ip address) as key.
851 * Used to keep track on dummy VLAN ID allocation.
852 *
853 * @return dummy vlan id store.
854 */
855 public EventuallyConsistentMap<DummyVlanIdStoreKey, VlanId> dummyVlanIdStore() {
856 return dummyVlanIdStore;
857 }
858
859 /**
Saurav Das261c3002017-06-13 15:35:54 -0700860 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
861 * labeled packets should be programmed or not.
Pier Ventre7a78de22016-10-31 15:00:01 -0700862 *
863 * @return MPLS-ECMP value
864 */
865 public boolean getMplsEcmp() {
866 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
867 .getConfig(this.appId, SegmentRoutingAppConfig.class);
868 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
869 }
870
871 /**
sangho80f1f892015-05-19 11:57:42 -0700872 * Returns the tunnel object with the tunnel ID.
873 *
874 * @param tunnelId Tunnel ID
875 * @return Tunnel reference
876 */
sangho27462c62015-05-14 00:39:53 -0700877 public Tunnel getTunnel(String tunnelId) {
878 return tunnelHandler.getTunnel(tunnelId);
879 }
880
Charles Chan90772a72017-02-08 15:52:08 -0800881 /**
Charles Chand9265a32017-06-16 15:19:24 -0700882 * Returns internal VLAN for untagged hosts on given connect point.
883 * <p>
884 * The internal VLAN is either vlan-untagged for an access port,
885 * or vlan-native for a trunk port.
886 *
887 * @param connectPoint connect point
888 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
889 */
Pier Luigi69f774d2018-02-28 12:10:50 +0100890 public VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chan098ca202018-05-01 11:50:20 -0700891 VlanId untaggedVlanId = interfaceService.getUntaggedVlanId(connectPoint);
892 VlanId nativeVlanId = interfaceService.getNativeVlanId(connectPoint);
Charles Chand9265a32017-06-16 15:19:24 -0700893 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
894 }
895
896 /**
Charles Chan3ed34d82017-06-22 18:03:14 -0700897 * Returns optional pair device ID of given device.
898 *
899 * @param deviceId device ID
900 * @return optional pair device ID. Might be empty if pair device is not configured
901 */
902 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
903 SegmentRoutingDeviceConfig deviceConfig =
904 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
905 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
906 }
907 /**
908 * Returns optional pair device local port of given device.
909 *
910 * @param deviceId device ID
911 * @return optional pair device ID. Might be empty if pair device is not configured
912 */
Saurav Dasec683dc2018-04-27 18:42:30 -0700913 public Optional<PortNumber> getPairLocalPort(DeviceId deviceId) {
Charles Chan3ed34d82017-06-22 18:03:14 -0700914 SegmentRoutingDeviceConfig deviceConfig =
915 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
916 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
917 }
918
919 /**
Charles Chanf0ae41e2017-08-23 13:55:39 -0700920 * Returns locations of given resolved route.
921 *
922 * @param resolvedRoute resolved route
923 * @return locations of nexthop. Might be empty if next hop is not found
924 */
925 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
926 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
927 return Optional.ofNullable(hostService.getHost(hostId))
928 .map(Host::locations).orElse(Sets.newHashSet())
929 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
930 }
931
932 /**
Charles Chan90772a72017-02-08 15:52:08 -0800933 * Returns vlan port map of given device.
934 *
935 * @param deviceId device id
936 * @return vlan-port multimap
937 */
938 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
939 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
940
941 interfaceService.getInterfaces().stream()
942 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
943 .forEach(intf -> {
944 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan3ed34d82017-06-22 18:03:14 -0700945 intf.vlanTagged().forEach(vlanTagged ->
946 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
947 );
Charles Chan90772a72017-02-08 15:52:08 -0800948 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
949 });
950 vlanPortMap.removeAll(VlanId.NONE);
951
952 return vlanPortMap;
953 }
954
955 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800956 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -0800957 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -0700958 *
959 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -0800960 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -0800961 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -0700962 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700963 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -0700964 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800965 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
966 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -0700967 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -0800968 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -0800969 + "device {} not found", deviceId);
970 return -1;
971 }
972 }
973
974 /**
975 * Returns the next objective ID for the given portNumber, given the treatment.
976 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -0700977 * would result in different objectives. If the next object does not exist,
978 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -0800979 *
980 * @param deviceId Device ID
981 * @param portNum port number on device for which NextObjective is queried
982 * @param treatment the actions to apply on the packets (should include outport)
983 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -0700984 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -0700985 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -0800986 */
987 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
988 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -0700989 TrafficSelector meta,
990 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -0800991 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
992 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -0700993 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -0800994 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -0800995 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
996 + " not found", deviceId);
997 return -1;
998 }
999 }
1000
Saurav Das62ae6792017-05-15 15:34:25 -07001001 /**
1002 * Returns the group handler object for the specified device id.
1003 *
1004 * @param devId the device identifier
1005 * @return the groupHandler object for the device id, or null if not found
1006 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001007 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Das62ae6792017-05-15 15:34:25 -07001008 return groupHandlerMap.get(devId);
1009 }
1010
1011 /**
Saurav Dasfbe74572017-08-03 18:30:35 -07001012 * Returns the default routing handler object.
1013 *
1014 * @return the default routing handler object
1015 */
1016 public DefaultRoutingHandler getRoutingHandler() {
1017 return defaultRoutingHandler;
1018 }
1019
Jonghwan Hyun9aaa34f2018-04-09 09:40:50 -07001020 /**
1021 * Returns new dummy VLAN ID.
1022 * Dummy VLAN ID should be unique in each connect point.
1023 *
1024 * @param cp connect point
1025 * @param ipAddress IP address
1026 * @return new dummy VLAN ID. Returns VlanId.NONE if no VLAN ID is available.
1027 */
1028 public synchronized VlanId allocateDummyVlanId(ConnectPoint cp, IpAddress ipAddress) {
1029 Set<VlanId> usedVlanId = Sets.union(getVlanPortMap(cp.deviceId()).keySet(),
1030 dummyVlanIdStore.entrySet().stream().filter(entry ->
1031 (entry.getKey()).connectPoint().equals(cp))
1032 .map(Map.Entry::getValue)
1033 .collect(Collectors.toSet()));
1034
1035 VlanId dummyVlanId = IntStream.range(MIN_DUMMY_VLAN_ID, MAX_DUMMY_VLAN_ID).mapToObj(
1036 i -> VlanId.vlanId((short) (i & 0xFFFF))
1037 ).filter(vlanId -> !usedVlanId.contains(vlanId)).findFirst().orElse(VlanId.NONE);
1038
1039 if (!dummyVlanId.equals(VlanId.NONE)) {
1040 this.dummyVlanIdStore.put(new DummyVlanIdStoreKey(cp, ipAddress), dummyVlanId);
1041 log.debug("Dummy VLAN ID {} is allocated to {}, {}", dummyVlanId, cp, ipAddress);
1042 } else {
1043 log.error("Failed to allocate dummy VLAN ID for {}, {}", cp, ipAddress);
1044 }
1045 return dummyVlanId;
1046 }
1047
1048
sangho80f11cb2015-04-01 13:05:26 -07001049 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -07001050 @Override
1051 public void process(PacketContext context) {
Charles Chan77cdde42018-05-08 21:35:50 -07001052 packetExecutor.execute(() -> processPacketInternal(context));
1053 }
sangho80f11cb2015-04-01 13:05:26 -07001054
Charles Chan77cdde42018-05-08 21:35:50 -07001055 private void processPacketInternal(PacketContext context) {
sangho80f11cb2015-04-01 13:05:26 -07001056 if (context.isHandled()) {
1057 return;
1058 }
1059
1060 InboundPacket pkt = context.inPacket();
1061 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -08001062
1063 if (ethernet == null) {
1064 return;
1065 }
1066
Saurav Das261c3002017-06-13 15:35:54 -07001067 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1068 ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -08001069 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -07001070 log.warn("Received unexpected ARP packet on {}",
1071 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -07001072 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001073 return;
sangho80f11cb2015-04-01 13:05:26 -07001074 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -08001075 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1076 //ipHandler.addToPacketBuffer(ipv4Packet);
1077 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1078 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001079 } else {
Charles Chand041ad82017-01-13 17:20:44 -08001080 // NOTE: We don't support IP learning at this moment so this
1081 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001082 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001083 }
Pier Ventreb6a7f342016-11-26 21:05:22 -08001084 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1085 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -08001086 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -08001087 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -08001088 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1089 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1090 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1091 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1092 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1093 } else {
Saurav Das62ae6792017-05-15 15:34:25 -07001094 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -07001095 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -08001096 }
1097 } else {
1098 // NOTE: We don't support IP learning at this moment so this
1099 // is not necessary. Also it causes duplication of DHCPv6 packets.
1100 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1101 }
sangho80f11cb2015-04-01 13:05:26 -07001102 }
1103 }
1104 }
1105
sangho80f11cb2015-04-01 13:05:26 -07001106 private class InternalEventHandler implements Runnable {
Charles Chan52003922018-04-05 16:31:15 -07001107 private Event event;
1108
1109 InternalEventHandler(Event event) {
1110 this.event = event;
1111 }
1112
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001113 @Override
sangho80f11cb2015-04-01 13:05:26 -07001114 public void run() {
Charles Chan52003922018-04-05 16:31:15 -07001115 try {
1116 // TODO We should also change SR routing and PW to listen to TopologyEvents
1117 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1118 event.type() == LinkEvent.Type.LINK_UPDATED) {
1119 linkHandler.processLinkAdded((Link) event.subject());
1120 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
1121 linkHandler.processLinkRemoved((Link) event.subject());
1122 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1123 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1124 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
1125 DeviceId deviceId = ((Device) event.subject()).id();
1126 if (deviceService.isAvailable(deviceId)) {
1127 log.info("** DEVICE UP Processing device event {} "
1128 + "for available device {}",
1129 event.type(), ((Device) event.subject()).id());
1130 processDeviceAdded((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001131 } else {
Charles Chan52003922018-04-05 16:31:15 -07001132 log.info(" ** DEVICE DOWN Processing device event {}"
1133 + " for unavailable device {}",
1134 event.type(), ((Device) event.subject()).id());
1135 processDeviceRemoved((Device) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001136 }
Charles Chan52003922018-04-05 16:31:15 -07001137 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
1138 // typically these calls come when device is added first time
1139 // so port filtering rules are handled at the device_added event.
1140 // port added calls represent all ports on the device,
1141 // enabled or not.
1142 log.trace("** PORT ADDED {}/{} -> {}",
1143 ((DeviceEvent) event).subject().id(),
1144 ((DeviceEvent) event).port().number(),
1145 event.type());
1146 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
1147 // these calls happen for every subsequent event
1148 // ports enabled, disabled, switch goes away, comes back
1149 log.info("** PORT UPDATED {}/{} -> {}",
1150 event.subject(),
1151 ((DeviceEvent) event).port(),
1152 event.type());
Saurav Dasec683dc2018-04-27 18:42:30 -07001153 processPortUpdatedInternal(((Device) event.subject()),
Charles Chan52003922018-04-05 16:31:15 -07001154 ((DeviceEvent) event).port());
1155 } else if (event.type() == TopologyEvent.Type.TOPOLOGY_CHANGED) {
1156 // Process topology event, needed for all modules relying on
1157 // topology service for path computation
1158 TopologyEvent topologyEvent = (TopologyEvent) event;
1159 log.info("Processing topology event {}, topology age {}, reasons {}",
1160 event.type(), topologyEvent.subject().time(),
1161 topologyEvent.reasons().size());
1162 topologyHandler.processTopologyChange(topologyEvent.reasons());
1163 } else if (event.type() == HostEvent.Type.HOST_ADDED) {
1164 hostHandler.processHostAddedEvent((HostEvent) event);
1165 } else if (event.type() == HostEvent.Type.HOST_MOVED) {
1166 hostHandler.processHostMovedEvent((HostEvent) event);
1167 routeHandler.processHostMovedEvent((HostEvent) event);
1168 } else if (event.type() == HostEvent.Type.HOST_REMOVED) {
1169 hostHandler.processHostRemovedEvent((HostEvent) event);
1170 } else if (event.type() == HostEvent.Type.HOST_UPDATED) {
1171 hostHandler.processHostUpdatedEvent((HostEvent) event);
1172 } else if (event.type() == RouteEvent.Type.ROUTE_ADDED) {
1173 routeHandler.processRouteAdded((RouteEvent) event);
1174 } else if (event.type() == RouteEvent.Type.ROUTE_UPDATED) {
1175 routeHandler.processRouteUpdated((RouteEvent) event);
1176 } else if (event.type() == RouteEvent.Type.ROUTE_REMOVED) {
1177 routeHandler.processRouteRemoved((RouteEvent) event);
1178 } else if (event.type() == RouteEvent.Type.ALTERNATIVE_ROUTES_CHANGED) {
1179 routeHandler.processAlternativeRoutesChanged((RouteEvent) event);
1180 } else if (event.type() == McastEvent.Type.SOURCES_ADDED ||
1181 event.type() == McastEvent.Type.SOURCES_REMOVED ||
1182 event.type() == McastEvent.Type.SINKS_ADDED ||
1183 event.type() == McastEvent.Type.SINKS_REMOVED ||
1184 event.type() == McastEvent.Type.ROUTE_ADDED ||
1185 event.type() == McastEvent.Type.ROUTE_REMOVED) {
1186 mcastHandler.processMcastEvent((McastEvent) event);
1187 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
1188 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1189 Class configClass = netcfgEvent.configClass();
1190 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1191 appCfgHandler.processAppConfigAdded(netcfgEvent);
1192 log.info("App config event .. configuring network");
1193 cfgListener.configureNetwork();
1194 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1195 log.info("Segment Routing Device Config added for {}", event.subject());
1196 cfgListener.configureNetwork();
1197 } else if (configClass.equals(XConnectConfig.class)) {
1198 xConnectHandler.processXConnectConfigAdded(netcfgEvent);
1199 } else if (configClass.equals(InterfaceConfig.class)) {
1200 log.info("Interface Config added for {}", event.subject());
1201 cfgListener.configureNetwork();
1202 } else {
1203 log.error("Unhandled config class: {}", configClass);
1204 }
1205 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
1206 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1207 Class configClass = netcfgEvent.configClass();
1208 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1209 appCfgHandler.processAppConfigUpdated(netcfgEvent);
1210 log.info("App config event .. configuring network");
1211 cfgListener.configureNetwork();
1212 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1213 log.info("Segment Routing Device Config updated for {}", event.subject());
1214 createOrUpdateDeviceConfiguration();
1215 } else if (configClass.equals(XConnectConfig.class)) {
1216 xConnectHandler.processXConnectConfigUpdated(netcfgEvent);
1217 } else if (configClass.equals(InterfaceConfig.class)) {
1218 log.info("Interface Config updated for {}", event.subject());
1219 createOrUpdateDeviceConfiguration();
1220 updateInterface((InterfaceConfig) netcfgEvent.config().get(),
1221 (InterfaceConfig) netcfgEvent.prevConfig().get());
1222 } else {
1223 log.error("Unhandled config class: {}", configClass);
1224 }
1225 } else if (event.type() == NetworkConfigEvent.Type.CONFIG_REMOVED) {
1226 NetworkConfigEvent netcfgEvent = (NetworkConfigEvent) event;
1227 Class configClass = netcfgEvent.configClass();
1228 if (configClass.equals(SegmentRoutingAppConfig.class)) {
1229 appCfgHandler.processAppConfigRemoved(netcfgEvent);
1230 log.info("App config event .. configuring network");
1231 cfgListener.configureNetwork();
1232 } else if (configClass.equals(SegmentRoutingDeviceConfig.class)) {
1233 // TODO Handle sr device config removal
1234 log.info("SegmentRoutingDeviceConfig removal is not handled in current implementation");
1235 } else if (configClass.equals(XConnectConfig.class)) {
1236 xConnectHandler.processXConnectConfigRemoved(netcfgEvent);
1237 } else if (configClass.equals(InterfaceConfig.class)) {
1238 // TODO Handle interface removal
1239 log.info("InterfaceConfig removal is not handled in current implementation");
1240 } else {
1241 log.error("Unhandled config class: {}", configClass);
1242 }
Saurav Das00e553b2018-04-21 17:19:48 -07001243 } else if (event.type() == MastershipEvent.Type.MASTER_CHANGED) {
1244 MastershipEvent me = (MastershipEvent) event;
1245 DeviceId deviceId = me.subject();
1246 Optional<DeviceId> pairDeviceId = getPairDeviceId(deviceId);
1247 log.info(" ** MASTERSHIP CHANGED Invalidating shouldProgram cache"
1248 + " for {}/pair={} due to change", deviceId, pairDeviceId);
1249 defaultRoutingHandler.invalidateShouldProgramCache(deviceId);
1250 pairDeviceId.ifPresent(defaultRoutingHandler::invalidateShouldProgramCache);
1251 defaultRoutingHandler.checkFullRerouteForMasterChange(deviceId, me);
Charles Chan52003922018-04-05 16:31:15 -07001252 } else {
1253 log.warn("Unhandled event type: {}", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001254 }
Charles Chan52003922018-04-05 16:31:15 -07001255 } catch (Exception e) {
1256 log.error("SegmentRouting event handler thread thrown an exception: {}",
1257 e.getMessage(), e);
sangho80f11cb2015-04-01 13:05:26 -07001258 }
sangho80f11cb2015-04-01 13:05:26 -07001259 }
1260 }
1261
Saurav Dase6c448a2018-01-18 12:07:33 -08001262 void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001263 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan9bd6aea2017-06-27 18:48:32 -07001264
1265 // NOTE: Punt ARP/NDP even when the device is not configured.
1266 // Host learning without network config is required for CORD config generator.
1267 routingRulePopulator.populateIpPunts(device.id());
1268 routingRulePopulator.populateArpNdpPunts(device.id());
1269
Charles Chan319d1a22015-11-03 10:42:14 -08001270 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das261c3002017-06-13 15:35:54 -07001271 log.warn("Device configuration unavailable. Device {} will be "
1272 + "processed after configuration.", device.id());
Saurav Das8ec0ec42015-11-03 14:39:27 -08001273 return;
1274 }
Charles Chan72779502016-04-23 17:36:10 -07001275 processDeviceAddedInternal(device.id());
1276 }
1277
1278 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001279 // Irrespective of whether the local is a MASTER or not for this device,
1280 // we need to create a SR-group-handler instance. This is because in a
1281 // multi-instance setup, any instance can initiate forwarding/next-objectives
1282 // for any switch (even if this instance is a SLAVE or not even connected
1283 // to the switch). To handle this, a default-group-handler instance is necessary
1284 // per switch.
Charles Chan72779502016-04-23 17:36:10 -07001285 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1286 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001287 DefaultGroupHandler groupHandler;
1288 try {
1289 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001290 createGroupHandler(deviceId,
1291 appId,
1292 deviceConfiguration,
1293 linkService,
1294 flowObjectiveService,
1295 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001296 } catch (DeviceConfigNotFoundException e) {
1297 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1298 return;
1299 }
Saurav Das2b6a00f2017-12-05 15:00:23 -08001300 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan72779502016-04-23 17:36:10 -07001301 deviceId);
1302 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001303 }
Saurav Dasb149be12016-06-07 10:08:06 -07001304
Charles Chan72779502016-04-23 17:36:10 -07001305 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001306 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan82f19972016-05-17 13:13:55 -07001307 xConnectHandler.init(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001308 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001309 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001310 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001311 }
Charles Chan82ab1932016-01-30 23:22:37 -08001312
Charles Chandebfea32016-10-24 14:52:01 -07001313 appCfgHandler.init(deviceId);
Charles Chan074fae62018-04-29 18:11:37 -07001314 hostEventExecutor.execute(() -> hostHandler.init(deviceId));
1315 routeEventExecutor.execute(() -> routeHandler.init(deviceId));
sangho80f11cb2015-04-01 13:05:26 -07001316 }
1317
Saurav Dasc3604f12016-03-23 11:22:49 -07001318 private void processDeviceRemoved(Device device) {
Saurav Das261c3002017-06-13 15:35:54 -07001319 dsNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001320 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan873661e2017-11-30 15:37:50 -08001321 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan10b0fb72017-02-02 16:20:42 -08001322 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001323 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001324 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001325 portNextObjStore.entrySet().stream()
1326 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001327 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Dase6c448a2018-01-18 12:07:33 -08001328 linkHandler.processDeviceRemoved(device);
Charles Chan17d75d82017-06-15 00:44:51 -07001329
Saurav Dasfbe74572017-08-03 18:30:35 -07001330 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1331 if (gh != null) {
1332 gh.shutdown();
1333 }
Saurav Das62ae6792017-05-15 15:34:25 -07001334 // Note that a switch going down is associated with all of its links
1335 // going down as well, but it is treated as a single switch down event
Saurav Dasdebcf882018-04-06 20:16:01 -07001336 // while the link-downs are ignored. We cannot rely on the ordering of
1337 // events - i.e we cannot expect all link-downs to come before the
1338 // switch down - so we purge all seen-links for the switch before
1339 // handling route-path changes for the switch-down
Saurav Das62ae6792017-05-15 15:34:25 -07001340 defaultRoutingHandler
Saurav Dasdc7f2752018-03-18 21:28:15 -07001341 .populateRoutingRulesForLinkStatusChange(null, null, device.id(), true);
Saurav Das6430f412018-01-25 09:49:01 -08001342 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan82f19972016-05-17 13:13:55 -07001343 xConnectHandler.removeDevice(device.id());
Saurav Das97241862018-02-14 14:14:54 -08001344
1345 // Cleanup all internal groupHandler stores for this device. Should be
1346 // done after all rerouting or rehashing has been completed
1347 groupHandlerMap.entrySet()
1348 .forEach(entry -> entry.getValue().cleanUpForNeighborDown(device.id()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001349 }
1350
Saurav Das49368392018-04-23 18:42:12 -07001351 /**
1352 * Purge the destinationSet nextObjective store of entries with this device
1353 * as key. Erases app-level knowledge of hashed groups in this device.
1354 *
1355 * @param devId the device identifier
1356 */
Saurav Das00e553b2018-04-21 17:19:48 -07001357 void purgeHashedNextObjectiveStore(DeviceId devId) {
Saurav Das49368392018-04-23 18:42:12 -07001358 log.debug("Purging hashed next-obj store for dev:{}", devId);
Saurav Das00e553b2018-04-21 17:19:48 -07001359 dsNextObjStore.entrySet().stream()
1360 .filter(entry -> entry.getKey().deviceId().equals(devId))
1361 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
1362 }
1363
Saurav Dasec683dc2018-04-27 18:42:30 -07001364 private void processPortUpdatedInternal(Device device, Port port) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001365 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1366 log.warn("Device configuration uploading. Not handling port event for"
1367 + "dev: {} port: {}", device.id(), port.number());
1368 return;
1369 }
Saurav Dasf9332192017-02-18 14:05:44 -08001370
Saurav Dasec683dc2018-04-27 18:42:30 -07001371 if (interfaceService.isConfigured(new ConnectPoint(device.id(), port.number()))) {
1372 lastEdgePortEvent = Instant.now();
1373 }
1374
Saurav Dasf9332192017-02-18 14:05:44 -08001375 if (!mastershipService.isLocalMaster(device.id())) {
1376 log.debug("Not master for dev:{} .. not handling port updated event"
1377 + "for port {}", device.id(), port.number());
1378 return;
1379 }
Saurav Dasec683dc2018-04-27 18:42:30 -07001380 processPortUpdated(device.id(), port);
1381 }
Saurav Dasf9332192017-02-18 14:05:44 -08001382
Saurav Dasec683dc2018-04-27 18:42:30 -07001383 /**
1384 * Adds or remove filtering rules for the given switchport. If switchport is
1385 * an edge facing port, additionally handles host probing and broadcast
1386 * rules. Must be called by local master of device.
1387 *
1388 * @param deviceId the device identifier
1389 * @param port the port to update
1390 */
1391 void processPortUpdated(DeviceId deviceId, Port port) {
Saurav Dasf9332192017-02-18 14:05:44 -08001392 // first we handle filtering rules associated with the port
1393 if (port.isEnabled()) {
1394 log.info("Switchport {}/{} enabled..programming filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001395 deviceId, port.number());
1396 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001397 } else {
1398 log.info("Switchport {}/{} disabled..removing filters",
Saurav Dasec683dc2018-04-27 18:42:30 -07001399 deviceId, port.number());
1400 routingRulePopulator.processSinglePortFilters(deviceId, port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001401 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001402
1403 // portUpdated calls are for ports that have gone down or up. For switch
1404 // to switch ports, link-events should take care of any re-routing or
1405 // group editing necessary for port up/down. Here we only process edge ports
1406 // that are already configured.
Saurav Dasec683dc2018-04-27 18:42:30 -07001407 ConnectPoint cp = new ConnectPoint(deviceId, port.number());
Charles Chan098ca202018-05-01 11:50:20 -07001408 VlanId untaggedVlan = interfaceService.getUntaggedVlanId(cp);
1409 VlanId nativeVlan = interfaceService.getNativeVlanId(cp);
1410 Set<VlanId> taggedVlans = interfaceService.getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001411
Saurav Das3fb28272017-03-04 16:08:47 -08001412 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001413 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasec683dc2018-04-27 18:42:30 -07001414 + "dev/port: {}/{}", deviceId, port.number());
Saurav Dasf0f592d2016-11-18 15:21:57 -08001415 return;
1416 }
Saurav Das3fb28272017-03-04 16:08:47 -08001417 if (untaggedVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001418 processEdgePort(deviceId, port, untaggedVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001419 }
1420 if (nativeVlan != null) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001421 processEdgePort(deviceId, port, nativeVlan, true);
Saurav Das3fb28272017-03-04 16:08:47 -08001422 }
1423 if (!taggedVlans.isEmpty()) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001424 taggedVlans.forEach(tag -> processEdgePort(deviceId, port, tag, false));
Saurav Das3fb28272017-03-04 16:08:47 -08001425 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001426 }
1427
Saurav Dasec683dc2018-04-27 18:42:30 -07001428 private void processEdgePort(DeviceId deviceId, Port port, VlanId vlanId,
Saurav Das3fb28272017-03-04 16:08:47 -08001429 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001430 boolean portUp = port.isEnabled();
1431 if (portUp) {
Saurav Dasec683dc2018-04-27 18:42:30 -07001432 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001433 port.number(), vlanId);
Charles Chan074fae62018-04-29 18:11:37 -07001434 hostEventExecutor.execute(() -> hostHandler.processPortUp(new ConnectPoint(deviceId, port.number())));
Saurav Dasf0f592d2016-11-18 15:21:57 -08001435 } else {
Saurav Dasec683dc2018-04-27 18:42:30 -07001436 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", deviceId,
Charles Chan10b0fb72017-02-02 16:20:42 -08001437 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001438 }
1439
Saurav Dasec683dc2018-04-27 18:42:30 -07001440 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001441 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001442 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001443 } else {
1444 log.warn("Group handler not found for dev:{}. Not handling edge port"
Saurav Dasec683dc2018-04-27 18:42:30 -07001445 + " {} event for port:{}", deviceId,
Saurav Dasf0f592d2016-11-18 15:21:57 -08001446 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001447 }
1448 }
sangho27462c62015-05-14 00:39:53 -07001449
Charles Chan8ca5a122017-10-20 16:06:55 -07001450 private void createOrUpdateDeviceConfiguration() {
1451 if (deviceConfiguration == null) {
Saurav Dase321cff2018-02-09 17:26:45 -08001452 log.info("Creating new DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001453 deviceConfiguration = new DeviceConfiguration(this);
1454 } else {
Saurav Dase321cff2018-02-09 17:26:45 -08001455 log.info("Updating DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001456 deviceConfiguration.updateConfig();
1457 }
1458 }
1459
Charles Chanfbcb8812018-04-18 18:41:05 -07001460 private void createOrUpdateDefaultRoutingHandler() {
1461 if (defaultRoutingHandler == null) {
1462 log.info("Creating new DefaultRoutingHandler");
1463 defaultRoutingHandler = new DefaultRoutingHandler(this);
1464 } else {
1465 log.info("Updating DefaultRoutingHandler");
1466 defaultRoutingHandler.update(this);
1467 }
1468 }
1469
Pier Ventreb6a7f342016-11-26 21:05:22 -08001470 /**
1471 * Registers the given connect point with the NRS, this is necessary
1472 * to receive the NDP and ARP packets from the NRS.
1473 *
1474 * @param portToRegister connect point to register
1475 */
1476 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001477 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001478 portToRegister,
1479 neighbourHandler,
1480 appId
1481 );
1482 }
1483
Charles Chan72f556a2015-10-05 17:50:33 -07001484 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das261c3002017-06-13 15:35:54 -07001485 private static final long PROGRAM_DELAY = 2;
Charles Chan46fdfaf2016-11-09 20:51:44 -08001486 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001487
Charles Chanb7f75ac2016-01-11 18:28:54 -08001488 /**
1489 * Constructs the internal network config listener.
1490 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001491 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001492 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001493 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001494 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001495 }
1496
Charles Chanb7f75ac2016-01-11 18:28:54 -08001497 /**
1498 * Reads network config and initializes related data structure accordingly.
1499 */
Charles Chan873661e2017-11-30 15:37:50 -08001500 void configureNetwork() {
Saurav Dase321cff2018-02-09 17:26:45 -08001501 log.info("Configuring network ...");
Andrea Campanella060cad82018-04-17 12:09:34 +02001502
1503 // Setting handling of network configuration events completable future
1504 // The completable future is needed because of the async behaviour of the configureNetwork,
1505 // listener registration and event arrival
1506 // Enables us to buffer the events and execute them when the configure network is done.
1507 networkConfigCompletion = new CompletableFuture<>();
1508
1509 // add a small delay to absorb multiple network config added notifications
1510 if (!programmingScheduled.get()) {
1511 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
1512 programmingScheduled.set(true);
1513 mainEventExecutor.schedule(new ConfigChange(), PROGRAM_DELAY, TimeUnit.SECONDS);
1514 }
1515
Charles Chan8ca5a122017-10-20 16:06:55 -07001516 createOrUpdateDeviceConfiguration();
Charles Chane7c61022015-10-07 14:21:45 -07001517
Charles Chan46fdfaf2016-11-09 20:51:44 -08001518 arpHandler = new ArpHandler(srManager);
1519 icmpHandler = new IcmpHandler(srManager);
1520 ipHandler = new IpHandler(srManager);
1521 routingRulePopulator = new RoutingRulePopulator(srManager);
Charles Chanfbcb8812018-04-18 18:41:05 -07001522 createOrUpdateDefaultRoutingHandler();
Charles Chane7c61022015-10-07 14:21:45 -07001523
1524 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1525 groupHandlerMap, tunnelStore);
1526 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1527 flowObjectiveService,
1528 tunnelHandler, policyStore);
Andrea Campanella060cad82018-04-17 12:09:34 +02001529 networkConfigCompletion.complete(true);
1530
Charles Chan72779502016-04-23 17:36:10 -07001531 mcastHandler.init();
Andrea Campanella060cad82018-04-17 12:09:34 +02001532
Charles Chane7c61022015-10-07 14:21:45 -07001533 }
1534
Charles Chan72f556a2015-10-05 17:50:33 -07001535 @Override
1536 public void event(NetworkConfigEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001537 if (mainEventExecutor == null) {
1538 return;
1539 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001540 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
1541 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1542 switch (event.type()) {
1543 case CONFIG_ADDED:
1544 case CONFIG_UPDATED:
1545 case CONFIG_REMOVED:
1546 log.trace("Schedule Network Config event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001547 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1548 mainEventExecutor.execute(new InternalEventHandler(event));
1549 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001550 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001551 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001552 break;
1553 default:
1554 break;
Charles Chan72f556a2015-10-05 17:50:33 -07001555 }
1556 }
Saurav Das261c3002017-06-13 15:35:54 -07001557
Charles Chanc4a68c32018-01-03 16:26:32 -08001558 @Override
1559 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase321cff2018-02-09 17:26:45 -08001560 if (event.type() == CONFIG_REGISTERED ||
1561 event.type() == CONFIG_UNREGISTERED) {
1562 log.debug("Ignore event {} due to type mismatch", event);
1563 return false;
Charles Chanc4a68c32018-01-03 16:26:32 -08001564 }
Saurav Dase321cff2018-02-09 17:26:45 -08001565
1566 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1567 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1568 !event.configClass().equals(InterfaceConfig.class) &&
Andreas Pantelopouloscdbb22c2018-02-23 14:18:00 -08001569 !event.configClass().equals(XConnectConfig.class)) {
Saurav Dase321cff2018-02-09 17:26:45 -08001570 log.debug("Ignore event {} due to class mismatch", event);
1571 return false;
1572 }
1573
1574 return true;
Charles Chanc4a68c32018-01-03 16:26:32 -08001575 }
1576
Saurav Das261c3002017-06-13 15:35:54 -07001577 private final class ConfigChange implements Runnable {
1578 @Override
1579 public void run() {
1580 programmingScheduled.set(false);
Saurav Dase321cff2018-02-09 17:26:45 -08001581 log.info("Reacting to config changes after buffer delay");
Saurav Das261c3002017-06-13 15:35:54 -07001582 for (Device device : deviceService.getDevices()) {
1583 processDeviceAdded(device);
1584 }
1585 defaultRoutingHandler.startPopulationProcess();
1586 }
1587 }
Charles Chan72f556a2015-10-05 17:50:33 -07001588 }
Charles Chanf4586112015-11-09 16:37:23 -08001589
Charles Chan3f4aca62018-03-21 16:57:47 -07001590 private class InternalLinkListener implements LinkListener {
1591 @Override
1592 public void event(LinkEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001593 if (mainEventExecutor == null) {
1594 return;
1595 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001596 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1597 event.type() == LinkEvent.Type.LINK_UPDATED ||
1598 event.type() == LinkEvent.Type.LINK_REMOVED) {
1599 log.trace("Schedule Link event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001600 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1601 mainEventExecutor.execute(new InternalEventHandler(event));
1602 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001603 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001604 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001605 }
1606 }
1607 }
1608
1609 private class InternalDeviceListener implements DeviceListener {
1610 @Override
1611 public void event(DeviceEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001612 if (mainEventExecutor == null) {
1613 return;
1614 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001615 switch (event.type()) {
1616 case DEVICE_ADDED:
1617 case PORT_UPDATED:
1618 case PORT_ADDED:
1619 case DEVICE_UPDATED:
1620 case DEVICE_AVAILABILITY_CHANGED:
1621 log.trace("Schedule Device event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001622 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1623 mainEventExecutor.execute(new InternalEventHandler(event));
1624 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001625 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001626 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001627 break;
1628 default:
1629 }
1630 }
1631 }
1632
1633 private class InternalTopologyListener implements TopologyListener {
1634 @Override
1635 public void event(TopologyEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001636 if (mainEventExecutor == null) {
1637 return;
1638 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001639 switch (event.type()) {
1640 case TOPOLOGY_CHANGED:
1641 log.trace("Schedule Topology event {}", event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001642 if (networkConfigCompletion == null || networkConfigCompletion.isDone()) {
1643 mainEventExecutor.execute(new InternalEventHandler(event));
1644 } else {
Charles Chan39b75522018-04-21 00:44:29 -07001645 queuedEvents.add(event);
Andrea Campanella060cad82018-04-17 12:09:34 +02001646 }
Charles Chan3f4aca62018-03-21 16:57:47 -07001647 break;
1648 default:
1649 }
1650 }
1651 }
1652
Charles Chanf4586112015-11-09 16:37:23 -08001653 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001654 @Override
1655 public void event(HostEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001656 if (hostEventExecutor == null) {
1657 return;
1658 }
Charles Chanf4586112015-11-09 16:37:23 -08001659 switch (event.type()) {
1660 case HOST_ADDED:
Charles Chanf4586112015-11-09 16:37:23 -08001661 case HOST_MOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001662 case HOST_REMOVED:
Charles Chanf4586112015-11-09 16:37:23 -08001663 case HOST_UPDATED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001664 log.trace("Schedule Host event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001665 hostEventExecutor.execute(new InternalEventHandler(event));
Charles Chanf4586112015-11-09 16:37:23 -08001666 break;
1667 default:
1668 log.warn("Unsupported host event type: {}", event.type());
1669 break;
1670 }
1671 }
1672 }
1673
Charles Chanc91c8782016-03-30 17:54:24 -07001674 private class InternalMcastListener implements McastListener {
1675 @Override
1676 public void event(McastEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001677 if (mcastEventExecutor == null) {
1678 return;
1679 }
Charles Chanc91c8782016-03-30 17:54:24 -07001680 switch (event.type()) {
Pier1f87aca2018-03-14 16:47:32 -07001681 case SOURCES_ADDED:
1682 case SOURCES_REMOVED:
1683 case SINKS_ADDED:
1684 case SINKS_REMOVED:
Charles Chanc91c8782016-03-30 17:54:24 -07001685 case ROUTE_REMOVED:
Pierdb27b8d2018-04-17 16:29:56 +02001686 case ROUTE_ADDED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001687 log.trace("Schedule Mcast event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001688 mcastEventExecutor.execute(new InternalEventHandler(event));
Pier Luigi6786b922018-02-02 16:19:11 +01001689 break;
Charles Chanc91c8782016-03-30 17:54:24 -07001690 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07001691 log.warn("Unsupported mcast event type: {}", event.type());
Charles Chanc91c8782016-03-30 17:54:24 -07001692 break;
1693 }
1694 }
1695 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001696
Charles Chandebfea32016-10-24 14:52:01 -07001697 private class InternalRouteEventListener implements RouteListener {
1698 @Override
1699 public void event(RouteEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001700 if (routeEventExecutor == null) {
1701 return;
1702 }
Charles Chandebfea32016-10-24 14:52:01 -07001703 switch (event.type()) {
1704 case ROUTE_ADDED:
Charles Chandebfea32016-10-24 14:52:01 -07001705 case ROUTE_UPDATED:
Charles Chandebfea32016-10-24 14:52:01 -07001706 case ROUTE_REMOVED:
Charles Chan910be6a2017-08-23 14:46:43 -07001707 case ALTERNATIVE_ROUTES_CHANGED:
Charles Chan3f4aca62018-03-21 16:57:47 -07001708 log.trace("Schedule Route event {}", event);
Charles Chandbbbb8a2018-04-07 11:35:08 -07001709 routeEventExecutor.execute(new InternalEventHandler(event));
Charles Chan910be6a2017-08-23 14:46:43 -07001710 break;
Charles Chandebfea32016-10-24 14:52:01 -07001711 default:
Charles Chan3f4aca62018-03-21 16:57:47 -07001712 log.warn("Unsupported route event type: {}", event.type());
Charles Chandebfea32016-10-24 14:52:01 -07001713 break;
1714 }
1715 }
1716 }
Saurav Das62ae6792017-05-15 15:34:25 -07001717
Charles Chanfbcb8812018-04-18 18:41:05 -07001718 private class InternalMastershipListener implements MastershipListener {
1719 @Override
1720 public void event(MastershipEvent event) {
Ray Milkeyaee4d3e2018-05-11 09:59:19 -07001721 if (mainEventExecutor == null) {
1722 return;
1723 }
Charles Chanfbcb8812018-04-18 18:41:05 -07001724 switch (event.type()) {
Saurav Das00e553b2018-04-21 17:19:48 -07001725 case MASTER_CHANGED:
1726 log.debug("Mastership event: {}/{}", event.subject(),
1727 event.roleInfo());
1728 mainEventExecutor.execute(new InternalEventHandler(event));
1729 break;
1730 case BACKUPS_CHANGED:
1731 case SUSPENDED:
1732 default:
1733 log.debug("Mastership event type {} not handled", event.type());
1734 break;
Charles Chanfbcb8812018-04-18 18:41:05 -07001735 }
1736 }
1737 }
1738
Saurav Das00e553b2018-04-21 17:19:48 -07001739 class InternalClusterListener implements ClusterEventListener {
1740 private Instant lastClusterEvent = Instant.EPOCH;
1741
1742 long timeSinceLastClusterEvent() {
1743 return Instant.now().toEpochMilli() - lastClusterEvent.toEpochMilli();
1744 }
1745
1746 @Override
1747 public void event(ClusterEvent event) {
1748 switch (event.type()) {
1749 case INSTANCE_ACTIVATED:
1750 case INSTANCE_ADDED:
1751 case INSTANCE_READY:
1752 log.debug("Cluster event {} ignored", event.type());
1753 break;
1754 case INSTANCE_DEACTIVATED:
1755 case INSTANCE_REMOVED:
1756 log.info("** Cluster event {}", event.type());
1757 lastClusterEvent = Instant.now();
1758 break;
1759 default:
1760 break;
1761 }
1762
1763 }
1764
1765 }
1766
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001767 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1768 try {
1769 Set<Interface> intfs = conf.getInterfaces();
1770 Set<Interface> prevIntfs = prevConf.getInterfaces();
1771
1772 // Now we only handle one interface config at each port.
1773 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1774 log.warn("Interface update aborted - one at a time is allowed, " +
1775 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1776 return;
1777 }
1778
Andrea Campanella060cad82018-04-17 12:09:34 +02001779 //The system is in an incoherent state, abort
1780 if (defaultRoutingHandler == null) {
1781 log.warn("Interface update aborted, defaultRoutingHandler is null");
1782 return;
1783 }
1784
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001785 Interface intf = intfs.stream().findFirst().get();
1786 Interface prevIntf = prevIntfs.stream().findFirst().get();
1787
1788 DeviceId deviceId = intf.connectPoint().deviceId();
1789 PortNumber portNum = intf.connectPoint().port();
1790
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001791 removeSubnetConfig(prevIntf.connectPoint(),
1792 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1793 new HashSet<>(intf.ipAddressesList())));
1794
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001795 if (!prevIntf.vlanNative().equals(VlanId.NONE)
1796 && !prevIntf.vlanNative().equals(intf.vlanUntagged())
1797 && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1798 if (intf.vlanTagged().contains(prevIntf.vlanNative())) {
1799 // Update filtering objective and L2IG group bucket
1800 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanNative(), false);
1801 } else {
1802 // RemoveVlanNative
1803 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1804 }
1805 }
1806
1807 if (!prevIntf.vlanUntagged().equals(VlanId.NONE)
1808 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1809 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1810 if (intf.vlanTagged().contains(prevIntf.vlanUntagged())) {
1811 // Update filtering objective and L2IG group bucket
1812 updatePortVlanTreatment(deviceId, portNum, prevIntf.vlanUntagged(), false);
1813 } else {
1814 // RemoveVlanUntagged
1815 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1816 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001817 }
1818
1819 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1820 // RemoveVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001821 Sets.difference(prevIntf.vlanTagged(), intf.vlanTagged()).stream()
1822 .filter(i -> !intf.vlanUntagged().equals(i))
1823 .filter(i -> !intf.vlanNative().equals(i))
1824 .forEach(vlanId -> updateVlanConfigInternal(
1825 deviceId, portNum, vlanId, false, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001826 }
1827
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001828 if (!intf.vlanNative().equals(VlanId.NONE)
1829 && !prevIntf.vlanNative().equals(intf.vlanNative())
1830 && !prevIntf.vlanUntagged().equals(intf.vlanNative())) {
1831 if (prevIntf.vlanTagged().contains(intf.vlanNative())) {
1832 // Update filtering objective and L2IG group bucket
1833 updatePortVlanTreatment(deviceId, portNum, intf.vlanNative(), true);
1834 } else {
1835 // AddVlanNative
1836 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1837 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001838 }
1839
1840 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1841 // AddVlanTagged
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001842 Sets.difference(intf.vlanTagged(), prevIntf.vlanTagged()).stream()
1843 .filter(i -> !prevIntf.vlanUntagged().equals(i))
1844 .filter(i -> !prevIntf.vlanNative().equals(i))
1845 .forEach(vlanId -> updateVlanConfigInternal(
1846 deviceId, portNum, vlanId, false, true)
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001847 );
1848 }
1849
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001850 if (!intf.vlanUntagged().equals(VlanId.NONE)
1851 && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())
1852 && !prevIntf.vlanNative().equals(intf.vlanUntagged())) {
1853 if (prevIntf.vlanTagged().contains(intf.vlanUntagged())) {
1854 // Update filtering objective and L2IG group bucket
1855 updatePortVlanTreatment(deviceId, portNum, intf.vlanUntagged(), true);
1856 } else {
1857 // AddVlanUntagged
1858 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1859 }
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001860 }
1861 addSubnetConfig(prevIntf.connectPoint(),
1862 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1863 new HashSet<>(prevIntf.ipAddressesList())));
1864 } catch (ConfigException e) {
1865 log.error("Error in configuration");
1866 }
1867 }
1868
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001869 private void updatePortVlanTreatment(DeviceId deviceId, PortNumber portNum,
1870 VlanId vlanId, boolean pushVlan) {
1871 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1872 if (grpHandler == null) {
1873 log.warn("Failed to retrieve group handler for device {}", deviceId);
1874 return;
1875 }
1876
1877 // Update filtering objective for a single port
1878 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, !pushVlan, vlanId, false);
1879 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, true);
1880
1881 if (getVlanNextObjectiveId(deviceId, vlanId) != -1) {
1882 // Update L2IG bucket of the port
1883 grpHandler.updateL2InterfaceGroupBucket(portNum, vlanId, pushVlan);
1884 } else {
1885 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1886 }
1887 }
1888
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001889 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1890 VlanId vlanId, boolean pushVlan, boolean install) {
1891 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1892 if (grpHandler == null) {
1893 log.warn("Failed to retrieve group handler for device {}", deviceId);
1894 return;
1895 }
1896
1897 // Update filtering objective for a single port
1898 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1899
1900 // Update filtering objective for multicast ingress port
1901 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1902
1903 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1904
1905 if (nextId != -1 && !install) {
1906 // Update next objective for a single port as an output port
1907 // Remove a single port from L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001908 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001909 // Remove L2 Bridging rule and L3 Unicast rule to the host
Charles Chan074fae62018-04-29 18:11:37 -07001910 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
1911 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001912 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1913 // only if there is no port configured on that VLAN ID
1914 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1915 // Remove broadcast forwarding rule for the VLAN
1916 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1917 // Remove L2FG for VLAN
1918 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1919 } else {
1920 // Remove L2IG of the port
1921 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1922 }
1923 } else if (install) {
1924 if (nextId != -1) {
1925 // Add a single port to L2FG
Jonghwan Hyuna4ce0aa2018-02-12 16:43:45 +09001926 grpHandler.updateGroupFromVlanConfiguration(vlanId, portNum, nextId, install);
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001927 } else {
1928 // Create L2FG for VLAN
1929 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1930 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1931 }
Charles Chan074fae62018-04-29 18:11:37 -07001932 hostEventExecutor.execute(() -> hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum,
1933 vlanId, pushVlan, install));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001934 } else {
1935 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1936 }
1937 }
1938
1939 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1940 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1941 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1942
1943 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1944 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1945 .filter(intf -> !intf.connectPoint().equals(cp))
1946 .flatMap(intf -> intf.ipAddressesList().stream())
1947 .collect(Collectors.toSet());
1948 // 1. Partial subnet population
1949 // Remove routing rules for removed subnet from previous configuration,
1950 // which does not also exist in other interfaces in the same device
1951 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1952 .map(InterfaceIpAddress::subnetAddress)
1953 .collect(Collectors.toSet());
1954
1955 defaultRoutingHandler.revokeSubnet(
1956 ipPrefixSet.stream()
1957 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1958 .collect(Collectors.toSet()));
1959
1960 // 2. Interface IP punts
1961 // Remove IP punts for old Intf address
1962 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1963 .map(InterfaceIpAddress::ipAddress)
1964 .collect(Collectors.toSet());
1965 ipAddressSet.stream()
1966 .map(InterfaceIpAddress::ipAddress)
1967 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1968 .forEach(interfaceIpAddress ->
1969 routingRulePopulator.revokeSingleIpPunts(
1970 cp.deviceId(), interfaceIpAddress));
1971
1972 // 3. Host unicast routing rule
1973 // Remove unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07001974 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001975 }
1976
1977 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1978 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1979 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1980
1981 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1982 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1983 .filter(intf -> !intf.connectPoint().equals(cp))
1984 .flatMap(intf -> intf.ipAddressesList().stream())
1985 .collect(Collectors.toSet());
1986 // 1. Partial subnet population
1987 // Add routing rules for newly added subnet, which does not also exist in
1988 // other interfaces in the same device
1989 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1990 .map(InterfaceIpAddress::subnetAddress)
1991 .collect(Collectors.toSet());
1992
1993 defaultRoutingHandler.populateSubnet(
1994 Collections.singleton(cp),
1995 ipPrefixSet.stream()
1996 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1997 .collect(Collectors.toSet()));
1998
1999 // 2. Interface IP punts
2000 // Add IP punts for new Intf address
2001 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
2002 .map(InterfaceIpAddress::ipAddress)
2003 .collect(Collectors.toSet());
2004 ipAddressSet.stream()
2005 .map(InterfaceIpAddress::ipAddress)
2006 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
2007 .forEach(interfaceIpAddress ->
2008 routingRulePopulator.populateSingleIpPunts(
2009 cp.deviceId(), interfaceIpAddress));
2010
2011 // 3. Host unicast routing rule
2012 // Add unicast routing rule
Charles Chan074fae62018-04-29 18:11:37 -07002013 hostEventExecutor.execute(() -> hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true));
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07002014 }
sangho80f11cb2015-04-01 13:05:26 -07002015}