blob: 4610f4afb04bfbfa83087b44275ae530b50bdaa2 [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;
sanghob35a6192015-04-01 13:05:26 -070043import org.onosproject.core.ApplicationId;
44import org.onosproject.core.CoreService;
45import org.onosproject.event.Event;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070046import org.onosproject.mastership.MastershipService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080047import org.onosproject.net.ConnectPoint;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070048import org.onosproject.net.Device;
49import org.onosproject.net.DeviceId;
Charles Chan9640c812017-08-23 13:55:39 -070050import org.onosproject.net.Host;
51import org.onosproject.net.HostId;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070052import org.onosproject.net.Link;
53import org.onosproject.net.Port;
Charles Chan68aa62d2015-11-09 16:37:23 -080054import org.onosproject.net.PortNumber;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070055import org.onosproject.net.config.ConfigException;
Charles Chand6832882015-10-05 17:50:33 -070056import org.onosproject.net.config.ConfigFactory;
57import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070058import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070059import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070060import org.onosproject.net.config.basics.InterfaceConfig;
61import org.onosproject.net.config.basics.McastConfig;
Charles Chand6832882015-10-05 17:50:33 -070062import org.onosproject.net.config.basics.SubjectFactories;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070063import org.onosproject.net.device.DeviceEvent;
64import org.onosproject.net.device.DeviceListener;
65import org.onosproject.net.device.DeviceService;
Charles Chan68aa62d2015-11-09 16:37:23 -080066import org.onosproject.net.flow.TrafficSelector;
67import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070068import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan68aa62d2015-11-09 16:37:23 -080069import org.onosproject.net.host.HostEvent;
70import org.onosproject.net.host.HostListener;
Charles Chan47933752017-11-30 15:37:50 -080071import org.onosproject.net.host.HostLocationProbingService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080072import org.onosproject.net.host.HostService;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070073import org.onosproject.net.host.InterfaceIpAddress;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070074import org.onosproject.net.intf.Interface;
75import org.onosproject.net.intf.InterfaceService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080076import org.onosproject.net.link.LinkEvent;
77import org.onosproject.net.link.LinkListener;
78import org.onosproject.net.link.LinkService;
Charles Chand55e84d2016-03-30 17:54:24 -070079import org.onosproject.net.mcast.McastEvent;
80import org.onosproject.net.mcast.McastListener;
81import org.onosproject.net.mcast.MulticastRouteService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070082import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080083import org.onosproject.net.packet.InboundPacket;
84import org.onosproject.net.packet.PacketContext;
85import org.onosproject.net.packet.PacketProcessor;
86import org.onosproject.net.packet.PacketService;
Charles Chand55e84d2016-03-30 17:54:24 -070087import org.onosproject.net.topology.TopologyService;
Charles Chan9640c812017-08-23 13:55:39 -070088import org.onosproject.routeservice.ResolvedRoute;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070089import org.onosproject.routeservice.RouteEvent;
90import org.onosproject.routeservice.RouteListener;
91import org.onosproject.routeservice.RouteService;
Charles Chand55e84d2016-03-30 17:54:24 -070092import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
93import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventref34966c2016-11-07 16:21:04 -080094import org.onosproject.segmentrouting.config.PwaasConfig;
Pier Ventref34966c2016-11-07 16:21:04 -080095import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070096import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -070097import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -070098import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -070099import org.onosproject.segmentrouting.grouphandler.DestinationSet;
100import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700101import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
102import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700103import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700104import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Charles Chand2990362016-04-18 13:44:03 -0700105import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -0800106import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -0700107import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700108import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700109import org.onosproject.store.service.EventuallyConsistentMap;
110import org.onosproject.store.service.EventuallyConsistentMapBuilder;
111import org.onosproject.store.service.StorageService;
112import org.onosproject.store.service.WallClockTimestamp;
Charles Chan47933752017-11-30 15:37:50 -0800113import org.osgi.service.component.ComponentContext;
sanghob35a6192015-04-01 13:05:26 -0700114import org.slf4j.Logger;
115import org.slf4j.LoggerFactory;
116
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800117import java.util.Collections;
Charles Chan47933752017-11-30 15:37:50 -0800118import java.util.Dictionary;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800119import java.util.HashSet;
120import java.util.List;
121import java.util.Map;
122import java.util.Map.Entry;
123import java.util.Optional;
124import java.util.Set;
125import java.util.concurrent.ConcurrentHashMap;
126import java.util.concurrent.ConcurrentLinkedQueue;
127import java.util.concurrent.Executors;
128import java.util.concurrent.ScheduledExecutorService;
129import java.util.concurrent.ScheduledFuture;
130import java.util.concurrent.TimeUnit;
131import java.util.concurrent.atomic.AtomicBoolean;
132import java.util.stream.Collectors;
sanghob35a6192015-04-01 13:05:26 -0700133
Charles Chan3e783d02016-02-26 22:19:52 -0800134import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800135import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700136import static org.onlab.util.Tools.groupedThreads;
Charles Chan3e783d02016-02-26 22:19:52 -0800137
Charles Chane849c192016-01-11 18:28:54 -0800138/**
139 * Segment routing manager.
140 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700141@Service
142@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700143public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700144
Charles Chan2c15aca2016-11-09 20:51:44 -0800145 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan2fde6d42017-08-23 14:46:43 -0700146 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sanghob35a6192015-04-01 13:05:26 -0700147
148 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700149 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700150
151 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre10bd8d12016-11-26 21:05:22 -0800152 private NeighbourResolutionService neighbourResolutionService;
153
154 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700155 CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700156
157 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700158 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700159
160 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700161 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700162
163 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan47933752017-11-30 15:37:50 -0800164 HostLocationProbingService probingService;
165
166 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700167 DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700168
169 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800170 public FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700171
172 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700173 LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700174
Charles Chan5270ed02016-01-30 23:22:37 -0800175 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800176 public MastershipService mastershipService;
Charles Chan03a73e02016-10-24 14:52:01 -0700177
178 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800179 public StorageService storageService;
Charles Chan03a73e02016-10-24 14:52:01 -0700180
181 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
182 MulticastRouteService multicastRouteService;
183
184 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chanf237e1b2018-01-09 13:45:07 -0800185 public TopologyService topologyService;
Charles Chan03a73e02016-10-24 14:52:01 -0700186
187 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700188 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800189
190 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800191 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800192
Saurav Das80980c72016-03-23 11:22:49 -0700193 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800194 public InterfaceService interfaceService;
195
Charles Chan47933752017-11-30 15:37:50 -0800196 @Property(name = "activeProbing", boolValue = true,
197 label = "Enable active probing to discover dual-homed hosts.")
198 boolean activeProbing = true;
199
Charles Chan03a73e02016-10-24 14:52:01 -0700200 ArpHandler arpHandler = null;
201 IcmpHandler icmpHandler = null;
202 IpHandler ipHandler = null;
203 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700204 ApplicationId appId;
205 DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700206
Charles Chan03a73e02016-10-24 14:52:01 -0700207 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700208 private TunnelHandler tunnelHandler = null;
209 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700210 private InternalPacketProcessor processor = null;
211 private InternalLinkListener linkListener = null;
212 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700213 private AppConfigHandler appCfgHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700214 XConnectHandler xConnectHandler = null;
Charles Chand2990362016-04-18 13:44:03 -0700215 private McastHandler mcastHandler = null;
Charles Chan65238242017-06-22 18:03:14 -0700216 private HostHandler hostHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800217 private RouteHandler routeHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800218 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Pier Ventref34966c2016-11-07 16:21:04 -0800219 private L2TunnelHandler l2TunnelHandler = null;
sanghob35a6192015-04-01 13:05:26 -0700220 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan5270ed02016-01-30 23:22:37 -0800221 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700222 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
223 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700224 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sanghob35a6192015-04-01 13:05:26 -0700225
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700226 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700227 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sanghob35a6192015-04-01 13:05:26 -0700228
Saurav Das4ce45962015-11-24 23:21:05 -0800229 @SuppressWarnings("unused")
sanghob35a6192015-04-01 13:05:26 -0700230 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800231 @SuppressWarnings("rawtypes")
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700232 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Charles Chan68aa62d2015-11-09 16:37:23 -0800233 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chane849c192016-01-11 18:28:54 -0800234 new ConcurrentHashMap<>();
235 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700236 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700237 * Used to keep track on MPLS group information.
Charles Chane849c192016-01-11 18:28:54 -0800238 */
Charles Chan47933752017-11-30 15:37:50 -0800239 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das7bcbe702017-06-13 15:35:54 -0700240 dsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800241 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700242 * Per device next objective ID store with (device id + vlanid) as key.
243 * Used to keep track on L2 flood group information.
Charles Chane849c192016-01-11 18:28:54 -0800244 */
Charles Chan47933752017-11-30 15:37:50 -0800245 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan59cc16d2017-02-02 16:20:42 -0800246 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800247 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700248 * Per device next objective ID store with (device id + port + treatment + meta) as key.
249 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chane849c192016-01-11 18:28:54 -0800250 */
Charles Chan47933752017-11-30 15:37:50 -0800251 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800252 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800253
Saurav Dasc88d4662017-05-15 15:34:25 -0700254 // Local store for all links seen and their present status, used for
255 // optimized routing. The existence of the link in the keys is enough to know
256 // if the link has been "seen-before" by this instance of the controller.
257 // The boolean value indicates if the link is currently up or not.
258 // XXX Currently the optimized routing logic depends on "forgetting" a link
259 // when a switch goes down, but "remembering" it when only the link goes down.
260 // Consider changing this logic so we can use the Link Service instead of
261 // a local cache.
262 private Map<Link, Boolean> seenLinks = new ConcurrentHashMap<>();
263
Saurav Das4ce45962015-11-24 23:21:05 -0800264 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
265 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700266
Saurav Das7bcbe702017-06-13 15:35:54 -0700267 private AtomicBoolean programmingScheduled = new AtomicBoolean();
268
Charles Chand55e84d2016-03-30 17:54:24 -0700269 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700270 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
271 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700272 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700273 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800274 public SegmentRoutingDeviceConfig createConfig() {
275 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700276 }
277 };
Pier Ventref34966c2016-11-07 16:21:04 -0800278
Charles Chand55e84d2016-03-30 17:54:24 -0700279 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700280 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
281 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700282 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800283 @Override
284 public SegmentRoutingAppConfig createConfig() {
285 return new SegmentRoutingAppConfig();
286 }
287 };
Pier Ventref34966c2016-11-07 16:21:04 -0800288
Charles Chanfc5c7802016-05-17 13:13:55 -0700289 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
290 new ConfigFactory<ApplicationId, XConnectConfig>(
291 SubjectFactories.APP_SUBJECT_FACTORY,
292 XConnectConfig.class, "xconnect") {
293 @Override
294 public XConnectConfig createConfig() {
295 return new XConnectConfig();
296 }
297 };
Pier Ventref34966c2016-11-07 16:21:04 -0800298
Charles Chand55e84d2016-03-30 17:54:24 -0700299 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700300 new ConfigFactory<ApplicationId, McastConfig>(
301 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700302 McastConfig.class, "multicast") {
303 @Override
304 public McastConfig createConfig() {
305 return new McastConfig();
306 }
307 };
308
Pier Ventref34966c2016-11-07 16:21:04 -0800309 private final ConfigFactory<ApplicationId, PwaasConfig> pwaasConfigFactory =
310 new ConfigFactory<ApplicationId, PwaasConfig>(
311 SubjectFactories.APP_SUBJECT_FACTORY,
312 PwaasConfig.class, "pwaas") {
313 @Override
314 public PwaasConfig createConfig() {
315 return new PwaasConfig();
316 }
317 };
318
Charles Chan65238242017-06-22 18:03:14 -0700319 private static final Object THREAD_SCHED_LOCK = new Object();
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700320 private static int numOfEventsQueued = 0;
321 private static int numOfEventsExecuted = 0;
sanghob35a6192015-04-01 13:05:26 -0700322 private static int numOfHandlerExecution = 0;
323 private static int numOfHandlerScheduled = 0;
324
Charles Chan116188d2016-02-18 14:22:42 -0800325 /**
326 * Segment Routing App ID.
327 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800328 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan59cc16d2017-02-02 16:20:42 -0800329
Charles Chane849c192016-01-11 18:28:54 -0800330 /**
331 * The default VLAN ID assigned to the interfaces without subnet config.
332 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800333 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700334
sanghob35a6192015-04-01 13:05:26 -0700335 @Activate
Charles Chan47933752017-11-30 15:37:50 -0800336 protected void activate(ComponentContext context) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800337 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700338
339 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das7bcbe702017-06-13 15:35:54 -0700340 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700341 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700342 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700343 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700344 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700345 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700346 .build();
Saurav Das7bcbe702017-06-13 15:35:54 -0700347 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700348
Charles Chan59cc16d2017-02-02 16:20:42 -0800349 log.debug("Creating EC map vlannextobjectivestore");
350 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
351 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
352 vlanNextObjStore = vlanNextObjMapBuilder
353 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700354 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700355 .withTimestampProvider((k, v) -> new WallClockTimestamp())
356 .build();
357
Saurav Das4ce45962015-11-24 23:21:05 -0800358 log.debug("Creating EC map subnetnextobjectivestore");
359 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
360 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
361 portNextObjStore = portNextObjMapBuilder
362 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700363 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800364 .withTimestampProvider((k, v) -> new WallClockTimestamp())
365 .build();
366
sangho0b2b6d12015-05-20 22:16:38 -0700367 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
368 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700369 tunnelStore = tunnelMapBuilder
370 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700371 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700372 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700373 .build();
374
375 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
376 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700377 policyStore = policyMapBuilder
378 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700379 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700380 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700381 .build();
382
Saurav Das80980c72016-03-23 11:22:49 -0700383 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800384 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700385 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800386 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800387 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
388 "requestInterceptsEnabled", "false");
Charles Chand3baaba2017-08-08 15:13:37 -0700389 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800390 "requestInterceptsEnabled", "false");
Charles Chanc760f382017-07-24 15:56:10 -0700391 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800392 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800393 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
394 "greedyLearningIpv6", "true");
Charles Chanc6d227e2017-02-28 15:15:17 -0800395 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
396 "forceUnprovision", "true");
Charles Chanef624ed2017-08-10 16:57:28 -0700397 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan73316522017-07-20 16:16:25 -0700398 "distributed", "true");
Charles Chan35a32322017-08-14 11:42:11 -0700399 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
400 "multihomingEnabled", "true");
Charles Chan807d87a2017-11-29 19:54:20 -0800401 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
402 "staleLinkAge", "15000");
403 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
404 "allowDuplicateIps", "false");
Charles Chan47933752017-11-30 15:37:50 -0800405 compCfgService.registerProperties(getClass());
406 modified(context);
Saurav Das80980c72016-03-23 11:22:49 -0700407
Charles Chanb8e10c82015-10-14 11:24:40 -0700408 processor = new InternalPacketProcessor();
409 linkListener = new InternalLinkListener();
410 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700411 appCfgHandler = new AppConfigHandler(this);
412 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700413 mcastHandler = new McastHandler(this);
414 hostHandler = new HostHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700415 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800416 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Pier Ventref34966c2016-11-07 16:21:04 -0800417 l2TunnelHandler = new L2TunnelHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700418
Charles Chan3e783d02016-02-26 22:19:52 -0800419 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700420 cfgService.registerConfigFactory(deviceConfigFactory);
421 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700422 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700423 cfgService.registerConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800424 cfgService.registerConfigFactory(pwaasConfigFactory);
Charles Chan132393a2018-01-04 14:26:07 -0800425
426 cfgListener.configureNetwork();
427
Charles Chan5270ed02016-01-30 23:22:37 -0800428 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700429 packetService.addProcessor(processor, PacketProcessor.director(2));
430 linkService.addListener(linkListener);
431 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700432 multicastRouteService.addListener(mcastListener);
Charles Chanc7a8a682017-06-19 00:43:31 -0700433 routeService.addListener(routeListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700434
Andreas Pantelopoulos4a768c02018-01-11 07:53:48 -0800435 l2TunnelHandler.init();
436
sanghob35a6192015-04-01 13:05:26 -0700437 log.info("Started");
438 }
439
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700440 private KryoNamespace.Builder createSerializer() {
441 return new KryoNamespace.Builder()
442 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700443 .register(DestinationSetNextObjectiveStoreKey.class,
Charles Chan59cc16d2017-02-02 16:20:42 -0800444 VlanNextObjectiveStoreKey.class,
Saurav Das7bcbe702017-06-13 15:35:54 -0700445 DestinationSet.class,
446 NextNeighbors.class,
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700447 Tunnel.class,
448 DefaultTunnel.class,
449 Policy.class,
450 TunnelPolicy.class,
451 Policy.Type.class,
452 PortNextObjectiveStoreKey.class,
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700453 XConnectStoreKey.class,
454 DefaultL2Tunnel.class,
455 DefaultL2TunnelPolicy.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700456 );
457 }
458
sanghob35a6192015-04-01 13:05:26 -0700459 @Deactivate
460 protected void deactivate() {
Charles Chand6832882015-10-05 17:50:33 -0700461 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700462 cfgService.unregisterConfigFactory(deviceConfigFactory);
463 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700464 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700465 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800466 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chan47933752017-11-30 15:37:50 -0800467 compCfgService.unregisterProperties(getClass(), false);
Charles Chand6832882015-10-05 17:50:33 -0700468
Charles Chanc7a8a682017-06-19 00:43:31 -0700469 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700470 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700471 linkService.removeListener(linkListener);
472 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700473 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700474 routeService.removeListener(routeListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700475
Charles Chan0aa674e2017-02-23 15:44:08 -0800476 neighbourResolutionService.unregisterNeighbourHandlers(appId);
477
sanghob35a6192015-04-01 13:05:26 -0700478 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700479 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700480 deviceListener = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700481 groupHandlerMap.clear();
482
Saurav Das7bcbe702017-06-13 15:35:54 -0700483 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800484 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700485 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700486 tunnelStore.destroy();
487 policyStore.destroy();
sanghob35a6192015-04-01 13:05:26 -0700488 log.info("Stopped");
489 }
490
Charles Chan47933752017-11-30 15:37:50 -0800491 @Modified
492 private void modified(ComponentContext context) {
493 Dictionary<?, ?> properties = context.getProperties();
494 if (properties == null) {
495 return;
496 }
497
498 String strActiveProving = Tools.get(properties, "activeProbing");
499 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
500
501 if (expectActiveProbing != activeProbing) {
502 activeProbing = expectActiveProbing;
503 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
504 }
505 }
506
sangho1e575652015-05-14 00:39:53 -0700507 @Override
508 public List<Tunnel> getTunnels() {
509 return tunnelHandler.getTunnels();
510 }
511
512 @Override
sangho71abe1b2015-06-29 14:58:47 -0700513 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
514 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700515 }
516
517 @Override
sangho71abe1b2015-06-29 14:58:47 -0700518 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700519 for (Policy policy: policyHandler.getPolicies()) {
520 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
521 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
522 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
523 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700524 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700525 }
526 }
527 }
sangho71abe1b2015-06-29 14:58:47 -0700528 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700529 }
530
531 @Override
sangho71abe1b2015-06-29 14:58:47 -0700532 public PolicyHandler.Result removePolicy(Policy policy) {
533 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700534 }
535
536 @Override
sangho71abe1b2015-06-29 14:58:47 -0700537 public PolicyHandler.Result createPolicy(Policy policy) {
538 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700539 }
540
541 @Override
542 public List<Policy> getPolicies() {
543 return policyHandler.getPolicies();
544 }
545
Saurav Das59232cf2016-04-27 18:35:50 -0700546 @Override
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700547 public List<DefaultL2Tunnel> getL2Tunnels() {
548 return l2TunnelHandler.getL2Tunnels();
549 }
550
551 @Override
552 public List<DefaultL2TunnelPolicy> getL2Policies() {
553 return l2TunnelHandler.getL2Policies();
554 }
555
556 @Override
557 public L2TunnelHandler.Result addPseudowire(String tunnelId, String pwLabel, String cP1,
558 String cP1InnerVlan, String cP1OuterVlan, String cP2,
559 String cP2InnerVlan, String cP2OuterVlan,
560 String mode, String sdTag) {
Andreas Pantelopoulos20474e02017-12-20 18:04:27 -0800561 // Try to inject an empty Pwaas config if it is not found for the first time
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700562 PwaasConfig config = cfgService.getConfig(appId(), PwaasConfig.class);
563 if (config == null) {
Andreas Pantelopoulos20474e02017-12-20 18:04:27 -0800564 log.debug("Pwaas config not found. Try to create an empty one.");
565 cfgService.applyConfig(appId(), PwaasConfig.class, new ObjectMapper().createObjectNode());
566 config = cfgService.getConfig(appId(), PwaasConfig.class);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700567 }
568
569 ObjectNode object = config.addPseudowire(tunnelId, pwLabel,
570 cP1, cP1InnerVlan, cP1OuterVlan,
571 cP2, cP2InnerVlan, cP2OuterVlan,
572 mode, sdTag);
573 if (object == null) {
574 log.warn("Could not add pseudowire to the configuration!");
575 return L2TunnelHandler.Result.ADDITION_ERROR;
576 }
577
578 // inform everyone about the valid change in the pw configuration
579 cfgService.applyConfig(appId(), PwaasConfig.class, object);
580 return L2TunnelHandler.Result.SUCCESS;
581 }
582
583 @Override
584 public L2TunnelHandler.Result removePseudowire(String pwId) {
585
586 PwaasConfig config = cfgService.getConfig(appId(), PwaasConfig.class);
587 if (config == null) {
588 log.warn("Configuration for Pwaas class could not be found!");
589 return L2TunnelHandler.Result.CONFIG_NOT_FOUND;
590 }
591
592 ObjectNode object = config.removePseudowire(pwId);
593 if (object == null) {
594 log.warn("Could not delete pseudowire from configuration!");
595 return L2TunnelHandler.Result.REMOVAL_ERROR;
596 }
597
598 // sanity check, this should never fail since we removed a pw
599 // and we always check when we update the configuration
600 config.isValid();
601
602 // inform everyone
603 cfgService.applyConfig(appId(), PwaasConfig.class, object);
604
605 return L2TunnelHandler.Result.SUCCESS;
606 }
607
608 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700609 public void rerouteNetwork() {
610 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700611 }
612
Charles Chanc81c45b2016-10-20 17:02:44 -0700613 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800614 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
615 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800616 deviceConfiguration.getRouters().forEach(device ->
617 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chanc81c45b2016-10-20 17:02:44 -0700618 return deviceSubnetMap;
619 }
620
Saurav Dasc88d4662017-05-15 15:34:25 -0700621
622 @Override
623 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
624 if (defaultRoutingHandler != null) {
625 return defaultRoutingHandler.getCurrentEmcpSpgMap();
626 } else {
627 return null;
628 }
629 }
630
631 @Override
Saurav Das7bcbe702017-06-13 15:35:54 -0700632 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
633 if (dsNextObjStore != null) {
634 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700635 } else {
636 return ImmutableMap.of();
637 }
638 }
639
Saurav Dasceccf242017-08-03 18:30:35 -0700640 @Override
641 public void verifyGroups(DeviceId id) {
642 DefaultGroupHandler gh = groupHandlerMap.get(id);
643 if (gh != null) {
644 gh.triggerBucketCorrector();
645 }
646 }
647
sanghof9d0bf12015-05-19 11:57:42 -0700648 /**
Ray Milkeye4afdb52017-04-05 09:42:04 -0700649 * Extracts the application ID from the manager.
650 *
651 * @return application ID
652 */
653 public ApplicationId appId() {
654 return appId;
655 }
656
657 /**
658 * Returns the device configuration.
659 *
660 * @return device configuration
661 */
662 public DeviceConfiguration deviceConfiguration() {
663 return deviceConfiguration;
664 }
665
666 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700667 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700668 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700669 *
670 * @return next objective ID store
671 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700672 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
673 dsNextObjStore() {
674 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700675 }
676
677 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700678 * Per device next objective ID store with (device id + vlanid) as key.
679 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700680 *
681 * @return vlan next object store
682 */
683 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
684 return vlanNextObjStore;
685 }
686
687 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700688 * Per device next objective ID store with (device id + port + treatment + meta) as key.
689 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700690 *
691 * @return port next object store.
692 */
693 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
694 return portNextObjStore;
695 }
696
697 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700698 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
699 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -0700700 *
701 * @return MPLS-ECMP value
702 */
703 public boolean getMplsEcmp() {
704 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
705 .getConfig(this.appId, SegmentRoutingAppConfig.class);
706 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
707 }
708
709 /**
sanghof9d0bf12015-05-19 11:57:42 -0700710 * Returns the tunnel object with the tunnel ID.
711 *
712 * @param tunnelId Tunnel ID
713 * @return Tunnel reference
714 */
sangho1e575652015-05-14 00:39:53 -0700715 public Tunnel getTunnel(String tunnelId) {
716 return tunnelHandler.getTunnel(tunnelId);
717 }
718
Charles Chan7ffd81f2017-02-08 15:52:08 -0800719 // TODO Consider moving these to InterfaceService
sanghob35a6192015-04-01 13:05:26 -0700720 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800721 * Returns untagged VLAN configured on given connect point.
Charles Chan7ffd81f2017-02-08 15:52:08 -0800722 * <p>
723 * Only returns the first match if there are multiple untagged VLAN configured
724 * on the connect point.
sanghob35a6192015-04-01 13:05:26 -0700725 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800726 * @param connectPoint connect point
727 * @return untagged VLAN or null if not configured
sanghob35a6192015-04-01 13:05:26 -0700728 */
Charles Chan65238242017-06-22 18:03:14 -0700729 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800730 return interfaceService.getInterfacesByPort(connectPoint).stream()
731 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
732 .map(Interface::vlanUntagged)
733 .findFirst().orElse(null);
sanghob35a6192015-04-01 13:05:26 -0700734 }
735
sangho1e575652015-05-14 00:39:53 -0700736 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800737 * Returns tagged VLAN configured on given connect point.
738 * <p>
739 * Returns all matches if there are multiple tagged VLAN configured
740 * on the connect point.
741 *
742 * @param connectPoint connect point
743 * @return tagged VLAN or empty set if not configured
744 */
Charles Chan65238242017-06-22 18:03:14 -0700745 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800746 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
747 return interfaces.stream()
748 .map(Interface::vlanTagged)
Charles Chan65238242017-06-22 18:03:14 -0700749 .flatMap(Set::stream)
Charles Chan7ffd81f2017-02-08 15:52:08 -0800750 .collect(Collectors.toSet());
751 }
752
753 /**
754 * Returns native VLAN configured on given connect point.
755 * <p>
756 * Only returns the first match if there are multiple native VLAN configured
757 * on the connect point.
758 *
759 * @param connectPoint connect point
760 * @return native VLAN or null if not configured
761 */
Charles Chan65238242017-06-22 18:03:14 -0700762 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800763 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
764 return interfaces.stream()
765 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
766 .map(Interface::vlanNative)
767 .findFirst()
768 .orElse(null);
769 }
770
771 /**
Charles Chanf9a52702017-06-16 15:19:24 -0700772 * Returns internal VLAN for untagged hosts on given connect point.
773 * <p>
774 * The internal VLAN is either vlan-untagged for an access port,
775 * or vlan-native for a trunk port.
776 *
777 * @param connectPoint connect point
778 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
779 */
Charles Chan65238242017-06-22 18:03:14 -0700780 VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chanf9a52702017-06-16 15:19:24 -0700781 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
782 VlanId nativeVlanId = getNativeVlanId(connectPoint);
783 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
784 }
785
786 /**
Charles Chan65238242017-06-22 18:03:14 -0700787 * Returns optional pair device ID of given device.
788 *
789 * @param deviceId device ID
790 * @return optional pair device ID. Might be empty if pair device is not configured
791 */
792 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
793 SegmentRoutingDeviceConfig deviceConfig =
794 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
795 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
796 }
797 /**
798 * Returns optional pair device local port of given device.
799 *
800 * @param deviceId device ID
801 * @return optional pair device ID. Might be empty if pair device is not configured
802 */
803 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
804 SegmentRoutingDeviceConfig deviceConfig =
805 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
806 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
807 }
808
809 /**
Charles Chan2fde6d42017-08-23 14:46:43 -0700810 * Determine if current instance is the master of given connect point.
811 *
812 * @param cp connect point
813 * @return true if current instance is the master of given connect point
814 */
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700815 public boolean isMasterOf(ConnectPoint cp) {
Charles Chan2fde6d42017-08-23 14:46:43 -0700816 boolean isMaster = mastershipService.isLocalMaster(cp.deviceId());
817 if (!isMaster) {
818 log.debug(NOT_MASTER, cp);
819 }
820 return isMaster;
821 }
822
823 /**
Charles Chan9640c812017-08-23 13:55:39 -0700824 * Returns locations of given resolved route.
825 *
826 * @param resolvedRoute resolved route
827 * @return locations of nexthop. Might be empty if next hop is not found
828 */
829 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
830 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
831 return Optional.ofNullable(hostService.getHost(hostId))
832 .map(Host::locations).orElse(Sets.newHashSet())
833 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
834 }
835
836 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800837 * Returns vlan port map of given device.
838 *
839 * @param deviceId device id
840 * @return vlan-port multimap
841 */
842 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
843 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
844
845 interfaceService.getInterfaces().stream()
846 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
847 .forEach(intf -> {
848 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -0700849 intf.vlanTagged().forEach(vlanTagged ->
850 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
851 );
Charles Chan7ffd81f2017-02-08 15:52:08 -0800852 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
853 });
854 vlanPortMap.removeAll(VlanId.NONE);
855
856 return vlanPortMap;
857 }
858
859 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800860 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -0800861 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700862 *
863 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -0800864 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -0800865 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700866 */
Charles Chan65238242017-06-22 18:03:14 -0700867 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -0700868 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800869 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
870 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700871 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -0800872 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -0800873 + "device {} not found", deviceId);
874 return -1;
875 }
876 }
877
878 /**
879 * Returns the next objective ID for the given portNumber, given the treatment.
880 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -0700881 * would result in different objectives. If the next object does not exist,
882 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -0800883 *
884 * @param deviceId Device ID
885 * @param portNum port number on device for which NextObjective is queried
886 * @param treatment the actions to apply on the packets (should include outport)
887 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -0700888 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -0700889 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800890 */
891 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
892 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700893 TrafficSelector meta,
894 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -0800895 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
896 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -0700897 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -0800898 } else {
Charles Chane849c192016-01-11 18:28:54 -0800899 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
900 + " not found", deviceId);
901 return -1;
902 }
903 }
904
Saurav Dasc88d4662017-05-15 15:34:25 -0700905 /**
906 * Returns the group handler object for the specified device id.
907 *
908 * @param devId the device identifier
909 * @return the groupHandler object for the device id, or null if not found
910 */
Charles Chan65238242017-06-22 18:03:14 -0700911 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700912 return groupHandlerMap.get(devId);
913 }
914
915 /**
Saurav Dasceccf242017-08-03 18:30:35 -0700916 * Returns the default routing handler object.
917 *
918 * @return the default routing handler object
919 */
920 public DefaultRoutingHandler getRoutingHandler() {
921 return defaultRoutingHandler;
922 }
923
924 /**
Saurav Dasc88d4662017-05-15 15:34:25 -0700925 * Returns true if this controller instance has seen this link before. The
926 * link may not be currently up, but as long as the link had been seen before
927 * this method will return true. The one exception is when the link was
928 * indeed seen before, but this controller instance was forced to forget it
929 * by a call to purgeSeenLink method.
930 *
931 * @param link the infrastructure link being queried
932 * @return true if this controller instance has seen this link before
933 */
Charles Chan65238242017-06-22 18:03:14 -0700934 boolean isSeenLink(Link link) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700935 return seenLinks.containsKey(link);
936 }
937
938 /**
939 * Updates the seen link store. Updates can be for links that are currently
940 * available or not.
941 *
942 * @param link the link to update in the seen-link local store
943 * @param up the status of the link, true if up, false if down
944 */
Charles Chan65238242017-06-22 18:03:14 -0700945 void updateSeenLink(Link link, boolean up) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700946 seenLinks.put(link, up);
947 }
948
949 /**
950 * Returns the status of a seen-link (up or down). If the link has not
951 * been seen-before, a null object is returned.
952 *
953 * @param link the infrastructure link being queried
954 * @return null if the link was not seen-before;
955 * true if the seen-link is up;
956 * false if the seen-link is down
957 */
Charles Chan47933752017-11-30 15:37:50 -0800958 private Boolean isSeenLinkUp(Link link) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700959 return seenLinks.get(link);
960 }
961
962 /**
963 * Makes this controller instance forget a previously seen before link.
964 *
965 * @param link the infrastructure link to purge
966 */
Charles Chan65238242017-06-22 18:03:14 -0700967 private void purgeSeenLink(Link link) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700968 seenLinks.remove(link);
969 }
970
971 /**
972 * Returns the status of a link as parallel link. A parallel link
973 * is defined as a link which has common src and dst switches as another
974 * seen-link that is currently enabled. It is not necessary for the link being
975 * queried to be a seen-link.
976 *
977 * @param link the infrastructure link being queried
978 * @return true if a seen-link exists that is up, and shares the
979 * same src and dst switches as the link being queried
980 */
Charles Chan65238242017-06-22 18:03:14 -0700981 private boolean isParallelLink(Link link) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700982 for (Entry<Link, Boolean> seen : seenLinks.entrySet()) {
983 Link seenLink = seen.getKey();
984 if (seenLink.equals(link)) {
985 continue;
986 }
987 if (seenLink.src().deviceId().equals(link.src().deviceId()) &&
988 seenLink.dst().deviceId().equals(link.dst().deviceId()) &&
989 seen.getValue()) {
990 return true;
991 }
992 }
993 return false;
994 }
995
Saurav Das7bcbe702017-06-13 15:35:54 -0700996 /**
997 * Returns true if the link being queried is a bidirectional link. A bidi
998 * link is defined as a link, whose reverse link - ie. the link in the reverse
999 * direction - has been seen-before and is up. It is not necessary for the link
1000 * being queried to be a seen-link.
1001 *
1002 * @param link the infrastructure link being queried
1003 * @return true if another unidirectional link exists in the reverse direction,
1004 * has been seen-before and is up
1005 */
Charles Chan47933752017-11-30 15:37:50 -08001006 boolean isBidirectional(Link link) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001007 Link reverseLink = linkService.getLink(link.dst(), link.src());
1008 if (reverseLink == null) {
1009 return false;
1010 }
1011 Boolean result = isSeenLinkUp(reverseLink);
1012 if (result == null) {
1013 return false;
1014 }
1015 return result.booleanValue();
1016 }
1017
1018 /**
1019 * Determines if the given link should be avoided in routing calculations
1020 * by policy or design.
1021 *
1022 * @param link the infrastructure link being queried
1023 * @return true if link should be avoided
1024 */
Charles Chan47933752017-11-30 15:37:50 -08001025 boolean avoidLink(Link link) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001026 // XXX currently only avoids all pair-links. In the future can be
1027 // extended to avoid any generic link
1028 DeviceId src = link.src().deviceId();
1029 PortNumber srcPort = link.src().port();
1030 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(src)) {
1031 log.warn("Device {} not configured..cannot avoid link {}", src, link);
1032 return false;
1033 }
1034 DeviceId pairDev;
1035 PortNumber pairLocalPort, pairRemotePort = null;
1036 try {
1037 pairDev = deviceConfiguration.getPairDeviceId(src);
1038 pairLocalPort = deviceConfiguration.getPairLocalPort(src);
1039 if (pairDev != null) {
1040 pairRemotePort = deviceConfiguration.getPairLocalPort(pairDev);
1041 }
1042 } catch (DeviceConfigNotFoundException e) {
1043 log.warn("Pair dev for dev {} not configured..cannot avoid link {}",
1044 src, link);
1045 return false;
1046 }
1047
Charles Chan47933752017-11-30 15:37:50 -08001048 return srcPort.equals(pairLocalPort) &&
Saurav Das7bcbe702017-06-13 15:35:54 -07001049 link.dst().deviceId().equals(pairDev) &&
Charles Chan47933752017-11-30 15:37:50 -08001050 link.dst().port().equals(pairRemotePort);
Saurav Das7bcbe702017-06-13 15:35:54 -07001051 }
1052
sanghob35a6192015-04-01 13:05:26 -07001053 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -07001054 @Override
1055 public void process(PacketContext context) {
1056
1057 if (context.isHandled()) {
1058 return;
1059 }
1060
1061 InboundPacket pkt = context.inPacket();
1062 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -08001063
1064 if (ethernet == null) {
1065 return;
1066 }
1067
Saurav Das7bcbe702017-06-13 15:35:54 -07001068 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1069 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001070 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001071 log.warn("Received unexpected ARP packet on {}",
1072 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -07001073 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -08001074 return;
sanghob35a6192015-04-01 13:05:26 -07001075 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -08001076 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1077 //ipHandler.addToPacketBuffer(ipv4Packet);
1078 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1079 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001080 } else {
Charles Chan50035632017-01-13 17:20:44 -08001081 // NOTE: We don't support IP learning at this moment so this
1082 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -08001083 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001084 }
Pier Ventre10bd8d12016-11-26 21:05:22 -08001085 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1086 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -08001087 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -08001088 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -08001089 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1090 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1091 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1092 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1093 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1094 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -07001095 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -07001096 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -08001097 }
1098 } else {
1099 // NOTE: We don't support IP learning at this moment so this
1100 // is not necessary. Also it causes duplication of DHCPv6 packets.
1101 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1102 }
sanghob35a6192015-04-01 13:05:26 -07001103 }
1104 }
1105 }
1106
1107 private class InternalLinkListener implements LinkListener {
1108 @Override
1109 public void event(LinkEvent event) {
Charles Chanb1f8c762017-03-29 16:39:05 -07001110 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1111 event.type() == LinkEvent.Type.LINK_UPDATED ||
1112 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001113 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -07001114 scheduleEventHandlerIfNotScheduled(event);
1115 }
1116 }
1117 }
1118
1119 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -07001120 @Override
1121 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -07001122 switch (event.type()) {
1123 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -08001124 case PORT_UPDATED:
1125 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -07001126 case DEVICE_UPDATED:
1127 case DEVICE_AVAILABILITY_CHANGED:
Saurav Dasc88d4662017-05-15 15:34:25 -07001128 log.trace("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -07001129 scheduleEventHandlerIfNotScheduled(event);
1130 break;
1131 default:
1132 }
1133 }
1134 }
1135
Saurav Das4ce45962015-11-24 23:21:05 -08001136 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -07001137 private void scheduleEventHandlerIfNotScheduled(Event event) {
Charles Chan65238242017-06-22 18:03:14 -07001138 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001139 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001140 numOfEventsQueued++;
1141
1142 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
1143 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001144 eventHandlerFuture = executorService
1145 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
1146 numOfHandlerScheduled++;
1147 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -07001148 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001149 numOfEventsQueued,
1150 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -07001151 }
sanghob35a6192015-04-01 13:05:26 -07001152 }
1153
1154 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -07001155 @Override
sanghob35a6192015-04-01 13:05:26 -07001156 public void run() {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001157 try {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001158 while (true) {
Saurav Das4ce45962015-11-24 23:21:05 -08001159 @SuppressWarnings("rawtypes")
Charles Chan65238242017-06-22 18:03:14 -07001160 Event event;
1161 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001162 if (!eventQueue.isEmpty()) {
1163 event = eventQueue.poll();
1164 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001165 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001166 numOfHandlerExecution++;
1167 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
1168 numOfHandlerExecution, numOfEventsExecuted);
1169 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001170 }
sangho20eff1d2015-04-13 15:15:58 -07001171 }
Charles Chanb1f8c762017-03-29 16:39:05 -07001172 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1173 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001174 // Note: do not update seenLinks here, otherwise every
1175 // link, even one seen for the first time, will be appear
1176 // to be a previously seen link
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001177 processLinkAdded((Link) event.subject());
1178 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre2c515312016-09-13 21:33:40 -07001179 Link linkRemoved = (Link) event.subject();
Saurav Dasc88d4662017-05-15 15:34:25 -07001180 if (linkRemoved.type() == Link.Type.DIRECT) {
1181 updateSeenLink(linkRemoved, false);
1182 }
1183 // device availability check helps to ensure that
1184 // multiple link-removed events are actually treated as a
1185 // single switch removed event. purgeSeenLink is necessary
1186 // so we do rerouting (instead of rehashing) when switch
1187 // comes back.
Pier Ventre2c515312016-09-13 21:33:40 -07001188 if (linkRemoved.src().elementId() instanceof DeviceId &&
1189 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001190 purgeSeenLink(linkRemoved);
Pier Ventre2c515312016-09-13 21:33:40 -07001191 continue;
1192 }
1193 if (linkRemoved.dst().elementId() instanceof DeviceId &&
1194 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001195 purgeSeenLink(linkRemoved);
Pier Ventre2c515312016-09-13 21:33:40 -07001196 continue;
1197 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001198 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001199 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1200 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1201 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001202 DeviceId deviceId = ((Device) event.subject()).id();
1203 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001204 log.info("Processing device event {} for available device {}",
1205 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001206 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -07001207 } else {
1208 log.info("Processing device event {} for unavailable device {}",
1209 event.type(), ((Device) event.subject()).id());
1210 processDeviceRemoved((Device) event.subject());
1211 }
Saurav Das1a129a02016-11-18 15:21:57 -08001212 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001213 // typically these calls come when device is added first time
1214 // so port filtering rules are handled at the device_added event.
1215 // port added calls represent all ports on the device,
1216 // enabled or not.
Saurav Dasc88d4662017-05-15 15:34:25 -07001217 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001218 ((DeviceEvent) event).subject().id(),
1219 ((DeviceEvent) event).port().number(),
1220 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -08001221 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001222 // these calls happen for every subsequent event
1223 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -08001224 log.info("** PORT UPDATED {}/{} -> {}",
1225 event.subject(),
1226 ((DeviceEvent) event).port(),
1227 event.type());
1228 processPortUpdated(((Device) event.subject()),
1229 ((DeviceEvent) event).port());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001230 } else {
1231 log.warn("Unhandled event type: {}", event.type());
1232 }
sanghob35a6192015-04-01 13:05:26 -07001233 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001234 } catch (Exception e) {
1235 log.error("SegmentRouting event handler "
1236 + "thread thrown an exception: {}", e);
sanghob35a6192015-04-01 13:05:26 -07001237 }
sanghob35a6192015-04-01 13:05:26 -07001238 }
1239 }
1240
sanghob35a6192015-04-01 13:05:26 -07001241 private void processLinkAdded(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001242 log.info("** LINK ADDED {}", link.toString());
Saurav Das41890db2017-12-19 16:08:42 -08001243 if (!isLinkValid(link)) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001244 return;
1245 }
Saurav Das9df5b7c2017-08-14 16:44:43 -07001246 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
1247 updateSeenLink(link, true);
1248 // XXX revisit - what about devicePortMap
1249 log.warn("Source device of this link is not configured.. "
1250 + "not processing further");
1251 return;
1252 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001253
1254 //Irrespective of whether the local is a MASTER or not for this device,
1255 //create group handler instance and push default TTP flow rules if needed,
1256 //as in a multi-instance setup, instances can initiate groups for any device.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001257 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
1258 .deviceId());
1259 if (groupHandler != null) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001260 groupHandler.portUpForLink(link);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001261 } else {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001262 // XXX revisit/cleanup
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001263 Device device = deviceService.getDevice(link.src().deviceId());
1264 if (device != null) {
1265 log.warn("processLinkAdded: Link Added "
1266 + "Notification without Device Added "
1267 + "event, still handling it");
1268 processDeviceAdded(device);
1269 groupHandler = groupHandlerMap.get(link.src()
1270 .deviceId());
Saurav Dasc88d4662017-05-15 15:34:25 -07001271 groupHandler.portUpForLink(link);
sanghob35a6192015-04-01 13:05:26 -07001272 }
1273 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001274
Saurav Das7bcbe702017-06-13 15:35:54 -07001275 /*// process link only if it is bidirectional
1276 if (!isBidirectional(link)) {
1277 log.debug("Link not bidirectional.. waiting for other direction "
1278 + "src {} --> dst {} ", link.dst(), link.src());
1279 // note that if we are not processing for routing, it should at least
1280 // be considered a seen-link
1281 updateSeenLink(link, true);
1282 return;
1283 }
1284 TO DO this ensure that rehash is still done correctly even if link is
1285 not processed for rerouting - perhaps rehash in both directions when
1286 it ultimately becomes bidi?
1287 */
1288
1289 log.debug("Starting optimized route population process for link "
1290 + "{} --> {}", link.src(), link.dst());
Saurav Dasc88d4662017-05-15 15:34:25 -07001291 boolean seenBefore = isSeenLink(link);
1292 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null, link, null);
Saurav Das7bcbe702017-06-13 15:35:54 -07001293
1294 // It's possible that linkUp causes no route-path change as ECMP graph does
1295 // not change if the link is a parallel link (same src-dst as another link.
1296 // However we still need to update ECMP hash groups to include new buckets
1297 // for the link that has come up.
Ray Milkey74e59132018-01-17 15:24:52 -08001298 if (groupHandler != null && mastershipService.isLocalMaster(link.src().deviceId())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001299 if (!seenBefore && isParallelLink(link)) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001300 // if link seen first time, we need to ensure hash-groups have all ports
Saurav Das7bcbe702017-06-13 15:35:54 -07001301 log.debug("Attempting retryHash for paralled first-time link {}", link);
Saurav Dasc88d4662017-05-15 15:34:25 -07001302 groupHandler.retryHash(link, false, true);
1303 } else {
1304 //seen before-link
1305 if (isParallelLink(link)) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001306 log.debug("Attempting retryHash for paralled seen-before "
1307 + "link {}", link);
Saurav Dasc88d4662017-05-15 15:34:25 -07001308 groupHandler.retryHash(link, false, false);
1309 }
1310 }
1311 }
Charles Chan2199c302016-04-23 17:36:10 -07001312
1313 mcastHandler.init();
sanghob35a6192015-04-01 13:05:26 -07001314 }
1315
1316 private void processLinkRemoved(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001317 log.info("** LINK REMOVED {}", link.toString());
Saurav Das41890db2017-12-19 16:08:42 -08001318 if (!isLinkValid(link)) {
1319 return;
1320 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001321 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link, null, null);
1322
1323 // update local groupHandler stores
sangho834e4b02015-05-01 09:38:25 -07001324 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
1325 if (groupHandler != null) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001326 if (mastershipService.isLocalMaster(link.src().deviceId()) &&
1327 isParallelLink(link)) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001328 log.debug("* retrying hash for parallel link removed:{}", link);
Saurav Dasc88d4662017-05-15 15:34:25 -07001329 groupHandler.retryHash(link, true, false);
Saurav Das9df5b7c2017-08-14 16:44:43 -07001330 } else {
1331 log.debug("Not attempting retry-hash for link removed: {} .. {}", link,
1332 (mastershipService.isLocalMaster(link.src().deviceId()))
1333 ? "not parallel" : "not master");
Saurav Dasc88d4662017-05-15 15:34:25 -07001334 }
1335 // ensure local stores are updated
1336 groupHandler.portDown(link.src().port());
1337 } else {
1338 log.warn("group handler not found for dev:{} when removing link: {}",
1339 link.src().deviceId(), link);
sangho834e4b02015-05-01 09:38:25 -07001340 }
Charles Chan2199c302016-04-23 17:36:10 -07001341
1342 mcastHandler.processLinkDown(link);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001343 l2TunnelHandler.processLinkDown(link);
sanghob35a6192015-04-01 13:05:26 -07001344 }
1345
Saurav Das41890db2017-12-19 16:08:42 -08001346 private boolean isLinkValid(Link link) {
1347 if (link.type() != Link.Type.DIRECT) {
1348 // NOTE: A DIRECT link might be transiently marked as INDIRECT
1349 // if BDDP is received before LLDP. We can safely ignore that
1350 // until the LLDP is received and the link is marked as DIRECT.
1351 log.info("Ignore link {}->{}. Link type is {} instead of DIRECT.",
1352 link.src(), link.dst(), link.type());
1353 return false;
1354 }
1355 DeviceId srcId = link.src().deviceId();
1356 DeviceId dstId = link.dst().deviceId();
1357 if (srcId.equals(dstId)) {
1358 log.warn("Links between ports on the same switch are not "
1359 + "allowed .. ignoring link {}", link);
1360 return false;
1361 }
1362 try {
1363 if (!deviceConfiguration.isEdgeDevice(srcId)
1364 && !deviceConfiguration.isEdgeDevice(dstId)) {
1365 // ignore links between spines
1366 // XXX revisit when handling multi-stage fabrics
1367 log.warn("Links between spines not allowed...ignoring "
1368 + "link {}", link);
1369 return false;
1370 }
1371 if (deviceConfiguration.isEdgeDevice(srcId)
1372 && deviceConfiguration.isEdgeDevice(dstId)) {
1373 // ignore links between leaves if they are not pair-links
1374 // XXX revisit if removing pair-link config or allowing more than
1375 // one pair-link
1376 if (deviceConfiguration.getPairDeviceId(srcId).equals(dstId)
1377 && deviceConfiguration.getPairLocalPort(srcId)
1378 .equals(link.src().port())
1379 && deviceConfiguration.getPairLocalPort(dstId)
1380 .equals(link.dst().port())) {
1381 // found pair link - allow it
1382 return true;
1383 } else {
1384 log.warn("Links between leaves other than pair-links are "
1385 + "not allowed...ignoring link {}", link);
1386 return false;
1387 }
1388 }
1389 } catch (DeviceConfigNotFoundException e) {
1390 // We still want to count the links in seenLinks even though there
1391 // is no config. So we let it return true
1392 log.warn("Could not check validity of link {} as subtending devices "
1393 + "are not yet configured", link);
1394 }
1395 return true;
1396 }
1397
sanghob35a6192015-04-01 13:05:26 -07001398 private void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001399 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001400
1401 // NOTE: Punt ARP/NDP even when the device is not configured.
1402 // Host learning without network config is required for CORD config generator.
1403 routingRulePopulator.populateIpPunts(device.id());
1404 routingRulePopulator.populateArpNdpPunts(device.id());
1405
Charles Chan0b4e6182015-11-03 10:42:14 -08001406 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001407 log.warn("Device configuration unavailable. Device {} will be "
1408 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001409 return;
1410 }
Charles Chan2199c302016-04-23 17:36:10 -07001411 processDeviceAddedInternal(device.id());
1412 }
1413
1414 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001415 // Irrespective of whether the local is a MASTER or not for this device,
1416 // we need to create a SR-group-handler instance. This is because in a
1417 // multi-instance setup, any instance can initiate forwarding/next-objectives
1418 // for any switch (even if this instance is a SLAVE or not even connected
1419 // to the switch). To handle this, a default-group-handler instance is necessary
1420 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001421 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1422 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001423 DefaultGroupHandler groupHandler;
1424 try {
1425 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001426 createGroupHandler(deviceId,
1427 appId,
1428 deviceConfiguration,
1429 linkService,
1430 flowObjectiveService,
1431 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001432 } catch (DeviceConfigNotFoundException e) {
1433 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1434 return;
1435 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001436 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001437 deviceId);
1438 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001439 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001440
Charles Chan2199c302016-04-23 17:36:10 -07001441 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001442 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan03a73e02016-10-24 14:52:01 -07001443 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001444 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001445 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001446 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001447 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001448 }
Charles Chan5270ed02016-01-30 23:22:37 -08001449
Charles Chan03a73e02016-10-24 14:52:01 -07001450 appCfgHandler.init(deviceId);
1451 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001452 }
1453
Saurav Das80980c72016-03-23 11:22:49 -07001454 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001455 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001456 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan47933752017-11-30 15:37:50 -08001457 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001458 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001459 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001460 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001461 portNextObjStore.entrySet().stream()
1462 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001463 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Charles Chaned3742352017-06-15 00:44:51 -07001464
1465 seenLinks.keySet().removeIf(key -> key.src().deviceId().equals(device.id()) ||
1466 key.dst().deviceId().equals(device.id()));
1467
Saurav Dasceccf242017-08-03 18:30:35 -07001468 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1469 if (gh != null) {
1470 gh.shutdown();
1471 }
Saurav Das80980c72016-03-23 11:22:49 -07001472 defaultRoutingHandler.purgeEcmpGraph(device.id());
Saurav Dasc88d4662017-05-15 15:34:25 -07001473 // Note that a switch going down is associated with all of its links
1474 // going down as well, but it is treated as a single switch down event
1475 // while the link-downs are ignored.
1476 defaultRoutingHandler
1477 .populateRoutingRulesForLinkStatusChange(null, null, device.id());
Charles Chan2199c302016-04-23 17:36:10 -07001478 mcastHandler.removeDevice(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001479 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -07001480 }
1481
Saurav Das1a129a02016-11-18 15:21:57 -08001482 private void processPortUpdated(Device device, Port port) {
1483 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1484 log.warn("Device configuration uploading. Not handling port event for"
1485 + "dev: {} port: {}", device.id(), port.number());
1486 return;
1487 }
Saurav Das018605f2017-02-18 14:05:44 -08001488
1489 if (!mastershipService.isLocalMaster(device.id())) {
1490 log.debug("Not master for dev:{} .. not handling port updated event"
1491 + "for port {}", device.id(), port.number());
1492 return;
1493 }
1494
1495 // first we handle filtering rules associated with the port
1496 if (port.isEnabled()) {
1497 log.info("Switchport {}/{} enabled..programming filters",
1498 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001499 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001500 } else {
1501 log.info("Switchport {}/{} disabled..removing filters",
1502 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001503 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001504 }
Saurav Das1a129a02016-11-18 15:21:57 -08001505
1506 // portUpdated calls are for ports that have gone down or up. For switch
1507 // to switch ports, link-events should take care of any re-routing or
1508 // group editing necessary for port up/down. Here we only process edge ports
1509 // that are already configured.
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001510 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1511 VlanId untaggedVlan = getUntaggedVlanId(cp);
1512 VlanId nativeVlan = getNativeVlanId(cp);
1513 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001514
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001515 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001516 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das1a129a02016-11-18 15:21:57 -08001517 + "dev/port: {}/{}", device.id(), port.number());
1518 return;
1519 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001520 if (untaggedVlan != null) {
1521 processEdgePort(device, port, untaggedVlan, true);
1522 }
1523 if (nativeVlan != null) {
1524 processEdgePort(device, port, nativeVlan, true);
1525 }
1526 if (!taggedVlans.isEmpty()) {
1527 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1528 }
Saurav Das1a129a02016-11-18 15:21:57 -08001529 }
1530
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001531 private void processEdgePort(Device device, Port port, VlanId vlanId,
1532 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001533 boolean portUp = port.isEnabled();
1534 if (portUp) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001535 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001536 port.number(), vlanId);
Charles Chan47933752017-11-30 15:37:50 -08001537 hostHandler.processPortUp(new ConnectPoint(device.id(), port.number()));
Saurav Das1a129a02016-11-18 15:21:57 -08001538 } else {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001539 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001540 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001541 }
1542
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001543 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001544 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001545 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001546 } else {
1547 log.warn("Group handler not found for dev:{}. Not handling edge port"
1548 + " {} event for port:{}", device.id(),
1549 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001550 }
1551 }
sangho1e575652015-05-14 00:39:53 -07001552
Charles Chan27fe1a52017-10-20 16:06:55 -07001553 private void createOrUpdateDeviceConfiguration() {
1554 if (deviceConfiguration == null) {
1555 deviceConfiguration = new DeviceConfiguration(this);
1556 } else {
1557 deviceConfiguration.updateConfig();
1558 }
1559 }
1560
Pier Ventre10bd8d12016-11-26 21:05:22 -08001561 /**
1562 * Registers the given connect point with the NRS, this is necessary
1563 * to receive the NDP and ARP packets from the NRS.
1564 *
1565 * @param portToRegister connect point to register
1566 */
1567 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001568 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001569 portToRegister,
1570 neighbourHandler,
1571 appId
1572 );
1573 }
1574
Charles Chand6832882015-10-05 17:50:33 -07001575 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001576 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001577 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001578
Charles Chane849c192016-01-11 18:28:54 -08001579 /**
1580 * Constructs the internal network config listener.
1581 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001582 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001583 */
Charles Chan65238242017-06-22 18:03:14 -07001584 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001585 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001586 }
1587
Charles Chane849c192016-01-11 18:28:54 -08001588 /**
1589 * Reads network config and initializes related data structure accordingly.
1590 */
Charles Chan47933752017-11-30 15:37:50 -08001591 void configureNetwork() {
Charles Chan27fe1a52017-10-20 16:06:55 -07001592 createOrUpdateDeviceConfiguration();
Charles Chan4636be02015-10-07 14:21:45 -07001593
Charles Chan2c15aca2016-11-09 20:51:44 -08001594 arpHandler = new ArpHandler(srManager);
1595 icmpHandler = new IcmpHandler(srManager);
1596 ipHandler = new IpHandler(srManager);
1597 routingRulePopulator = new RoutingRulePopulator(srManager);
1598 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001599
1600 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1601 groupHandlerMap, tunnelStore);
1602 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1603 flowObjectiveService,
1604 tunnelHandler, policyStore);
Saurav Das7bcbe702017-06-13 15:35:54 -07001605 // add a small delay to absorb multiple network config added notifications
1606 if (!programmingScheduled.get()) {
1607 programmingScheduled.set(true);
1608 executorService.schedule(new ConfigChange(), PROGRAM_DELAY,
1609 TimeUnit.SECONDS);
Charles Chan4636be02015-10-07 14:21:45 -07001610 }
Charles Chan2199c302016-04-23 17:36:10 -07001611 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001612 }
1613
Charles Chand6832882015-10-05 17:50:33 -07001614 @Override
1615 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001616 // TODO move this part to NetworkConfigEventHandler
1617 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1618 switch (event.type()) {
1619 case CONFIG_ADDED:
Charles Chan278ce832017-06-26 15:25:09 -07001620 log.info("Segment Routing Device Config added for {}", event.subject());
Charles Chan5270ed02016-01-30 23:22:37 -08001621 configureNetwork();
1622 break;
1623 case CONFIG_UPDATED:
Charles Chan278ce832017-06-26 15:25:09 -07001624 log.info("Segment Routing Config updated for {}", event.subject());
Charles Chan27fe1a52017-10-20 16:06:55 -07001625 createOrUpdateDeviceConfiguration();
Charles Chan278ce832017-06-26 15:25:09 -07001626 // TODO support dynamic configuration
1627 break;
1628 default:
1629 break;
1630 }
1631 } else if (event.configClass().equals(InterfaceConfig.class)) {
1632 switch (event.type()) {
1633 case CONFIG_ADDED:
1634 log.info("Interface Config added for {}", event.subject());
1635 configureNetwork();
1636 break;
1637 case CONFIG_UPDATED:
1638 log.info("Interface Config updated for {}", event.subject());
Charles Chan27fe1a52017-10-20 16:06:55 -07001639 createOrUpdateDeviceConfiguration();
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001640
1641 // Following code will be uncommented when [CORD-634] is fully implemented.
1642 // [CORD-634] Add dynamic config support for interfaces
1643 updateInterface((InterfaceConfig) event.config().get(),
1644 (InterfaceConfig) event.prevConfig().get());
Charles Chan5270ed02016-01-30 23:22:37 -08001645 // TODO support dynamic configuration
1646 break;
1647 default:
1648 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001649 }
Charles Chan5270ed02016-01-30 23:22:37 -08001650 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001651 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001652 switch (event.type()) {
1653 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001654 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001655 break;
1656 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001657 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001658 break;
1659 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001660 appCfgHandler.processAppConfigRemoved(event);
1661 break;
1662 default:
1663 break;
1664 }
Charles Chan03a73e02016-10-24 14:52:01 -07001665 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001666 } else if (event.configClass().equals(XConnectConfig.class)) {
1667 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1668 switch (event.type()) {
1669 case CONFIG_ADDED:
1670 xConnectHandler.processXConnectConfigAdded(event);
1671 break;
1672 case CONFIG_UPDATED:
1673 xConnectHandler.processXConnectConfigUpdated(event);
1674 break;
1675 case CONFIG_REMOVED:
1676 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001677 break;
1678 default:
1679 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001680 }
Pier Ventref34966c2016-11-07 16:21:04 -08001681 } else if (event.configClass().equals(PwaasConfig.class)) {
1682 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1683 switch (event.type()) {
1684 case CONFIG_ADDED:
1685 l2TunnelHandler.processPwaasConfigAdded(event);
1686 break;
1687 case CONFIG_UPDATED:
1688 l2TunnelHandler.processPwaasConfigUpdated(event);
1689 break;
1690 case CONFIG_REMOVED:
1691 l2TunnelHandler.processPwaasConfigRemoved(event);
1692 break;
1693 default:
1694 break;
1695 }
Charles Chand6832882015-10-05 17:50:33 -07001696 }
1697 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001698
Charles Chan50443e82018-01-03 16:26:32 -08001699 @Override
1700 public boolean isRelevant(NetworkConfigEvent event) {
1701 if (event.configClass().equals(SegmentRoutingDeviceConfig.class) ||
1702 event.configClass().equals(SegmentRoutingAppConfig.class) ||
1703 event.configClass().equals(InterfaceConfig.class) ||
1704 event.configClass().equals(XConnectConfig.class) ||
1705 event.configClass().equals(PwaasConfig.class)) {
1706 return true;
1707 }
1708 log.debug("Ignore irrelevant event class {}", event.configClass().getName());
1709 return false;
1710 }
1711
Saurav Das7bcbe702017-06-13 15:35:54 -07001712 private final class ConfigChange implements Runnable {
1713 @Override
1714 public void run() {
1715 programmingScheduled.set(false);
1716 for (Device device : deviceService.getDevices()) {
1717 processDeviceAdded(device);
1718 }
1719 defaultRoutingHandler.startPopulationProcess();
1720 }
1721 }
Charles Chand6832882015-10-05 17:50:33 -07001722 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001723
1724 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001725 @Override
1726 public void event(HostEvent event) {
Charles Chan68aa62d2015-11-09 16:37:23 -08001727 switch (event.type()) {
1728 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001729 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001730 break;
1731 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001732 hostHandler.processHostMovedEvent(event);
Charles Chan2fde6d42017-08-23 14:46:43 -07001733 routeHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001734 break;
1735 case HOST_REMOVED:
Charles Chanf9a52702017-06-16 15:19:24 -07001736 hostHandler.processHostRemovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001737 break;
1738 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001739 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001740 break;
1741 default:
1742 log.warn("Unsupported host event type: {}", event.type());
1743 break;
1744 }
1745 }
1746 }
1747
Charles Chand55e84d2016-03-30 17:54:24 -07001748 private class InternalMcastListener implements McastListener {
1749 @Override
1750 public void event(McastEvent event) {
1751 switch (event.type()) {
1752 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001753 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001754 break;
1755 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001756 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001757 break;
1758 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001759 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001760 break;
1761 case ROUTE_ADDED:
1762 case ROUTE_REMOVED:
1763 default:
1764 break;
1765 }
1766 }
1767 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001768
Charles Chan03a73e02016-10-24 14:52:01 -07001769 private class InternalRouteEventListener implements RouteListener {
1770 @Override
1771 public void event(RouteEvent event) {
1772 switch (event.type()) {
1773 case ROUTE_ADDED:
1774 routeHandler.processRouteAdded(event);
1775 break;
1776 case ROUTE_UPDATED:
1777 routeHandler.processRouteUpdated(event);
1778 break;
1779 case ROUTE_REMOVED:
1780 routeHandler.processRouteRemoved(event);
1781 break;
Charles Chan2fde6d42017-08-23 14:46:43 -07001782 case ALTERNATIVE_ROUTES_CHANGED:
1783 routeHandler.processAlternativeRoutesChanged(event);
1784 break;
Charles Chan03a73e02016-10-24 14:52:01 -07001785 default:
1786 break;
1787 }
1788 }
1789 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001790
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001791 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1792 try {
1793 Set<Interface> intfs = conf.getInterfaces();
1794 Set<Interface> prevIntfs = prevConf.getInterfaces();
1795
1796 // Now we only handle one interface config at each port.
1797 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1798 log.warn("Interface update aborted - one at a time is allowed, " +
1799 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1800 return;
1801 }
1802
1803 Interface intf = intfs.stream().findFirst().get();
1804 Interface prevIntf = prevIntfs.stream().findFirst().get();
1805
1806 DeviceId deviceId = intf.connectPoint().deviceId();
1807 PortNumber portNum = intf.connectPoint().port();
1808
1809 if (!mastershipService.isLocalMaster(deviceId)) {
1810 log.debug("CONFIG_UPDATED event for interfaces should be " +
1811 "handled by master node for device {}", deviceId);
1812 return;
1813 }
1814
1815 removeSubnetConfig(prevIntf.connectPoint(),
1816 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1817 new HashSet<>(intf.ipAddressesList())));
1818
1819 if (prevIntf.vlanNative() != VlanId.NONE && !intf.vlanNative().equals(prevIntf.vlanNative())) {
1820 // RemoveVlanNative
1821 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1822 }
1823
1824 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1825 // RemoveVlanTagged
1826 prevIntf.vlanTagged().stream().filter(i -> !intf.vlanTagged().contains(i)).forEach(
1827 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, false)
1828 );
1829 }
1830
1831 if (prevIntf.vlanUntagged() != VlanId.NONE && !intf.vlanUntagged().equals(prevIntf.vlanUntagged())) {
1832 // RemoveVlanUntagged
1833 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1834 }
1835
1836 if (intf.vlanNative() != VlanId.NONE && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1837 // AddVlanNative
1838 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1839 }
1840
1841 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1842 // AddVlanTagged
1843 intf.vlanTagged().stream().filter(i -> !prevIntf.vlanTagged().contains(i)).forEach(
1844 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, true)
1845 );
1846 }
1847
1848 if (intf.vlanUntagged() != VlanId.NONE && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())) {
1849 // AddVlanUntagged
1850 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1851 }
1852 addSubnetConfig(prevIntf.connectPoint(),
1853 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1854 new HashSet<>(prevIntf.ipAddressesList())));
1855 } catch (ConfigException e) {
1856 log.error("Error in configuration");
1857 }
1858 }
1859
1860 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1861 VlanId vlanId, boolean pushVlan, boolean install) {
1862 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1863 if (grpHandler == null) {
1864 log.warn("Failed to retrieve group handler for device {}", deviceId);
1865 return;
1866 }
1867
1868 // Update filtering objective for a single port
1869 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1870
1871 // Update filtering objective for multicast ingress port
1872 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1873
1874 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1875
1876 if (nextId != -1 && !install) {
1877 // Update next objective for a single port as an output port
1878 // Remove a single port from L2FG
1879 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1880 // Remove L2 Bridging rule and L3 Unicast rule to the host
1881 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1882 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1883 // only if there is no port configured on that VLAN ID
1884 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1885 // Remove broadcast forwarding rule for the VLAN
1886 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1887 // Remove L2FG for VLAN
1888 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1889 } else {
1890 // Remove L2IG of the port
1891 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1892 }
1893 } else if (install) {
1894 if (nextId != -1) {
1895 // Add a single port to L2FG
1896 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1897 } else {
1898 // Create L2FG for VLAN
1899 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1900 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1901 }
1902 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1903 } else {
1904 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1905 }
1906 }
1907
1908 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1909 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1910 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1911
1912 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1913 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1914 .filter(intf -> !intf.connectPoint().equals(cp))
1915 .flatMap(intf -> intf.ipAddressesList().stream())
1916 .collect(Collectors.toSet());
1917 // 1. Partial subnet population
1918 // Remove routing rules for removed subnet from previous configuration,
1919 // which does not also exist in other interfaces in the same device
1920 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1921 .map(InterfaceIpAddress::subnetAddress)
1922 .collect(Collectors.toSet());
1923
1924 defaultRoutingHandler.revokeSubnet(
1925 ipPrefixSet.stream()
1926 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1927 .collect(Collectors.toSet()));
1928
1929 // 2. Interface IP punts
1930 // Remove IP punts for old Intf address
1931 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1932 .map(InterfaceIpAddress::ipAddress)
1933 .collect(Collectors.toSet());
1934 ipAddressSet.stream()
1935 .map(InterfaceIpAddress::ipAddress)
1936 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1937 .forEach(interfaceIpAddress ->
1938 routingRulePopulator.revokeSingleIpPunts(
1939 cp.deviceId(), interfaceIpAddress));
1940
1941 // 3. Host unicast routing rule
1942 // Remove unicast routing rule
1943 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1944 }
1945
1946 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1947 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1948 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1949
1950 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1951 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1952 .filter(intf -> !intf.connectPoint().equals(cp))
1953 .flatMap(intf -> intf.ipAddressesList().stream())
1954 .collect(Collectors.toSet());
1955 // 1. Partial subnet population
1956 // Add routing rules for newly added subnet, which does not also exist in
1957 // other interfaces in the same device
1958 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1959 .map(InterfaceIpAddress::subnetAddress)
1960 .collect(Collectors.toSet());
1961
1962 defaultRoutingHandler.populateSubnet(
1963 Collections.singleton(cp),
1964 ipPrefixSet.stream()
1965 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1966 .collect(Collectors.toSet()));
1967
1968 // 2. Interface IP punts
1969 // Add IP punts for new Intf address
1970 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1971 .map(InterfaceIpAddress::ipAddress)
1972 .collect(Collectors.toSet());
1973 ipAddressSet.stream()
1974 .map(InterfaceIpAddress::ipAddress)
1975 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1976 .forEach(interfaceIpAddress ->
1977 routingRulePopulator.populateSingleIpPunts(
1978 cp.deviceId(), interfaceIpAddress));
1979
1980 // 3. Host unicast routing rule
1981 // Add unicast routing rule
1982 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1983 }
sanghob35a6192015-04-01 13:05:26 -07001984}