blob: 7bfa29b1d91a23ad6bc992d016b220bf85c2831a [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;
Charles Chand2990362016-04-18 13:44:03 -0700108import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -0800109import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -0700110import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700111import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700112import org.onosproject.store.service.EventuallyConsistentMap;
113import org.onosproject.store.service.EventuallyConsistentMapBuilder;
114import org.onosproject.store.service.StorageService;
115import org.onosproject.store.service.WallClockTimestamp;
Charles Chan47933752017-11-30 15:37:50 -0800116import org.osgi.service.component.ComponentContext;
sanghob35a6192015-04-01 13:05:26 -0700117import org.slf4j.Logger;
118import org.slf4j.LoggerFactory;
119
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800120import java.util.Collections;
Charles Chan47933752017-11-30 15:37:50 -0800121import java.util.Dictionary;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800122import java.util.HashSet;
123import java.util.List;
124import java.util.Map;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800125import java.util.Optional;
126import java.util.Set;
127import java.util.concurrent.ConcurrentHashMap;
128import java.util.concurrent.ConcurrentLinkedQueue;
129import java.util.concurrent.Executors;
130import java.util.concurrent.ScheduledExecutorService;
131import java.util.concurrent.ScheduledFuture;
132import java.util.concurrent.TimeUnit;
133import java.util.concurrent.atomic.AtomicBoolean;
134import java.util.stream.Collectors;
sanghob35a6192015-04-01 13:05:26 -0700135
Charles Chan3e783d02016-02-26 22:19:52 -0800136import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800137import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700138import static org.onlab.util.Tools.groupedThreads;
Charles Chan3e783d02016-02-26 22:19:52 -0800139
Charles Chane849c192016-01-11 18:28:54 -0800140/**
141 * Segment routing manager.
142 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700143@Service
144@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700145public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700146
Charles Chan2c15aca2016-11-09 20:51:44 -0800147 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan2fde6d42017-08-23 14:46:43 -0700148 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sanghob35a6192015-04-01 13:05:26 -0700149
150 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700151 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700152
153 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre10bd8d12016-11-26 21:05:22 -0800154 private NeighbourResolutionService neighbourResolutionService;
155
156 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700157 CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700158
159 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700160 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700161
162 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700163 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700164
165 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan47933752017-11-30 15:37:50 -0800166 HostLocationProbingService probingService;
167
168 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700169 DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700170
171 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Saurav Das45f48152018-01-18 12:07:33 -0800172 DeviceAdminService deviceAdminService;
173
174 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800175 public FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700176
177 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700178 LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700179
Charles Chan5270ed02016-01-30 23:22:37 -0800180 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800181 public MastershipService mastershipService;
Charles Chan03a73e02016-10-24 14:52:01 -0700182
183 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800184 public StorageService storageService;
Charles Chan03a73e02016-10-24 14:52:01 -0700185
186 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
187 MulticastRouteService multicastRouteService;
188
189 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chanf237e1b2018-01-09 13:45:07 -0800190 public TopologyService topologyService;
Charles Chan03a73e02016-10-24 14:52:01 -0700191
192 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700193 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800194
195 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800196 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800197
Saurav Das80980c72016-03-23 11:22:49 -0700198 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800199 public InterfaceService interfaceService;
200
Pier Luigieba73a02018-01-16 10:47:50 +0100201 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
202 public ClusterService clusterService;
203
204 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
205 public LeadershipService leadershipService;
206
Charles Chan47933752017-11-30 15:37:50 -0800207 @Property(name = "activeProbing", boolValue = true,
208 label = "Enable active probing to discover dual-homed hosts.")
209 boolean activeProbing = true;
210
Charles Chan03a73e02016-10-24 14:52:01 -0700211 ArpHandler arpHandler = null;
212 IcmpHandler icmpHandler = null;
213 IpHandler ipHandler = null;
214 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700215 ApplicationId appId;
216 DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700217
Charles Chan03a73e02016-10-24 14:52:01 -0700218 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700219 private TunnelHandler tunnelHandler = null;
220 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700221 private InternalPacketProcessor processor = null;
222 private InternalLinkListener linkListener = null;
223 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700224 private AppConfigHandler appCfgHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700225 XConnectHandler xConnectHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800226 McastHandler mcastHandler = null;
227 HostHandler hostHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800228 private RouteHandler routeHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800229 LinkHandler linkHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800230 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Pier Ventref34966c2016-11-07 16:21:04 -0800231 private L2TunnelHandler l2TunnelHandler = null;
sanghob35a6192015-04-01 13:05:26 -0700232 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan5270ed02016-01-30 23:22:37 -0800233 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700234 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
235 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700236 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sanghob35a6192015-04-01 13:05:26 -0700237
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700238 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700239 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sanghob35a6192015-04-01 13:05:26 -0700240
Saurav Das4ce45962015-11-24 23:21:05 -0800241 @SuppressWarnings("unused")
sanghob35a6192015-04-01 13:05:26 -0700242 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800243 @SuppressWarnings("rawtypes")
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700244 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Saurav Das45f48152018-01-18 12:07:33 -0800245 Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chane849c192016-01-11 18:28:54 -0800246 new ConcurrentHashMap<>();
247 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700248 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700249 * Used to keep track on MPLS group information.
Charles Chane849c192016-01-11 18:28:54 -0800250 */
Charles Chan47933752017-11-30 15:37:50 -0800251 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das7bcbe702017-06-13 15:35:54 -0700252 dsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800253 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700254 * Per device next objective ID store with (device id + vlanid) as key.
255 * Used to keep track on L2 flood group information.
Charles Chane849c192016-01-11 18:28:54 -0800256 */
Charles Chan47933752017-11-30 15:37:50 -0800257 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan59cc16d2017-02-02 16:20:42 -0800258 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800259 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700260 * Per device next objective ID store with (device id + port + treatment + meta) as key.
261 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chane849c192016-01-11 18:28:54 -0800262 */
Charles Chan47933752017-11-30 15:37:50 -0800263 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800264 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800265
Saurav Das4ce45962015-11-24 23:21:05 -0800266 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
267 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700268
Saurav Das7bcbe702017-06-13 15:35:54 -0700269 private AtomicBoolean programmingScheduled = new AtomicBoolean();
270
Charles Chand55e84d2016-03-30 17:54:24 -0700271 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700272 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
273 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700274 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700275 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800276 public SegmentRoutingDeviceConfig createConfig() {
277 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700278 }
279 };
Pier Ventref34966c2016-11-07 16:21:04 -0800280
Charles Chand55e84d2016-03-30 17:54:24 -0700281 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700282 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
283 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700284 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800285 @Override
286 public SegmentRoutingAppConfig createConfig() {
287 return new SegmentRoutingAppConfig();
288 }
289 };
Pier Ventref34966c2016-11-07 16:21:04 -0800290
Charles Chanfc5c7802016-05-17 13:13:55 -0700291 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
292 new ConfigFactory<ApplicationId, XConnectConfig>(
293 SubjectFactories.APP_SUBJECT_FACTORY,
294 XConnectConfig.class, "xconnect") {
295 @Override
296 public XConnectConfig createConfig() {
297 return new XConnectConfig();
298 }
299 };
Pier Ventref34966c2016-11-07 16:21:04 -0800300
Charles Chand55e84d2016-03-30 17:54:24 -0700301 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700302 new ConfigFactory<ApplicationId, McastConfig>(
303 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700304 McastConfig.class, "multicast") {
305 @Override
306 public McastConfig createConfig() {
307 return new McastConfig();
308 }
309 };
310
Pier Ventref34966c2016-11-07 16:21:04 -0800311 private final ConfigFactory<ApplicationId, PwaasConfig> pwaasConfigFactory =
312 new ConfigFactory<ApplicationId, PwaasConfig>(
313 SubjectFactories.APP_SUBJECT_FACTORY,
314 PwaasConfig.class, "pwaas") {
315 @Override
316 public PwaasConfig createConfig() {
317 return new PwaasConfig();
318 }
319 };
320
Charles Chan65238242017-06-22 18:03:14 -0700321 private static final Object THREAD_SCHED_LOCK = new Object();
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700322 private static int numOfEventsQueued = 0;
323 private static int numOfEventsExecuted = 0;
sanghob35a6192015-04-01 13:05:26 -0700324 private static int numOfHandlerExecution = 0;
325 private static int numOfHandlerScheduled = 0;
326
Charles Chan116188d2016-02-18 14:22:42 -0800327 /**
328 * Segment Routing App ID.
329 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800330 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan59cc16d2017-02-02 16:20:42 -0800331
Charles Chane849c192016-01-11 18:28:54 -0800332 /**
333 * The default VLAN ID assigned to the interfaces without subnet config.
334 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800335 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700336
sanghob35a6192015-04-01 13:05:26 -0700337 @Activate
Charles Chan47933752017-11-30 15:37:50 -0800338 protected void activate(ComponentContext context) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800339 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700340
341 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das7bcbe702017-06-13 15:35:54 -0700342 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700343 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700344 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700345 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700346 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700347 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700348 .build();
Saurav Das7bcbe702017-06-13 15:35:54 -0700349 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700350
Charles Chan59cc16d2017-02-02 16:20:42 -0800351 log.debug("Creating EC map vlannextobjectivestore");
352 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
353 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
354 vlanNextObjStore = vlanNextObjMapBuilder
355 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700356 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700357 .withTimestampProvider((k, v) -> new WallClockTimestamp())
358 .build();
359
Saurav Das4ce45962015-11-24 23:21:05 -0800360 log.debug("Creating EC map subnetnextobjectivestore");
361 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
362 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
363 portNextObjStore = portNextObjMapBuilder
364 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700365 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800366 .withTimestampProvider((k, v) -> new WallClockTimestamp())
367 .build();
368
sangho0b2b6d12015-05-20 22:16:38 -0700369 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
370 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700371 tunnelStore = tunnelMapBuilder
372 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700373 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700374 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700375 .build();
376
377 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
378 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700379 policyStore = policyMapBuilder
380 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700381 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700382 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700383 .build();
384
Saurav Das80980c72016-03-23 11:22:49 -0700385 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800386 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700387 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800388 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800389 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
390 "requestInterceptsEnabled", "false");
Charles Chand3baaba2017-08-08 15:13:37 -0700391 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800392 "requestInterceptsEnabled", "false");
Charles Chanc760f382017-07-24 15:56:10 -0700393 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800394 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800395 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
396 "greedyLearningIpv6", "true");
Charles Chanc6d227e2017-02-28 15:15:17 -0800397 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
398 "forceUnprovision", "true");
Charles Chanef624ed2017-08-10 16:57:28 -0700399 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan73316522017-07-20 16:16:25 -0700400 "distributed", "true");
Charles Chan35a32322017-08-14 11:42:11 -0700401 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
402 "multihomingEnabled", "true");
Charles Chan807d87a2017-11-29 19:54:20 -0800403 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
404 "staleLinkAge", "15000");
405 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
406 "allowDuplicateIps", "false");
Charles Chan47933752017-11-30 15:37:50 -0800407 compCfgService.registerProperties(getClass());
408 modified(context);
Saurav Das80980c72016-03-23 11:22:49 -0700409
Charles Chanb8e10c82015-10-14 11:24:40 -0700410 processor = new InternalPacketProcessor();
411 linkListener = new InternalLinkListener();
412 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700413 appCfgHandler = new AppConfigHandler(this);
414 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700415 mcastHandler = new McastHandler(this);
416 hostHandler = new HostHandler(this);
Saurav Das45f48152018-01-18 12:07:33 -0800417 linkHandler = new LinkHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700418 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800419 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Pier Ventref34966c2016-11-07 16:21:04 -0800420 l2TunnelHandler = new L2TunnelHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700421
Charles Chan3e783d02016-02-26 22:19:52 -0800422 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700423 cfgService.registerConfigFactory(deviceConfigFactory);
424 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700425 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700426 cfgService.registerConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800427 cfgService.registerConfigFactory(pwaasConfigFactory);
Charles Chan132393a2018-01-04 14:26:07 -0800428
429 cfgListener.configureNetwork();
430
Charles Chan5270ed02016-01-30 23:22:37 -0800431 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700432 packetService.addProcessor(processor, PacketProcessor.director(2));
433 linkService.addListener(linkListener);
434 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700435 multicastRouteService.addListener(mcastListener);
Charles Chanc7a8a682017-06-19 00:43:31 -0700436 routeService.addListener(routeListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700437
Andreas Pantelopoulos4a768c02018-01-11 07:53:48 -0800438 l2TunnelHandler.init();
439
sanghob35a6192015-04-01 13:05:26 -0700440 log.info("Started");
441 }
442
Saurav Das45f48152018-01-18 12:07:33 -0800443 KryoNamespace.Builder createSerializer() {
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700444 return new KryoNamespace.Builder()
445 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700446 .register(DestinationSetNextObjectiveStoreKey.class,
Charles Chan59cc16d2017-02-02 16:20:42 -0800447 VlanNextObjectiveStoreKey.class,
Saurav Das7bcbe702017-06-13 15:35:54 -0700448 DestinationSet.class,
449 NextNeighbors.class,
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700450 Tunnel.class,
451 DefaultTunnel.class,
452 Policy.class,
453 TunnelPolicy.class,
454 Policy.Type.class,
455 PortNextObjectiveStoreKey.class,
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700456 XConnectStoreKey.class,
457 DefaultL2Tunnel.class,
458 DefaultL2TunnelPolicy.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700459 );
460 }
461
sanghob35a6192015-04-01 13:05:26 -0700462 @Deactivate
463 protected void deactivate() {
Charles Chand6832882015-10-05 17:50:33 -0700464 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700465 cfgService.unregisterConfigFactory(deviceConfigFactory);
466 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700467 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700468 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800469 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chan47933752017-11-30 15:37:50 -0800470 compCfgService.unregisterProperties(getClass(), false);
Charles Chand6832882015-10-05 17:50:33 -0700471
Charles Chanc7a8a682017-06-19 00:43:31 -0700472 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700473 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700474 linkService.removeListener(linkListener);
475 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700476 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700477 routeService.removeListener(routeListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700478
Charles Chan0aa674e2017-02-23 15:44:08 -0800479 neighbourResolutionService.unregisterNeighbourHandlers(appId);
480
sanghob35a6192015-04-01 13:05:26 -0700481 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700482 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700483 deviceListener = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700484 groupHandlerMap.clear();
485
Saurav Das7bcbe702017-06-13 15:35:54 -0700486 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800487 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700488 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700489 tunnelStore.destroy();
490 policyStore.destroy();
sanghob35a6192015-04-01 13:05:26 -0700491 log.info("Stopped");
492 }
493
Charles Chan47933752017-11-30 15:37:50 -0800494 @Modified
495 private void modified(ComponentContext context) {
496 Dictionary<?, ?> properties = context.getProperties();
497 if (properties == null) {
498 return;
499 }
500
501 String strActiveProving = Tools.get(properties, "activeProbing");
502 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
503
504 if (expectActiveProbing != activeProbing) {
505 activeProbing = expectActiveProbing;
506 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
507 }
508 }
509
sangho1e575652015-05-14 00:39:53 -0700510 @Override
511 public List<Tunnel> getTunnels() {
512 return tunnelHandler.getTunnels();
513 }
514
515 @Override
sangho71abe1b2015-06-29 14:58:47 -0700516 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
517 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700518 }
519
520 @Override
sangho71abe1b2015-06-29 14:58:47 -0700521 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700522 for (Policy policy: policyHandler.getPolicies()) {
523 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
524 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
525 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
526 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700527 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700528 }
529 }
530 }
sangho71abe1b2015-06-29 14:58:47 -0700531 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700532 }
533
534 @Override
sangho71abe1b2015-06-29 14:58:47 -0700535 public PolicyHandler.Result removePolicy(Policy policy) {
536 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700537 }
538
539 @Override
sangho71abe1b2015-06-29 14:58:47 -0700540 public PolicyHandler.Result createPolicy(Policy policy) {
541 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700542 }
543
544 @Override
545 public List<Policy> getPolicies() {
546 return policyHandler.getPolicies();
547 }
548
Saurav Das59232cf2016-04-27 18:35:50 -0700549 @Override
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700550 public List<DefaultL2Tunnel> getL2Tunnels() {
551 return l2TunnelHandler.getL2Tunnels();
552 }
553
554 @Override
555 public List<DefaultL2TunnelPolicy> getL2Policies() {
556 return l2TunnelHandler.getL2Policies();
557 }
558
559 @Override
560 public L2TunnelHandler.Result addPseudowire(String tunnelId, String pwLabel, String cP1,
561 String cP1InnerVlan, String cP1OuterVlan, String cP2,
562 String cP2InnerVlan, String cP2OuterVlan,
563 String mode, String sdTag) {
Andreas Pantelopoulos20474e02017-12-20 18:04:27 -0800564 // Try to inject an empty Pwaas config if it is not found for the first time
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700565 PwaasConfig config = cfgService.getConfig(appId(), PwaasConfig.class);
566 if (config == null) {
Andreas Pantelopoulos20474e02017-12-20 18:04:27 -0800567 log.debug("Pwaas config not found. Try to create an empty one.");
568 cfgService.applyConfig(appId(), PwaasConfig.class, new ObjectMapper().createObjectNode());
569 config = cfgService.getConfig(appId(), PwaasConfig.class);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700570 }
571
572 ObjectNode object = config.addPseudowire(tunnelId, pwLabel,
573 cP1, cP1InnerVlan, cP1OuterVlan,
574 cP2, cP2InnerVlan, cP2OuterVlan,
575 mode, sdTag);
576 if (object == null) {
577 log.warn("Could not add pseudowire to the configuration!");
578 return L2TunnelHandler.Result.ADDITION_ERROR;
579 }
580
581 // inform everyone about the valid change in the pw configuration
582 cfgService.applyConfig(appId(), PwaasConfig.class, object);
583 return L2TunnelHandler.Result.SUCCESS;
584 }
585
586 @Override
587 public L2TunnelHandler.Result removePseudowire(String pwId) {
588
589 PwaasConfig config = cfgService.getConfig(appId(), PwaasConfig.class);
590 if (config == null) {
591 log.warn("Configuration for Pwaas class could not be found!");
592 return L2TunnelHandler.Result.CONFIG_NOT_FOUND;
593 }
594
595 ObjectNode object = config.removePseudowire(pwId);
596 if (object == null) {
597 log.warn("Could not delete pseudowire from configuration!");
598 return L2TunnelHandler.Result.REMOVAL_ERROR;
599 }
600
601 // sanity check, this should never fail since we removed a pw
602 // and we always check when we update the configuration
603 config.isValid();
604
605 // inform everyone
606 cfgService.applyConfig(appId(), PwaasConfig.class, object);
607
608 return L2TunnelHandler.Result.SUCCESS;
609 }
610
611 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700612 public void rerouteNetwork() {
613 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700614 }
615
Charles Chanc81c45b2016-10-20 17:02:44 -0700616 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800617 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
618 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800619 deviceConfiguration.getRouters().forEach(device ->
620 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chanc81c45b2016-10-20 17:02:44 -0700621 return deviceSubnetMap;
622 }
623
Saurav Dasc88d4662017-05-15 15:34:25 -0700624
625 @Override
626 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
627 if (defaultRoutingHandler != null) {
628 return defaultRoutingHandler.getCurrentEmcpSpgMap();
629 } else {
630 return null;
631 }
632 }
633
634 @Override
Saurav Das7bcbe702017-06-13 15:35:54 -0700635 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
636 if (dsNextObjStore != null) {
637 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700638 } else {
639 return ImmutableMap.of();
640 }
641 }
642
Saurav Dasceccf242017-08-03 18:30:35 -0700643 @Override
644 public void verifyGroups(DeviceId id) {
645 DefaultGroupHandler gh = groupHandlerMap.get(id);
646 if (gh != null) {
647 gh.triggerBucketCorrector();
648 }
649 }
650
Saurav Dasc568c342018-01-25 09:49:01 -0800651 @Override
652 public ImmutableMap<Link, Boolean> getSeenLinks() {
653 return linkHandler.getSeenLinks();
654 }
655
656 @Override
657 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
658 return linkHandler.getDownedPorts();
659 }
660
sanghof9d0bf12015-05-19 11:57:42 -0700661 /**
Ray Milkeye4afdb52017-04-05 09:42:04 -0700662 * Extracts the application ID from the manager.
663 *
664 * @return application ID
665 */
666 public ApplicationId appId() {
667 return appId;
668 }
669
670 /**
671 * Returns the device configuration.
672 *
673 * @return device configuration
674 */
675 public DeviceConfiguration deviceConfiguration() {
676 return deviceConfiguration;
677 }
678
679 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700680 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700681 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700682 *
683 * @return next objective ID store
684 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700685 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
686 dsNextObjStore() {
687 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700688 }
689
690 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700691 * Per device next objective ID store with (device id + vlanid) as key.
692 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700693 *
694 * @return vlan next object store
695 */
696 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
697 return vlanNextObjStore;
698 }
699
700 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700701 * Per device next objective ID store with (device id + port + treatment + meta) as key.
702 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700703 *
704 * @return port next object store.
705 */
706 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
707 return portNextObjStore;
708 }
709
710 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700711 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
712 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -0700713 *
714 * @return MPLS-ECMP value
715 */
716 public boolean getMplsEcmp() {
717 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
718 .getConfig(this.appId, SegmentRoutingAppConfig.class);
719 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
720 }
721
722 /**
sanghof9d0bf12015-05-19 11:57:42 -0700723 * Returns the tunnel object with the tunnel ID.
724 *
725 * @param tunnelId Tunnel ID
726 * @return Tunnel reference
727 */
sangho1e575652015-05-14 00:39:53 -0700728 public Tunnel getTunnel(String tunnelId) {
729 return tunnelHandler.getTunnel(tunnelId);
730 }
731
Charles Chan7ffd81f2017-02-08 15:52:08 -0800732 // TODO Consider moving these to InterfaceService
sanghob35a6192015-04-01 13:05:26 -0700733 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800734 * Returns untagged VLAN configured on given connect point.
Charles Chan7ffd81f2017-02-08 15:52:08 -0800735 * <p>
736 * Only returns the first match if there are multiple untagged VLAN configured
737 * on the connect point.
sanghob35a6192015-04-01 13:05:26 -0700738 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800739 * @param connectPoint connect point
740 * @return untagged VLAN or null if not configured
sanghob35a6192015-04-01 13:05:26 -0700741 */
Charles Chan65238242017-06-22 18:03:14 -0700742 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800743 return interfaceService.getInterfacesByPort(connectPoint).stream()
744 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
745 .map(Interface::vlanUntagged)
746 .findFirst().orElse(null);
sanghob35a6192015-04-01 13:05:26 -0700747 }
748
sangho1e575652015-05-14 00:39:53 -0700749 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800750 * Returns tagged VLAN configured on given connect point.
751 * <p>
752 * Returns all matches if there are multiple tagged VLAN configured
753 * on the connect point.
754 *
755 * @param connectPoint connect point
756 * @return tagged VLAN or empty set if not configured
757 */
Charles Chan65238242017-06-22 18:03:14 -0700758 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800759 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
760 return interfaces.stream()
761 .map(Interface::vlanTagged)
Charles Chan65238242017-06-22 18:03:14 -0700762 .flatMap(Set::stream)
Charles Chan7ffd81f2017-02-08 15:52:08 -0800763 .collect(Collectors.toSet());
764 }
765
766 /**
767 * Returns native VLAN configured on given connect point.
768 * <p>
769 * Only returns the first match if there are multiple native VLAN configured
770 * on the connect point.
771 *
772 * @param connectPoint connect point
773 * @return native VLAN or null if not configured
774 */
Charles Chan65238242017-06-22 18:03:14 -0700775 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800776 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
777 return interfaces.stream()
778 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
779 .map(Interface::vlanNative)
780 .findFirst()
781 .orElse(null);
782 }
783
784 /**
Charles Chanf9a52702017-06-16 15:19:24 -0700785 * Returns internal VLAN for untagged hosts on given connect point.
786 * <p>
787 * The internal VLAN is either vlan-untagged for an access port,
788 * or vlan-native for a trunk port.
789 *
790 * @param connectPoint connect point
791 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
792 */
Charles Chan65238242017-06-22 18:03:14 -0700793 VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chanf9a52702017-06-16 15:19:24 -0700794 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
795 VlanId nativeVlanId = getNativeVlanId(connectPoint);
796 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
797 }
798
799 /**
Charles Chan65238242017-06-22 18:03:14 -0700800 * Returns optional pair device ID of given device.
801 *
802 * @param deviceId device ID
803 * @return optional pair device ID. Might be empty if pair device is not configured
804 */
805 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
806 SegmentRoutingDeviceConfig deviceConfig =
807 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
808 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
809 }
810 /**
811 * Returns optional pair device local port of given device.
812 *
813 * @param deviceId device ID
814 * @return optional pair device ID. Might be empty if pair device is not configured
815 */
816 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
817 SegmentRoutingDeviceConfig deviceConfig =
818 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
819 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
820 }
821
822 /**
Charles Chan2fde6d42017-08-23 14:46:43 -0700823 * Determine if current instance is the master of given connect point.
824 *
825 * @param cp connect point
826 * @return true if current instance is the master of given connect point
827 */
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700828 public boolean isMasterOf(ConnectPoint cp) {
Charles Chan2fde6d42017-08-23 14:46:43 -0700829 boolean isMaster = mastershipService.isLocalMaster(cp.deviceId());
830 if (!isMaster) {
831 log.debug(NOT_MASTER, cp);
832 }
833 return isMaster;
834 }
835
836 /**
Charles Chan9640c812017-08-23 13:55:39 -0700837 * Returns locations of given resolved route.
838 *
839 * @param resolvedRoute resolved route
840 * @return locations of nexthop. Might be empty if next hop is not found
841 */
842 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
843 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
844 return Optional.ofNullable(hostService.getHost(hostId))
845 .map(Host::locations).orElse(Sets.newHashSet())
846 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
847 }
848
849 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800850 * Returns vlan port map of given device.
851 *
852 * @param deviceId device id
853 * @return vlan-port multimap
854 */
855 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
856 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
857
858 interfaceService.getInterfaces().stream()
859 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
860 .forEach(intf -> {
861 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -0700862 intf.vlanTagged().forEach(vlanTagged ->
863 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
864 );
Charles Chan7ffd81f2017-02-08 15:52:08 -0800865 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
866 });
867 vlanPortMap.removeAll(VlanId.NONE);
868
869 return vlanPortMap;
870 }
871
872 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800873 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -0800874 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700875 *
876 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -0800877 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -0800878 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700879 */
Charles Chan65238242017-06-22 18:03:14 -0700880 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -0700881 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800882 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
883 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700884 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -0800885 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -0800886 + "device {} not found", deviceId);
887 return -1;
888 }
889 }
890
891 /**
892 * Returns the next objective ID for the given portNumber, given the treatment.
893 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -0700894 * would result in different objectives. If the next object does not exist,
895 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -0800896 *
897 * @param deviceId Device ID
898 * @param portNum port number on device for which NextObjective is queried
899 * @param treatment the actions to apply on the packets (should include outport)
900 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -0700901 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -0700902 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800903 */
904 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
905 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700906 TrafficSelector meta,
907 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -0800908 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
909 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -0700910 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -0800911 } else {
Charles Chane849c192016-01-11 18:28:54 -0800912 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
913 + " not found", deviceId);
914 return -1;
915 }
916 }
917
Saurav Dasc88d4662017-05-15 15:34:25 -0700918 /**
919 * Returns the group handler object for the specified device id.
920 *
921 * @param devId the device identifier
922 * @return the groupHandler object for the device id, or null if not found
923 */
Charles Chan65238242017-06-22 18:03:14 -0700924 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700925 return groupHandlerMap.get(devId);
926 }
927
928 /**
Saurav Dasceccf242017-08-03 18:30:35 -0700929 * Returns the default routing handler object.
930 *
931 * @return the default routing handler object
932 */
933 public DefaultRoutingHandler getRoutingHandler() {
934 return defaultRoutingHandler;
935 }
936
sanghob35a6192015-04-01 13:05:26 -0700937 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700938 @Override
939 public void process(PacketContext context) {
940
941 if (context.isHandled()) {
942 return;
943 }
944
945 InboundPacket pkt = context.inPacket();
946 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -0800947
948 if (ethernet == null) {
949 return;
950 }
951
Saurav Das7bcbe702017-06-13 15:35:54 -0700952 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
953 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800954 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700955 log.warn("Received unexpected ARP packet on {}",
956 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -0700957 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -0800958 return;
sanghob35a6192015-04-01 13:05:26 -0700959 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800960 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
961 //ipHandler.addToPacketBuffer(ipv4Packet);
962 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
963 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700964 } else {
Charles Chan50035632017-01-13 17:20:44 -0800965 // NOTE: We don't support IP learning at this moment so this
966 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -0800967 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700968 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800969 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
970 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -0800971 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -0800972 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -0800973 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
974 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
975 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
976 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
977 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
978 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -0700979 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -0700980 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -0800981 }
982 } else {
983 // NOTE: We don't support IP learning at this moment so this
984 // is not necessary. Also it causes duplication of DHCPv6 packets.
985 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
986 }
sanghob35a6192015-04-01 13:05:26 -0700987 }
988 }
989 }
990
991 private class InternalLinkListener implements LinkListener {
992 @Override
993 public void event(LinkEvent event) {
Charles Chanb1f8c762017-03-29 16:39:05 -0700994 if (event.type() == LinkEvent.Type.LINK_ADDED ||
995 event.type() == LinkEvent.Type.LINK_UPDATED ||
996 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700997 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700998 scheduleEventHandlerIfNotScheduled(event);
999 }
1000 }
1001 }
1002
1003 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -07001004 @Override
1005 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -07001006 switch (event.type()) {
1007 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -08001008 case PORT_UPDATED:
1009 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -07001010 case DEVICE_UPDATED:
1011 case DEVICE_AVAILABILITY_CHANGED:
Saurav Dasc88d4662017-05-15 15:34:25 -07001012 log.trace("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -07001013 scheduleEventHandlerIfNotScheduled(event);
1014 break;
1015 default:
1016 }
1017 }
1018 }
1019
Saurav Das4ce45962015-11-24 23:21:05 -08001020 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -07001021 private void scheduleEventHandlerIfNotScheduled(Event event) {
Charles Chan65238242017-06-22 18:03:14 -07001022 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001023 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001024 numOfEventsQueued++;
1025
1026 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
1027 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001028 eventHandlerFuture = executorService
1029 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
1030 numOfHandlerScheduled++;
1031 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -07001032 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001033 numOfEventsQueued,
1034 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -07001035 }
sanghob35a6192015-04-01 13:05:26 -07001036 }
1037
1038 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -07001039 @Override
sanghob35a6192015-04-01 13:05:26 -07001040 public void run() {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001041 try {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001042 while (true) {
Saurav Das4ce45962015-11-24 23:21:05 -08001043 @SuppressWarnings("rawtypes")
Charles Chan65238242017-06-22 18:03:14 -07001044 Event event;
1045 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001046 if (!eventQueue.isEmpty()) {
1047 event = eventQueue.poll();
1048 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001049 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001050 numOfHandlerExecution++;
1051 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
1052 numOfHandlerExecution, numOfEventsExecuted);
1053 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001054 }
sangho20eff1d2015-04-13 15:15:58 -07001055 }
Charles Chanb1f8c762017-03-29 16:39:05 -07001056 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1057 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001058 // Note: do not update seenLinks here, otherwise every
1059 // link, even one seen for the first time, will be appear
1060 // to be a previously seen link
Saurav Das45f48152018-01-18 12:07:33 -08001061 linkHandler.processLinkAdded((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001062 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Saurav Das45f48152018-01-18 12:07:33 -08001063 linkHandler.processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001064 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1065 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1066 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001067 DeviceId deviceId = ((Device) event.subject()).id();
1068 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001069 log.info("Processing device event {} for available device {}",
1070 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001071 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -07001072 } else {
1073 log.info("Processing device event {} for unavailable device {}",
1074 event.type(), ((Device) event.subject()).id());
1075 processDeviceRemoved((Device) event.subject());
1076 }
Saurav Das1a129a02016-11-18 15:21:57 -08001077 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001078 // typically these calls come when device is added first time
1079 // so port filtering rules are handled at the device_added event.
1080 // port added calls represent all ports on the device,
1081 // enabled or not.
Saurav Dasc88d4662017-05-15 15:34:25 -07001082 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001083 ((DeviceEvent) event).subject().id(),
1084 ((DeviceEvent) event).port().number(),
1085 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -08001086 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001087 // these calls happen for every subsequent event
1088 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -08001089 log.info("** PORT UPDATED {}/{} -> {}",
1090 event.subject(),
1091 ((DeviceEvent) event).port(),
1092 event.type());
1093 processPortUpdated(((Device) event.subject()),
1094 ((DeviceEvent) event).port());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001095 } else {
1096 log.warn("Unhandled event type: {}", event.type());
1097 }
sanghob35a6192015-04-01 13:05:26 -07001098 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001099 } catch (Exception e) {
1100 log.error("SegmentRouting event handler "
1101 + "thread thrown an exception: {}", e);
sanghob35a6192015-04-01 13:05:26 -07001102 }
sanghob35a6192015-04-01 13:05:26 -07001103 }
1104 }
1105
Saurav Das45f48152018-01-18 12:07:33 -08001106 void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001107 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001108
1109 // NOTE: Punt ARP/NDP even when the device is not configured.
1110 // Host learning without network config is required for CORD config generator.
1111 routingRulePopulator.populateIpPunts(device.id());
1112 routingRulePopulator.populateArpNdpPunts(device.id());
1113
Charles Chan0b4e6182015-11-03 10:42:14 -08001114 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001115 log.warn("Device configuration unavailable. Device {} will be "
1116 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001117 return;
1118 }
Charles Chan2199c302016-04-23 17:36:10 -07001119 processDeviceAddedInternal(device.id());
1120 }
1121
1122 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001123 // Irrespective of whether the local is a MASTER or not for this device,
1124 // we need to create a SR-group-handler instance. This is because in a
1125 // multi-instance setup, any instance can initiate forwarding/next-objectives
1126 // for any switch (even if this instance is a SLAVE or not even connected
1127 // to the switch). To handle this, a default-group-handler instance is necessary
1128 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001129 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1130 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001131 DefaultGroupHandler groupHandler;
1132 try {
1133 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001134 createGroupHandler(deviceId,
1135 appId,
1136 deviceConfiguration,
1137 linkService,
1138 flowObjectiveService,
1139 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001140 } catch (DeviceConfigNotFoundException e) {
1141 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1142 return;
1143 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001144 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001145 deviceId);
1146 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001147 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001148
Charles Chan2199c302016-04-23 17:36:10 -07001149 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001150 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan03a73e02016-10-24 14:52:01 -07001151 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001152 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001153 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001154 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001155 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001156 }
Charles Chan5270ed02016-01-30 23:22:37 -08001157
Charles Chan03a73e02016-10-24 14:52:01 -07001158 appCfgHandler.init(deviceId);
1159 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001160 }
1161
Saurav Das80980c72016-03-23 11:22:49 -07001162 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001163 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001164 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan47933752017-11-30 15:37:50 -08001165 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001166 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001167 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001168 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001169 portNextObjStore.entrySet().stream()
1170 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001171 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Das45f48152018-01-18 12:07:33 -08001172 linkHandler.processDeviceRemoved(device);
Charles Chaned3742352017-06-15 00:44:51 -07001173
Saurav Dasceccf242017-08-03 18:30:35 -07001174 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1175 if (gh != null) {
1176 gh.shutdown();
1177 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001178 // Note that a switch going down is associated with all of its links
1179 // going down as well, but it is treated as a single switch down event
1180 // while the link-downs are ignored.
1181 defaultRoutingHandler
1182 .populateRoutingRulesForLinkStatusChange(null, null, device.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001183 defaultRoutingHandler.purgeEcmpGraph(device.id());
Pier Luigieba73a02018-01-16 10:47:50 +01001184 mcastHandler.processDeviceDown(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001185 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -07001186 }
1187
Saurav Das1a129a02016-11-18 15:21:57 -08001188 private void processPortUpdated(Device device, Port port) {
1189 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1190 log.warn("Device configuration uploading. Not handling port event for"
1191 + "dev: {} port: {}", device.id(), port.number());
1192 return;
1193 }
Saurav Das018605f2017-02-18 14:05:44 -08001194
1195 if (!mastershipService.isLocalMaster(device.id())) {
1196 log.debug("Not master for dev:{} .. not handling port updated event"
1197 + "for port {}", device.id(), port.number());
1198 return;
1199 }
1200
1201 // first we handle filtering rules associated with the port
1202 if (port.isEnabled()) {
1203 log.info("Switchport {}/{} enabled..programming filters",
1204 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001205 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001206 } else {
1207 log.info("Switchport {}/{} disabled..removing filters",
1208 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001209 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001210 }
Saurav Das1a129a02016-11-18 15:21:57 -08001211
1212 // portUpdated calls are for ports that have gone down or up. For switch
1213 // to switch ports, link-events should take care of any re-routing or
1214 // group editing necessary for port up/down. Here we only process edge ports
1215 // that are already configured.
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001216 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1217 VlanId untaggedVlan = getUntaggedVlanId(cp);
1218 VlanId nativeVlan = getNativeVlanId(cp);
1219 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001220
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001221 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001222 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das1a129a02016-11-18 15:21:57 -08001223 + "dev/port: {}/{}", device.id(), port.number());
1224 return;
1225 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001226 if (untaggedVlan != null) {
1227 processEdgePort(device, port, untaggedVlan, true);
1228 }
1229 if (nativeVlan != null) {
1230 processEdgePort(device, port, nativeVlan, true);
1231 }
1232 if (!taggedVlans.isEmpty()) {
1233 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1234 }
Saurav Das1a129a02016-11-18 15:21:57 -08001235 }
1236
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001237 private void processEdgePort(Device device, Port port, VlanId vlanId,
1238 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001239 boolean portUp = port.isEnabled();
1240 if (portUp) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001241 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001242 port.number(), vlanId);
Charles Chan47933752017-11-30 15:37:50 -08001243 hostHandler.processPortUp(new ConnectPoint(device.id(), port.number()));
Saurav Das1a129a02016-11-18 15:21:57 -08001244 } else {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001245 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001246 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001247 }
1248
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001249 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001250 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001251 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001252 } else {
1253 log.warn("Group handler not found for dev:{}. Not handling edge port"
1254 + " {} event for port:{}", device.id(),
1255 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001256 }
1257 }
sangho1e575652015-05-14 00:39:53 -07001258
Charles Chan27fe1a52017-10-20 16:06:55 -07001259 private void createOrUpdateDeviceConfiguration() {
1260 if (deviceConfiguration == null) {
1261 deviceConfiguration = new DeviceConfiguration(this);
1262 } else {
1263 deviceConfiguration.updateConfig();
1264 }
1265 }
1266
Pier Ventre10bd8d12016-11-26 21:05:22 -08001267 /**
1268 * Registers the given connect point with the NRS, this is necessary
1269 * to receive the NDP and ARP packets from the NRS.
1270 *
1271 * @param portToRegister connect point to register
1272 */
1273 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001274 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001275 portToRegister,
1276 neighbourHandler,
1277 appId
1278 );
1279 }
1280
Charles Chand6832882015-10-05 17:50:33 -07001281 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001282 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001283 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001284
Charles Chane849c192016-01-11 18:28:54 -08001285 /**
1286 * Constructs the internal network config listener.
1287 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001288 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001289 */
Charles Chan65238242017-06-22 18:03:14 -07001290 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001291 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001292 }
1293
Charles Chane849c192016-01-11 18:28:54 -08001294 /**
1295 * Reads network config and initializes related data structure accordingly.
1296 */
Charles Chan47933752017-11-30 15:37:50 -08001297 void configureNetwork() {
Charles Chan27fe1a52017-10-20 16:06:55 -07001298 createOrUpdateDeviceConfiguration();
Charles Chan4636be02015-10-07 14:21:45 -07001299
Charles Chan2c15aca2016-11-09 20:51:44 -08001300 arpHandler = new ArpHandler(srManager);
1301 icmpHandler = new IcmpHandler(srManager);
1302 ipHandler = new IpHandler(srManager);
1303 routingRulePopulator = new RoutingRulePopulator(srManager);
1304 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001305
1306 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1307 groupHandlerMap, tunnelStore);
1308 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1309 flowObjectiveService,
1310 tunnelHandler, policyStore);
Saurav Das7bcbe702017-06-13 15:35:54 -07001311 // add a small delay to absorb multiple network config added notifications
1312 if (!programmingScheduled.get()) {
1313 programmingScheduled.set(true);
1314 executorService.schedule(new ConfigChange(), PROGRAM_DELAY,
1315 TimeUnit.SECONDS);
Charles Chan4636be02015-10-07 14:21:45 -07001316 }
Charles Chan2199c302016-04-23 17:36:10 -07001317 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001318 }
1319
Charles Chand6832882015-10-05 17:50:33 -07001320 @Override
1321 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001322 // TODO move this part to NetworkConfigEventHandler
1323 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1324 switch (event.type()) {
1325 case CONFIG_ADDED:
Charles Chan278ce832017-06-26 15:25:09 -07001326 log.info("Segment Routing Device Config added for {}", event.subject());
Charles Chan5270ed02016-01-30 23:22:37 -08001327 configureNetwork();
1328 break;
1329 case CONFIG_UPDATED:
Charles Chan278ce832017-06-26 15:25:09 -07001330 log.info("Segment Routing Config updated for {}", event.subject());
Charles Chan27fe1a52017-10-20 16:06:55 -07001331 createOrUpdateDeviceConfiguration();
Charles Chan278ce832017-06-26 15:25:09 -07001332 // TODO support dynamic configuration
1333 break;
1334 default:
1335 break;
1336 }
1337 } else if (event.configClass().equals(InterfaceConfig.class)) {
1338 switch (event.type()) {
1339 case CONFIG_ADDED:
1340 log.info("Interface Config added for {}", event.subject());
1341 configureNetwork();
1342 break;
1343 case CONFIG_UPDATED:
1344 log.info("Interface Config updated for {}", event.subject());
Charles Chan27fe1a52017-10-20 16:06:55 -07001345 createOrUpdateDeviceConfiguration();
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001346
1347 // Following code will be uncommented when [CORD-634] is fully implemented.
1348 // [CORD-634] Add dynamic config support for interfaces
1349 updateInterface((InterfaceConfig) event.config().get(),
1350 (InterfaceConfig) event.prevConfig().get());
Charles Chan5270ed02016-01-30 23:22:37 -08001351 // TODO support dynamic configuration
1352 break;
1353 default:
1354 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001355 }
Charles Chan5270ed02016-01-30 23:22:37 -08001356 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001357 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001358 switch (event.type()) {
1359 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001360 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001361 break;
1362 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001363 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001364 break;
1365 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001366 appCfgHandler.processAppConfigRemoved(event);
1367 break;
1368 default:
1369 break;
1370 }
Charles Chan03a73e02016-10-24 14:52:01 -07001371 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001372 } else if (event.configClass().equals(XConnectConfig.class)) {
1373 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1374 switch (event.type()) {
1375 case CONFIG_ADDED:
1376 xConnectHandler.processXConnectConfigAdded(event);
1377 break;
1378 case CONFIG_UPDATED:
1379 xConnectHandler.processXConnectConfigUpdated(event);
1380 break;
1381 case CONFIG_REMOVED:
1382 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001383 break;
1384 default:
1385 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001386 }
Pier Ventref34966c2016-11-07 16:21:04 -08001387 } else if (event.configClass().equals(PwaasConfig.class)) {
1388 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1389 switch (event.type()) {
1390 case CONFIG_ADDED:
1391 l2TunnelHandler.processPwaasConfigAdded(event);
1392 break;
1393 case CONFIG_UPDATED:
1394 l2TunnelHandler.processPwaasConfigUpdated(event);
1395 break;
1396 case CONFIG_REMOVED:
1397 l2TunnelHandler.processPwaasConfigRemoved(event);
1398 break;
1399 default:
1400 break;
1401 }
Charles Chand6832882015-10-05 17:50:33 -07001402 }
1403 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001404
Charles Chan50443e82018-01-03 16:26:32 -08001405 @Override
1406 public boolean isRelevant(NetworkConfigEvent event) {
1407 if (event.configClass().equals(SegmentRoutingDeviceConfig.class) ||
1408 event.configClass().equals(SegmentRoutingAppConfig.class) ||
1409 event.configClass().equals(InterfaceConfig.class) ||
1410 event.configClass().equals(XConnectConfig.class) ||
1411 event.configClass().equals(PwaasConfig.class)) {
1412 return true;
1413 }
1414 log.debug("Ignore irrelevant event class {}", event.configClass().getName());
1415 return false;
1416 }
1417
Saurav Das7bcbe702017-06-13 15:35:54 -07001418 private final class ConfigChange implements Runnable {
1419 @Override
1420 public void run() {
1421 programmingScheduled.set(false);
1422 for (Device device : deviceService.getDevices()) {
1423 processDeviceAdded(device);
1424 }
1425 defaultRoutingHandler.startPopulationProcess();
1426 }
1427 }
Charles Chand6832882015-10-05 17:50:33 -07001428 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001429
1430 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001431 @Override
1432 public void event(HostEvent event) {
Charles Chan68aa62d2015-11-09 16:37:23 -08001433 switch (event.type()) {
1434 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001435 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001436 break;
1437 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001438 hostHandler.processHostMovedEvent(event);
Charles Chan2fde6d42017-08-23 14:46:43 -07001439 routeHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001440 break;
1441 case HOST_REMOVED:
Charles Chanf9a52702017-06-16 15:19:24 -07001442 hostHandler.processHostRemovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001443 break;
1444 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001445 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001446 break;
1447 default:
1448 log.warn("Unsupported host event type: {}", event.type());
1449 break;
1450 }
1451 }
1452 }
1453
Charles Chand55e84d2016-03-30 17:54:24 -07001454 private class InternalMcastListener implements McastListener {
1455 @Override
1456 public void event(McastEvent event) {
1457 switch (event.type()) {
1458 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001459 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001460 break;
1461 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001462 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001463 break;
1464 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001465 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001466 break;
1467 case ROUTE_ADDED:
1468 case ROUTE_REMOVED:
1469 default:
1470 break;
1471 }
1472 }
1473 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001474
Charles Chan03a73e02016-10-24 14:52:01 -07001475 private class InternalRouteEventListener implements RouteListener {
1476 @Override
1477 public void event(RouteEvent event) {
1478 switch (event.type()) {
1479 case ROUTE_ADDED:
1480 routeHandler.processRouteAdded(event);
1481 break;
1482 case ROUTE_UPDATED:
1483 routeHandler.processRouteUpdated(event);
1484 break;
1485 case ROUTE_REMOVED:
1486 routeHandler.processRouteRemoved(event);
1487 break;
Charles Chan2fde6d42017-08-23 14:46:43 -07001488 case ALTERNATIVE_ROUTES_CHANGED:
1489 routeHandler.processAlternativeRoutesChanged(event);
1490 break;
Charles Chan03a73e02016-10-24 14:52:01 -07001491 default:
1492 break;
1493 }
1494 }
1495 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001496
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001497 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1498 try {
1499 Set<Interface> intfs = conf.getInterfaces();
1500 Set<Interface> prevIntfs = prevConf.getInterfaces();
1501
1502 // Now we only handle one interface config at each port.
1503 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1504 log.warn("Interface update aborted - one at a time is allowed, " +
1505 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1506 return;
1507 }
1508
1509 Interface intf = intfs.stream().findFirst().get();
1510 Interface prevIntf = prevIntfs.stream().findFirst().get();
1511
1512 DeviceId deviceId = intf.connectPoint().deviceId();
1513 PortNumber portNum = intf.connectPoint().port();
1514
1515 if (!mastershipService.isLocalMaster(deviceId)) {
1516 log.debug("CONFIG_UPDATED event for interfaces should be " +
1517 "handled by master node for device {}", deviceId);
1518 return;
1519 }
1520
1521 removeSubnetConfig(prevIntf.connectPoint(),
1522 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1523 new HashSet<>(intf.ipAddressesList())));
1524
1525 if (prevIntf.vlanNative() != VlanId.NONE && !intf.vlanNative().equals(prevIntf.vlanNative())) {
1526 // RemoveVlanNative
1527 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1528 }
1529
1530 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1531 // RemoveVlanTagged
1532 prevIntf.vlanTagged().stream().filter(i -> !intf.vlanTagged().contains(i)).forEach(
1533 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, false)
1534 );
1535 }
1536
1537 if (prevIntf.vlanUntagged() != VlanId.NONE && !intf.vlanUntagged().equals(prevIntf.vlanUntagged())) {
1538 // RemoveVlanUntagged
1539 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1540 }
1541
1542 if (intf.vlanNative() != VlanId.NONE && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1543 // AddVlanNative
1544 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1545 }
1546
1547 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1548 // AddVlanTagged
1549 intf.vlanTagged().stream().filter(i -> !prevIntf.vlanTagged().contains(i)).forEach(
1550 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, true)
1551 );
1552 }
1553
1554 if (intf.vlanUntagged() != VlanId.NONE && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())) {
1555 // AddVlanUntagged
1556 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1557 }
1558 addSubnetConfig(prevIntf.connectPoint(),
1559 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1560 new HashSet<>(prevIntf.ipAddressesList())));
1561 } catch (ConfigException e) {
1562 log.error("Error in configuration");
1563 }
1564 }
1565
1566 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1567 VlanId vlanId, boolean pushVlan, boolean install) {
1568 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1569 if (grpHandler == null) {
1570 log.warn("Failed to retrieve group handler for device {}", deviceId);
1571 return;
1572 }
1573
1574 // Update filtering objective for a single port
1575 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1576
1577 // Update filtering objective for multicast ingress port
1578 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1579
1580 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1581
1582 if (nextId != -1 && !install) {
1583 // Update next objective for a single port as an output port
1584 // Remove a single port from L2FG
1585 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1586 // Remove L2 Bridging rule and L3 Unicast rule to the host
1587 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1588 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1589 // only if there is no port configured on that VLAN ID
1590 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1591 // Remove broadcast forwarding rule for the VLAN
1592 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1593 // Remove L2FG for VLAN
1594 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1595 } else {
1596 // Remove L2IG of the port
1597 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1598 }
1599 } else if (install) {
1600 if (nextId != -1) {
1601 // Add a single port to L2FG
1602 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1603 } else {
1604 // Create L2FG for VLAN
1605 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1606 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1607 }
1608 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1609 } else {
1610 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1611 }
1612 }
1613
1614 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1615 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1616 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1617
1618 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1619 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1620 .filter(intf -> !intf.connectPoint().equals(cp))
1621 .flatMap(intf -> intf.ipAddressesList().stream())
1622 .collect(Collectors.toSet());
1623 // 1. Partial subnet population
1624 // Remove routing rules for removed subnet from previous configuration,
1625 // which does not also exist in other interfaces in the same device
1626 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1627 .map(InterfaceIpAddress::subnetAddress)
1628 .collect(Collectors.toSet());
1629
1630 defaultRoutingHandler.revokeSubnet(
1631 ipPrefixSet.stream()
1632 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1633 .collect(Collectors.toSet()));
1634
1635 // 2. Interface IP punts
1636 // Remove IP punts for old Intf address
1637 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1638 .map(InterfaceIpAddress::ipAddress)
1639 .collect(Collectors.toSet());
1640 ipAddressSet.stream()
1641 .map(InterfaceIpAddress::ipAddress)
1642 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1643 .forEach(interfaceIpAddress ->
1644 routingRulePopulator.revokeSingleIpPunts(
1645 cp.deviceId(), interfaceIpAddress));
1646
1647 // 3. Host unicast routing rule
1648 // Remove unicast routing rule
1649 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1650 }
1651
1652 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1653 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1654 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1655
1656 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1657 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1658 .filter(intf -> !intf.connectPoint().equals(cp))
1659 .flatMap(intf -> intf.ipAddressesList().stream())
1660 .collect(Collectors.toSet());
1661 // 1. Partial subnet population
1662 // Add routing rules for newly added subnet, which does not also exist in
1663 // other interfaces in the same device
1664 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1665 .map(InterfaceIpAddress::subnetAddress)
1666 .collect(Collectors.toSet());
1667
1668 defaultRoutingHandler.populateSubnet(
1669 Collections.singleton(cp),
1670 ipPrefixSet.stream()
1671 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1672 .collect(Collectors.toSet()));
1673
1674 // 2. Interface IP punts
1675 // Add IP punts for new Intf address
1676 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1677 .map(InterfaceIpAddress::ipAddress)
1678 .collect(Collectors.toSet());
1679 ipAddressSet.stream()
1680 .map(InterfaceIpAddress::ipAddress)
1681 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1682 .forEach(interfaceIpAddress ->
1683 routingRulePopulator.populateSingleIpPunts(
1684 cp.deviceId(), interfaceIpAddress));
1685
1686 // 3. Host unicast routing rule
1687 // Add unicast routing rule
1688 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1689 }
sanghob35a6192015-04-01 13:05:26 -07001690}