blob: a0fc5323aae22d29cf0c363cbc47b68c22987c3b [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;
Pier Ventre42287df2016-11-09 14:17:26 -080087import org.onosproject.net.topology.PathService;
Charles Chand55e84d2016-03-30 17:54:24 -070088import org.onosproject.net.topology.TopologyService;
Charles Chan9640c812017-08-23 13:55:39 -070089import org.onosproject.routeservice.ResolvedRoute;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070090import org.onosproject.routeservice.RouteEvent;
91import org.onosproject.routeservice.RouteListener;
92import org.onosproject.routeservice.RouteService;
Charles Chand55e84d2016-03-30 17:54:24 -070093import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
94import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventref34966c2016-11-07 16:21:04 -080095import org.onosproject.segmentrouting.config.PwaasConfig;
Pier Ventref34966c2016-11-07 16:21:04 -080096import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070097import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -070098import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -070099import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700100import org.onosproject.segmentrouting.grouphandler.DestinationSet;
101import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700102import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
103import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700104import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700105import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Charles Chand2990362016-04-18 13:44:03 -0700106import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -0800107import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -0700108import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700109import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700110import org.onosproject.store.service.EventuallyConsistentMap;
111import org.onosproject.store.service.EventuallyConsistentMapBuilder;
112import org.onosproject.store.service.StorageService;
113import org.onosproject.store.service.WallClockTimestamp;
Charles Chan47933752017-11-30 15:37:50 -0800114import org.osgi.service.component.ComponentContext;
sanghob35a6192015-04-01 13:05:26 -0700115import org.slf4j.Logger;
116import org.slf4j.LoggerFactory;
117
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800118import java.util.Collections;
Charles Chan47933752017-11-30 15:37:50 -0800119import java.util.Dictionary;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800120import java.util.HashSet;
121import java.util.List;
122import java.util.Map;
123import java.util.Map.Entry;
124import java.util.Optional;
125import java.util.Set;
126import java.util.concurrent.ConcurrentHashMap;
127import java.util.concurrent.ConcurrentLinkedQueue;
128import java.util.concurrent.Executors;
129import java.util.concurrent.ScheduledExecutorService;
130import java.util.concurrent.ScheduledFuture;
131import java.util.concurrent.TimeUnit;
132import java.util.concurrent.atomic.AtomicBoolean;
133import java.util.stream.Collectors;
sanghob35a6192015-04-01 13:05:26 -0700134
Charles Chan3e783d02016-02-26 22:19:52 -0800135import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800136import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700137import static org.onlab.util.Tools.groupedThreads;
Charles Chan3e783d02016-02-26 22:19:52 -0800138
Charles Chane849c192016-01-11 18:28:54 -0800139/**
140 * Segment routing manager.
141 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700142@Service
143@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700144public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700145
Charles Chan2c15aca2016-11-09 20:51:44 -0800146 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan2fde6d42017-08-23 14:46:43 -0700147 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sanghob35a6192015-04-01 13:05:26 -0700148
149 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700150 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700151
152 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre10bd8d12016-11-26 21:05:22 -0800153 private NeighbourResolutionService neighbourResolutionService;
154
155 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800156 public PathService pathService;
157
158 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700159 CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700160
161 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700162 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700163
164 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700165 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700166
167 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan47933752017-11-30 15:37:50 -0800168 HostLocationProbingService probingService;
169
170 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700171 DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700172
173 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800174 public FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700175
176 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700177 LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700178
Charles Chan5270ed02016-01-30 23:22:37 -0800179 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800180 public MastershipService mastershipService;
Charles Chan03a73e02016-10-24 14:52:01 -0700181
182 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800183 public StorageService storageService;
Charles Chan03a73e02016-10-24 14:52:01 -0700184
185 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
186 MulticastRouteService multicastRouteService;
187
188 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
189 TopologyService topologyService;
190
191 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700192 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800193
194 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800195 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800196
Saurav Das80980c72016-03-23 11:22:49 -0700197 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800198 public InterfaceService interfaceService;
199
Charles Chan47933752017-11-30 15:37:50 -0800200 @Property(name = "activeProbing", boolValue = true,
201 label = "Enable active probing to discover dual-homed hosts.")
202 boolean activeProbing = true;
203
Charles Chan03a73e02016-10-24 14:52:01 -0700204 ArpHandler arpHandler = null;
205 IcmpHandler icmpHandler = null;
206 IpHandler ipHandler = null;
207 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700208 ApplicationId appId;
209 DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700210
Charles Chan03a73e02016-10-24 14:52:01 -0700211 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700212 private TunnelHandler tunnelHandler = null;
213 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700214 private InternalPacketProcessor processor = null;
215 private InternalLinkListener linkListener = null;
216 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700217 private AppConfigHandler appCfgHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700218 XConnectHandler xConnectHandler = null;
Charles Chand2990362016-04-18 13:44:03 -0700219 private McastHandler mcastHandler = null;
Charles Chan65238242017-06-22 18:03:14 -0700220 private HostHandler hostHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800221 private RouteHandler routeHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800222 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Pier Ventref34966c2016-11-07 16:21:04 -0800223 private L2TunnelHandler l2TunnelHandler = null;
sanghob35a6192015-04-01 13:05:26 -0700224 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan5270ed02016-01-30 23:22:37 -0800225 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700226 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
227 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700228 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sanghob35a6192015-04-01 13:05:26 -0700229
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700230 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700231 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sanghob35a6192015-04-01 13:05:26 -0700232
Saurav Das4ce45962015-11-24 23:21:05 -0800233 @SuppressWarnings("unused")
sanghob35a6192015-04-01 13:05:26 -0700234 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800235 @SuppressWarnings("rawtypes")
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700236 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Charles Chan68aa62d2015-11-09 16:37:23 -0800237 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chane849c192016-01-11 18:28:54 -0800238 new ConcurrentHashMap<>();
239 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700240 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700241 * Used to keep track on MPLS group information.
Charles Chane849c192016-01-11 18:28:54 -0800242 */
Charles Chan47933752017-11-30 15:37:50 -0800243 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das7bcbe702017-06-13 15:35:54 -0700244 dsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800245 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700246 * Per device next objective ID store with (device id + vlanid) as key.
247 * Used to keep track on L2 flood group information.
Charles Chane849c192016-01-11 18:28:54 -0800248 */
Charles Chan47933752017-11-30 15:37:50 -0800249 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan59cc16d2017-02-02 16:20:42 -0800250 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800251 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700252 * Per device next objective ID store with (device id + port + treatment + meta) as key.
253 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chane849c192016-01-11 18:28:54 -0800254 */
Charles Chan47933752017-11-30 15:37:50 -0800255 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800256 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800257
Saurav Dasc88d4662017-05-15 15:34:25 -0700258 // Local store for all links seen and their present status, used for
259 // optimized routing. The existence of the link in the keys is enough to know
260 // if the link has been "seen-before" by this instance of the controller.
261 // The boolean value indicates if the link is currently up or not.
262 // XXX Currently the optimized routing logic depends on "forgetting" a link
263 // when a switch goes down, but "remembering" it when only the link goes down.
264 // Consider changing this logic so we can use the Link Service instead of
265 // a local cache.
266 private Map<Link, Boolean> seenLinks = new ConcurrentHashMap<>();
267
Saurav Das4ce45962015-11-24 23:21:05 -0800268 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
269 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700270
Saurav Das7bcbe702017-06-13 15:35:54 -0700271 private AtomicBoolean programmingScheduled = new AtomicBoolean();
272
Charles Chand55e84d2016-03-30 17:54:24 -0700273 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700274 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
275 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700276 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700277 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800278 public SegmentRoutingDeviceConfig createConfig() {
279 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700280 }
281 };
Pier Ventref34966c2016-11-07 16:21:04 -0800282
Charles Chand55e84d2016-03-30 17:54:24 -0700283 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700284 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
285 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700286 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800287 @Override
288 public SegmentRoutingAppConfig createConfig() {
289 return new SegmentRoutingAppConfig();
290 }
291 };
Pier Ventref34966c2016-11-07 16:21:04 -0800292
Charles Chanfc5c7802016-05-17 13:13:55 -0700293 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
294 new ConfigFactory<ApplicationId, XConnectConfig>(
295 SubjectFactories.APP_SUBJECT_FACTORY,
296 XConnectConfig.class, "xconnect") {
297 @Override
298 public XConnectConfig createConfig() {
299 return new XConnectConfig();
300 }
301 };
Pier Ventref34966c2016-11-07 16:21:04 -0800302
Charles Chand55e84d2016-03-30 17:54:24 -0700303 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700304 new ConfigFactory<ApplicationId, McastConfig>(
305 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700306 McastConfig.class, "multicast") {
307 @Override
308 public McastConfig createConfig() {
309 return new McastConfig();
310 }
311 };
312
Pier Ventref34966c2016-11-07 16:21:04 -0800313 private final ConfigFactory<ApplicationId, PwaasConfig> pwaasConfigFactory =
314 new ConfigFactory<ApplicationId, PwaasConfig>(
315 SubjectFactories.APP_SUBJECT_FACTORY,
316 PwaasConfig.class, "pwaas") {
317 @Override
318 public PwaasConfig createConfig() {
319 return new PwaasConfig();
320 }
321 };
322
Charles Chan65238242017-06-22 18:03:14 -0700323 private static final Object THREAD_SCHED_LOCK = new Object();
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700324 private static int numOfEventsQueued = 0;
325 private static int numOfEventsExecuted = 0;
sanghob35a6192015-04-01 13:05:26 -0700326 private static int numOfHandlerExecution = 0;
327 private static int numOfHandlerScheduled = 0;
328
Charles Chan116188d2016-02-18 14:22:42 -0800329 /**
330 * Segment Routing App ID.
331 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800332 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan59cc16d2017-02-02 16:20:42 -0800333
Charles Chane849c192016-01-11 18:28:54 -0800334 /**
335 * The default VLAN ID assigned to the interfaces without subnet config.
336 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800337 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700338
sanghob35a6192015-04-01 13:05:26 -0700339 @Activate
Charles Chan47933752017-11-30 15:37:50 -0800340 protected void activate(ComponentContext context) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800341 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700342
343 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das7bcbe702017-06-13 15:35:54 -0700344 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700345 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700346 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700347 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700348 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700349 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700350 .build();
Saurav Das7bcbe702017-06-13 15:35:54 -0700351 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700352
Charles Chan59cc16d2017-02-02 16:20:42 -0800353 log.debug("Creating EC map vlannextobjectivestore");
354 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
355 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
356 vlanNextObjStore = vlanNextObjMapBuilder
357 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700358 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700359 .withTimestampProvider((k, v) -> new WallClockTimestamp())
360 .build();
361
Saurav Das4ce45962015-11-24 23:21:05 -0800362 log.debug("Creating EC map subnetnextobjectivestore");
363 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
364 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
365 portNextObjStore = portNextObjMapBuilder
366 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700367 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800368 .withTimestampProvider((k, v) -> new WallClockTimestamp())
369 .build();
370
sangho0b2b6d12015-05-20 22:16:38 -0700371 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
372 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700373 tunnelStore = tunnelMapBuilder
374 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700375 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700376 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700377 .build();
378
379 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
380 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700381 policyStore = policyMapBuilder
382 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700383 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700384 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700385 .build();
386
Saurav Das80980c72016-03-23 11:22:49 -0700387 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800388 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700389 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800390 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800391 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
392 "requestInterceptsEnabled", "false");
Charles Chand3baaba2017-08-08 15:13:37 -0700393 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800394 "requestInterceptsEnabled", "false");
Charles Chanc760f382017-07-24 15:56:10 -0700395 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800396 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800397 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
398 "greedyLearningIpv6", "true");
Charles Chanc6d227e2017-02-28 15:15:17 -0800399 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
400 "forceUnprovision", "true");
Charles Chanef624ed2017-08-10 16:57:28 -0700401 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan73316522017-07-20 16:16:25 -0700402 "distributed", "true");
Charles Chan35a32322017-08-14 11:42:11 -0700403 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
404 "multihomingEnabled", "true");
Charles Chan807d87a2017-11-29 19:54:20 -0800405 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
406 "staleLinkAge", "15000");
407 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
408 "allowDuplicateIps", "false");
Charles Chan47933752017-11-30 15:37:50 -0800409 compCfgService.registerProperties(getClass());
410 modified(context);
Saurav Das80980c72016-03-23 11:22:49 -0700411
Charles Chanb8e10c82015-10-14 11:24:40 -0700412 processor = new InternalPacketProcessor();
413 linkListener = new InternalLinkListener();
414 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700415 appCfgHandler = new AppConfigHandler(this);
416 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700417 mcastHandler = new McastHandler(this);
418 hostHandler = new HostHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700419 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800420 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Pier Ventref34966c2016-11-07 16:21:04 -0800421 l2TunnelHandler = new L2TunnelHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700422
Charles Chan3e783d02016-02-26 22:19:52 -0800423 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700424 cfgService.registerConfigFactory(deviceConfigFactory);
425 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700426 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700427 cfgService.registerConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800428 cfgService.registerConfigFactory(pwaasConfigFactory);
Charles Chan5270ed02016-01-30 23:22:37 -0800429 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700430 packetService.addProcessor(processor, PacketProcessor.director(2));
431 linkService.addListener(linkListener);
432 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700433 multicastRouteService.addListener(mcastListener);
Charles Chanc7a8a682017-06-19 00:43:31 -0700434 routeService.addListener(routeListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700435
436 cfgListener.configureNetwork();
437
sanghob35a6192015-04-01 13:05:26 -0700438 log.info("Started");
439 }
440
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700441 private KryoNamespace.Builder createSerializer() {
442 return new KryoNamespace.Builder()
443 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700444 .register(DestinationSetNextObjectiveStoreKey.class,
Charles Chan59cc16d2017-02-02 16:20:42 -0800445 VlanNextObjectiveStoreKey.class,
Saurav Das7bcbe702017-06-13 15:35:54 -0700446 DestinationSet.class,
447 NextNeighbors.class,
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700448 Tunnel.class,
449 DefaultTunnel.class,
450 Policy.class,
451 TunnelPolicy.class,
452 Policy.Type.class,
453 PortNextObjectiveStoreKey.class,
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700454 XConnectStoreKey.class,
455 DefaultL2Tunnel.class,
456 DefaultL2TunnelPolicy.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700457 );
458 }
459
sanghob35a6192015-04-01 13:05:26 -0700460 @Deactivate
461 protected void deactivate() {
Charles Chand6832882015-10-05 17:50:33 -0700462 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700463 cfgService.unregisterConfigFactory(deviceConfigFactory);
464 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700465 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700466 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800467 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chan47933752017-11-30 15:37:50 -0800468 compCfgService.unregisterProperties(getClass(), false);
Charles Chand6832882015-10-05 17:50:33 -0700469
Charles Chanc7a8a682017-06-19 00:43:31 -0700470 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700471 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700472 linkService.removeListener(linkListener);
473 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700474 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700475 routeService.removeListener(routeListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700476
Charles Chan0aa674e2017-02-23 15:44:08 -0800477 neighbourResolutionService.unregisterNeighbourHandlers(appId);
478
sanghob35a6192015-04-01 13:05:26 -0700479 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700480 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700481 deviceListener = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700482 groupHandlerMap.clear();
483
Saurav Das7bcbe702017-06-13 15:35:54 -0700484 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800485 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700486 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700487 tunnelStore.destroy();
488 policyStore.destroy();
sanghob35a6192015-04-01 13:05:26 -0700489 log.info("Stopped");
490 }
491
Charles Chan47933752017-11-30 15:37:50 -0800492 @Modified
493 private void modified(ComponentContext context) {
494 Dictionary<?, ?> properties = context.getProperties();
495 if (properties == null) {
496 return;
497 }
498
499 String strActiveProving = Tools.get(properties, "activeProbing");
500 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
501
502 if (expectActiveProbing != activeProbing) {
503 activeProbing = expectActiveProbing;
504 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
505 }
506 }
507
sangho1e575652015-05-14 00:39:53 -0700508 @Override
509 public List<Tunnel> getTunnels() {
510 return tunnelHandler.getTunnels();
511 }
512
513 @Override
sangho71abe1b2015-06-29 14:58:47 -0700514 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
515 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700516 }
517
518 @Override
sangho71abe1b2015-06-29 14:58:47 -0700519 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700520 for (Policy policy: policyHandler.getPolicies()) {
521 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
522 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
523 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
524 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700525 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700526 }
527 }
528 }
sangho71abe1b2015-06-29 14:58:47 -0700529 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700530 }
531
532 @Override
sangho71abe1b2015-06-29 14:58:47 -0700533 public PolicyHandler.Result removePolicy(Policy policy) {
534 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700535 }
536
537 @Override
sangho71abe1b2015-06-29 14:58:47 -0700538 public PolicyHandler.Result createPolicy(Policy policy) {
539 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700540 }
541
542 @Override
543 public List<Policy> getPolicies() {
544 return policyHandler.getPolicies();
545 }
546
Saurav Das59232cf2016-04-27 18:35:50 -0700547 @Override
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700548 public List<DefaultL2Tunnel> getL2Tunnels() {
549 return l2TunnelHandler.getL2Tunnels();
550 }
551
552 @Override
553 public List<DefaultL2TunnelPolicy> getL2Policies() {
554 return l2TunnelHandler.getL2Policies();
555 }
556
557 @Override
558 public L2TunnelHandler.Result addPseudowire(String tunnelId, String pwLabel, String cP1,
559 String cP1InnerVlan, String cP1OuterVlan, String cP2,
560 String cP2InnerVlan, String cP2OuterVlan,
561 String mode, String sdTag) {
Andreas Pantelopoulos20474e02017-12-20 18:04:27 -0800562 // Try to inject an empty Pwaas config if it is not found for the first time
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700563 PwaasConfig config = cfgService.getConfig(appId(), PwaasConfig.class);
564 if (config == null) {
Andreas Pantelopoulos20474e02017-12-20 18:04:27 -0800565 log.debug("Pwaas config not found. Try to create an empty one.");
566 cfgService.applyConfig(appId(), PwaasConfig.class, new ObjectMapper().createObjectNode());
567 config = cfgService.getConfig(appId(), PwaasConfig.class);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700568 }
569
570 ObjectNode object = config.addPseudowire(tunnelId, pwLabel,
571 cP1, cP1InnerVlan, cP1OuterVlan,
572 cP2, cP2InnerVlan, cP2OuterVlan,
573 mode, sdTag);
574 if (object == null) {
575 log.warn("Could not add pseudowire to the configuration!");
576 return L2TunnelHandler.Result.ADDITION_ERROR;
577 }
578
579 // inform everyone about the valid change in the pw configuration
580 cfgService.applyConfig(appId(), PwaasConfig.class, object);
581 return L2TunnelHandler.Result.SUCCESS;
582 }
583
584 @Override
585 public L2TunnelHandler.Result removePseudowire(String pwId) {
586
587 PwaasConfig config = cfgService.getConfig(appId(), PwaasConfig.class);
588 if (config == null) {
589 log.warn("Configuration for Pwaas class could not be found!");
590 return L2TunnelHandler.Result.CONFIG_NOT_FOUND;
591 }
592
593 ObjectNode object = config.removePseudowire(pwId);
594 if (object == null) {
595 log.warn("Could not delete pseudowire from configuration!");
596 return L2TunnelHandler.Result.REMOVAL_ERROR;
597 }
598
599 // sanity check, this should never fail since we removed a pw
600 // and we always check when we update the configuration
601 config.isValid();
602
603 // inform everyone
604 cfgService.applyConfig(appId(), PwaasConfig.class, object);
605
606 return L2TunnelHandler.Result.SUCCESS;
607 }
608
609 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700610 public void rerouteNetwork() {
611 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700612 }
613
Charles Chanc81c45b2016-10-20 17:02:44 -0700614 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800615 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
616 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800617 deviceConfiguration.getRouters().forEach(device ->
618 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chanc81c45b2016-10-20 17:02:44 -0700619 return deviceSubnetMap;
620 }
621
Saurav Dasc88d4662017-05-15 15:34:25 -0700622
623 @Override
624 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
625 if (defaultRoutingHandler != null) {
626 return defaultRoutingHandler.getCurrentEmcpSpgMap();
627 } else {
628 return null;
629 }
630 }
631
632 @Override
Saurav Das7bcbe702017-06-13 15:35:54 -0700633 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
634 if (dsNextObjStore != null) {
635 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700636 } else {
637 return ImmutableMap.of();
638 }
639 }
640
Saurav Dasceccf242017-08-03 18:30:35 -0700641 @Override
642 public void verifyGroups(DeviceId id) {
643 DefaultGroupHandler gh = groupHandlerMap.get(id);
644 if (gh != null) {
645 gh.triggerBucketCorrector();
646 }
647 }
648
sanghof9d0bf12015-05-19 11:57:42 -0700649 /**
Ray Milkeye4afdb52017-04-05 09:42:04 -0700650 * Extracts the application ID from the manager.
651 *
652 * @return application ID
653 */
654 public ApplicationId appId() {
655 return appId;
656 }
657
658 /**
659 * Returns the device configuration.
660 *
661 * @return device configuration
662 */
663 public DeviceConfiguration deviceConfiguration() {
664 return deviceConfiguration;
665 }
666
667 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700668 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700669 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700670 *
671 * @return next objective ID store
672 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700673 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
674 dsNextObjStore() {
675 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700676 }
677
678 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700679 * Per device next objective ID store with (device id + vlanid) as key.
680 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700681 *
682 * @return vlan next object store
683 */
684 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
685 return vlanNextObjStore;
686 }
687
688 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700689 * Per device next objective ID store with (device id + port + treatment + meta) as key.
690 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700691 *
692 * @return port next object store.
693 */
694 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
695 return portNextObjStore;
696 }
697
698 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700699 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
700 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -0700701 *
702 * @return MPLS-ECMP value
703 */
704 public boolean getMplsEcmp() {
705 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
706 .getConfig(this.appId, SegmentRoutingAppConfig.class);
707 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
708 }
709
710 /**
sanghof9d0bf12015-05-19 11:57:42 -0700711 * Returns the tunnel object with the tunnel ID.
712 *
713 * @param tunnelId Tunnel ID
714 * @return Tunnel reference
715 */
sangho1e575652015-05-14 00:39:53 -0700716 public Tunnel getTunnel(String tunnelId) {
717 return tunnelHandler.getTunnel(tunnelId);
718 }
719
Charles Chan7ffd81f2017-02-08 15:52:08 -0800720 // TODO Consider moving these to InterfaceService
sanghob35a6192015-04-01 13:05:26 -0700721 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800722 * Returns untagged VLAN configured on given connect point.
Charles Chan7ffd81f2017-02-08 15:52:08 -0800723 * <p>
724 * Only returns the first match if there are multiple untagged VLAN configured
725 * on the connect point.
sanghob35a6192015-04-01 13:05:26 -0700726 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800727 * @param connectPoint connect point
728 * @return untagged VLAN or null if not configured
sanghob35a6192015-04-01 13:05:26 -0700729 */
Charles Chan65238242017-06-22 18:03:14 -0700730 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800731 return interfaceService.getInterfacesByPort(connectPoint).stream()
732 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
733 .map(Interface::vlanUntagged)
734 .findFirst().orElse(null);
sanghob35a6192015-04-01 13:05:26 -0700735 }
736
sangho1e575652015-05-14 00:39:53 -0700737 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800738 * Returns tagged VLAN configured on given connect point.
739 * <p>
740 * Returns all matches if there are multiple tagged VLAN configured
741 * on the connect point.
742 *
743 * @param connectPoint connect point
744 * @return tagged VLAN or empty set if not configured
745 */
Charles Chan65238242017-06-22 18:03:14 -0700746 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800747 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
748 return interfaces.stream()
749 .map(Interface::vlanTagged)
Charles Chan65238242017-06-22 18:03:14 -0700750 .flatMap(Set::stream)
Charles Chan7ffd81f2017-02-08 15:52:08 -0800751 .collect(Collectors.toSet());
752 }
753
754 /**
755 * Returns native VLAN configured on given connect point.
756 * <p>
757 * Only returns the first match if there are multiple native VLAN configured
758 * on the connect point.
759 *
760 * @param connectPoint connect point
761 * @return native VLAN or null if not configured
762 */
Charles Chan65238242017-06-22 18:03:14 -0700763 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800764 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
765 return interfaces.stream()
766 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
767 .map(Interface::vlanNative)
768 .findFirst()
769 .orElse(null);
770 }
771
772 /**
Charles Chanf9a52702017-06-16 15:19:24 -0700773 * Returns internal VLAN for untagged hosts on given connect point.
774 * <p>
775 * The internal VLAN is either vlan-untagged for an access port,
776 * or vlan-native for a trunk port.
777 *
778 * @param connectPoint connect point
779 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
780 */
Charles Chan65238242017-06-22 18:03:14 -0700781 VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chanf9a52702017-06-16 15:19:24 -0700782 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
783 VlanId nativeVlanId = getNativeVlanId(connectPoint);
784 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
785 }
786
787 /**
Charles Chan65238242017-06-22 18:03:14 -0700788 * Returns optional pair device ID of given device.
789 *
790 * @param deviceId device ID
791 * @return optional pair device ID. Might be empty if pair device is not configured
792 */
793 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
794 SegmentRoutingDeviceConfig deviceConfig =
795 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
796 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
797 }
798 /**
799 * Returns optional pair device local port of given device.
800 *
801 * @param deviceId device ID
802 * @return optional pair device ID. Might be empty if pair device is not configured
803 */
804 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
805 SegmentRoutingDeviceConfig deviceConfig =
806 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
807 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
808 }
809
810 /**
Charles Chan2fde6d42017-08-23 14:46:43 -0700811 * Determine if current instance is the master of given connect point.
812 *
813 * @param cp connect point
814 * @return true if current instance is the master of given connect point
815 */
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700816 public boolean isMasterOf(ConnectPoint cp) {
Charles Chan2fde6d42017-08-23 14:46:43 -0700817 boolean isMaster = mastershipService.isLocalMaster(cp.deviceId());
818 if (!isMaster) {
819 log.debug(NOT_MASTER, cp);
820 }
821 return isMaster;
822 }
823
824 /**
Charles Chan9640c812017-08-23 13:55:39 -0700825 * Returns locations of given resolved route.
826 *
827 * @param resolvedRoute resolved route
828 * @return locations of nexthop. Might be empty if next hop is not found
829 */
830 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
831 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
832 return Optional.ofNullable(hostService.getHost(hostId))
833 .map(Host::locations).orElse(Sets.newHashSet())
834 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
835 }
836
837 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800838 * Returns vlan port map of given device.
839 *
840 * @param deviceId device id
841 * @return vlan-port multimap
842 */
843 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
844 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
845
846 interfaceService.getInterfaces().stream()
847 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
848 .forEach(intf -> {
849 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -0700850 intf.vlanTagged().forEach(vlanTagged ->
851 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
852 );
Charles Chan7ffd81f2017-02-08 15:52:08 -0800853 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
854 });
855 vlanPortMap.removeAll(VlanId.NONE);
856
857 return vlanPortMap;
858 }
859
860 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800861 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -0800862 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700863 *
864 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -0800865 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -0800866 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700867 */
Charles Chan65238242017-06-22 18:03:14 -0700868 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -0700869 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800870 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
871 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700872 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -0800873 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -0800874 + "device {} not found", deviceId);
875 return -1;
876 }
877 }
878
879 /**
880 * Returns the next objective ID for the given portNumber, given the treatment.
881 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -0700882 * would result in different objectives. If the next object does not exist,
883 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -0800884 *
885 * @param deviceId Device ID
886 * @param portNum port number on device for which NextObjective is queried
887 * @param treatment the actions to apply on the packets (should include outport)
888 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -0700889 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -0700890 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800891 */
892 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
893 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700894 TrafficSelector meta,
895 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -0800896 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
897 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -0700898 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -0800899 } else {
Charles Chane849c192016-01-11 18:28:54 -0800900 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
901 + " not found", deviceId);
902 return -1;
903 }
904 }
905
Saurav Dasc88d4662017-05-15 15:34:25 -0700906 /**
907 * Returns the group handler object for the specified device id.
908 *
909 * @param devId the device identifier
910 * @return the groupHandler object for the device id, or null if not found
911 */
Charles Chan65238242017-06-22 18:03:14 -0700912 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700913 return groupHandlerMap.get(devId);
914 }
915
916 /**
Saurav Dasceccf242017-08-03 18:30:35 -0700917 * Returns the default routing handler object.
918 *
919 * @return the default routing handler object
920 */
921 public DefaultRoutingHandler getRoutingHandler() {
922 return defaultRoutingHandler;
923 }
924
925 /**
Saurav Dasc88d4662017-05-15 15:34:25 -0700926 * Returns true if this controller instance has seen this link before. The
927 * link may not be currently up, but as long as the link had been seen before
928 * this method will return true. The one exception is when the link was
929 * indeed seen before, but this controller instance was forced to forget it
930 * by a call to purgeSeenLink method.
931 *
932 * @param link the infrastructure link being queried
933 * @return true if this controller instance has seen this link before
934 */
Charles Chan65238242017-06-22 18:03:14 -0700935 boolean isSeenLink(Link link) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700936 return seenLinks.containsKey(link);
937 }
938
939 /**
940 * Updates the seen link store. Updates can be for links that are currently
941 * available or not.
942 *
943 * @param link the link to update in the seen-link local store
944 * @param up the status of the link, true if up, false if down
945 */
Charles Chan65238242017-06-22 18:03:14 -0700946 void updateSeenLink(Link link, boolean up) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700947 seenLinks.put(link, up);
948 }
949
950 /**
951 * Returns the status of a seen-link (up or down). If the link has not
952 * been seen-before, a null object is returned.
953 *
954 * @param link the infrastructure link being queried
955 * @return null if the link was not seen-before;
956 * true if the seen-link is up;
957 * false if the seen-link is down
958 */
Charles Chan47933752017-11-30 15:37:50 -0800959 private Boolean isSeenLinkUp(Link link) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700960 return seenLinks.get(link);
961 }
962
963 /**
964 * Makes this controller instance forget a previously seen before link.
965 *
966 * @param link the infrastructure link to purge
967 */
Charles Chan65238242017-06-22 18:03:14 -0700968 private void purgeSeenLink(Link link) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700969 seenLinks.remove(link);
970 }
971
972 /**
973 * Returns the status of a link as parallel link. A parallel link
974 * is defined as a link which has common src and dst switches as another
975 * seen-link that is currently enabled. It is not necessary for the link being
976 * queried to be a seen-link.
977 *
978 * @param link the infrastructure link being queried
979 * @return true if a seen-link exists that is up, and shares the
980 * same src and dst switches as the link being queried
981 */
Charles Chan65238242017-06-22 18:03:14 -0700982 private boolean isParallelLink(Link link) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700983 for (Entry<Link, Boolean> seen : seenLinks.entrySet()) {
984 Link seenLink = seen.getKey();
985 if (seenLink.equals(link)) {
986 continue;
987 }
988 if (seenLink.src().deviceId().equals(link.src().deviceId()) &&
989 seenLink.dst().deviceId().equals(link.dst().deviceId()) &&
990 seen.getValue()) {
991 return true;
992 }
993 }
994 return false;
995 }
996
Saurav Das7bcbe702017-06-13 15:35:54 -0700997 /**
998 * Returns true if the link being queried is a bidirectional link. A bidi
999 * link is defined as a link, whose reverse link - ie. the link in the reverse
1000 * direction - has been seen-before and is up. It is not necessary for the link
1001 * being queried to be a seen-link.
1002 *
1003 * @param link the infrastructure link being queried
1004 * @return true if another unidirectional link exists in the reverse direction,
1005 * has been seen-before and is up
1006 */
Charles Chan47933752017-11-30 15:37:50 -08001007 boolean isBidirectional(Link link) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001008 Link reverseLink = linkService.getLink(link.dst(), link.src());
1009 if (reverseLink == null) {
1010 return false;
1011 }
1012 Boolean result = isSeenLinkUp(reverseLink);
1013 if (result == null) {
1014 return false;
1015 }
1016 return result.booleanValue();
1017 }
1018
1019 /**
1020 * Determines if the given link should be avoided in routing calculations
1021 * by policy or design.
1022 *
1023 * @param link the infrastructure link being queried
1024 * @return true if link should be avoided
1025 */
Charles Chan47933752017-11-30 15:37:50 -08001026 boolean avoidLink(Link link) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001027 // XXX currently only avoids all pair-links. In the future can be
1028 // extended to avoid any generic link
1029 DeviceId src = link.src().deviceId();
1030 PortNumber srcPort = link.src().port();
1031 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(src)) {
1032 log.warn("Device {} not configured..cannot avoid link {}", src, link);
1033 return false;
1034 }
1035 DeviceId pairDev;
1036 PortNumber pairLocalPort, pairRemotePort = null;
1037 try {
1038 pairDev = deviceConfiguration.getPairDeviceId(src);
1039 pairLocalPort = deviceConfiguration.getPairLocalPort(src);
1040 if (pairDev != null) {
1041 pairRemotePort = deviceConfiguration.getPairLocalPort(pairDev);
1042 }
1043 } catch (DeviceConfigNotFoundException e) {
1044 log.warn("Pair dev for dev {} not configured..cannot avoid link {}",
1045 src, link);
1046 return false;
1047 }
1048
Charles Chan47933752017-11-30 15:37:50 -08001049 return srcPort.equals(pairLocalPort) &&
Saurav Das7bcbe702017-06-13 15:35:54 -07001050 link.dst().deviceId().equals(pairDev) &&
Charles Chan47933752017-11-30 15:37:50 -08001051 link.dst().port().equals(pairRemotePort);
Saurav Das7bcbe702017-06-13 15:35:54 -07001052 }
1053
sanghob35a6192015-04-01 13:05:26 -07001054 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -07001055 @Override
1056 public void process(PacketContext context) {
1057
1058 if (context.isHandled()) {
1059 return;
1060 }
1061
1062 InboundPacket pkt = context.inPacket();
1063 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -08001064
1065 if (ethernet == null) {
1066 return;
1067 }
1068
Saurav Das7bcbe702017-06-13 15:35:54 -07001069 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1070 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -08001071 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001072 log.warn("Received unexpected ARP packet on {}",
1073 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -07001074 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -08001075 return;
sanghob35a6192015-04-01 13:05:26 -07001076 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -08001077 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1078 //ipHandler.addToPacketBuffer(ipv4Packet);
1079 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1080 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001081 } else {
Charles Chan50035632017-01-13 17:20:44 -08001082 // NOTE: We don't support IP learning at this moment so this
1083 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -08001084 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -07001085 }
Pier Ventre10bd8d12016-11-26 21:05:22 -08001086 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1087 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -08001088 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -08001089 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -08001090 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1091 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1092 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1093 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1094 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1095 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -07001096 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -07001097 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -08001098 }
1099 } else {
1100 // NOTE: We don't support IP learning at this moment so this
1101 // is not necessary. Also it causes duplication of DHCPv6 packets.
1102 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1103 }
sanghob35a6192015-04-01 13:05:26 -07001104 }
1105 }
1106 }
1107
1108 private class InternalLinkListener implements LinkListener {
1109 @Override
1110 public void event(LinkEvent event) {
Charles Chanb1f8c762017-03-29 16:39:05 -07001111 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1112 event.type() == LinkEvent.Type.LINK_UPDATED ||
1113 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001114 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -07001115 scheduleEventHandlerIfNotScheduled(event);
1116 }
1117 }
1118 }
1119
1120 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -07001121 @Override
1122 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -07001123 switch (event.type()) {
1124 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -08001125 case PORT_UPDATED:
1126 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -07001127 case DEVICE_UPDATED:
1128 case DEVICE_AVAILABILITY_CHANGED:
Saurav Dasc88d4662017-05-15 15:34:25 -07001129 log.trace("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -07001130 scheduleEventHandlerIfNotScheduled(event);
1131 break;
1132 default:
1133 }
1134 }
1135 }
1136
Saurav Das4ce45962015-11-24 23:21:05 -08001137 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -07001138 private void scheduleEventHandlerIfNotScheduled(Event event) {
Charles Chan65238242017-06-22 18:03:14 -07001139 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001140 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001141 numOfEventsQueued++;
1142
1143 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
1144 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001145 eventHandlerFuture = executorService
1146 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
1147 numOfHandlerScheduled++;
1148 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -07001149 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001150 numOfEventsQueued,
1151 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -07001152 }
sanghob35a6192015-04-01 13:05:26 -07001153 }
1154
1155 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -07001156 @Override
sanghob35a6192015-04-01 13:05:26 -07001157 public void run() {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001158 try {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001159 while (true) {
Saurav Das4ce45962015-11-24 23:21:05 -08001160 @SuppressWarnings("rawtypes")
Charles Chan65238242017-06-22 18:03:14 -07001161 Event event;
1162 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001163 if (!eventQueue.isEmpty()) {
1164 event = eventQueue.poll();
1165 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001166 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001167 numOfHandlerExecution++;
1168 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
1169 numOfHandlerExecution, numOfEventsExecuted);
1170 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001171 }
sangho20eff1d2015-04-13 15:15:58 -07001172 }
Charles Chanb1f8c762017-03-29 16:39:05 -07001173 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1174 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001175 // Note: do not update seenLinks here, otherwise every
1176 // link, even one seen for the first time, will be appear
1177 // to be a previously seen link
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001178 processLinkAdded((Link) event.subject());
1179 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre2c515312016-09-13 21:33:40 -07001180 Link linkRemoved = (Link) event.subject();
Saurav Dasc88d4662017-05-15 15:34:25 -07001181 if (linkRemoved.type() == Link.Type.DIRECT) {
1182 updateSeenLink(linkRemoved, false);
1183 }
1184 // device availability check helps to ensure that
1185 // multiple link-removed events are actually treated as a
1186 // single switch removed event. purgeSeenLink is necessary
1187 // so we do rerouting (instead of rehashing) when switch
1188 // comes back.
Pier Ventre2c515312016-09-13 21:33:40 -07001189 if (linkRemoved.src().elementId() instanceof DeviceId &&
1190 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001191 purgeSeenLink(linkRemoved);
Pier Ventre2c515312016-09-13 21:33:40 -07001192 continue;
1193 }
1194 if (linkRemoved.dst().elementId() instanceof DeviceId &&
1195 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001196 purgeSeenLink(linkRemoved);
Pier Ventre2c515312016-09-13 21:33:40 -07001197 continue;
1198 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001199 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001200 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1201 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1202 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001203 DeviceId deviceId = ((Device) event.subject()).id();
1204 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001205 log.info("Processing device event {} for available device {}",
1206 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001207 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -07001208 } else {
1209 log.info("Processing device event {} for unavailable device {}",
1210 event.type(), ((Device) event.subject()).id());
1211 processDeviceRemoved((Device) event.subject());
1212 }
Saurav Das1a129a02016-11-18 15:21:57 -08001213 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001214 // typically these calls come when device is added first time
1215 // so port filtering rules are handled at the device_added event.
1216 // port added calls represent all ports on the device,
1217 // enabled or not.
Saurav Dasc88d4662017-05-15 15:34:25 -07001218 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001219 ((DeviceEvent) event).subject().id(),
1220 ((DeviceEvent) event).port().number(),
1221 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -08001222 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001223 // these calls happen for every subsequent event
1224 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -08001225 log.info("** PORT UPDATED {}/{} -> {}",
1226 event.subject(),
1227 ((DeviceEvent) event).port(),
1228 event.type());
1229 processPortUpdated(((Device) event.subject()),
1230 ((DeviceEvent) event).port());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001231 } else {
1232 log.warn("Unhandled event type: {}", event.type());
1233 }
sanghob35a6192015-04-01 13:05:26 -07001234 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001235 } catch (Exception e) {
1236 log.error("SegmentRouting event handler "
1237 + "thread thrown an exception: {}", e);
sanghob35a6192015-04-01 13:05:26 -07001238 }
sanghob35a6192015-04-01 13:05:26 -07001239 }
1240 }
1241
sanghob35a6192015-04-01 13:05:26 -07001242 private void processLinkAdded(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001243 log.info("** LINK ADDED {}", link.toString());
Saurav Das41890db2017-12-19 16:08:42 -08001244 if (!isLinkValid(link)) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001245 return;
1246 }
Saurav Das9df5b7c2017-08-14 16:44:43 -07001247 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
1248 updateSeenLink(link, true);
1249 // XXX revisit - what about devicePortMap
1250 log.warn("Source device of this link is not configured.. "
1251 + "not processing further");
1252 return;
1253 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001254
1255 //Irrespective of whether the local is a MASTER or not for this device,
1256 //create group handler instance and push default TTP flow rules if needed,
1257 //as in a multi-instance setup, instances can initiate groups for any device.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001258 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
1259 .deviceId());
1260 if (groupHandler != null) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001261 groupHandler.portUpForLink(link);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001262 } else {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001263 // XXX revisit/cleanup
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001264 Device device = deviceService.getDevice(link.src().deviceId());
1265 if (device != null) {
1266 log.warn("processLinkAdded: Link Added "
1267 + "Notification without Device Added "
1268 + "event, still handling it");
1269 processDeviceAdded(device);
1270 groupHandler = groupHandlerMap.get(link.src()
1271 .deviceId());
Saurav Dasc88d4662017-05-15 15:34:25 -07001272 groupHandler.portUpForLink(link);
sanghob35a6192015-04-01 13:05:26 -07001273 }
1274 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001275
Saurav Das7bcbe702017-06-13 15:35:54 -07001276 /*// process link only if it is bidirectional
1277 if (!isBidirectional(link)) {
1278 log.debug("Link not bidirectional.. waiting for other direction "
1279 + "src {} --> dst {} ", link.dst(), link.src());
1280 // note that if we are not processing for routing, it should at least
1281 // be considered a seen-link
1282 updateSeenLink(link, true);
1283 return;
1284 }
1285 TO DO this ensure that rehash is still done correctly even if link is
1286 not processed for rerouting - perhaps rehash in both directions when
1287 it ultimately becomes bidi?
1288 */
1289
1290 log.debug("Starting optimized route population process for link "
1291 + "{} --> {}", link.src(), link.dst());
Saurav Dasc88d4662017-05-15 15:34:25 -07001292 boolean seenBefore = isSeenLink(link);
1293 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null, link, null);
Saurav Das7bcbe702017-06-13 15:35:54 -07001294
1295 // It's possible that linkUp causes no route-path change as ECMP graph does
1296 // not change if the link is a parallel link (same src-dst as another link.
1297 // However we still need to update ECMP hash groups to include new buckets
1298 // for the link that has come up.
Saurav Dasc88d4662017-05-15 15:34:25 -07001299 if (mastershipService.isLocalMaster(link.src().deviceId())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001300 if (!seenBefore && isParallelLink(link)) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001301 // if link seen first time, we need to ensure hash-groups have all ports
Saurav Das7bcbe702017-06-13 15:35:54 -07001302 log.debug("Attempting retryHash for paralled first-time link {}", link);
Saurav Dasc88d4662017-05-15 15:34:25 -07001303 groupHandler.retryHash(link, false, true);
1304 } else {
1305 //seen before-link
1306 if (isParallelLink(link)) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001307 log.debug("Attempting retryHash for paralled seen-before "
1308 + "link {}", link);
Saurav Dasc88d4662017-05-15 15:34:25 -07001309 groupHandler.retryHash(link, false, false);
1310 }
1311 }
1312 }
Charles Chan2199c302016-04-23 17:36:10 -07001313
1314 mcastHandler.init();
sanghob35a6192015-04-01 13:05:26 -07001315 }
1316
1317 private void processLinkRemoved(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001318 log.info("** LINK REMOVED {}", link.toString());
Saurav Das41890db2017-12-19 16:08:42 -08001319 if (!isLinkValid(link)) {
1320 return;
1321 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001322 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link, null, null);
1323
1324 // update local groupHandler stores
sangho834e4b02015-05-01 09:38:25 -07001325 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
1326 if (groupHandler != null) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001327 if (mastershipService.isLocalMaster(link.src().deviceId()) &&
1328 isParallelLink(link)) {
Saurav Das9df5b7c2017-08-14 16:44:43 -07001329 log.debug("* retrying hash for parallel link removed:{}", link);
Saurav Dasc88d4662017-05-15 15:34:25 -07001330 groupHandler.retryHash(link, true, false);
Saurav Das9df5b7c2017-08-14 16:44:43 -07001331 } else {
1332 log.debug("Not attempting retry-hash for link removed: {} .. {}", link,
1333 (mastershipService.isLocalMaster(link.src().deviceId()))
1334 ? "not parallel" : "not master");
Saurav Dasc88d4662017-05-15 15:34:25 -07001335 }
1336 // ensure local stores are updated
1337 groupHandler.portDown(link.src().port());
1338 } else {
1339 log.warn("group handler not found for dev:{} when removing link: {}",
1340 link.src().deviceId(), link);
sangho834e4b02015-05-01 09:38:25 -07001341 }
Charles Chan2199c302016-04-23 17:36:10 -07001342
1343 mcastHandler.processLinkDown(link);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -07001344 l2TunnelHandler.processLinkDown(link);
sanghob35a6192015-04-01 13:05:26 -07001345 }
1346
Saurav Das41890db2017-12-19 16:08:42 -08001347 private boolean isLinkValid(Link link) {
1348 if (link.type() != Link.Type.DIRECT) {
1349 // NOTE: A DIRECT link might be transiently marked as INDIRECT
1350 // if BDDP is received before LLDP. We can safely ignore that
1351 // until the LLDP is received and the link is marked as DIRECT.
1352 log.info("Ignore link {}->{}. Link type is {} instead of DIRECT.",
1353 link.src(), link.dst(), link.type());
1354 return false;
1355 }
1356 DeviceId srcId = link.src().deviceId();
1357 DeviceId dstId = link.dst().deviceId();
1358 if (srcId.equals(dstId)) {
1359 log.warn("Links between ports on the same switch are not "
1360 + "allowed .. ignoring link {}", link);
1361 return false;
1362 }
1363 try {
1364 if (!deviceConfiguration.isEdgeDevice(srcId)
1365 && !deviceConfiguration.isEdgeDevice(dstId)) {
1366 // ignore links between spines
1367 // XXX revisit when handling multi-stage fabrics
1368 log.warn("Links between spines not allowed...ignoring "
1369 + "link {}", link);
1370 return false;
1371 }
1372 if (deviceConfiguration.isEdgeDevice(srcId)
1373 && deviceConfiguration.isEdgeDevice(dstId)) {
1374 // ignore links between leaves if they are not pair-links
1375 // XXX revisit if removing pair-link config or allowing more than
1376 // one pair-link
1377 if (deviceConfiguration.getPairDeviceId(srcId).equals(dstId)
1378 && deviceConfiguration.getPairLocalPort(srcId)
1379 .equals(link.src().port())
1380 && deviceConfiguration.getPairLocalPort(dstId)
1381 .equals(link.dst().port())) {
1382 // found pair link - allow it
1383 return true;
1384 } else {
1385 log.warn("Links between leaves other than pair-links are "
1386 + "not allowed...ignoring link {}", link);
1387 return false;
1388 }
1389 }
1390 } catch (DeviceConfigNotFoundException e) {
1391 // We still want to count the links in seenLinks even though there
1392 // is no config. So we let it return true
1393 log.warn("Could not check validity of link {} as subtending devices "
1394 + "are not yet configured", link);
1395 }
1396 return true;
1397 }
1398
sanghob35a6192015-04-01 13:05:26 -07001399 private void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001400 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001401
1402 // NOTE: Punt ARP/NDP even when the device is not configured.
1403 // Host learning without network config is required for CORD config generator.
1404 routingRulePopulator.populateIpPunts(device.id());
1405 routingRulePopulator.populateArpNdpPunts(device.id());
1406
Charles Chan0b4e6182015-11-03 10:42:14 -08001407 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001408 log.warn("Device configuration unavailable. Device {} will be "
1409 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001410 return;
1411 }
Charles Chan2199c302016-04-23 17:36:10 -07001412 processDeviceAddedInternal(device.id());
1413 }
1414
1415 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001416 // Irrespective of whether the local is a MASTER or not for this device,
1417 // we need to create a SR-group-handler instance. This is because in a
1418 // multi-instance setup, any instance can initiate forwarding/next-objectives
1419 // for any switch (even if this instance is a SLAVE or not even connected
1420 // to the switch). To handle this, a default-group-handler instance is necessary
1421 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001422 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1423 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001424 DefaultGroupHandler groupHandler;
1425 try {
1426 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001427 createGroupHandler(deviceId,
1428 appId,
1429 deviceConfiguration,
1430 linkService,
1431 flowObjectiveService,
1432 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001433 } catch (DeviceConfigNotFoundException e) {
1434 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1435 return;
1436 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001437 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001438 deviceId);
1439 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001440 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001441
Charles Chan2199c302016-04-23 17:36:10 -07001442 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001443 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan03a73e02016-10-24 14:52:01 -07001444 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001445 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001446 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001447 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001448 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001449 }
Charles Chan5270ed02016-01-30 23:22:37 -08001450
Charles Chan03a73e02016-10-24 14:52:01 -07001451 appCfgHandler.init(deviceId);
1452 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001453 }
1454
Saurav Das80980c72016-03-23 11:22:49 -07001455 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001456 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001457 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan47933752017-11-30 15:37:50 -08001458 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001459 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001460 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001461 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001462 portNextObjStore.entrySet().stream()
1463 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001464 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Charles Chaned3742352017-06-15 00:44:51 -07001465
1466 seenLinks.keySet().removeIf(key -> key.src().deviceId().equals(device.id()) ||
1467 key.dst().deviceId().equals(device.id()));
1468
Saurav Dasceccf242017-08-03 18:30:35 -07001469 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1470 if (gh != null) {
1471 gh.shutdown();
1472 }
Saurav Das80980c72016-03-23 11:22:49 -07001473 defaultRoutingHandler.purgeEcmpGraph(device.id());
Saurav Dasc88d4662017-05-15 15:34:25 -07001474 // Note that a switch going down is associated with all of its links
1475 // going down as well, but it is treated as a single switch down event
1476 // while the link-downs are ignored.
1477 defaultRoutingHandler
1478 .populateRoutingRulesForLinkStatusChange(null, null, device.id());
Charles Chan2199c302016-04-23 17:36:10 -07001479 mcastHandler.removeDevice(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001480 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -07001481 }
1482
Saurav Das1a129a02016-11-18 15:21:57 -08001483 private void processPortUpdated(Device device, Port port) {
1484 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1485 log.warn("Device configuration uploading. Not handling port event for"
1486 + "dev: {} port: {}", device.id(), port.number());
1487 return;
1488 }
Saurav Das018605f2017-02-18 14:05:44 -08001489
1490 if (!mastershipService.isLocalMaster(device.id())) {
1491 log.debug("Not master for dev:{} .. not handling port updated event"
1492 + "for port {}", device.id(), port.number());
1493 return;
1494 }
1495
1496 // first we handle filtering rules associated with the port
1497 if (port.isEnabled()) {
1498 log.info("Switchport {}/{} enabled..programming filters",
1499 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001500 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001501 } else {
1502 log.info("Switchport {}/{} disabled..removing filters",
1503 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001504 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001505 }
Saurav Das1a129a02016-11-18 15:21:57 -08001506
1507 // portUpdated calls are for ports that have gone down or up. For switch
1508 // to switch ports, link-events should take care of any re-routing or
1509 // group editing necessary for port up/down. Here we only process edge ports
1510 // that are already configured.
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001511 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1512 VlanId untaggedVlan = getUntaggedVlanId(cp);
1513 VlanId nativeVlan = getNativeVlanId(cp);
1514 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001515
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001516 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001517 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das1a129a02016-11-18 15:21:57 -08001518 + "dev/port: {}/{}", device.id(), port.number());
1519 return;
1520 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001521 if (untaggedVlan != null) {
1522 processEdgePort(device, port, untaggedVlan, true);
1523 }
1524 if (nativeVlan != null) {
1525 processEdgePort(device, port, nativeVlan, true);
1526 }
1527 if (!taggedVlans.isEmpty()) {
1528 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1529 }
Saurav Das1a129a02016-11-18 15:21:57 -08001530 }
1531
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001532 private void processEdgePort(Device device, Port port, VlanId vlanId,
1533 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001534 boolean portUp = port.isEnabled();
1535 if (portUp) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001536 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001537 port.number(), vlanId);
Charles Chan47933752017-11-30 15:37:50 -08001538 hostHandler.processPortUp(new ConnectPoint(device.id(), port.number()));
Saurav Das1a129a02016-11-18 15:21:57 -08001539 } else {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001540 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001541 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001542 }
1543
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001544 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001545 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001546 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001547 } else {
1548 log.warn("Group handler not found for dev:{}. Not handling edge port"
1549 + " {} event for port:{}", device.id(),
1550 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001551 }
1552 }
sangho1e575652015-05-14 00:39:53 -07001553
Charles Chan27fe1a52017-10-20 16:06:55 -07001554 private void createOrUpdateDeviceConfiguration() {
1555 if (deviceConfiguration == null) {
1556 deviceConfiguration = new DeviceConfiguration(this);
1557 } else {
1558 deviceConfiguration.updateConfig();
1559 }
1560 }
1561
Pier Ventre10bd8d12016-11-26 21:05:22 -08001562 /**
1563 * Registers the given connect point with the NRS, this is necessary
1564 * to receive the NDP and ARP packets from the NRS.
1565 *
1566 * @param portToRegister connect point to register
1567 */
1568 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001569 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001570 portToRegister,
1571 neighbourHandler,
1572 appId
1573 );
1574 }
1575
Charles Chand6832882015-10-05 17:50:33 -07001576 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001577 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001578 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001579
Charles Chane849c192016-01-11 18:28:54 -08001580 /**
1581 * Constructs the internal network config listener.
1582 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001583 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001584 */
Charles Chan65238242017-06-22 18:03:14 -07001585 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001586 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001587 }
1588
Charles Chane849c192016-01-11 18:28:54 -08001589 /**
1590 * Reads network config and initializes related data structure accordingly.
1591 */
Charles Chan47933752017-11-30 15:37:50 -08001592 void configureNetwork() {
Charles Chan27fe1a52017-10-20 16:06:55 -07001593 createOrUpdateDeviceConfiguration();
Charles Chan4636be02015-10-07 14:21:45 -07001594
Charles Chan2c15aca2016-11-09 20:51:44 -08001595 arpHandler = new ArpHandler(srManager);
1596 icmpHandler = new IcmpHandler(srManager);
1597 ipHandler = new IpHandler(srManager);
1598 routingRulePopulator = new RoutingRulePopulator(srManager);
1599 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001600
1601 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1602 groupHandlerMap, tunnelStore);
1603 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1604 flowObjectiveService,
1605 tunnelHandler, policyStore);
Saurav Das7bcbe702017-06-13 15:35:54 -07001606 // add a small delay to absorb multiple network config added notifications
1607 if (!programmingScheduled.get()) {
1608 programmingScheduled.set(true);
1609 executorService.schedule(new ConfigChange(), PROGRAM_DELAY,
1610 TimeUnit.SECONDS);
Charles Chan4636be02015-10-07 14:21:45 -07001611 }
Charles Chan2199c302016-04-23 17:36:10 -07001612 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001613 }
1614
Charles Chand6832882015-10-05 17:50:33 -07001615 @Override
1616 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001617 // TODO move this part to NetworkConfigEventHandler
1618 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1619 switch (event.type()) {
1620 case CONFIG_ADDED:
Charles Chan278ce832017-06-26 15:25:09 -07001621 log.info("Segment Routing Device Config added for {}", event.subject());
Charles Chan5270ed02016-01-30 23:22:37 -08001622 configureNetwork();
1623 break;
1624 case CONFIG_UPDATED:
Charles Chan278ce832017-06-26 15:25:09 -07001625 log.info("Segment Routing Config updated for {}", event.subject());
Charles Chan27fe1a52017-10-20 16:06:55 -07001626 createOrUpdateDeviceConfiguration();
Charles Chan278ce832017-06-26 15:25:09 -07001627 // TODO support dynamic configuration
1628 break;
1629 default:
1630 break;
1631 }
1632 } else if (event.configClass().equals(InterfaceConfig.class)) {
1633 switch (event.type()) {
1634 case CONFIG_ADDED:
1635 log.info("Interface Config added for {}", event.subject());
1636 configureNetwork();
1637 break;
1638 case CONFIG_UPDATED:
1639 log.info("Interface Config updated for {}", event.subject());
Charles Chan27fe1a52017-10-20 16:06:55 -07001640 createOrUpdateDeviceConfiguration();
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001641
1642 // Following code will be uncommented when [CORD-634] is fully implemented.
1643 // [CORD-634] Add dynamic config support for interfaces
1644 updateInterface((InterfaceConfig) event.config().get(),
1645 (InterfaceConfig) event.prevConfig().get());
Charles Chan5270ed02016-01-30 23:22:37 -08001646 // TODO support dynamic configuration
1647 break;
1648 default:
1649 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001650 }
Charles Chan5270ed02016-01-30 23:22:37 -08001651 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001652 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001653 switch (event.type()) {
1654 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001655 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001656 break;
1657 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001658 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001659 break;
1660 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001661 appCfgHandler.processAppConfigRemoved(event);
1662 break;
1663 default:
1664 break;
1665 }
Charles Chan03a73e02016-10-24 14:52:01 -07001666 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001667 } else if (event.configClass().equals(XConnectConfig.class)) {
1668 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1669 switch (event.type()) {
1670 case CONFIG_ADDED:
1671 xConnectHandler.processXConnectConfigAdded(event);
1672 break;
1673 case CONFIG_UPDATED:
1674 xConnectHandler.processXConnectConfigUpdated(event);
1675 break;
1676 case CONFIG_REMOVED:
1677 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001678 break;
1679 default:
1680 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001681 }
Pier Ventref34966c2016-11-07 16:21:04 -08001682 } else if (event.configClass().equals(PwaasConfig.class)) {
1683 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1684 switch (event.type()) {
1685 case CONFIG_ADDED:
1686 l2TunnelHandler.processPwaasConfigAdded(event);
1687 break;
1688 case CONFIG_UPDATED:
1689 l2TunnelHandler.processPwaasConfigUpdated(event);
1690 break;
1691 case CONFIG_REMOVED:
1692 l2TunnelHandler.processPwaasConfigRemoved(event);
1693 break;
1694 default:
1695 break;
1696 }
Charles Chand6832882015-10-05 17:50:33 -07001697 }
1698 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001699
Charles Chan50443e82018-01-03 16:26:32 -08001700 @Override
1701 public boolean isRelevant(NetworkConfigEvent event) {
1702 if (event.configClass().equals(SegmentRoutingDeviceConfig.class) ||
1703 event.configClass().equals(SegmentRoutingAppConfig.class) ||
1704 event.configClass().equals(InterfaceConfig.class) ||
1705 event.configClass().equals(XConnectConfig.class) ||
1706 event.configClass().equals(PwaasConfig.class)) {
1707 return true;
1708 }
1709 log.debug("Ignore irrelevant event class {}", event.configClass().getName());
1710 return false;
1711 }
1712
Saurav Das7bcbe702017-06-13 15:35:54 -07001713 private final class ConfigChange implements Runnable {
1714 @Override
1715 public void run() {
1716 programmingScheduled.set(false);
1717 for (Device device : deviceService.getDevices()) {
1718 processDeviceAdded(device);
1719 }
1720 defaultRoutingHandler.startPopulationProcess();
1721 }
1722 }
Charles Chand6832882015-10-05 17:50:33 -07001723 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001724
1725 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001726 @Override
1727 public void event(HostEvent event) {
Charles Chan68aa62d2015-11-09 16:37:23 -08001728 switch (event.type()) {
1729 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001730 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001731 break;
1732 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001733 hostHandler.processHostMovedEvent(event);
Charles Chan2fde6d42017-08-23 14:46:43 -07001734 routeHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001735 break;
1736 case HOST_REMOVED:
Charles Chanf9a52702017-06-16 15:19:24 -07001737 hostHandler.processHostRemovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001738 break;
1739 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001740 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001741 break;
1742 default:
1743 log.warn("Unsupported host event type: {}", event.type());
1744 break;
1745 }
1746 }
1747 }
1748
Charles Chand55e84d2016-03-30 17:54:24 -07001749 private class InternalMcastListener implements McastListener {
1750 @Override
1751 public void event(McastEvent event) {
1752 switch (event.type()) {
1753 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001754 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001755 break;
1756 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001757 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001758 break;
1759 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001760 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001761 break;
1762 case ROUTE_ADDED:
1763 case ROUTE_REMOVED:
1764 default:
1765 break;
1766 }
1767 }
1768 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001769
Charles Chan03a73e02016-10-24 14:52:01 -07001770 private class InternalRouteEventListener implements RouteListener {
1771 @Override
1772 public void event(RouteEvent event) {
1773 switch (event.type()) {
1774 case ROUTE_ADDED:
1775 routeHandler.processRouteAdded(event);
1776 break;
1777 case ROUTE_UPDATED:
1778 routeHandler.processRouteUpdated(event);
1779 break;
1780 case ROUTE_REMOVED:
1781 routeHandler.processRouteRemoved(event);
1782 break;
Charles Chan2fde6d42017-08-23 14:46:43 -07001783 case ALTERNATIVE_ROUTES_CHANGED:
1784 routeHandler.processAlternativeRoutesChanged(event);
1785 break;
Charles Chan03a73e02016-10-24 14:52:01 -07001786 default:
1787 break;
1788 }
1789 }
1790 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001791
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001792 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1793 try {
1794 Set<Interface> intfs = conf.getInterfaces();
1795 Set<Interface> prevIntfs = prevConf.getInterfaces();
1796
1797 // Now we only handle one interface config at each port.
1798 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1799 log.warn("Interface update aborted - one at a time is allowed, " +
1800 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1801 return;
1802 }
1803
1804 Interface intf = intfs.stream().findFirst().get();
1805 Interface prevIntf = prevIntfs.stream().findFirst().get();
1806
1807 DeviceId deviceId = intf.connectPoint().deviceId();
1808 PortNumber portNum = intf.connectPoint().port();
1809
1810 if (!mastershipService.isLocalMaster(deviceId)) {
1811 log.debug("CONFIG_UPDATED event for interfaces should be " +
1812 "handled by master node for device {}", deviceId);
1813 return;
1814 }
1815
1816 removeSubnetConfig(prevIntf.connectPoint(),
1817 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1818 new HashSet<>(intf.ipAddressesList())));
1819
1820 if (prevIntf.vlanNative() != VlanId.NONE && !intf.vlanNative().equals(prevIntf.vlanNative())) {
1821 // RemoveVlanNative
1822 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1823 }
1824
1825 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1826 // RemoveVlanTagged
1827 prevIntf.vlanTagged().stream().filter(i -> !intf.vlanTagged().contains(i)).forEach(
1828 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, false)
1829 );
1830 }
1831
1832 if (prevIntf.vlanUntagged() != VlanId.NONE && !intf.vlanUntagged().equals(prevIntf.vlanUntagged())) {
1833 // RemoveVlanUntagged
1834 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1835 }
1836
1837 if (intf.vlanNative() != VlanId.NONE && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1838 // AddVlanNative
1839 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1840 }
1841
1842 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1843 // AddVlanTagged
1844 intf.vlanTagged().stream().filter(i -> !prevIntf.vlanTagged().contains(i)).forEach(
1845 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, true)
1846 );
1847 }
1848
1849 if (intf.vlanUntagged() != VlanId.NONE && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())) {
1850 // AddVlanUntagged
1851 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1852 }
1853 addSubnetConfig(prevIntf.connectPoint(),
1854 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1855 new HashSet<>(prevIntf.ipAddressesList())));
1856 } catch (ConfigException e) {
1857 log.error("Error in configuration");
1858 }
1859 }
1860
1861 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1862 VlanId vlanId, boolean pushVlan, boolean install) {
1863 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1864 if (grpHandler == null) {
1865 log.warn("Failed to retrieve group handler for device {}", deviceId);
1866 return;
1867 }
1868
1869 // Update filtering objective for a single port
1870 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1871
1872 // Update filtering objective for multicast ingress port
1873 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1874
1875 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1876
1877 if (nextId != -1 && !install) {
1878 // Update next objective for a single port as an output port
1879 // Remove a single port from L2FG
1880 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1881 // Remove L2 Bridging rule and L3 Unicast rule to the host
1882 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1883 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1884 // only if there is no port configured on that VLAN ID
1885 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1886 // Remove broadcast forwarding rule for the VLAN
1887 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1888 // Remove L2FG for VLAN
1889 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1890 } else {
1891 // Remove L2IG of the port
1892 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1893 }
1894 } else if (install) {
1895 if (nextId != -1) {
1896 // Add a single port to L2FG
1897 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1898 } else {
1899 // Create L2FG for VLAN
1900 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1901 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1902 }
1903 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1904 } else {
1905 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1906 }
1907 }
1908
1909 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1910 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1911 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1912
1913 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1914 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1915 .filter(intf -> !intf.connectPoint().equals(cp))
1916 .flatMap(intf -> intf.ipAddressesList().stream())
1917 .collect(Collectors.toSet());
1918 // 1. Partial subnet population
1919 // Remove routing rules for removed subnet from previous configuration,
1920 // which does not also exist in other interfaces in the same device
1921 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1922 .map(InterfaceIpAddress::subnetAddress)
1923 .collect(Collectors.toSet());
1924
1925 defaultRoutingHandler.revokeSubnet(
1926 ipPrefixSet.stream()
1927 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1928 .collect(Collectors.toSet()));
1929
1930 // 2. Interface IP punts
1931 // Remove IP punts for old Intf address
1932 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1933 .map(InterfaceIpAddress::ipAddress)
1934 .collect(Collectors.toSet());
1935 ipAddressSet.stream()
1936 .map(InterfaceIpAddress::ipAddress)
1937 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1938 .forEach(interfaceIpAddress ->
1939 routingRulePopulator.revokeSingleIpPunts(
1940 cp.deviceId(), interfaceIpAddress));
1941
1942 // 3. Host unicast routing rule
1943 // Remove unicast routing rule
1944 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1945 }
1946
1947 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1948 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1949 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1950
1951 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1952 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1953 .filter(intf -> !intf.connectPoint().equals(cp))
1954 .flatMap(intf -> intf.ipAddressesList().stream())
1955 .collect(Collectors.toSet());
1956 // 1. Partial subnet population
1957 // Add routing rules for newly added subnet, which does not also exist in
1958 // other interfaces in the same device
1959 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1960 .map(InterfaceIpAddress::subnetAddress)
1961 .collect(Collectors.toSet());
1962
1963 defaultRoutingHandler.populateSubnet(
1964 Collections.singleton(cp),
1965 ipPrefixSet.stream()
1966 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1967 .collect(Collectors.toSet()));
1968
1969 // 2. Interface IP punts
1970 // Add IP punts for new Intf address
1971 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1972 .map(InterfaceIpAddress::ipAddress)
1973 .collect(Collectors.toSet());
1974 ipAddressSet.stream()
1975 .map(InterfaceIpAddress::ipAddress)
1976 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1977 .forEach(interfaceIpAddress ->
1978 routingRulePopulator.populateSingleIpPunts(
1979 cp.deviceId(), interfaceIpAddress));
1980
1981 // 3. Host unicast routing rule
1982 // Add unicast routing rule
1983 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1984 }
sanghob35a6192015-04-01 13:05:26 -07001985}