blob: 525f84a09b2c0e1de2e0d4e47b2af5e3c5e1040e [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sanghob35a6192015-04-01 13:05:26 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.segmentrouting;
17
Andreas Pantelopoulos20474e02017-12-20 18:04:27 -080018import com.fasterxml.jackson.databind.ObjectMapper;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -070019import com.fasterxml.jackson.databind.node.ObjectNode;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -080020import com.google.common.collect.HashMultimap;
21import com.google.common.collect.ImmutableMap;
22import com.google.common.collect.Maps;
23import com.google.common.collect.Multimap;
Charles Chan9640c812017-08-23 13:55:39 -070024import com.google.common.collect.Sets;
sanghob35a6192015-04-01 13:05:26 -070025import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
Charles Chan47933752017-11-30 15:37:50 -080028import org.apache.felix.scr.annotations.Modified;
29import org.apache.felix.scr.annotations.Property;
sanghob35a6192015-04-01 13:05:26 -070030import org.apache.felix.scr.annotations.Reference;
31import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho1e575652015-05-14 00:39:53 -070032import org.apache.felix.scr.annotations.Service;
sanghob35a6192015-04-01 13:05:26 -070033import org.onlab.packet.Ethernet;
Pier Ventre735b8c82016-12-02 08:16:05 -080034import org.onlab.packet.ICMP6;
Charles Chanc42e84e2015-10-20 16:24:19 -070035import org.onlab.packet.IPv4;
Pier Ventre10bd8d12016-11-26 21:05:22 -080036import org.onlab.packet.IPv6;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070037import org.onlab.packet.IpAddress;
Charles Chanc42e84e2015-10-20 16:24:19 -070038import org.onlab.packet.IpPrefix;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070039import org.onlab.packet.VlanId;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070040import org.onlab.util.KryoNamespace;
Charles Chan47933752017-11-30 15:37:50 -080041import org.onlab.util.Tools;
Saurav Das80980c72016-03-23 11:22:49 -070042import org.onosproject.cfg.ComponentConfigService;
Pier Luigieba73a02018-01-16 10:47:50 +010043import org.onosproject.cluster.ClusterService;
44import org.onosproject.cluster.LeadershipService;
sanghob35a6192015-04-01 13:05:26 -070045import org.onosproject.core.ApplicationId;
46import org.onosproject.core.CoreService;
47import org.onosproject.event.Event;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070048import org.onosproject.mastership.MastershipService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080049import org.onosproject.net.ConnectPoint;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070050import org.onosproject.net.Device;
51import org.onosproject.net.DeviceId;
Charles Chan9640c812017-08-23 13:55:39 -070052import org.onosproject.net.Host;
53import org.onosproject.net.HostId;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070054import org.onosproject.net.Link;
55import org.onosproject.net.Port;
Charles Chan68aa62d2015-11-09 16:37:23 -080056import org.onosproject.net.PortNumber;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070057import org.onosproject.net.config.ConfigException;
Charles Chand6832882015-10-05 17:50:33 -070058import org.onosproject.net.config.ConfigFactory;
59import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070060import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070061import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070062import org.onosproject.net.config.basics.InterfaceConfig;
63import org.onosproject.net.config.basics.McastConfig;
Charles Chand6832882015-10-05 17:50:33 -070064import org.onosproject.net.config.basics.SubjectFactories;
Saurav Das45f48152018-01-18 12:07:33 -080065import org.onosproject.net.device.DeviceAdminService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070066import org.onosproject.net.device.DeviceEvent;
67import org.onosproject.net.device.DeviceListener;
68import org.onosproject.net.device.DeviceService;
Charles Chan68aa62d2015-11-09 16:37:23 -080069import org.onosproject.net.flow.TrafficSelector;
70import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070071import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan68aa62d2015-11-09 16:37:23 -080072import org.onosproject.net.host.HostEvent;
73import org.onosproject.net.host.HostListener;
Charles Chan47933752017-11-30 15:37:50 -080074import org.onosproject.net.host.HostLocationProbingService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080075import org.onosproject.net.host.HostService;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070076import org.onosproject.net.host.InterfaceIpAddress;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070077import org.onosproject.net.intf.Interface;
78import org.onosproject.net.intf.InterfaceService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080079import org.onosproject.net.link.LinkEvent;
80import org.onosproject.net.link.LinkListener;
81import org.onosproject.net.link.LinkService;
Charles Chand55e84d2016-03-30 17:54:24 -070082import org.onosproject.net.mcast.McastEvent;
83import org.onosproject.net.mcast.McastListener;
84import org.onosproject.net.mcast.MulticastRouteService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070085import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080086import org.onosproject.net.packet.InboundPacket;
87import org.onosproject.net.packet.PacketContext;
88import org.onosproject.net.packet.PacketProcessor;
89import org.onosproject.net.packet.PacketService;
Charles Chand55e84d2016-03-30 17:54:24 -070090import org.onosproject.net.topology.TopologyService;
Charles Chan9640c812017-08-23 13:55:39 -070091import org.onosproject.routeservice.ResolvedRoute;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070092import org.onosproject.routeservice.RouteEvent;
93import org.onosproject.routeservice.RouteListener;
94import org.onosproject.routeservice.RouteService;
Charles Chand55e84d2016-03-30 17:54:24 -070095import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
96import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventref34966c2016-11-07 16:21:04 -080097import org.onosproject.segmentrouting.config.PwaasConfig;
Pier Ventref34966c2016-11-07 16:21:04 -080098import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070099import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -0700100import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -0700101import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700102import org.onosproject.segmentrouting.grouphandler.DestinationSet;
103import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700104import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
105import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700106import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700107import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Pier Luigib29144d2018-01-15 18:06:43 +0100108import org.onosproject.segmentrouting.storekey.McastStoreKey;
Charles Chand2990362016-04-18 13:44:03 -0700109import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -0800110import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -0700111import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700112import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700113import org.onosproject.store.service.EventuallyConsistentMap;
114import org.onosproject.store.service.EventuallyConsistentMapBuilder;
115import org.onosproject.store.service.StorageService;
116import org.onosproject.store.service.WallClockTimestamp;
Charles Chan47933752017-11-30 15:37:50 -0800117import org.osgi.service.component.ComponentContext;
sanghob35a6192015-04-01 13:05:26 -0700118import org.slf4j.Logger;
119import org.slf4j.LoggerFactory;
120
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800121import java.util.Collections;
Charles Chan47933752017-11-30 15:37:50 -0800122import java.util.Dictionary;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800123import java.util.HashSet;
124import java.util.List;
125import java.util.Map;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800126import java.util.Optional;
127import java.util.Set;
128import java.util.concurrent.ConcurrentHashMap;
129import java.util.concurrent.ConcurrentLinkedQueue;
130import java.util.concurrent.Executors;
131import java.util.concurrent.ScheduledExecutorService;
132import java.util.concurrent.ScheduledFuture;
133import java.util.concurrent.TimeUnit;
134import java.util.concurrent.atomic.AtomicBoolean;
135import java.util.stream.Collectors;
sanghob35a6192015-04-01 13:05:26 -0700136
Charles Chan3e783d02016-02-26 22:19:52 -0800137import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800138import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700139import static org.onlab.util.Tools.groupedThreads;
Saurav Dase7f51012018-02-09 17:26:45 -0800140import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
141import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Charles Chan3e783d02016-02-26 22:19:52 -0800142
Charles Chane849c192016-01-11 18:28:54 -0800143/**
144 * Segment routing manager.
145 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700146@Service
147@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700148public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700149
Charles Chan2c15aca2016-11-09 20:51:44 -0800150 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan2fde6d42017-08-23 14:46:43 -0700151 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sanghob35a6192015-04-01 13:05:26 -0700152
153 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700154 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700155
156 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre10bd8d12016-11-26 21:05:22 -0800157 private NeighbourResolutionService neighbourResolutionService;
158
159 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700160 CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700161
162 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700163 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700164
165 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700166 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700167
168 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan47933752017-11-30 15:37:50 -0800169 HostLocationProbingService probingService;
170
171 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700172 DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700173
174 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Saurav Das45f48152018-01-18 12:07:33 -0800175 DeviceAdminService deviceAdminService;
176
177 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800178 public FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700179
180 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700181 LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700182
Charles Chan5270ed02016-01-30 23:22:37 -0800183 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800184 public MastershipService mastershipService;
Charles Chan03a73e02016-10-24 14:52:01 -0700185
186 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800187 public StorageService storageService;
Charles Chan03a73e02016-10-24 14:52:01 -0700188
189 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
190 MulticastRouteService multicastRouteService;
191
192 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chanf237e1b2018-01-09 13:45:07 -0800193 public TopologyService topologyService;
Charles Chan03a73e02016-10-24 14:52:01 -0700194
195 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700196 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800197
198 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800199 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800200
Saurav Das80980c72016-03-23 11:22:49 -0700201 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800202 public InterfaceService interfaceService;
203
Pier Luigieba73a02018-01-16 10:47:50 +0100204 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
205 public ClusterService clusterService;
206
207 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
208 public LeadershipService leadershipService;
209
Charles Chan47933752017-11-30 15:37:50 -0800210 @Property(name = "activeProbing", boolValue = true,
211 label = "Enable active probing to discover dual-homed hosts.")
212 boolean activeProbing = true;
213
Charles Chan03a73e02016-10-24 14:52:01 -0700214 ArpHandler arpHandler = null;
215 IcmpHandler icmpHandler = null;
216 IpHandler ipHandler = null;
217 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700218 ApplicationId appId;
219 DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700220
Charles Chan03a73e02016-10-24 14:52:01 -0700221 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700222 private TunnelHandler tunnelHandler = null;
223 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700224 private InternalPacketProcessor processor = null;
225 private InternalLinkListener linkListener = null;
226 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700227 private AppConfigHandler appCfgHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700228 XConnectHandler xConnectHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800229 McastHandler mcastHandler = null;
230 HostHandler hostHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800231 private RouteHandler routeHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800232 LinkHandler linkHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800233 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Pier Ventref34966c2016-11-07 16:21:04 -0800234 private L2TunnelHandler l2TunnelHandler = null;
sanghob35a6192015-04-01 13:05:26 -0700235 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan5270ed02016-01-30 23:22:37 -0800236 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700237 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
238 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700239 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sanghob35a6192015-04-01 13:05:26 -0700240
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700241 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700242 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sanghob35a6192015-04-01 13:05:26 -0700243
Saurav Das4ce45962015-11-24 23:21:05 -0800244 @SuppressWarnings("unused")
sanghob35a6192015-04-01 13:05:26 -0700245 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800246 @SuppressWarnings("rawtypes")
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700247 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Saurav Das45f48152018-01-18 12:07:33 -0800248 Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chane849c192016-01-11 18:28:54 -0800249 new ConcurrentHashMap<>();
250 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700251 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700252 * Used to keep track on MPLS group information.
Charles Chane849c192016-01-11 18:28:54 -0800253 */
Charles Chan47933752017-11-30 15:37:50 -0800254 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das7bcbe702017-06-13 15:35:54 -0700255 dsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800256 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700257 * Per device next objective ID store with (device id + vlanid) as key.
258 * Used to keep track on L2 flood group information.
Charles Chane849c192016-01-11 18:28:54 -0800259 */
Charles Chan47933752017-11-30 15:37:50 -0800260 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan59cc16d2017-02-02 16:20:42 -0800261 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800262 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700263 * Per device next objective ID store with (device id + port + treatment + meta) as key.
264 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chane849c192016-01-11 18:28:54 -0800265 */
Charles Chan47933752017-11-30 15:37:50 -0800266 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800267 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800268
Saurav Das4ce45962015-11-24 23:21:05 -0800269 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
270 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700271
Saurav Das7bcbe702017-06-13 15:35:54 -0700272 private AtomicBoolean programmingScheduled = new AtomicBoolean();
273
Charles Chand55e84d2016-03-30 17:54:24 -0700274 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700275 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
276 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700277 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700278 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800279 public SegmentRoutingDeviceConfig createConfig() {
280 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700281 }
282 };
Pier Ventref34966c2016-11-07 16:21:04 -0800283
Charles Chand55e84d2016-03-30 17:54:24 -0700284 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700285 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
286 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700287 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800288 @Override
289 public SegmentRoutingAppConfig createConfig() {
290 return new SegmentRoutingAppConfig();
291 }
292 };
Pier Ventref34966c2016-11-07 16:21:04 -0800293
Charles Chanfc5c7802016-05-17 13:13:55 -0700294 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
295 new ConfigFactory<ApplicationId, XConnectConfig>(
296 SubjectFactories.APP_SUBJECT_FACTORY,
297 XConnectConfig.class, "xconnect") {
298 @Override
299 public XConnectConfig createConfig() {
300 return new XConnectConfig();
301 }
302 };
Pier Ventref34966c2016-11-07 16:21:04 -0800303
Charles Chand55e84d2016-03-30 17:54:24 -0700304 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700305 new ConfigFactory<ApplicationId, McastConfig>(
306 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700307 McastConfig.class, "multicast") {
308 @Override
309 public McastConfig createConfig() {
310 return new McastConfig();
311 }
312 };
313
Pier Ventref34966c2016-11-07 16:21:04 -0800314 private final ConfigFactory<ApplicationId, PwaasConfig> pwaasConfigFactory =
315 new ConfigFactory<ApplicationId, PwaasConfig>(
316 SubjectFactories.APP_SUBJECT_FACTORY,
317 PwaasConfig.class, "pwaas") {
318 @Override
319 public PwaasConfig createConfig() {
320 return new PwaasConfig();
321 }
322 };
323
Charles Chan65238242017-06-22 18:03:14 -0700324 private static final Object THREAD_SCHED_LOCK = new Object();
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700325 private static int numOfEventsQueued = 0;
326 private static int numOfEventsExecuted = 0;
sanghob35a6192015-04-01 13:05:26 -0700327 private static int numOfHandlerExecution = 0;
328 private static int numOfHandlerScheduled = 0;
329
Charles Chan116188d2016-02-18 14:22:42 -0800330 /**
331 * Segment Routing App ID.
332 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800333 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan59cc16d2017-02-02 16:20:42 -0800334
Charles Chane849c192016-01-11 18:28:54 -0800335 /**
336 * The default VLAN ID assigned to the interfaces without subnet config.
337 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800338 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700339
sanghob35a6192015-04-01 13:05:26 -0700340 @Activate
Charles Chan47933752017-11-30 15:37:50 -0800341 protected void activate(ComponentContext context) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800342 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700343
344 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das7bcbe702017-06-13 15:35:54 -0700345 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700346 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700347 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700348 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700349 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700350 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700351 .build();
Saurav Das7bcbe702017-06-13 15:35:54 -0700352 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700353
Charles Chan59cc16d2017-02-02 16:20:42 -0800354 log.debug("Creating EC map vlannextobjectivestore");
355 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
356 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
357 vlanNextObjStore = vlanNextObjMapBuilder
358 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700359 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700360 .withTimestampProvider((k, v) -> new WallClockTimestamp())
361 .build();
362
Saurav Das4ce45962015-11-24 23:21:05 -0800363 log.debug("Creating EC map subnetnextobjectivestore");
364 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
365 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
366 portNextObjStore = portNextObjMapBuilder
367 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700368 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800369 .withTimestampProvider((k, v) -> new WallClockTimestamp())
370 .build();
371
sangho0b2b6d12015-05-20 22:16:38 -0700372 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
373 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700374 tunnelStore = tunnelMapBuilder
375 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700376 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700377 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700378 .build();
379
380 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
381 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700382 policyStore = policyMapBuilder
383 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700384 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700385 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700386 .build();
387
Saurav Das80980c72016-03-23 11:22:49 -0700388 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800389 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700390 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800391 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800392 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
393 "requestInterceptsEnabled", "false");
Charles Chand3baaba2017-08-08 15:13:37 -0700394 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800395 "requestInterceptsEnabled", "false");
Charles Chanc760f382017-07-24 15:56:10 -0700396 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800397 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800398 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
399 "greedyLearningIpv6", "true");
Charles Chanc6d227e2017-02-28 15:15:17 -0800400 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
401 "forceUnprovision", "true");
Charles Chanef624ed2017-08-10 16:57:28 -0700402 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan73316522017-07-20 16:16:25 -0700403 "distributed", "true");
Charles Chan35a32322017-08-14 11:42:11 -0700404 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
405 "multihomingEnabled", "true");
Charles Chan807d87a2017-11-29 19:54:20 -0800406 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
407 "staleLinkAge", "15000");
408 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
409 "allowDuplicateIps", "false");
Charles Chan47933752017-11-30 15:37:50 -0800410 compCfgService.registerProperties(getClass());
411 modified(context);
Saurav Das80980c72016-03-23 11:22:49 -0700412
Charles Chanb8e10c82015-10-14 11:24:40 -0700413 processor = new InternalPacketProcessor();
414 linkListener = new InternalLinkListener();
415 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700416 appCfgHandler = new AppConfigHandler(this);
417 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700418 mcastHandler = new McastHandler(this);
419 hostHandler = new HostHandler(this);
Saurav Das45f48152018-01-18 12:07:33 -0800420 linkHandler = new LinkHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700421 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800422 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Pier Ventref34966c2016-11-07 16:21:04 -0800423 l2TunnelHandler = new L2TunnelHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700424
Charles Chan3e783d02016-02-26 22:19:52 -0800425 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700426 cfgService.registerConfigFactory(deviceConfigFactory);
427 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700428 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700429 cfgService.registerConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800430 cfgService.registerConfigFactory(pwaasConfigFactory);
Saurav Dase7f51012018-02-09 17:26:45 -0800431 log.info("Configuring network before adding listeners");
Charles Chan132393a2018-01-04 14:26:07 -0800432 cfgListener.configureNetwork();
433
Charles Chan5270ed02016-01-30 23:22:37 -0800434 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700435 packetService.addProcessor(processor, PacketProcessor.director(2));
436 linkService.addListener(linkListener);
437 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700438 multicastRouteService.addListener(mcastListener);
Charles Chanc7a8a682017-06-19 00:43:31 -0700439 routeService.addListener(routeListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700440
Andreas Pantelopoulos4a768c02018-01-11 07:53:48 -0800441 l2TunnelHandler.init();
442
sanghob35a6192015-04-01 13:05:26 -0700443 log.info("Started");
444 }
445
Saurav Das45f48152018-01-18 12:07:33 -0800446 KryoNamespace.Builder createSerializer() {
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700447 return new KryoNamespace.Builder()
448 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700449 .register(DestinationSetNextObjectiveStoreKey.class,
Charles Chan59cc16d2017-02-02 16:20:42 -0800450 VlanNextObjectiveStoreKey.class,
Saurav Das7bcbe702017-06-13 15:35:54 -0700451 DestinationSet.class,
452 NextNeighbors.class,
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700453 Tunnel.class,
454 DefaultTunnel.class,
455 Policy.class,
456 TunnelPolicy.class,
457 Policy.Type.class,
458 PortNextObjectiveStoreKey.class,
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700459 XConnectStoreKey.class,
460 DefaultL2Tunnel.class,
461 DefaultL2TunnelPolicy.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700462 );
463 }
464
sanghob35a6192015-04-01 13:05:26 -0700465 @Deactivate
466 protected void deactivate() {
Charles Chand6832882015-10-05 17:50:33 -0700467 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700468 cfgService.unregisterConfigFactory(deviceConfigFactory);
469 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700470 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700471 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800472 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chan47933752017-11-30 15:37:50 -0800473 compCfgService.unregisterProperties(getClass(), false);
Charles Chand6832882015-10-05 17:50:33 -0700474
Charles Chanc7a8a682017-06-19 00:43:31 -0700475 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700476 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700477 linkService.removeListener(linkListener);
478 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700479 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700480 routeService.removeListener(routeListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700481
Charles Chan0aa674e2017-02-23 15:44:08 -0800482 neighbourResolutionService.unregisterNeighbourHandlers(appId);
483
sanghob35a6192015-04-01 13:05:26 -0700484 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700485 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700486 deviceListener = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700487 groupHandlerMap.clear();
488
Saurav Das7bcbe702017-06-13 15:35:54 -0700489 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800490 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700491 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700492 tunnelStore.destroy();
493 policyStore.destroy();
Pier Luigib72201b2018-01-25 16:16:02 +0100494
495 mcastHandler.terminate();
sanghob35a6192015-04-01 13:05:26 -0700496 log.info("Stopped");
497 }
498
Charles Chan47933752017-11-30 15:37:50 -0800499 @Modified
500 private void modified(ComponentContext context) {
501 Dictionary<?, ?> properties = context.getProperties();
502 if (properties == null) {
503 return;
504 }
505
506 String strActiveProving = Tools.get(properties, "activeProbing");
507 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
508
509 if (expectActiveProbing != activeProbing) {
510 activeProbing = expectActiveProbing;
511 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
512 }
513 }
514
sangho1e575652015-05-14 00:39:53 -0700515 @Override
516 public List<Tunnel> getTunnels() {
517 return tunnelHandler.getTunnels();
518 }
519
520 @Override
sangho71abe1b2015-06-29 14:58:47 -0700521 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
522 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700523 }
524
525 @Override
sangho71abe1b2015-06-29 14:58:47 -0700526 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700527 for (Policy policy: policyHandler.getPolicies()) {
528 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
529 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
530 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
531 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700532 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700533 }
534 }
535 }
sangho71abe1b2015-06-29 14:58:47 -0700536 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700537 }
538
539 @Override
sangho71abe1b2015-06-29 14:58:47 -0700540 public PolicyHandler.Result removePolicy(Policy policy) {
541 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700542 }
543
544 @Override
sangho71abe1b2015-06-29 14:58:47 -0700545 public PolicyHandler.Result createPolicy(Policy policy) {
546 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700547 }
548
549 @Override
550 public List<Policy> getPolicies() {
551 return policyHandler.getPolicies();
552 }
553
Saurav Das59232cf2016-04-27 18:35:50 -0700554 @Override
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700555 public List<DefaultL2Tunnel> getL2Tunnels() {
556 return l2TunnelHandler.getL2Tunnels();
557 }
558
559 @Override
560 public List<DefaultL2TunnelPolicy> getL2Policies() {
561 return l2TunnelHandler.getL2Policies();
562 }
563
564 @Override
565 public L2TunnelHandler.Result addPseudowire(String tunnelId, String pwLabel, String cP1,
566 String cP1InnerVlan, String cP1OuterVlan, String cP2,
567 String cP2InnerVlan, String cP2OuterVlan,
568 String mode, String sdTag) {
Andreas Pantelopoulos20474e02017-12-20 18:04:27 -0800569 // Try to inject an empty Pwaas config if it is not found for the first time
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700570 PwaasConfig config = cfgService.getConfig(appId(), PwaasConfig.class);
571 if (config == null) {
Andreas Pantelopoulos20474e02017-12-20 18:04:27 -0800572 log.debug("Pwaas config not found. Try to create an empty one.");
573 cfgService.applyConfig(appId(), PwaasConfig.class, new ObjectMapper().createObjectNode());
574 config = cfgService.getConfig(appId(), PwaasConfig.class);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700575 }
576
577 ObjectNode object = config.addPseudowire(tunnelId, pwLabel,
578 cP1, cP1InnerVlan, cP1OuterVlan,
579 cP2, cP2InnerVlan, cP2OuterVlan,
580 mode, sdTag);
581 if (object == null) {
582 log.warn("Could not add pseudowire to the configuration!");
583 return L2TunnelHandler.Result.ADDITION_ERROR;
584 }
585
586 // inform everyone about the valid change in the pw configuration
587 cfgService.applyConfig(appId(), PwaasConfig.class, object);
588 return L2TunnelHandler.Result.SUCCESS;
589 }
590
591 @Override
592 public L2TunnelHandler.Result removePseudowire(String pwId) {
593
594 PwaasConfig config = cfgService.getConfig(appId(), PwaasConfig.class);
595 if (config == null) {
596 log.warn("Configuration for Pwaas class could not be found!");
597 return L2TunnelHandler.Result.CONFIG_NOT_FOUND;
598 }
599
600 ObjectNode object = config.removePseudowire(pwId);
601 if (object == null) {
602 log.warn("Could not delete pseudowire from configuration!");
603 return L2TunnelHandler.Result.REMOVAL_ERROR;
604 }
605
606 // sanity check, this should never fail since we removed a pw
607 // and we always check when we update the configuration
608 config.isValid();
609
610 // inform everyone
611 cfgService.applyConfig(appId(), PwaasConfig.class, object);
612
613 return L2TunnelHandler.Result.SUCCESS;
614 }
615
616 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700617 public void rerouteNetwork() {
618 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700619 }
620
Charles Chanc81c45b2016-10-20 17:02:44 -0700621 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800622 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
623 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800624 deviceConfiguration.getRouters().forEach(device ->
625 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chanc81c45b2016-10-20 17:02:44 -0700626 return deviceSubnetMap;
627 }
628
Saurav Dasc88d4662017-05-15 15:34:25 -0700629
630 @Override
631 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
632 if (defaultRoutingHandler != null) {
633 return defaultRoutingHandler.getCurrentEmcpSpgMap();
634 } else {
635 return null;
636 }
637 }
638
639 @Override
Saurav Das7bcbe702017-06-13 15:35:54 -0700640 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
641 if (dsNextObjStore != null) {
642 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700643 } else {
644 return ImmutableMap.of();
645 }
646 }
647
Saurav Dasceccf242017-08-03 18:30:35 -0700648 @Override
649 public void verifyGroups(DeviceId id) {
650 DefaultGroupHandler gh = groupHandlerMap.get(id);
651 if (gh != null) {
652 gh.triggerBucketCorrector();
653 }
654 }
655
Saurav Dasc568c342018-01-25 09:49:01 -0800656 @Override
657 public ImmutableMap<Link, Boolean> getSeenLinks() {
658 return linkHandler.getSeenLinks();
659 }
660
661 @Override
662 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
663 return linkHandler.getDownedPorts();
664 }
665
Pier Luigib29144d2018-01-15 18:06:43 +0100666 @Override
667 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
668 return mcastHandler.getMcastNextIds(mcastIp);
669 }
670
671 @Override
672 public Map<McastStoreKey, McastHandler.McastRole> getMcastRoles(IpAddress mcastIp) {
673 return mcastHandler.getMcastRoles(mcastIp);
674 }
675
676 @Override
677 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
678 return mcastHandler.getMcastPaths(mcastIp);
679 }
680
sanghof9d0bf12015-05-19 11:57:42 -0700681 /**
Ray Milkeye4afdb52017-04-05 09:42:04 -0700682 * Extracts the application ID from the manager.
683 *
684 * @return application ID
685 */
686 public ApplicationId appId() {
687 return appId;
688 }
689
690 /**
691 * Returns the device configuration.
692 *
693 * @return device configuration
694 */
695 public DeviceConfiguration deviceConfiguration() {
696 return deviceConfiguration;
697 }
698
699 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700700 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700701 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700702 *
703 * @return next objective ID store
704 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700705 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
706 dsNextObjStore() {
707 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700708 }
709
710 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700711 * Per device next objective ID store with (device id + vlanid) as key.
712 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700713 *
714 * @return vlan next object store
715 */
716 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
717 return vlanNextObjStore;
718 }
719
720 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700721 * Per device next objective ID store with (device id + port + treatment + meta) as key.
722 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700723 *
724 * @return port next object store.
725 */
726 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
727 return portNextObjStore;
728 }
729
730 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700731 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
732 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -0700733 *
734 * @return MPLS-ECMP value
735 */
736 public boolean getMplsEcmp() {
737 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
738 .getConfig(this.appId, SegmentRoutingAppConfig.class);
739 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
740 }
741
742 /**
sanghof9d0bf12015-05-19 11:57:42 -0700743 * Returns the tunnel object with the tunnel ID.
744 *
745 * @param tunnelId Tunnel ID
746 * @return Tunnel reference
747 */
sangho1e575652015-05-14 00:39:53 -0700748 public Tunnel getTunnel(String tunnelId) {
749 return tunnelHandler.getTunnel(tunnelId);
750 }
751
Charles Chan7ffd81f2017-02-08 15:52:08 -0800752 // TODO Consider moving these to InterfaceService
sanghob35a6192015-04-01 13:05:26 -0700753 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800754 * Returns untagged VLAN configured on given connect point.
Charles Chan7ffd81f2017-02-08 15:52:08 -0800755 * <p>
756 * Only returns the first match if there are multiple untagged VLAN configured
757 * on the connect point.
sanghob35a6192015-04-01 13:05:26 -0700758 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800759 * @param connectPoint connect point
760 * @return untagged VLAN or null if not configured
sanghob35a6192015-04-01 13:05:26 -0700761 */
Charles Chan65238242017-06-22 18:03:14 -0700762 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800763 return interfaceService.getInterfacesByPort(connectPoint).stream()
764 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
765 .map(Interface::vlanUntagged)
766 .findFirst().orElse(null);
sanghob35a6192015-04-01 13:05:26 -0700767 }
768
sangho1e575652015-05-14 00:39:53 -0700769 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800770 * Returns tagged VLAN configured on given connect point.
771 * <p>
772 * Returns all matches if there are multiple tagged VLAN configured
773 * on the connect point.
774 *
775 * @param connectPoint connect point
776 * @return tagged VLAN or empty set if not configured
777 */
Charles Chan65238242017-06-22 18:03:14 -0700778 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800779 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
780 return interfaces.stream()
781 .map(Interface::vlanTagged)
Charles Chan65238242017-06-22 18:03:14 -0700782 .flatMap(Set::stream)
Charles Chan7ffd81f2017-02-08 15:52:08 -0800783 .collect(Collectors.toSet());
784 }
785
786 /**
787 * Returns native VLAN configured on given connect point.
788 * <p>
789 * Only returns the first match if there are multiple native VLAN configured
790 * on the connect point.
791 *
792 * @param connectPoint connect point
793 * @return native VLAN or null if not configured
794 */
Charles Chan65238242017-06-22 18:03:14 -0700795 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800796 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
797 return interfaces.stream()
798 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
799 .map(Interface::vlanNative)
800 .findFirst()
801 .orElse(null);
802 }
803
804 /**
Charles Chanf9a52702017-06-16 15:19:24 -0700805 * Returns internal VLAN for untagged hosts on given connect point.
806 * <p>
807 * The internal VLAN is either vlan-untagged for an access port,
808 * or vlan-native for a trunk port.
809 *
810 * @param connectPoint connect point
811 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
812 */
Charles Chan65238242017-06-22 18:03:14 -0700813 VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chanf9a52702017-06-16 15:19:24 -0700814 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
815 VlanId nativeVlanId = getNativeVlanId(connectPoint);
816 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
817 }
818
819 /**
Charles Chan65238242017-06-22 18:03:14 -0700820 * Returns optional pair device ID of given device.
821 *
822 * @param deviceId device ID
823 * @return optional pair device ID. Might be empty if pair device is not configured
824 */
825 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
826 SegmentRoutingDeviceConfig deviceConfig =
827 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
828 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
829 }
830 /**
831 * Returns optional pair device local port of given device.
832 *
833 * @param deviceId device ID
834 * @return optional pair device ID. Might be empty if pair device is not configured
835 */
836 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
837 SegmentRoutingDeviceConfig deviceConfig =
838 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
839 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
840 }
841
842 /**
Charles Chan2fde6d42017-08-23 14:46:43 -0700843 * Determine if current instance is the master of given connect point.
844 *
845 * @param cp connect point
846 * @return true if current instance is the master of given connect point
847 */
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700848 public boolean isMasterOf(ConnectPoint cp) {
Charles Chan2fde6d42017-08-23 14:46:43 -0700849 boolean isMaster = mastershipService.isLocalMaster(cp.deviceId());
850 if (!isMaster) {
851 log.debug(NOT_MASTER, cp);
852 }
853 return isMaster;
854 }
855
856 /**
Charles Chan9640c812017-08-23 13:55:39 -0700857 * Returns locations of given resolved route.
858 *
859 * @param resolvedRoute resolved route
860 * @return locations of nexthop. Might be empty if next hop is not found
861 */
862 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
863 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
864 return Optional.ofNullable(hostService.getHost(hostId))
865 .map(Host::locations).orElse(Sets.newHashSet())
866 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
867 }
868
869 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800870 * Returns vlan port map of given device.
871 *
872 * @param deviceId device id
873 * @return vlan-port multimap
874 */
875 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
876 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
877
878 interfaceService.getInterfaces().stream()
879 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
880 .forEach(intf -> {
881 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -0700882 intf.vlanTagged().forEach(vlanTagged ->
883 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
884 );
Charles Chan7ffd81f2017-02-08 15:52:08 -0800885 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
886 });
887 vlanPortMap.removeAll(VlanId.NONE);
888
889 return vlanPortMap;
890 }
891
892 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800893 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -0800894 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700895 *
896 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -0800897 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -0800898 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700899 */
Charles Chan65238242017-06-22 18:03:14 -0700900 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -0700901 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800902 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
903 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700904 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -0800905 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -0800906 + "device {} not found", deviceId);
907 return -1;
908 }
909 }
910
911 /**
912 * Returns the next objective ID for the given portNumber, given the treatment.
913 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -0700914 * would result in different objectives. If the next object does not exist,
915 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -0800916 *
917 * @param deviceId Device ID
918 * @param portNum port number on device for which NextObjective is queried
919 * @param treatment the actions to apply on the packets (should include outport)
920 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -0700921 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -0700922 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800923 */
924 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
925 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700926 TrafficSelector meta,
927 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -0800928 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
929 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -0700930 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -0800931 } else {
Charles Chane849c192016-01-11 18:28:54 -0800932 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
933 + " not found", deviceId);
934 return -1;
935 }
936 }
937
Saurav Dasc88d4662017-05-15 15:34:25 -0700938 /**
939 * Returns the group handler object for the specified device id.
940 *
941 * @param devId the device identifier
942 * @return the groupHandler object for the device id, or null if not found
943 */
Charles Chan65238242017-06-22 18:03:14 -0700944 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700945 return groupHandlerMap.get(devId);
946 }
947
948 /**
Saurav Dasceccf242017-08-03 18:30:35 -0700949 * Returns the default routing handler object.
950 *
951 * @return the default routing handler object
952 */
953 public DefaultRoutingHandler getRoutingHandler() {
954 return defaultRoutingHandler;
955 }
956
sanghob35a6192015-04-01 13:05:26 -0700957 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700958 @Override
959 public void process(PacketContext context) {
960
961 if (context.isHandled()) {
962 return;
963 }
964
965 InboundPacket pkt = context.inPacket();
966 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -0800967
968 if (ethernet == null) {
969 return;
970 }
971
Saurav Das7bcbe702017-06-13 15:35:54 -0700972 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
973 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800974 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700975 log.warn("Received unexpected ARP packet on {}",
976 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -0700977 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -0800978 return;
sanghob35a6192015-04-01 13:05:26 -0700979 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800980 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
981 //ipHandler.addToPacketBuffer(ipv4Packet);
982 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
983 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700984 } else {
Charles Chan50035632017-01-13 17:20:44 -0800985 // NOTE: We don't support IP learning at this moment so this
986 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -0800987 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700988 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800989 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
990 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -0800991 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -0800992 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -0800993 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
994 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
995 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
996 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
997 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
998 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -0700999 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -07001000 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -08001001 }
1002 } else {
1003 // NOTE: We don't support IP learning at this moment so this
1004 // is not necessary. Also it causes duplication of DHCPv6 packets.
1005 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1006 }
sanghob35a6192015-04-01 13:05:26 -07001007 }
1008 }
1009 }
1010
1011 private class InternalLinkListener implements LinkListener {
1012 @Override
1013 public void event(LinkEvent event) {
Charles Chanb1f8c762017-03-29 16:39:05 -07001014 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1015 event.type() == LinkEvent.Type.LINK_UPDATED ||
1016 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001017 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -07001018 scheduleEventHandlerIfNotScheduled(event);
1019 }
1020 }
1021 }
1022
1023 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -07001024 @Override
1025 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -07001026 switch (event.type()) {
1027 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -08001028 case PORT_UPDATED:
1029 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -07001030 case DEVICE_UPDATED:
1031 case DEVICE_AVAILABILITY_CHANGED:
Saurav Dasc88d4662017-05-15 15:34:25 -07001032 log.trace("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -07001033 scheduleEventHandlerIfNotScheduled(event);
1034 break;
1035 default:
1036 }
1037 }
1038 }
1039
Saurav Das4ce45962015-11-24 23:21:05 -08001040 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -07001041 private void scheduleEventHandlerIfNotScheduled(Event event) {
Charles Chan65238242017-06-22 18:03:14 -07001042 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001043 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001044 numOfEventsQueued++;
1045
1046 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
1047 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001048 eventHandlerFuture = executorService
1049 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
1050 numOfHandlerScheduled++;
1051 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -07001052 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001053 numOfEventsQueued,
1054 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -07001055 }
sanghob35a6192015-04-01 13:05:26 -07001056 }
1057
1058 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -07001059 @Override
sanghob35a6192015-04-01 13:05:26 -07001060 public void run() {
Pier Luigieefa2762018-02-12 09:56:20 +01001061 while (true) {
1062 try {
Saurav Das4ce45962015-11-24 23:21:05 -08001063 @SuppressWarnings("rawtypes")
Charles Chan65238242017-06-22 18:03:14 -07001064 Event event;
1065 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001066 if (!eventQueue.isEmpty()) {
1067 event = eventQueue.poll();
1068 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001069 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001070 numOfHandlerExecution++;
1071 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
1072 numOfHandlerExecution, numOfEventsExecuted);
1073 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001074 }
sangho20eff1d2015-04-13 15:15:58 -07001075 }
Charles Chanb1f8c762017-03-29 16:39:05 -07001076 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1077 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Das45f48152018-01-18 12:07:33 -08001078 linkHandler.processLinkAdded((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001079 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Saurav Das45f48152018-01-18 12:07:33 -08001080 linkHandler.processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001081 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1082 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1083 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001084 DeviceId deviceId = ((Device) event.subject()).id();
1085 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001086 log.info("Processing device event {} for available device {}",
1087 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001088 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -07001089 } else {
1090 log.info("Processing device event {} for unavailable device {}",
Pier Luigieefa2762018-02-12 09:56:20 +01001091 event.type(), ((Device) event.subject()).id());
Saurav Das80980c72016-03-23 11:22:49 -07001092 processDeviceRemoved((Device) event.subject());
1093 }
Saurav Das1a129a02016-11-18 15:21:57 -08001094 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001095 // typically these calls come when device is added first time
1096 // so port filtering rules are handled at the device_added event.
1097 // port added calls represent all ports on the device,
1098 // enabled or not.
Saurav Dasc88d4662017-05-15 15:34:25 -07001099 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001100 ((DeviceEvent) event).subject().id(),
1101 ((DeviceEvent) event).port().number(),
1102 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -08001103 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001104 // these calls happen for every subsequent event
1105 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -08001106 log.info("** PORT UPDATED {}/{} -> {}",
1107 event.subject(),
1108 ((DeviceEvent) event).port(),
1109 event.type());
1110 processPortUpdated(((Device) event.subject()),
1111 ((DeviceEvent) event).port());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001112 } else {
1113 log.warn("Unhandled event type: {}", event.type());
1114 }
Pier Luigieefa2762018-02-12 09:56:20 +01001115 } catch (Exception e) {
1116 log.error("SegmentRouting event handler thread thrown an exception: {}",
1117 e.getMessage(), e);
sanghob35a6192015-04-01 13:05:26 -07001118 }
1119 }
sanghob35a6192015-04-01 13:05:26 -07001120 }
1121 }
1122
Saurav Das45f48152018-01-18 12:07:33 -08001123 void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001124 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001125
1126 // NOTE: Punt ARP/NDP even when the device is not configured.
1127 // Host learning without network config is required for CORD config generator.
1128 routingRulePopulator.populateIpPunts(device.id());
1129 routingRulePopulator.populateArpNdpPunts(device.id());
1130
Charles Chan0b4e6182015-11-03 10:42:14 -08001131 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001132 log.warn("Device configuration unavailable. Device {} will be "
1133 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001134 return;
1135 }
Charles Chan2199c302016-04-23 17:36:10 -07001136 processDeviceAddedInternal(device.id());
1137 }
1138
1139 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001140 // Irrespective of whether the local is a MASTER or not for this device,
1141 // we need to create a SR-group-handler instance. This is because in a
1142 // multi-instance setup, any instance can initiate forwarding/next-objectives
1143 // for any switch (even if this instance is a SLAVE or not even connected
1144 // to the switch). To handle this, a default-group-handler instance is necessary
1145 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001146 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1147 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001148 DefaultGroupHandler groupHandler;
1149 try {
1150 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001151 createGroupHandler(deviceId,
1152 appId,
1153 deviceConfiguration,
1154 linkService,
1155 flowObjectiveService,
1156 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001157 } catch (DeviceConfigNotFoundException e) {
1158 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1159 return;
1160 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001161 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001162 deviceId);
1163 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001164 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001165
Charles Chan2199c302016-04-23 17:36:10 -07001166 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001167 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan03a73e02016-10-24 14:52:01 -07001168 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001169 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001170 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001171 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001172 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001173 }
Charles Chan5270ed02016-01-30 23:22:37 -08001174
Charles Chan03a73e02016-10-24 14:52:01 -07001175 appCfgHandler.init(deviceId);
1176 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001177 }
1178
Saurav Das80980c72016-03-23 11:22:49 -07001179 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001180 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001181 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan47933752017-11-30 15:37:50 -08001182 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001183 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001184 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001185 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001186 portNextObjStore.entrySet().stream()
1187 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001188 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Das45f48152018-01-18 12:07:33 -08001189 linkHandler.processDeviceRemoved(device);
Charles Chaned3742352017-06-15 00:44:51 -07001190
Saurav Dasceccf242017-08-03 18:30:35 -07001191 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1192 if (gh != null) {
1193 gh.shutdown();
1194 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001195 // Note that a switch going down is associated with all of its links
1196 // going down as well, but it is treated as a single switch down event
1197 // while the link-downs are ignored.
1198 defaultRoutingHandler
1199 .populateRoutingRulesForLinkStatusChange(null, null, device.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001200 defaultRoutingHandler.purgeEcmpGraph(device.id());
Pier Luigieba73a02018-01-16 10:47:50 +01001201 mcastHandler.processDeviceDown(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001202 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -07001203 }
1204
Saurav Das1a129a02016-11-18 15:21:57 -08001205 private void processPortUpdated(Device device, Port port) {
1206 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1207 log.warn("Device configuration uploading. Not handling port event for"
1208 + "dev: {} port: {}", device.id(), port.number());
1209 return;
1210 }
Saurav Das018605f2017-02-18 14:05:44 -08001211
1212 if (!mastershipService.isLocalMaster(device.id())) {
1213 log.debug("Not master for dev:{} .. not handling port updated event"
1214 + "for port {}", device.id(), port.number());
1215 return;
1216 }
1217
1218 // first we handle filtering rules associated with the port
1219 if (port.isEnabled()) {
1220 log.info("Switchport {}/{} enabled..programming filters",
1221 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001222 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001223 } else {
1224 log.info("Switchport {}/{} disabled..removing filters",
1225 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001226 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001227 }
Saurav Das1a129a02016-11-18 15:21:57 -08001228
1229 // portUpdated calls are for ports that have gone down or up. For switch
1230 // to switch ports, link-events should take care of any re-routing or
1231 // group editing necessary for port up/down. Here we only process edge ports
1232 // that are already configured.
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001233 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1234 VlanId untaggedVlan = getUntaggedVlanId(cp);
1235 VlanId nativeVlan = getNativeVlanId(cp);
1236 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001237
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001238 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001239 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das1a129a02016-11-18 15:21:57 -08001240 + "dev/port: {}/{}", device.id(), port.number());
1241 return;
1242 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001243 if (untaggedVlan != null) {
1244 processEdgePort(device, port, untaggedVlan, true);
1245 }
1246 if (nativeVlan != null) {
1247 processEdgePort(device, port, nativeVlan, true);
1248 }
1249 if (!taggedVlans.isEmpty()) {
1250 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1251 }
Saurav Das1a129a02016-11-18 15:21:57 -08001252 }
1253
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001254 private void processEdgePort(Device device, Port port, VlanId vlanId,
1255 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001256 boolean portUp = port.isEnabled();
1257 if (portUp) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001258 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001259 port.number(), vlanId);
Charles Chan47933752017-11-30 15:37:50 -08001260 hostHandler.processPortUp(new ConnectPoint(device.id(), port.number()));
Saurav Das1a129a02016-11-18 15:21:57 -08001261 } else {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001262 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001263 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001264 }
1265
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001266 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001267 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001268 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001269 } else {
1270 log.warn("Group handler not found for dev:{}. Not handling edge port"
1271 + " {} event for port:{}", device.id(),
1272 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001273 }
1274 }
sangho1e575652015-05-14 00:39:53 -07001275
Charles Chan27fe1a52017-10-20 16:06:55 -07001276 private void createOrUpdateDeviceConfiguration() {
1277 if (deviceConfiguration == null) {
Saurav Dase7f51012018-02-09 17:26:45 -08001278 log.info("Creating new DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001279 deviceConfiguration = new DeviceConfiguration(this);
1280 } else {
Saurav Dase7f51012018-02-09 17:26:45 -08001281 log.info("Updating DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001282 deviceConfiguration.updateConfig();
1283 }
1284 }
1285
Pier Ventre10bd8d12016-11-26 21:05:22 -08001286 /**
1287 * Registers the given connect point with the NRS, this is necessary
1288 * to receive the NDP and ARP packets from the NRS.
1289 *
1290 * @param portToRegister connect point to register
1291 */
1292 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001293 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001294 portToRegister,
1295 neighbourHandler,
1296 appId
1297 );
1298 }
1299
Charles Chand6832882015-10-05 17:50:33 -07001300 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001301 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001302 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001303
Charles Chane849c192016-01-11 18:28:54 -08001304 /**
1305 * Constructs the internal network config listener.
1306 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001307 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001308 */
Charles Chan65238242017-06-22 18:03:14 -07001309 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001310 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001311 }
1312
Charles Chane849c192016-01-11 18:28:54 -08001313 /**
1314 * Reads network config and initializes related data structure accordingly.
1315 */
Charles Chan47933752017-11-30 15:37:50 -08001316 void configureNetwork() {
Saurav Dase7f51012018-02-09 17:26:45 -08001317 log.info("Configuring network ...");
Charles Chan27fe1a52017-10-20 16:06:55 -07001318 createOrUpdateDeviceConfiguration();
Charles Chan4636be02015-10-07 14:21:45 -07001319
Charles Chan2c15aca2016-11-09 20:51:44 -08001320 arpHandler = new ArpHandler(srManager);
1321 icmpHandler = new IcmpHandler(srManager);
1322 ipHandler = new IpHandler(srManager);
1323 routingRulePopulator = new RoutingRulePopulator(srManager);
1324 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001325
1326 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1327 groupHandlerMap, tunnelStore);
1328 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1329 flowObjectiveService,
1330 tunnelHandler, policyStore);
Saurav Das7bcbe702017-06-13 15:35:54 -07001331 // add a small delay to absorb multiple network config added notifications
1332 if (!programmingScheduled.get()) {
Saurav Dase7f51012018-02-09 17:26:45 -08001333 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
Saurav Das7bcbe702017-06-13 15:35:54 -07001334 programmingScheduled.set(true);
1335 executorService.schedule(new ConfigChange(), PROGRAM_DELAY,
1336 TimeUnit.SECONDS);
Charles Chan4636be02015-10-07 14:21:45 -07001337 }
Charles Chan2199c302016-04-23 17:36:10 -07001338 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001339 }
1340
Charles Chand6832882015-10-05 17:50:33 -07001341 @Override
1342 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001343 // TODO move this part to NetworkConfigEventHandler
1344 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1345 switch (event.type()) {
1346 case CONFIG_ADDED:
Charles Chan278ce832017-06-26 15:25:09 -07001347 log.info("Segment Routing Device Config added for {}", event.subject());
Charles Chan5270ed02016-01-30 23:22:37 -08001348 configureNetwork();
1349 break;
1350 case CONFIG_UPDATED:
Charles Chan278ce832017-06-26 15:25:09 -07001351 log.info("Segment Routing Config updated for {}", event.subject());
Charles Chan27fe1a52017-10-20 16:06:55 -07001352 createOrUpdateDeviceConfiguration();
Charles Chan278ce832017-06-26 15:25:09 -07001353 // TODO support dynamic configuration
1354 break;
1355 default:
1356 break;
1357 }
1358 } else if (event.configClass().equals(InterfaceConfig.class)) {
1359 switch (event.type()) {
1360 case CONFIG_ADDED:
1361 log.info("Interface Config added for {}", event.subject());
1362 configureNetwork();
1363 break;
1364 case CONFIG_UPDATED:
1365 log.info("Interface Config updated for {}", event.subject());
Charles Chan27fe1a52017-10-20 16:06:55 -07001366 createOrUpdateDeviceConfiguration();
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001367
1368 // Following code will be uncommented when [CORD-634] is fully implemented.
1369 // [CORD-634] Add dynamic config support for interfaces
1370 updateInterface((InterfaceConfig) event.config().get(),
1371 (InterfaceConfig) event.prevConfig().get());
Charles Chan5270ed02016-01-30 23:22:37 -08001372 // TODO support dynamic configuration
1373 break;
1374 default:
1375 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001376 }
Charles Chan5270ed02016-01-30 23:22:37 -08001377 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001378 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001379 switch (event.type()) {
1380 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001381 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001382 break;
1383 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001384 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001385 break;
1386 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001387 appCfgHandler.processAppConfigRemoved(event);
1388 break;
1389 default:
1390 break;
1391 }
Saurav Dase7f51012018-02-09 17:26:45 -08001392 log.info("App config event .. configuring network");
Charles Chan03a73e02016-10-24 14:52:01 -07001393 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001394 } else if (event.configClass().equals(XConnectConfig.class)) {
1395 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1396 switch (event.type()) {
1397 case CONFIG_ADDED:
1398 xConnectHandler.processXConnectConfigAdded(event);
1399 break;
1400 case CONFIG_UPDATED:
1401 xConnectHandler.processXConnectConfigUpdated(event);
1402 break;
1403 case CONFIG_REMOVED:
1404 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001405 break;
1406 default:
1407 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001408 }
Pier Ventref34966c2016-11-07 16:21:04 -08001409 } else if (event.configClass().equals(PwaasConfig.class)) {
1410 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1411 switch (event.type()) {
1412 case CONFIG_ADDED:
1413 l2TunnelHandler.processPwaasConfigAdded(event);
1414 break;
1415 case CONFIG_UPDATED:
1416 l2TunnelHandler.processPwaasConfigUpdated(event);
1417 break;
1418 case CONFIG_REMOVED:
1419 l2TunnelHandler.processPwaasConfigRemoved(event);
1420 break;
1421 default:
1422 break;
1423 }
Charles Chand6832882015-10-05 17:50:33 -07001424 }
1425 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001426
Charles Chan50443e82018-01-03 16:26:32 -08001427 @Override
1428 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase7f51012018-02-09 17:26:45 -08001429 if (event.type() == CONFIG_REGISTERED ||
1430 event.type() == CONFIG_UNREGISTERED) {
1431 log.debug("Ignore event {} due to type mismatch", event);
1432 return false;
Charles Chan50443e82018-01-03 16:26:32 -08001433 }
Saurav Dase7f51012018-02-09 17:26:45 -08001434
1435 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1436 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1437 !event.configClass().equals(InterfaceConfig.class) &&
1438 !event.configClass().equals(XConnectConfig.class) &&
1439 !event.configClass().equals(PwaasConfig.class)) {
1440 log.debug("Ignore event {} due to class mismatch", event);
1441 return false;
1442 }
1443
1444 return true;
Charles Chan50443e82018-01-03 16:26:32 -08001445 }
1446
Saurav Das7bcbe702017-06-13 15:35:54 -07001447 private final class ConfigChange implements Runnable {
1448 @Override
1449 public void run() {
1450 programmingScheduled.set(false);
Saurav Dase7f51012018-02-09 17:26:45 -08001451 log.info("Reacting to config changes after buffer delay");
Saurav Das7bcbe702017-06-13 15:35:54 -07001452 for (Device device : deviceService.getDevices()) {
1453 processDeviceAdded(device);
1454 }
1455 defaultRoutingHandler.startPopulationProcess();
1456 }
1457 }
Charles Chand6832882015-10-05 17:50:33 -07001458 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001459
1460 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001461 @Override
1462 public void event(HostEvent event) {
Charles Chan68aa62d2015-11-09 16:37:23 -08001463 switch (event.type()) {
1464 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001465 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001466 break;
1467 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001468 hostHandler.processHostMovedEvent(event);
Charles Chan2fde6d42017-08-23 14:46:43 -07001469 routeHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001470 break;
1471 case HOST_REMOVED:
Charles Chanf9a52702017-06-16 15:19:24 -07001472 hostHandler.processHostRemovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001473 break;
1474 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001475 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001476 break;
1477 default:
1478 log.warn("Unsupported host event type: {}", event.type());
1479 break;
1480 }
1481 }
1482 }
1483
Charles Chand55e84d2016-03-30 17:54:24 -07001484 private class InternalMcastListener implements McastListener {
1485 @Override
1486 public void event(McastEvent event) {
1487 switch (event.type()) {
1488 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001489 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001490 break;
1491 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001492 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001493 break;
1494 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001495 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001496 break;
Charles Chand55e84d2016-03-30 17:54:24 -07001497 case ROUTE_REMOVED:
Pier Luigi9930da52018-02-02 16:19:11 +01001498 mcastHandler.processRouteRemoved(event);
1499 break;
1500 case ROUTE_ADDED:
Charles Chand55e84d2016-03-30 17:54:24 -07001501 default:
1502 break;
1503 }
1504 }
1505 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001506
Charles Chan03a73e02016-10-24 14:52:01 -07001507 private class InternalRouteEventListener implements RouteListener {
1508 @Override
1509 public void event(RouteEvent event) {
1510 switch (event.type()) {
1511 case ROUTE_ADDED:
1512 routeHandler.processRouteAdded(event);
1513 break;
1514 case ROUTE_UPDATED:
1515 routeHandler.processRouteUpdated(event);
1516 break;
1517 case ROUTE_REMOVED:
1518 routeHandler.processRouteRemoved(event);
1519 break;
Charles Chan2fde6d42017-08-23 14:46:43 -07001520 case ALTERNATIVE_ROUTES_CHANGED:
1521 routeHandler.processAlternativeRoutesChanged(event);
1522 break;
Charles Chan03a73e02016-10-24 14:52:01 -07001523 default:
1524 break;
1525 }
1526 }
1527 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001528
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001529 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1530 try {
1531 Set<Interface> intfs = conf.getInterfaces();
1532 Set<Interface> prevIntfs = prevConf.getInterfaces();
1533
1534 // Now we only handle one interface config at each port.
1535 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1536 log.warn("Interface update aborted - one at a time is allowed, " +
1537 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1538 return;
1539 }
1540
1541 Interface intf = intfs.stream().findFirst().get();
1542 Interface prevIntf = prevIntfs.stream().findFirst().get();
1543
1544 DeviceId deviceId = intf.connectPoint().deviceId();
1545 PortNumber portNum = intf.connectPoint().port();
1546
1547 if (!mastershipService.isLocalMaster(deviceId)) {
1548 log.debug("CONFIG_UPDATED event for interfaces should be " +
1549 "handled by master node for device {}", deviceId);
1550 return;
1551 }
1552
1553 removeSubnetConfig(prevIntf.connectPoint(),
1554 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1555 new HashSet<>(intf.ipAddressesList())));
1556
1557 if (prevIntf.vlanNative() != VlanId.NONE && !intf.vlanNative().equals(prevIntf.vlanNative())) {
1558 // RemoveVlanNative
1559 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1560 }
1561
1562 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1563 // RemoveVlanTagged
1564 prevIntf.vlanTagged().stream().filter(i -> !intf.vlanTagged().contains(i)).forEach(
1565 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, false)
1566 );
1567 }
1568
1569 if (prevIntf.vlanUntagged() != VlanId.NONE && !intf.vlanUntagged().equals(prevIntf.vlanUntagged())) {
1570 // RemoveVlanUntagged
1571 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1572 }
1573
1574 if (intf.vlanNative() != VlanId.NONE && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1575 // AddVlanNative
1576 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1577 }
1578
1579 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1580 // AddVlanTagged
1581 intf.vlanTagged().stream().filter(i -> !prevIntf.vlanTagged().contains(i)).forEach(
1582 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, true)
1583 );
1584 }
1585
1586 if (intf.vlanUntagged() != VlanId.NONE && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())) {
1587 // AddVlanUntagged
1588 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1589 }
1590 addSubnetConfig(prevIntf.connectPoint(),
1591 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1592 new HashSet<>(prevIntf.ipAddressesList())));
1593 } catch (ConfigException e) {
1594 log.error("Error in configuration");
1595 }
1596 }
1597
1598 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1599 VlanId vlanId, boolean pushVlan, boolean install) {
1600 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1601 if (grpHandler == null) {
1602 log.warn("Failed to retrieve group handler for device {}", deviceId);
1603 return;
1604 }
1605
1606 // Update filtering objective for a single port
1607 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1608
1609 // Update filtering objective for multicast ingress port
1610 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1611
1612 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1613
1614 if (nextId != -1 && !install) {
1615 // Update next objective for a single port as an output port
1616 // Remove a single port from L2FG
1617 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1618 // Remove L2 Bridging rule and L3 Unicast rule to the host
1619 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1620 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1621 // only if there is no port configured on that VLAN ID
1622 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1623 // Remove broadcast forwarding rule for the VLAN
1624 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1625 // Remove L2FG for VLAN
1626 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1627 } else {
1628 // Remove L2IG of the port
1629 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1630 }
1631 } else if (install) {
1632 if (nextId != -1) {
1633 // Add a single port to L2FG
1634 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1635 } else {
1636 // Create L2FG for VLAN
1637 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1638 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1639 }
1640 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1641 } else {
1642 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1643 }
1644 }
1645
1646 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1647 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1648 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1649
1650 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1651 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1652 .filter(intf -> !intf.connectPoint().equals(cp))
1653 .flatMap(intf -> intf.ipAddressesList().stream())
1654 .collect(Collectors.toSet());
1655 // 1. Partial subnet population
1656 // Remove routing rules for removed subnet from previous configuration,
1657 // which does not also exist in other interfaces in the same device
1658 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1659 .map(InterfaceIpAddress::subnetAddress)
1660 .collect(Collectors.toSet());
1661
1662 defaultRoutingHandler.revokeSubnet(
1663 ipPrefixSet.stream()
1664 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1665 .collect(Collectors.toSet()));
1666
1667 // 2. Interface IP punts
1668 // Remove IP punts for old Intf address
1669 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1670 .map(InterfaceIpAddress::ipAddress)
1671 .collect(Collectors.toSet());
1672 ipAddressSet.stream()
1673 .map(InterfaceIpAddress::ipAddress)
1674 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1675 .forEach(interfaceIpAddress ->
1676 routingRulePopulator.revokeSingleIpPunts(
1677 cp.deviceId(), interfaceIpAddress));
1678
1679 // 3. Host unicast routing rule
1680 // Remove unicast routing rule
1681 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1682 }
1683
1684 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1685 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1686 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1687
1688 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1689 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1690 .filter(intf -> !intf.connectPoint().equals(cp))
1691 .flatMap(intf -> intf.ipAddressesList().stream())
1692 .collect(Collectors.toSet());
1693 // 1. Partial subnet population
1694 // Add routing rules for newly added subnet, which does not also exist in
1695 // other interfaces in the same device
1696 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1697 .map(InterfaceIpAddress::subnetAddress)
1698 .collect(Collectors.toSet());
1699
1700 defaultRoutingHandler.populateSubnet(
1701 Collections.singleton(cp),
1702 ipPrefixSet.stream()
1703 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1704 .collect(Collectors.toSet()));
1705
1706 // 2. Interface IP punts
1707 // Add IP punts for new Intf address
1708 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1709 .map(InterfaceIpAddress::ipAddress)
1710 .collect(Collectors.toSet());
1711 ipAddressSet.stream()
1712 .map(InterfaceIpAddress::ipAddress)
1713 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1714 .forEach(interfaceIpAddress ->
1715 routingRulePopulator.populateSingleIpPunts(
1716 cp.deviceId(), interfaceIpAddress));
1717
1718 // 3. Host unicast routing rule
1719 // Add unicast routing rule
1720 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1721 }
sanghob35a6192015-04-01 13:05:26 -07001722}