blob: 62ad7634e0bd15c5c201bd76c31e01632fae1c8e [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sangho80f11cb2015-04-01 13:05:26 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.segmentrouting;
17
Andreas Pantelopoulosafc637f2017-12-20 18:04:27 -080018import com.fasterxml.jackson.databind.ObjectMapper;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -070019import com.fasterxml.jackson.databind.node.ObjectNode;
Jonathan Hart61e24e12017-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 Chanf0ae41e2017-08-23 13:55:39 -070024import com.google.common.collect.Sets;
sangho80f11cb2015-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 Chan873661e2017-11-30 15:37:50 -080028import org.apache.felix.scr.annotations.Modified;
29import org.apache.felix.scr.annotations.Property;
sangho80f11cb2015-04-01 13:05:26 -070030import org.apache.felix.scr.annotations.Reference;
31import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho27462c62015-05-14 00:39:53 -070032import org.apache.felix.scr.annotations.Service;
sangho80f11cb2015-04-01 13:05:26 -070033import org.onlab.packet.Ethernet;
Pier Ventreb6b81d52016-12-02 08:16:05 -080034import org.onlab.packet.ICMP6;
Charles Chan77277672015-10-20 16:24:19 -070035import org.onlab.packet.IPv4;
Pier Ventreb6a7f342016-11-26 21:05:22 -080036import org.onlab.packet.IPv6;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070037import org.onlab.packet.IpAddress;
Charles Chan77277672015-10-20 16:24:19 -070038import org.onlab.packet.IpPrefix;
Jonathan Hart54541d12016-04-12 15:39:44 -070039import org.onlab.packet.VlanId;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070040import org.onlab.util.KryoNamespace;
Charles Chan873661e2017-11-30 15:37:50 -080041import org.onlab.util.Tools;
Saurav Dasc3604f12016-03-23 11:22:49 -070042import org.onosproject.cfg.ComponentConfigService;
sangho80f11cb2015-04-01 13:05:26 -070043import org.onosproject.core.ApplicationId;
44import org.onosproject.core.CoreService;
45import org.onosproject.event.Event;
Jonathan Hart54541d12016-04-12 15:39:44 -070046import org.onosproject.mastership.MastershipService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080047import org.onosproject.net.ConnectPoint;
Jonathan Hart54541d12016-04-12 15:39:44 -070048import org.onosproject.net.Device;
49import org.onosproject.net.DeviceId;
Charles Chanf0ae41e2017-08-23 13:55:39 -070050import org.onosproject.net.Host;
51import org.onosproject.net.HostId;
Jonathan Hart54541d12016-04-12 15:39:44 -070052import org.onosproject.net.Link;
53import org.onosproject.net.Port;
Charles Chanf4586112015-11-09 16:37:23 -080054import org.onosproject.net.PortNumber;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070055import org.onosproject.net.config.ConfigException;
Charles Chan72f556a2015-10-05 17:50:33 -070056import org.onosproject.net.config.ConfigFactory;
57import org.onosproject.net.config.NetworkConfigEvent;
Charles Chan72f556a2015-10-05 17:50:33 -070058import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070059import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkeyae0068a2017-08-15 11:02:29 -070060import org.onosproject.net.config.basics.InterfaceConfig;
61import org.onosproject.net.config.basics.McastConfig;
Charles Chan72f556a2015-10-05 17:50:33 -070062import org.onosproject.net.config.basics.SubjectFactories;
Jonathan Hart54541d12016-04-12 15:39:44 -070063import org.onosproject.net.device.DeviceEvent;
64import org.onosproject.net.device.DeviceListener;
65import org.onosproject.net.device.DeviceService;
Charles Chanf4586112015-11-09 16:37:23 -080066import org.onosproject.net.flow.TrafficSelector;
67import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hart54541d12016-04-12 15:39:44 -070068import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chanf4586112015-11-09 16:37:23 -080069import org.onosproject.net.host.HostEvent;
70import org.onosproject.net.host.HostListener;
Charles Chan873661e2017-11-30 15:37:50 -080071import org.onosproject.net.host.HostLocationProbingService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080072import org.onosproject.net.host.HostService;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070073import org.onosproject.net.host.InterfaceIpAddress;
Ray Milkeyae0068a2017-08-15 11:02:29 -070074import org.onosproject.net.intf.Interface;
75import org.onosproject.net.intf.InterfaceService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080076import org.onosproject.net.link.LinkEvent;
77import org.onosproject.net.link.LinkListener;
78import org.onosproject.net.link.LinkService;
Charles Chanc91c8782016-03-30 17:54:24 -070079import org.onosproject.net.mcast.McastEvent;
80import org.onosproject.net.mcast.McastListener;
81import org.onosproject.net.mcast.MulticastRouteService;
Ray Milkeyae0068a2017-08-15 11:02:29 -070082import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventreb6a7f342016-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 Ventref3cf5b92016-11-09 14:17:26 -080087import org.onosproject.net.topology.PathService;
Charles Chanc91c8782016-03-30 17:54:24 -070088import org.onosproject.net.topology.TopologyService;
Charles Chanf0ae41e2017-08-23 13:55:39 -070089import org.onosproject.routeservice.ResolvedRoute;
Ray Milkeyae0068a2017-08-15 11:02:29 -070090import org.onosproject.routeservice.RouteEvent;
91import org.onosproject.routeservice.RouteListener;
92import org.onosproject.routeservice.RouteService;
Charles Chanc91c8782016-03-30 17:54:24 -070093import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
94import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventre6b19e482016-11-07 16:21:04 -080095import org.onosproject.segmentrouting.config.PwaasConfig;
Pier Ventre6b19e482016-11-07 16:21:04 -080096import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkeyae0068a2017-08-15 11:02:29 -070097import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chan82f19972016-05-17 13:13:55 -070098import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chanc91c8782016-03-30 17:54:24 -070099import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -0700100import org.onosproject.segmentrouting.grouphandler.DestinationSet;
101import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700102import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
103import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Ray Milkeyae0068a2017-08-15 11:02:29 -0700104import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Saurav Das261c3002017-06-13 15:35:54 -0700105import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -0700106import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan10b0fb72017-02-02 16:20:42 -0800107import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chan82f19972016-05-17 13:13:55 -0700108import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hart54541d12016-04-12 15:39:44 -0700109import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-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 Chan873661e2017-11-30 15:37:50 -0800114import org.osgi.service.component.ComponentContext;
sangho80f11cb2015-04-01 13:05:26 -0700115import org.slf4j.Logger;
116import org.slf4j.LoggerFactory;
117
Jonathan Hart61e24e12017-11-30 18:23:42 -0800118import java.util.Collections;
Charles Chan873661e2017-11-30 15:37:50 -0800119import java.util.Dictionary;
Jonathan Hart61e24e12017-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;
sangho80f11cb2015-04-01 13:05:26 -0700134
Charles Chand6d25332016-02-26 22:19:52 -0800135import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800136import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700137import static org.onlab.util.Tools.groupedThreads;
Charles Chand6d25332016-02-26 22:19:52 -0800138
Charles Chanb7f75ac2016-01-11 18:28:54 -0800139/**
140 * Segment routing manager.
141 */
Jonathan Hart54541d12016-04-12 15:39:44 -0700142@Service
143@Component(immediate = true)
sangho27462c62015-05-14 00:39:53 -0700144public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700145
Charles Chan46fdfaf2016-11-09 20:51:44 -0800146 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan910be6a2017-08-23 14:46:43 -0700147 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sangho80f11cb2015-04-01 13:05:26 -0700148
149 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700150 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700151
152 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventreb6a7f342016-11-26 21:05:22 -0800153 private NeighbourResolutionService neighbourResolutionService;
154
155 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800156 public PathService pathService;
157
158 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700159 CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700160
161 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700162 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700163
164 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700165 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700166
167 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan873661e2017-11-30 15:37:50 -0800168 HostLocationProbingService probingService;
169
170 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700171 DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700172
173 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800174 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700175
176 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700177 LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700178
Charles Chan82ab1932016-01-30 23:22:37 -0800179 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800180 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700181
182 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800183 public StorageService storageService;
Charles Chandebfea32016-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 Chandebfea32016-10-24 14:52:01 -0700192 RouteService routeService;
Charles Chan82ab1932016-01-30 23:22:37 -0800193
194 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800195 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800196
Saurav Dasc3604f12016-03-23 11:22:49 -0700197 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800198 public InterfaceService interfaceService;
199
Charles Chan873661e2017-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 Chandebfea32016-10-24 14:52:01 -0700204 ArpHandler arpHandler = null;
205 IcmpHandler icmpHandler = null;
206 IpHandler ipHandler = null;
207 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700208 ApplicationId appId;
209 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700210
Charles Chandebfea32016-10-24 14:52:01 -0700211 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700212 private TunnelHandler tunnelHandler = null;
213 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700214 private InternalPacketProcessor processor = null;
215 private InternalLinkListener linkListener = null;
216 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700217 private AppConfigHandler appCfgHandler = null;
Charles Chandebfea32016-10-24 14:52:01 -0700218 XConnectHandler xConnectHandler = null;
Charles Chan1eaf4802016-04-18 13:44:03 -0700219 private McastHandler mcastHandler = null;
Charles Chan3ed34d82017-06-22 18:03:14 -0700220 private HostHandler hostHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800221 private RouteHandler routeHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800222 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Pier Ventre6b19e482016-11-07 16:21:04 -0800223 private L2TunnelHandler l2TunnelHandler = null;
sangho80f11cb2015-04-01 13:05:26 -0700224 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan82ab1932016-01-30 23:22:37 -0800225 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700226 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
227 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chandebfea32016-10-24 14:52:01 -0700228 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sangho80f11cb2015-04-01 13:05:26 -0700229
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700230 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700231 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sangho80f11cb2015-04-01 13:05:26 -0700232
Saurav Das2d94d312015-11-24 23:21:05 -0800233 @SuppressWarnings("unused")
sangho80f11cb2015-04-01 13:05:26 -0700234 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das2d94d312015-11-24 23:21:05 -0800235 @SuppressWarnings("rawtypes")
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700236 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Charles Chanf4586112015-11-09 16:37:23 -0800237 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chanb7f75ac2016-01-11 18:28:54 -0800238 new ConcurrentHashMap<>();
239 /**
Saurav Das261c3002017-06-13 15:35:54 -0700240 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700241 * Used to keep track on MPLS group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800242 */
Charles Chan873661e2017-11-30 15:37:50 -0800243 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das261c3002017-06-13 15:35:54 -0700244 dsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800245 /**
Charles Chan53de91f2017-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 Chanb7f75ac2016-01-11 18:28:54 -0800248 */
Charles Chan873661e2017-11-30 15:37:50 -0800249 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800250 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800251 /**
Charles Chan53de91f2017-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 Chanb7f75ac2016-01-11 18:28:54 -0800254 */
Charles Chan873661e2017-11-30 15:37:50 -0800255 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800256 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800257
Saurav Das62ae6792017-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 Das2d94d312015-11-24 23:21:05 -0800268 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
269 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700270
Saurav Das261c3002017-06-13 15:35:54 -0700271 private AtomicBoolean programmingScheduled = new AtomicBoolean();
272
Charles Chanc91c8782016-03-30 17:54:24 -0700273 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700274 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
275 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700276 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700277 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800278 public SegmentRoutingDeviceConfig createConfig() {
279 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700280 }
281 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800282
Charles Chanc91c8782016-03-30 17:54:24 -0700283 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700284 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
285 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700286 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800287 @Override
288 public SegmentRoutingAppConfig createConfig() {
289 return new SegmentRoutingAppConfig();
290 }
291 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800292
Charles Chan82f19972016-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 Ventre6b19e482016-11-07 16:21:04 -0800302
Charles Chanc91c8782016-03-30 17:54:24 -0700303 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700304 new ConfigFactory<ApplicationId, McastConfig>(
305 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700306 McastConfig.class, "multicast") {
307 @Override
308 public McastConfig createConfig() {
309 return new McastConfig();
310 }
311 };
312
Pier Ventre6b19e482016-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 Chan3ed34d82017-06-22 18:03:14 -0700323 private static final Object THREAD_SCHED_LOCK = new Object();
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700324 private static int numOfEventsQueued = 0;
325 private static int numOfEventsExecuted = 0;
sangho80f11cb2015-04-01 13:05:26 -0700326 private static int numOfHandlerExecution = 0;
327 private static int numOfHandlerScheduled = 0;
328
Charles Chan1963f4f2016-02-18 14:22:42 -0800329 /**
330 * Segment Routing App ID.
331 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800332 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan10b0fb72017-02-02 16:20:42 -0800333
Charles Chanb7f75ac2016-01-11 18:28:54 -0800334 /**
335 * The default VLAN ID assigned to the interfaces without subnet config.
336 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800337 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das7c305372015-10-28 12:39:42 -0700338
sangho80f11cb2015-04-01 13:05:26 -0700339 @Activate
Charles Chan873661e2017-11-30 15:37:50 -0800340 protected void activate(ComponentContext context) {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800341 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700342
343 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das261c3002017-06-13 15:35:54 -0700344 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700345 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das261c3002017-06-13 15:35:54 -0700346 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700347 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700348 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700349 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700350 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700351 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700352
Charles Chan10b0fb72017-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 Hart54541d12016-04-12 15:39:44 -0700358 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700359 .withTimestampProvider((k, v) -> new WallClockTimestamp())
360 .build();
361
Saurav Das2d94d312015-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 Hart54541d12016-04-12 15:39:44 -0700367 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800368 .withTimestampProvider((k, v) -> new WallClockTimestamp())
369 .build();
370
sangho4a5c42a2015-05-20 22:16:38 -0700371 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
372 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700373 tunnelStore = tunnelMapBuilder
374 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700375 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700376 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700377 .build();
378
379 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
380 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700381 policyStore = policyMapBuilder
382 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700383 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700384 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700385 .build();
386
Saurav Dasc3604f12016-03-23 11:22:49 -0700387 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800388 "purgeOnDisconnection", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700389 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800390 "purgeOnDisconnection", "true");
Pier Luigib9632ba2017-01-12 18:14:58 -0800391 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
392 "requestInterceptsEnabled", "false");
Charles Chanc1909e12017-08-08 15:13:37 -0700393 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800394 "requestInterceptsEnabled", "false");
Charles Chan9597f8d2017-07-24 15:56:10 -0700395 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800396 "arpEnabled", "false");
Pier Luigi0ebeb312017-02-02 22:31:34 -0800397 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
398 "greedyLearningIpv6", "true");
Charles Chancf8ea472017-02-28 15:15:17 -0800399 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
400 "forceUnprovision", "true");
Charles Chanc7b73c72017-08-10 16:57:28 -0700401 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan4c95c0d2017-07-20 16:16:25 -0700402 "distributed", "true");
Charles Chan804772f2017-08-14 11:42:11 -0700403 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
404 "multihomingEnabled", "true");
Charles Chan0c9c19f2017-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 Chan873661e2017-11-30 15:37:50 -0800409 compCfgService.registerProperties(getClass());
410 modified(context);
Saurav Dasc3604f12016-03-23 11:22:49 -0700411
Charles Chan2b078ae2015-10-14 11:24:40 -0700412 processor = new InternalPacketProcessor();
413 linkListener = new InternalLinkListener();
414 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700415 appCfgHandler = new AppConfigHandler(this);
416 xConnectHandler = new XConnectHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700417 mcastHandler = new McastHandler(this);
418 hostHandler = new HostHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700419 routeHandler = new RouteHandler(this);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800420 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Pier Ventre6b19e482016-11-07 16:21:04 -0800421 l2TunnelHandler = new L2TunnelHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700422
Charles Chand6d25332016-02-26 22:19:52 -0800423 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700424 cfgService.registerConfigFactory(deviceConfigFactory);
425 cfgService.registerConfigFactory(appConfigFactory);
Charles Chan82f19972016-05-17 13:13:55 -0700426 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700427 cfgService.registerConfigFactory(mcastConfigFactory);
Pier Ventre6b19e482016-11-07 16:21:04 -0800428 cfgService.registerConfigFactory(pwaasConfigFactory);
Charles Chan6961f222018-01-04 14:26:07 -0800429
430 cfgListener.configureNetwork();
431
Charles Chan82ab1932016-01-30 23:22:37 -0800432 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700433 packetService.addProcessor(processor, PacketProcessor.director(2));
434 linkService.addListener(linkListener);
435 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700436 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700437 routeService.addListener(routeListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700438
sangho80f11cb2015-04-01 13:05:26 -0700439 log.info("Started");
440 }
441
Jonathan Hart54541d12016-04-12 15:39:44 -0700442 private KryoNamespace.Builder createSerializer() {
443 return new KryoNamespace.Builder()
444 .register(KryoNamespaces.API)
Saurav Das261c3002017-06-13 15:35:54 -0700445 .register(DestinationSetNextObjectiveStoreKey.class,
Charles Chan10b0fb72017-02-02 16:20:42 -0800446 VlanNextObjectiveStoreKey.class,
Saurav Das261c3002017-06-13 15:35:54 -0700447 DestinationSet.class,
448 NextNeighbors.class,
Jonathan Hart54541d12016-04-12 15:39:44 -0700449 Tunnel.class,
450 DefaultTunnel.class,
451 Policy.class,
452 TunnelPolicy.class,
453 Policy.Type.class,
454 PortNextObjectiveStoreKey.class,
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700455 XConnectStoreKey.class,
456 DefaultL2Tunnel.class,
457 DefaultL2TunnelPolicy.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700458 );
459 }
460
sangho80f11cb2015-04-01 13:05:26 -0700461 @Deactivate
462 protected void deactivate() {
Charles Chan72f556a2015-10-05 17:50:33 -0700463 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700464 cfgService.unregisterConfigFactory(deviceConfigFactory);
465 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chandebfea32016-10-24 14:52:01 -0700466 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700467 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventre6b19e482016-11-07 16:21:04 -0800468 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chan873661e2017-11-30 15:37:50 -0800469 compCfgService.unregisterProperties(getClass(), false);
Charles Chan72f556a2015-10-05 17:50:33 -0700470
Charles Chanfd48c222017-06-19 00:43:31 -0700471 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700472 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700473 linkService.removeListener(linkListener);
474 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700475 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700476 routeService.removeListener(routeListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700477
Charles Chan2e71ef32017-02-23 15:44:08 -0800478 neighbourResolutionService.unregisterNeighbourHandlers(appId);
479
sangho80f11cb2015-04-01 13:05:26 -0700480 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700481 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700482 deviceListener = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700483 groupHandlerMap.clear();
484
Saurav Das261c3002017-06-13 15:35:54 -0700485 dsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800486 vlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700487 portNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700488 tunnelStore.destroy();
489 policyStore.destroy();
sangho80f11cb2015-04-01 13:05:26 -0700490 log.info("Stopped");
491 }
492
Charles Chan873661e2017-11-30 15:37:50 -0800493 @Modified
494 private void modified(ComponentContext context) {
495 Dictionary<?, ?> properties = context.getProperties();
496 if (properties == null) {
497 return;
498 }
499
500 String strActiveProving = Tools.get(properties, "activeProbing");
501 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
502
503 if (expectActiveProbing != activeProbing) {
504 activeProbing = expectActiveProbing;
505 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
506 }
507 }
508
sangho27462c62015-05-14 00:39:53 -0700509 @Override
510 public List<Tunnel> getTunnels() {
511 return tunnelHandler.getTunnels();
512 }
513
514 @Override
sanghobd812f82015-06-29 14:58:47 -0700515 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
516 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700517 }
518
519 @Override
sanghobd812f82015-06-29 14:58:47 -0700520 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700521 for (Policy policy: policyHandler.getPolicies()) {
522 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
523 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
524 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
525 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700526 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700527 }
528 }
529 }
sanghobd812f82015-06-29 14:58:47 -0700530 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700531 }
532
533 @Override
sanghobd812f82015-06-29 14:58:47 -0700534 public PolicyHandler.Result removePolicy(Policy policy) {
535 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700536 }
537
538 @Override
sanghobd812f82015-06-29 14:58:47 -0700539 public PolicyHandler.Result createPolicy(Policy policy) {
540 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700541 }
542
543 @Override
544 public List<Policy> getPolicies() {
545 return policyHandler.getPolicies();
546 }
547
Saurav Das07c74602016-04-27 18:35:50 -0700548 @Override
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700549 public List<DefaultL2Tunnel> getL2Tunnels() {
550 return l2TunnelHandler.getL2Tunnels();
551 }
552
553 @Override
554 public List<DefaultL2TunnelPolicy> getL2Policies() {
555 return l2TunnelHandler.getL2Policies();
556 }
557
558 @Override
559 public L2TunnelHandler.Result addPseudowire(String tunnelId, String pwLabel, String cP1,
560 String cP1InnerVlan, String cP1OuterVlan, String cP2,
561 String cP2InnerVlan, String cP2OuterVlan,
562 String mode, String sdTag) {
Andreas Pantelopoulosafc637f2017-12-20 18:04:27 -0800563 // Try to inject an empty Pwaas config if it is not found for the first time
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700564 PwaasConfig config = cfgService.getConfig(appId(), PwaasConfig.class);
565 if (config == null) {
Andreas Pantelopoulosafc637f2017-12-20 18:04:27 -0800566 log.debug("Pwaas config not found. Try to create an empty one.");
567 cfgService.applyConfig(appId(), PwaasConfig.class, new ObjectMapper().createObjectNode());
568 config = cfgService.getConfig(appId(), PwaasConfig.class);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700569 }
570
571 ObjectNode object = config.addPseudowire(tunnelId, pwLabel,
572 cP1, cP1InnerVlan, cP1OuterVlan,
573 cP2, cP2InnerVlan, cP2OuterVlan,
574 mode, sdTag);
575 if (object == null) {
576 log.warn("Could not add pseudowire to the configuration!");
577 return L2TunnelHandler.Result.ADDITION_ERROR;
578 }
579
580 // inform everyone about the valid change in the pw configuration
581 cfgService.applyConfig(appId(), PwaasConfig.class, object);
582 return L2TunnelHandler.Result.SUCCESS;
583 }
584
585 @Override
586 public L2TunnelHandler.Result removePseudowire(String pwId) {
587
588 PwaasConfig config = cfgService.getConfig(appId(), PwaasConfig.class);
589 if (config == null) {
590 log.warn("Configuration for Pwaas class could not be found!");
591 return L2TunnelHandler.Result.CONFIG_NOT_FOUND;
592 }
593
594 ObjectNode object = config.removePseudowire(pwId);
595 if (object == null) {
596 log.warn("Could not delete pseudowire from configuration!");
597 return L2TunnelHandler.Result.REMOVAL_ERROR;
598 }
599
600 // sanity check, this should never fail since we removed a pw
601 // and we always check when we update the configuration
602 config.isValid();
603
604 // inform everyone
605 cfgService.applyConfig(appId(), PwaasConfig.class, object);
606
607 return L2TunnelHandler.Result.SUCCESS;
608 }
609
610 @Override
Saurav Das07c74602016-04-27 18:35:50 -0700611 public void rerouteNetwork() {
612 cfgListener.configureNetwork();
Saurav Das07c74602016-04-27 18:35:50 -0700613 }
614
Charles Chand7844e52016-10-20 17:02:44 -0700615 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800616 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
617 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart61e24e12017-11-30 18:23:42 -0800618 deviceConfiguration.getRouters().forEach(device ->
619 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chand7844e52016-10-20 17:02:44 -0700620 return deviceSubnetMap;
621 }
622
Saurav Das62ae6792017-05-15 15:34:25 -0700623
624 @Override
625 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
626 if (defaultRoutingHandler != null) {
627 return defaultRoutingHandler.getCurrentEmcpSpgMap();
628 } else {
629 return null;
630 }
631 }
632
633 @Override
Saurav Das261c3002017-06-13 15:35:54 -0700634 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
635 if (dsNextObjStore != null) {
636 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Das62ae6792017-05-15 15:34:25 -0700637 } else {
638 return ImmutableMap.of();
639 }
640 }
641
Saurav Dasfbe74572017-08-03 18:30:35 -0700642 @Override
643 public void verifyGroups(DeviceId id) {
644 DefaultGroupHandler gh = groupHandlerMap.get(id);
645 if (gh != null) {
646 gh.triggerBucketCorrector();
647 }
648 }
649
sangho80f1f892015-05-19 11:57:42 -0700650 /**
Ray Milkeyb85de082017-04-05 09:42:04 -0700651 * Extracts the application ID from the manager.
652 *
653 * @return application ID
654 */
655 public ApplicationId appId() {
656 return appId;
657 }
658
659 /**
660 * Returns the device configuration.
661 *
662 * @return device configuration
663 */
664 public DeviceConfiguration deviceConfiguration() {
665 return deviceConfiguration;
666 }
667
668 /**
Saurav Das261c3002017-06-13 15:35:54 -0700669 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700670 * Used to keep track on MPLS group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700671 *
672 * @return next objective ID store
673 */
Saurav Das261c3002017-06-13 15:35:54 -0700674 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
675 dsNextObjStore() {
676 return dsNextObjStore;
Ray Milkeyb85de082017-04-05 09:42:04 -0700677 }
678
679 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700680 * Per device next objective ID store with (device id + vlanid) as key.
681 * Used to keep track on L2 flood group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700682 *
683 * @return vlan next object store
684 */
685 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
686 return vlanNextObjStore;
687 }
688
689 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700690 * Per device next objective ID store with (device id + port + treatment + meta) as key.
691 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700692 *
693 * @return port next object store.
694 */
695 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
696 return portNextObjStore;
697 }
698
699 /**
Saurav Das261c3002017-06-13 15:35:54 -0700700 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
701 * labeled packets should be programmed or not.
Pier Ventre7a78de22016-10-31 15:00:01 -0700702 *
703 * @return MPLS-ECMP value
704 */
705 public boolean getMplsEcmp() {
706 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
707 .getConfig(this.appId, SegmentRoutingAppConfig.class);
708 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
709 }
710
711 /**
sangho80f1f892015-05-19 11:57:42 -0700712 * Returns the tunnel object with the tunnel ID.
713 *
714 * @param tunnelId Tunnel ID
715 * @return Tunnel reference
716 */
sangho27462c62015-05-14 00:39:53 -0700717 public Tunnel getTunnel(String tunnelId) {
718 return tunnelHandler.getTunnel(tunnelId);
719 }
720
Charles Chan90772a72017-02-08 15:52:08 -0800721 // TODO Consider moving these to InterfaceService
sangho80f11cb2015-04-01 13:05:26 -0700722 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800723 * Returns untagged VLAN configured on given connect point.
Charles Chan90772a72017-02-08 15:52:08 -0800724 * <p>
725 * Only returns the first match if there are multiple untagged VLAN configured
726 * on the connect point.
sangho80f11cb2015-04-01 13:05:26 -0700727 *
Charles Chan10b0fb72017-02-02 16:20:42 -0800728 * @param connectPoint connect point
729 * @return untagged VLAN or null if not configured
sangho80f11cb2015-04-01 13:05:26 -0700730 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700731 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800732 return interfaceService.getInterfacesByPort(connectPoint).stream()
733 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
734 .map(Interface::vlanUntagged)
735 .findFirst().orElse(null);
sangho80f11cb2015-04-01 13:05:26 -0700736 }
737
sangho27462c62015-05-14 00:39:53 -0700738 /**
Charles Chan90772a72017-02-08 15:52:08 -0800739 * Returns tagged VLAN configured on given connect point.
740 * <p>
741 * Returns all matches if there are multiple tagged VLAN configured
742 * on the connect point.
743 *
744 * @param connectPoint connect point
745 * @return tagged VLAN or empty set if not configured
746 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700747 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan90772a72017-02-08 15:52:08 -0800748 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
749 return interfaces.stream()
750 .map(Interface::vlanTagged)
Charles Chan3ed34d82017-06-22 18:03:14 -0700751 .flatMap(Set::stream)
Charles Chan90772a72017-02-08 15:52:08 -0800752 .collect(Collectors.toSet());
753 }
754
755 /**
756 * Returns native VLAN configured on given connect point.
757 * <p>
758 * Only returns the first match if there are multiple native VLAN configured
759 * on the connect point.
760 *
761 * @param connectPoint connect point
762 * @return native VLAN or null if not configured
763 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700764 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan90772a72017-02-08 15:52:08 -0800765 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
766 return interfaces.stream()
767 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
768 .map(Interface::vlanNative)
769 .findFirst()
770 .orElse(null);
771 }
772
773 /**
Charles Chand9265a32017-06-16 15:19:24 -0700774 * Returns internal VLAN for untagged hosts on given connect point.
775 * <p>
776 * The internal VLAN is either vlan-untagged for an access port,
777 * or vlan-native for a trunk port.
778 *
779 * @param connectPoint connect point
780 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
781 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700782 VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chand9265a32017-06-16 15:19:24 -0700783 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
784 VlanId nativeVlanId = getNativeVlanId(connectPoint);
785 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
786 }
787
788 /**
Charles Chan3ed34d82017-06-22 18:03:14 -0700789 * Returns optional pair device ID of given device.
790 *
791 * @param deviceId device ID
792 * @return optional pair device ID. Might be empty if pair device is not configured
793 */
794 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
795 SegmentRoutingDeviceConfig deviceConfig =
796 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
797 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
798 }
799 /**
800 * Returns optional pair device local port of given device.
801 *
802 * @param deviceId device ID
803 * @return optional pair device ID. Might be empty if pair device is not configured
804 */
805 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
806 SegmentRoutingDeviceConfig deviceConfig =
807 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
808 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
809 }
810
811 /**
Charles Chan910be6a2017-08-23 14:46:43 -0700812 * Determine if current instance is the master of given connect point.
813 *
814 * @param cp connect point
815 * @return true if current instance is the master of given connect point
816 */
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700817 public boolean isMasterOf(ConnectPoint cp) {
Charles Chan910be6a2017-08-23 14:46:43 -0700818 boolean isMaster = mastershipService.isLocalMaster(cp.deviceId());
819 if (!isMaster) {
820 log.debug(NOT_MASTER, cp);
821 }
822 return isMaster;
823 }
824
825 /**
Charles Chanf0ae41e2017-08-23 13:55:39 -0700826 * Returns locations of given resolved route.
827 *
828 * @param resolvedRoute resolved route
829 * @return locations of nexthop. Might be empty if next hop is not found
830 */
831 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
832 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
833 return Optional.ofNullable(hostService.getHost(hostId))
834 .map(Host::locations).orElse(Sets.newHashSet())
835 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
836 }
837
838 /**
Charles Chan90772a72017-02-08 15:52:08 -0800839 * Returns vlan port map of given device.
840 *
841 * @param deviceId device id
842 * @return vlan-port multimap
843 */
844 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
845 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
846
847 interfaceService.getInterfaces().stream()
848 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
849 .forEach(intf -> {
850 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan3ed34d82017-06-22 18:03:14 -0700851 intf.vlanTagged().forEach(vlanTagged ->
852 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
853 );
Charles Chan90772a72017-02-08 15:52:08 -0800854 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
855 });
856 vlanPortMap.removeAll(VlanId.NONE);
857
858 return vlanPortMap;
859 }
860
861 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800862 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -0800863 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -0700864 *
865 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -0800866 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -0800867 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -0700868 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700869 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -0700870 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800871 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
872 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -0700873 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -0800874 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -0800875 + "device {} not found", deviceId);
876 return -1;
877 }
878 }
879
880 /**
881 * Returns the next objective ID for the given portNumber, given the treatment.
882 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -0700883 * would result in different objectives. If the next object does not exist,
884 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -0800885 *
886 * @param deviceId Device ID
887 * @param portNum port number on device for which NextObjective is queried
888 * @param treatment the actions to apply on the packets (should include outport)
889 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -0700890 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -0700891 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -0800892 */
893 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
894 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -0700895 TrafficSelector meta,
896 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -0800897 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
898 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -0700899 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -0800900 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -0800901 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
902 + " not found", deviceId);
903 return -1;
904 }
905 }
906
Saurav Das62ae6792017-05-15 15:34:25 -0700907 /**
908 * Returns the group handler object for the specified device id.
909 *
910 * @param devId the device identifier
911 * @return the groupHandler object for the device id, or null if not found
912 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700913 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Das62ae6792017-05-15 15:34:25 -0700914 return groupHandlerMap.get(devId);
915 }
916
917 /**
Saurav Dasfbe74572017-08-03 18:30:35 -0700918 * Returns the default routing handler object.
919 *
920 * @return the default routing handler object
921 */
922 public DefaultRoutingHandler getRoutingHandler() {
923 return defaultRoutingHandler;
924 }
925
926 /**
Saurav Das62ae6792017-05-15 15:34:25 -0700927 * Returns true if this controller instance has seen this link before. The
928 * link may not be currently up, but as long as the link had been seen before
929 * this method will return true. The one exception is when the link was
930 * indeed seen before, but this controller instance was forced to forget it
931 * by a call to purgeSeenLink method.
932 *
933 * @param link the infrastructure link being queried
934 * @return true if this controller instance has seen this link before
935 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700936 boolean isSeenLink(Link link) {
Saurav Das62ae6792017-05-15 15:34:25 -0700937 return seenLinks.containsKey(link);
938 }
939
940 /**
941 * Updates the seen link store. Updates can be for links that are currently
942 * available or not.
943 *
944 * @param link the link to update in the seen-link local store
945 * @param up the status of the link, true if up, false if down
946 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700947 void updateSeenLink(Link link, boolean up) {
Saurav Das62ae6792017-05-15 15:34:25 -0700948 seenLinks.put(link, up);
949 }
950
951 /**
952 * Returns the status of a seen-link (up or down). If the link has not
953 * been seen-before, a null object is returned.
954 *
955 * @param link the infrastructure link being queried
956 * @return null if the link was not seen-before;
957 * true if the seen-link is up;
958 * false if the seen-link is down
959 */
Charles Chan873661e2017-11-30 15:37:50 -0800960 private Boolean isSeenLinkUp(Link link) {
Saurav Das62ae6792017-05-15 15:34:25 -0700961 return seenLinks.get(link);
962 }
963
964 /**
965 * Makes this controller instance forget a previously seen before link.
966 *
967 * @param link the infrastructure link to purge
968 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700969 private void purgeSeenLink(Link link) {
Saurav Das62ae6792017-05-15 15:34:25 -0700970 seenLinks.remove(link);
971 }
972
973 /**
974 * Returns the status of a link as parallel link. A parallel link
975 * is defined as a link which has common src and dst switches as another
976 * seen-link that is currently enabled. It is not necessary for the link being
977 * queried to be a seen-link.
978 *
979 * @param link the infrastructure link being queried
980 * @return true if a seen-link exists that is up, and shares the
981 * same src and dst switches as the link being queried
982 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700983 private boolean isParallelLink(Link link) {
Saurav Das62ae6792017-05-15 15:34:25 -0700984 for (Entry<Link, Boolean> seen : seenLinks.entrySet()) {
985 Link seenLink = seen.getKey();
986 if (seenLink.equals(link)) {
987 continue;
988 }
989 if (seenLink.src().deviceId().equals(link.src().deviceId()) &&
990 seenLink.dst().deviceId().equals(link.dst().deviceId()) &&
991 seen.getValue()) {
992 return true;
993 }
994 }
995 return false;
996 }
997
Saurav Das261c3002017-06-13 15:35:54 -0700998 /**
999 * Returns true if the link being queried is a bidirectional link. A bidi
1000 * link is defined as a link, whose reverse link - ie. the link in the reverse
1001 * direction - has been seen-before and is up. It is not necessary for the link
1002 * being queried to be a seen-link.
1003 *
1004 * @param link the infrastructure link being queried
1005 * @return true if another unidirectional link exists in the reverse direction,
1006 * has been seen-before and is up
1007 */
Charles Chan873661e2017-11-30 15:37:50 -08001008 boolean isBidirectional(Link link) {
Saurav Das261c3002017-06-13 15:35:54 -07001009 Link reverseLink = linkService.getLink(link.dst(), link.src());
1010 if (reverseLink == null) {
1011 return false;
1012 }
1013 Boolean result = isSeenLinkUp(reverseLink);
1014 if (result == null) {
1015 return false;
1016 }
1017 return result.booleanValue();
1018 }
1019
1020 /**
1021 * Determines if the given link should be avoided in routing calculations
1022 * by policy or design.
1023 *
1024 * @param link the infrastructure link being queried
1025 * @return true if link should be avoided
1026 */
Charles Chan873661e2017-11-30 15:37:50 -08001027 boolean avoidLink(Link link) {
Saurav Das261c3002017-06-13 15:35:54 -07001028 // XXX currently only avoids all pair-links. In the future can be
1029 // extended to avoid any generic link
1030 DeviceId src = link.src().deviceId();
1031 PortNumber srcPort = link.src().port();
1032 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(src)) {
1033 log.warn("Device {} not configured..cannot avoid link {}", src, link);
1034 return false;
1035 }
1036 DeviceId pairDev;
1037 PortNumber pairLocalPort, pairRemotePort = null;
1038 try {
1039 pairDev = deviceConfiguration.getPairDeviceId(src);
1040 pairLocalPort = deviceConfiguration.getPairLocalPort(src);
1041 if (pairDev != null) {
1042 pairRemotePort = deviceConfiguration.getPairLocalPort(pairDev);
1043 }
1044 } catch (DeviceConfigNotFoundException e) {
1045 log.warn("Pair dev for dev {} not configured..cannot avoid link {}",
1046 src, link);
1047 return false;
1048 }
1049
Charles Chan873661e2017-11-30 15:37:50 -08001050 return srcPort.equals(pairLocalPort) &&
Saurav Das261c3002017-06-13 15:35:54 -07001051 link.dst().deviceId().equals(pairDev) &&
Charles Chan873661e2017-11-30 15:37:50 -08001052 link.dst().port().equals(pairRemotePort);
Saurav Das261c3002017-06-13 15:35:54 -07001053 }
1054
sangho80f11cb2015-04-01 13:05:26 -07001055 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -07001056 @Override
1057 public void process(PacketContext context) {
1058
1059 if (context.isHandled()) {
1060 return;
1061 }
1062
1063 InboundPacket pkt = context.inPacket();
1064 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -08001065
1066 if (ethernet == null) {
1067 return;
1068 }
1069
Saurav Das261c3002017-06-13 15:35:54 -07001070 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
1071 ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -08001072 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -07001073 log.warn("Received unexpected ARP packet on {}",
1074 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -07001075 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001076 return;
sangho80f11cb2015-04-01 13:05:26 -07001077 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -08001078 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
1079 //ipHandler.addToPacketBuffer(ipv4Packet);
1080 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
1081 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001082 } else {
Charles Chand041ad82017-01-13 17:20:44 -08001083 // NOTE: We don't support IP learning at this moment so this
1084 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -08001085 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -07001086 }
Pier Ventreb6a7f342016-11-26 21:05:22 -08001087 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
1088 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -08001089 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -08001090 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -08001091 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
1092 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1093 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1094 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1095 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1096 } else {
Saurav Das62ae6792017-05-15 15:34:25 -07001097 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -07001098 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -08001099 }
1100 } else {
1101 // NOTE: We don't support IP learning at this moment so this
1102 // is not necessary. Also it causes duplication of DHCPv6 packets.
1103 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1104 }
sangho80f11cb2015-04-01 13:05:26 -07001105 }
1106 }
1107 }
1108
1109 private class InternalLinkListener implements LinkListener {
1110 @Override
1111 public void event(LinkEvent event) {
Charles Chan0cff8aa2017-03-29 16:39:05 -07001112 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1113 event.type() == LinkEvent.Type.LINK_UPDATED ||
1114 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001115 log.debug("Event {} received from Link Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001116 scheduleEventHandlerIfNotScheduled(event);
1117 }
1118 }
1119 }
1120
1121 private class InternalDeviceListener implements DeviceListener {
sangho80f11cb2015-04-01 13:05:26 -07001122 @Override
1123 public void event(DeviceEvent event) {
sangho80f11cb2015-04-01 13:05:26 -07001124 switch (event.type()) {
1125 case DEVICE_ADDED:
Saurav Dasf0f592d2016-11-18 15:21:57 -08001126 case PORT_UPDATED:
1127 case PORT_ADDED:
sanghofb7c7292015-04-13 15:15:58 -07001128 case DEVICE_UPDATED:
1129 case DEVICE_AVAILABILITY_CHANGED:
Saurav Das62ae6792017-05-15 15:34:25 -07001130 log.trace("Event {} received from Device Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001131 scheduleEventHandlerIfNotScheduled(event);
1132 break;
1133 default:
1134 }
1135 }
1136 }
1137
Saurav Das2d94d312015-11-24 23:21:05 -08001138 @SuppressWarnings("rawtypes")
sangho80f11cb2015-04-01 13:05:26 -07001139 private void scheduleEventHandlerIfNotScheduled(Event event) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001140 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001141 eventQueue.add(event);
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001142 numOfEventsQueued++;
1143
1144 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
1145 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001146 eventHandlerFuture = executorService
1147 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
1148 numOfHandlerScheduled++;
1149 }
Jonathan Hart54541d12016-04-12 15:39:44 -07001150 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001151 numOfEventsQueued,
1152 numOfHandlerScheduled);
sangho80f11cb2015-04-01 13:05:26 -07001153 }
sangho80f11cb2015-04-01 13:05:26 -07001154 }
1155
1156 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001157 @Override
sangho80f11cb2015-04-01 13:05:26 -07001158 public void run() {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001159 try {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001160 while (true) {
Saurav Das2d94d312015-11-24 23:21:05 -08001161 @SuppressWarnings("rawtypes")
Charles Chan3ed34d82017-06-22 18:03:14 -07001162 Event event;
1163 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001164 if (!eventQueue.isEmpty()) {
1165 event = eventQueue.poll();
1166 numOfEventsExecuted++;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001167 } else {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001168 numOfHandlerExecution++;
1169 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
1170 numOfHandlerExecution, numOfEventsExecuted);
1171 break;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001172 }
sanghofb7c7292015-04-13 15:15:58 -07001173 }
Charles Chan0cff8aa2017-03-29 16:39:05 -07001174 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1175 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Das62ae6792017-05-15 15:34:25 -07001176 // Note: do not update seenLinks here, otherwise every
1177 // link, even one seen for the first time, will be appear
1178 // to be a previously seen link
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001179 processLinkAdded((Link) event.subject());
1180 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre6d593892016-09-13 21:33:40 -07001181 Link linkRemoved = (Link) event.subject();
Saurav Das62ae6792017-05-15 15:34:25 -07001182 if (linkRemoved.type() == Link.Type.DIRECT) {
1183 updateSeenLink(linkRemoved, false);
1184 }
1185 // device availability check helps to ensure that
1186 // multiple link-removed events are actually treated as a
1187 // single switch removed event. purgeSeenLink is necessary
1188 // so we do rerouting (instead of rehashing) when switch
1189 // comes back.
Pier Ventre6d593892016-09-13 21:33:40 -07001190 if (linkRemoved.src().elementId() instanceof DeviceId &&
1191 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
Saurav Das62ae6792017-05-15 15:34:25 -07001192 purgeSeenLink(linkRemoved);
Pier Ventre6d593892016-09-13 21:33:40 -07001193 continue;
1194 }
1195 if (linkRemoved.dst().elementId() instanceof DeviceId &&
1196 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
Saurav Das62ae6792017-05-15 15:34:25 -07001197 purgeSeenLink(linkRemoved);
Pier Ventre6d593892016-09-13 21:33:40 -07001198 continue;
1199 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001200 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001201 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1202 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1203 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das62af8802015-12-04 10:52:59 -08001204 DeviceId deviceId = ((Device) event.subject()).id();
1205 if (deviceService.isAvailable(deviceId)) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001206 log.info("Processing device event {} for available device {}",
1207 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001208 processDeviceAdded((Device) event.subject());
Saurav Dasc3604f12016-03-23 11:22:49 -07001209 } else {
1210 log.info("Processing device event {} for unavailable device {}",
1211 event.type(), ((Device) event.subject()).id());
1212 processDeviceRemoved((Device) event.subject());
1213 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001214 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd1872b02016-12-02 15:43:47 -08001215 // typically these calls come when device is added first time
1216 // so port filtering rules are handled at the device_added event.
1217 // port added calls represent all ports on the device,
1218 // enabled or not.
Saurav Das62ae6792017-05-15 15:34:25 -07001219 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd1872b02016-12-02 15:43:47 -08001220 ((DeviceEvent) event).subject().id(),
1221 ((DeviceEvent) event).port().number(),
1222 event.type());
Saurav Dasf0f592d2016-11-18 15:21:57 -08001223 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd1872b02016-12-02 15:43:47 -08001224 // these calls happen for every subsequent event
1225 // ports enabled, disabled, switch goes away, comes back
Saurav Dasf0f592d2016-11-18 15:21:57 -08001226 log.info("** PORT UPDATED {}/{} -> {}",
1227 event.subject(),
1228 ((DeviceEvent) event).port(),
1229 event.type());
1230 processPortUpdated(((Device) event.subject()),
1231 ((DeviceEvent) event).port());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001232 } else {
1233 log.warn("Unhandled event type: {}", event.type());
1234 }
sangho80f11cb2015-04-01 13:05:26 -07001235 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001236 } catch (Exception e) {
1237 log.error("SegmentRouting event handler "
1238 + "thread thrown an exception: {}", e);
sangho80f11cb2015-04-01 13:05:26 -07001239 }
sangho80f11cb2015-04-01 13:05:26 -07001240 }
1241 }
1242
sangho80f11cb2015-04-01 13:05:26 -07001243 private void processLinkAdded(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -07001244 log.info("** LINK ADDED {}", link.toString());
Saurav Das04944532017-12-19 16:08:42 -08001245 if (!isLinkValid(link)) {
Saurav Das62ae6792017-05-15 15:34:25 -07001246 return;
1247 }
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001248 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
1249 updateSeenLink(link, true);
1250 // XXX revisit - what about devicePortMap
1251 log.warn("Source device of this link is not configured.. "
1252 + "not processing further");
1253 return;
1254 }
Saurav Das62ae6792017-05-15 15:34:25 -07001255
1256 //Irrespective of whether the local is a MASTER or not for this device,
1257 //create group handler instance and push default TTP flow rules if needed,
1258 //as in a multi-instance setup, instances can initiate groups for any device.
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001259 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
1260 .deviceId());
1261 if (groupHandler != null) {
Saurav Das62ae6792017-05-15 15:34:25 -07001262 groupHandler.portUpForLink(link);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001263 } else {
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001264 // XXX revisit/cleanup
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001265 Device device = deviceService.getDevice(link.src().deviceId());
1266 if (device != null) {
1267 log.warn("processLinkAdded: Link Added "
1268 + "Notification without Device Added "
1269 + "event, still handling it");
1270 processDeviceAdded(device);
1271 groupHandler = groupHandlerMap.get(link.src()
1272 .deviceId());
Saurav Das62ae6792017-05-15 15:34:25 -07001273 groupHandler.portUpForLink(link);
sangho80f11cb2015-04-01 13:05:26 -07001274 }
1275 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001276
Saurav Das261c3002017-06-13 15:35:54 -07001277 /*// process link only if it is bidirectional
1278 if (!isBidirectional(link)) {
1279 log.debug("Link not bidirectional.. waiting for other direction "
1280 + "src {} --> dst {} ", link.dst(), link.src());
1281 // note that if we are not processing for routing, it should at least
1282 // be considered a seen-link
1283 updateSeenLink(link, true);
1284 return;
1285 }
1286 TO DO this ensure that rehash is still done correctly even if link is
1287 not processed for rerouting - perhaps rehash in both directions when
1288 it ultimately becomes bidi?
1289 */
1290
1291 log.debug("Starting optimized route population process for link "
1292 + "{} --> {}", link.src(), link.dst());
Saurav Das62ae6792017-05-15 15:34:25 -07001293 boolean seenBefore = isSeenLink(link);
1294 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null, link, null);
Saurav Das261c3002017-06-13 15:35:54 -07001295
1296 // It's possible that linkUp causes no route-path change as ECMP graph does
1297 // not change if the link is a parallel link (same src-dst as another link.
1298 // However we still need to update ECMP hash groups to include new buckets
1299 // for the link that has come up.
Saurav Das62ae6792017-05-15 15:34:25 -07001300 if (mastershipService.isLocalMaster(link.src().deviceId())) {
Saurav Das261c3002017-06-13 15:35:54 -07001301 if (!seenBefore && isParallelLink(link)) {
Saurav Das62ae6792017-05-15 15:34:25 -07001302 // if link seen first time, we need to ensure hash-groups have all ports
Saurav Das261c3002017-06-13 15:35:54 -07001303 log.debug("Attempting retryHash for paralled first-time link {}", link);
Saurav Das62ae6792017-05-15 15:34:25 -07001304 groupHandler.retryHash(link, false, true);
1305 } else {
1306 //seen before-link
1307 if (isParallelLink(link)) {
Saurav Das261c3002017-06-13 15:35:54 -07001308 log.debug("Attempting retryHash for paralled seen-before "
1309 + "link {}", link);
Saurav Das62ae6792017-05-15 15:34:25 -07001310 groupHandler.retryHash(link, false, false);
1311 }
1312 }
1313 }
Charles Chan72779502016-04-23 17:36:10 -07001314
1315 mcastHandler.init();
sangho80f11cb2015-04-01 13:05:26 -07001316 }
1317
1318 private void processLinkRemoved(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -07001319 log.info("** LINK REMOVED {}", link.toString());
Saurav Das04944532017-12-19 16:08:42 -08001320 if (!isLinkValid(link)) {
1321 return;
1322 }
Saurav Das62ae6792017-05-15 15:34:25 -07001323 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link, null, null);
1324
1325 // update local groupHandler stores
sangho2165d222015-05-01 09:38:25 -07001326 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
1327 if (groupHandler != null) {
Saurav Das62ae6792017-05-15 15:34:25 -07001328 if (mastershipService.isLocalMaster(link.src().deviceId()) &&
1329 isParallelLink(link)) {
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001330 log.debug("* retrying hash for parallel link removed:{}", link);
Saurav Das62ae6792017-05-15 15:34:25 -07001331 groupHandler.retryHash(link, true, false);
Saurav Dasfe0b05e2017-08-14 16:44:43 -07001332 } else {
1333 log.debug("Not attempting retry-hash for link removed: {} .. {}", link,
1334 (mastershipService.isLocalMaster(link.src().deviceId()))
1335 ? "not parallel" : "not master");
Saurav Das62ae6792017-05-15 15:34:25 -07001336 }
1337 // ensure local stores are updated
1338 groupHandler.portDown(link.src().port());
1339 } else {
1340 log.warn("group handler not found for dev:{} when removing link: {}",
1341 link.src().deviceId(), link);
sangho2165d222015-05-01 09:38:25 -07001342 }
Charles Chan72779502016-04-23 17:36:10 -07001343
1344 mcastHandler.processLinkDown(link);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -07001345 l2TunnelHandler.processLinkDown(link);
sangho80f11cb2015-04-01 13:05:26 -07001346 }
1347
Saurav Das04944532017-12-19 16:08:42 -08001348 private boolean isLinkValid(Link link) {
1349 if (link.type() != Link.Type.DIRECT) {
1350 // NOTE: A DIRECT link might be transiently marked as INDIRECT
1351 // if BDDP is received before LLDP. We can safely ignore that
1352 // until the LLDP is received and the link is marked as DIRECT.
1353 log.info("Ignore link {}->{}. Link type is {} instead of DIRECT.",
1354 link.src(), link.dst(), link.type());
1355 return false;
1356 }
1357 DeviceId srcId = link.src().deviceId();
1358 DeviceId dstId = link.dst().deviceId();
1359 if (srcId.equals(dstId)) {
1360 log.warn("Links between ports on the same switch are not "
1361 + "allowed .. ignoring link {}", link);
1362 return false;
1363 }
1364 try {
1365 if (!deviceConfiguration.isEdgeDevice(srcId)
1366 && !deviceConfiguration.isEdgeDevice(dstId)) {
1367 // ignore links between spines
1368 // XXX revisit when handling multi-stage fabrics
1369 log.warn("Links between spines not allowed...ignoring "
1370 + "link {}", link);
1371 return false;
1372 }
1373 if (deviceConfiguration.isEdgeDevice(srcId)
1374 && deviceConfiguration.isEdgeDevice(dstId)) {
1375 // ignore links between leaves if they are not pair-links
1376 // XXX revisit if removing pair-link config or allowing more than
1377 // one pair-link
1378 if (deviceConfiguration.getPairDeviceId(srcId).equals(dstId)
1379 && deviceConfiguration.getPairLocalPort(srcId)
1380 .equals(link.src().port())
1381 && deviceConfiguration.getPairLocalPort(dstId)
1382 .equals(link.dst().port())) {
1383 // found pair link - allow it
1384 return true;
1385 } else {
1386 log.warn("Links between leaves other than pair-links are "
1387 + "not allowed...ignoring link {}", link);
1388 return false;
1389 }
1390 }
1391 } catch (DeviceConfigNotFoundException e) {
1392 // We still want to count the links in seenLinks even though there
1393 // is no config. So we let it return true
1394 log.warn("Could not check validity of link {} as subtending devices "
1395 + "are not yet configured", link);
1396 }
1397 return true;
1398 }
1399
sangho80f11cb2015-04-01 13:05:26 -07001400 private void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001401 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan9bd6aea2017-06-27 18:48:32 -07001402
1403 // NOTE: Punt ARP/NDP even when the device is not configured.
1404 // Host learning without network config is required for CORD config generator.
1405 routingRulePopulator.populateIpPunts(device.id());
1406 routingRulePopulator.populateArpNdpPunts(device.id());
1407
Charles Chan319d1a22015-11-03 10:42:14 -08001408 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das261c3002017-06-13 15:35:54 -07001409 log.warn("Device configuration unavailable. Device {} will be "
1410 + "processed after configuration.", device.id());
Saurav Das8ec0ec42015-11-03 14:39:27 -08001411 return;
1412 }
Charles Chan72779502016-04-23 17:36:10 -07001413 processDeviceAddedInternal(device.id());
1414 }
1415
1416 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001417 // Irrespective of whether the local is a MASTER or not for this device,
1418 // we need to create a SR-group-handler instance. This is because in a
1419 // multi-instance setup, any instance can initiate forwarding/next-objectives
1420 // for any switch (even if this instance is a SLAVE or not even connected
1421 // to the switch). To handle this, a default-group-handler instance is necessary
1422 // per switch.
Charles Chan72779502016-04-23 17:36:10 -07001423 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1424 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001425 DefaultGroupHandler groupHandler;
1426 try {
1427 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001428 createGroupHandler(deviceId,
1429 appId,
1430 deviceConfiguration,
1431 linkService,
1432 flowObjectiveService,
1433 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001434 } catch (DeviceConfigNotFoundException e) {
1435 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1436 return;
1437 }
Saurav Das2b6a00f2017-12-05 15:00:23 -08001438 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan72779502016-04-23 17:36:10 -07001439 deviceId);
1440 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001441 }
Saurav Dasb149be12016-06-07 10:08:06 -07001442
Charles Chan72779502016-04-23 17:36:10 -07001443 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001444 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chandebfea32016-10-24 14:52:01 -07001445 hostHandler.init(deviceId);
Charles Chan82f19972016-05-17 13:13:55 -07001446 xConnectHandler.init(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001447 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001448 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001449 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001450 }
Charles Chan82ab1932016-01-30 23:22:37 -08001451
Charles Chandebfea32016-10-24 14:52:01 -07001452 appCfgHandler.init(deviceId);
1453 routeHandler.init(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001454 }
1455
Saurav Dasc3604f12016-03-23 11:22:49 -07001456 private void processDeviceRemoved(Device device) {
Saurav Das261c3002017-06-13 15:35:54 -07001457 dsNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001458 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan873661e2017-11-30 15:37:50 -08001459 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan10b0fb72017-02-02 16:20:42 -08001460 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001461 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001462 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001463 portNextObjStore.entrySet().stream()
1464 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001465 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Charles Chan17d75d82017-06-15 00:44:51 -07001466
1467 seenLinks.keySet().removeIf(key -> key.src().deviceId().equals(device.id()) ||
1468 key.dst().deviceId().equals(device.id()));
1469
Saurav Dasfbe74572017-08-03 18:30:35 -07001470 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1471 if (gh != null) {
1472 gh.shutdown();
1473 }
Saurav Dasc3604f12016-03-23 11:22:49 -07001474 defaultRoutingHandler.purgeEcmpGraph(device.id());
Saurav Das62ae6792017-05-15 15:34:25 -07001475 // Note that a switch going down is associated with all of its links
1476 // going down as well, but it is treated as a single switch down event
1477 // while the link-downs are ignored.
1478 defaultRoutingHandler
1479 .populateRoutingRulesForLinkStatusChange(null, null, device.id());
Charles Chan72779502016-04-23 17:36:10 -07001480 mcastHandler.removeDevice(device.id());
Charles Chan82f19972016-05-17 13:13:55 -07001481 xConnectHandler.removeDevice(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -07001482 }
1483
Saurav Dasf0f592d2016-11-18 15:21:57 -08001484 private void processPortUpdated(Device device, Port port) {
1485 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1486 log.warn("Device configuration uploading. Not handling port event for"
1487 + "dev: {} port: {}", device.id(), port.number());
1488 return;
1489 }
Saurav Dasf9332192017-02-18 14:05:44 -08001490
1491 if (!mastershipService.isLocalMaster(device.id())) {
1492 log.debug("Not master for dev:{} .. not handling port updated event"
1493 + "for port {}", device.id(), port.number());
1494 return;
1495 }
1496
1497 // first we handle filtering rules associated with the port
1498 if (port.isEnabled()) {
1499 log.info("Switchport {}/{} enabled..programming filters",
1500 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001501 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001502 } else {
1503 log.info("Switchport {}/{} disabled..removing filters",
1504 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001505 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001506 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001507
1508 // portUpdated calls are for ports that have gone down or up. For switch
1509 // to switch ports, link-events should take care of any re-routing or
1510 // group editing necessary for port up/down. Here we only process edge ports
1511 // that are already configured.
Saurav Das3fb28272017-03-04 16:08:47 -08001512 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1513 VlanId untaggedVlan = getUntaggedVlanId(cp);
1514 VlanId nativeVlan = getNativeVlanId(cp);
1515 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001516
Saurav Das3fb28272017-03-04 16:08:47 -08001517 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001518 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasf0f592d2016-11-18 15:21:57 -08001519 + "dev/port: {}/{}", device.id(), port.number());
1520 return;
1521 }
Saurav Das3fb28272017-03-04 16:08:47 -08001522 if (untaggedVlan != null) {
1523 processEdgePort(device, port, untaggedVlan, true);
1524 }
1525 if (nativeVlan != null) {
1526 processEdgePort(device, port, nativeVlan, true);
1527 }
1528 if (!taggedVlans.isEmpty()) {
1529 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1530 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001531 }
1532
Saurav Das3fb28272017-03-04 16:08:47 -08001533 private void processEdgePort(Device device, Port port, VlanId vlanId,
1534 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001535 boolean portUp = port.isEnabled();
1536 if (portUp) {
Saurav Das3fb28272017-03-04 16:08:47 -08001537 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001538 port.number(), vlanId);
Charles Chan873661e2017-11-30 15:37:50 -08001539 hostHandler.processPortUp(new ConnectPoint(device.id(), port.number()));
Saurav Dasf0f592d2016-11-18 15:21:57 -08001540 } else {
Saurav Das3fb28272017-03-04 16:08:47 -08001541 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001542 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001543 }
1544
Srikanth Vavilapalli64505482015-04-21 13:04:13 -07001545 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sangho80f11cb2015-04-01 13:05:26 -07001546 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001547 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001548 } else {
1549 log.warn("Group handler not found for dev:{}. Not handling edge port"
1550 + " {} event for port:{}", device.id(),
1551 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001552 }
1553 }
sangho27462c62015-05-14 00:39:53 -07001554
Charles Chan8ca5a122017-10-20 16:06:55 -07001555 private void createOrUpdateDeviceConfiguration() {
1556 if (deviceConfiguration == null) {
1557 deviceConfiguration = new DeviceConfiguration(this);
1558 } else {
1559 deviceConfiguration.updateConfig();
1560 }
1561 }
1562
Pier Ventreb6a7f342016-11-26 21:05:22 -08001563 /**
1564 * Registers the given connect point with the NRS, this is necessary
1565 * to receive the NDP and ARP packets from the NRS.
1566 *
1567 * @param portToRegister connect point to register
1568 */
1569 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001570 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001571 portToRegister,
1572 neighbourHandler,
1573 appId
1574 );
1575 }
1576
Charles Chan72f556a2015-10-05 17:50:33 -07001577 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das261c3002017-06-13 15:35:54 -07001578 private static final long PROGRAM_DELAY = 2;
Charles Chan46fdfaf2016-11-09 20:51:44 -08001579 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001580
Charles Chanb7f75ac2016-01-11 18:28:54 -08001581 /**
1582 * Constructs the internal network config listener.
1583 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001584 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001585 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001586 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001587 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001588 }
1589
Charles Chanb7f75ac2016-01-11 18:28:54 -08001590 /**
1591 * Reads network config and initializes related data structure accordingly.
1592 */
Charles Chan873661e2017-11-30 15:37:50 -08001593 void configureNetwork() {
Charles Chan8ca5a122017-10-20 16:06:55 -07001594 createOrUpdateDeviceConfiguration();
Charles Chane7c61022015-10-07 14:21:45 -07001595
Charles Chan46fdfaf2016-11-09 20:51:44 -08001596 arpHandler = new ArpHandler(srManager);
1597 icmpHandler = new IcmpHandler(srManager);
1598 ipHandler = new IpHandler(srManager);
1599 routingRulePopulator = new RoutingRulePopulator(srManager);
1600 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chane7c61022015-10-07 14:21:45 -07001601
1602 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1603 groupHandlerMap, tunnelStore);
1604 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1605 flowObjectiveService,
1606 tunnelHandler, policyStore);
Saurav Das261c3002017-06-13 15:35:54 -07001607 // add a small delay to absorb multiple network config added notifications
1608 if (!programmingScheduled.get()) {
1609 programmingScheduled.set(true);
1610 executorService.schedule(new ConfigChange(), PROGRAM_DELAY,
1611 TimeUnit.SECONDS);
Charles Chane7c61022015-10-07 14:21:45 -07001612 }
Charles Chan72779502016-04-23 17:36:10 -07001613 mcastHandler.init();
Charles Chane7c61022015-10-07 14:21:45 -07001614 }
1615
Charles Chan72f556a2015-10-05 17:50:33 -07001616 @Override
1617 public void event(NetworkConfigEvent event) {
Charles Chan82ab1932016-01-30 23:22:37 -08001618 // TODO move this part to NetworkConfigEventHandler
1619 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1620 switch (event.type()) {
1621 case CONFIG_ADDED:
Charles Chan68363b12017-06-26 15:25:09 -07001622 log.info("Segment Routing Device Config added for {}", event.subject());
Charles Chan82ab1932016-01-30 23:22:37 -08001623 configureNetwork();
1624 break;
1625 case CONFIG_UPDATED:
Charles Chan68363b12017-06-26 15:25:09 -07001626 log.info("Segment Routing Config updated for {}", event.subject());
Charles Chan8ca5a122017-10-20 16:06:55 -07001627 createOrUpdateDeviceConfiguration();
Charles Chan68363b12017-06-26 15:25:09 -07001628 // TODO support dynamic configuration
1629 break;
1630 default:
1631 break;
1632 }
1633 } else if (event.configClass().equals(InterfaceConfig.class)) {
1634 switch (event.type()) {
1635 case CONFIG_ADDED:
1636 log.info("Interface Config added for {}", event.subject());
1637 configureNetwork();
1638 break;
1639 case CONFIG_UPDATED:
1640 log.info("Interface Config updated for {}", event.subject());
Charles Chan8ca5a122017-10-20 16:06:55 -07001641 createOrUpdateDeviceConfiguration();
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001642
1643 // Following code will be uncommented when [CORD-634] is fully implemented.
1644 // [CORD-634] Add dynamic config support for interfaces
1645 updateInterface((InterfaceConfig) event.config().get(),
1646 (InterfaceConfig) event.prevConfig().get());
Charles Chan82ab1932016-01-30 23:22:37 -08001647 // TODO support dynamic configuration
1648 break;
1649 default:
1650 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001651 }
Charles Chan82ab1932016-01-30 23:22:37 -08001652 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chan82f19972016-05-17 13:13:55 -07001653 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan82ab1932016-01-30 23:22:37 -08001654 switch (event.type()) {
1655 case CONFIG_ADDED:
Charles Chan82f19972016-05-17 13:13:55 -07001656 appCfgHandler.processAppConfigAdded(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001657 break;
1658 case CONFIG_UPDATED:
Charles Chan82f19972016-05-17 13:13:55 -07001659 appCfgHandler.processAppConfigUpdated(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001660 break;
1661 case CONFIG_REMOVED:
Charles Chan82f19972016-05-17 13:13:55 -07001662 appCfgHandler.processAppConfigRemoved(event);
1663 break;
1664 default:
1665 break;
1666 }
Charles Chandebfea32016-10-24 14:52:01 -07001667 configureNetwork();
Charles Chan82f19972016-05-17 13:13:55 -07001668 } else if (event.configClass().equals(XConnectConfig.class)) {
1669 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1670 switch (event.type()) {
1671 case CONFIG_ADDED:
1672 xConnectHandler.processXConnectConfigAdded(event);
1673 break;
1674 case CONFIG_UPDATED:
1675 xConnectHandler.processXConnectConfigUpdated(event);
1676 break;
1677 case CONFIG_REMOVED:
1678 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001679 break;
1680 default:
1681 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001682 }
Pier Ventre6b19e482016-11-07 16:21:04 -08001683 } else if (event.configClass().equals(PwaasConfig.class)) {
1684 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1685 switch (event.type()) {
1686 case CONFIG_ADDED:
1687 l2TunnelHandler.processPwaasConfigAdded(event);
1688 break;
1689 case CONFIG_UPDATED:
1690 l2TunnelHandler.processPwaasConfigUpdated(event);
1691 break;
1692 case CONFIG_REMOVED:
1693 l2TunnelHandler.processPwaasConfigRemoved(event);
1694 break;
1695 default:
1696 break;
1697 }
Charles Chan72f556a2015-10-05 17:50:33 -07001698 }
1699 }
Saurav Das261c3002017-06-13 15:35:54 -07001700
Charles Chanc4a68c32018-01-03 16:26:32 -08001701 @Override
1702 public boolean isRelevant(NetworkConfigEvent event) {
1703 if (event.configClass().equals(SegmentRoutingDeviceConfig.class) ||
1704 event.configClass().equals(SegmentRoutingAppConfig.class) ||
1705 event.configClass().equals(InterfaceConfig.class) ||
1706 event.configClass().equals(XConnectConfig.class) ||
1707 event.configClass().equals(PwaasConfig.class)) {
1708 return true;
1709 }
1710 log.debug("Ignore irrelevant event class {}", event.configClass().getName());
1711 return false;
1712 }
1713
Saurav Das261c3002017-06-13 15:35:54 -07001714 private final class ConfigChange implements Runnable {
1715 @Override
1716 public void run() {
1717 programmingScheduled.set(false);
1718 for (Device device : deviceService.getDevices()) {
1719 processDeviceAdded(device);
1720 }
1721 defaultRoutingHandler.startPopulationProcess();
1722 }
1723 }
Charles Chan72f556a2015-10-05 17:50:33 -07001724 }
Charles Chanf4586112015-11-09 16:37:23 -08001725
1726 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001727 @Override
1728 public void event(HostEvent event) {
Charles Chanf4586112015-11-09 16:37:23 -08001729 switch (event.type()) {
1730 case HOST_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001731 hostHandler.processHostAddedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001732 break;
1733 case HOST_MOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001734 hostHandler.processHostMovedEvent(event);
Charles Chan910be6a2017-08-23 14:46:43 -07001735 routeHandler.processHostMovedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001736 break;
1737 case HOST_REMOVED:
Charles Chand9265a32017-06-16 15:19:24 -07001738 hostHandler.processHostRemovedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001739 break;
1740 case HOST_UPDATED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001741 hostHandler.processHostUpdatedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001742 break;
1743 default:
1744 log.warn("Unsupported host event type: {}", event.type());
1745 break;
1746 }
1747 }
1748 }
1749
Charles Chanc91c8782016-03-30 17:54:24 -07001750 private class InternalMcastListener implements McastListener {
1751 @Override
1752 public void event(McastEvent event) {
1753 switch (event.type()) {
1754 case SOURCE_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001755 mcastHandler.processSourceAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001756 break;
1757 case SINK_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001758 mcastHandler.processSinkAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001759 break;
1760 case SINK_REMOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001761 mcastHandler.processSinkRemoved(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001762 break;
1763 case ROUTE_ADDED:
1764 case ROUTE_REMOVED:
1765 default:
1766 break;
1767 }
1768 }
1769 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001770
Charles Chandebfea32016-10-24 14:52:01 -07001771 private class InternalRouteEventListener implements RouteListener {
1772 @Override
1773 public void event(RouteEvent event) {
1774 switch (event.type()) {
1775 case ROUTE_ADDED:
1776 routeHandler.processRouteAdded(event);
1777 break;
1778 case ROUTE_UPDATED:
1779 routeHandler.processRouteUpdated(event);
1780 break;
1781 case ROUTE_REMOVED:
1782 routeHandler.processRouteRemoved(event);
1783 break;
Charles Chan910be6a2017-08-23 14:46:43 -07001784 case ALTERNATIVE_ROUTES_CHANGED:
1785 routeHandler.processAlternativeRoutesChanged(event);
1786 break;
Charles Chandebfea32016-10-24 14:52:01 -07001787 default:
1788 break;
1789 }
1790 }
1791 }
Saurav Das62ae6792017-05-15 15:34:25 -07001792
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001793 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1794 try {
1795 Set<Interface> intfs = conf.getInterfaces();
1796 Set<Interface> prevIntfs = prevConf.getInterfaces();
1797
1798 // Now we only handle one interface config at each port.
1799 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1800 log.warn("Interface update aborted - one at a time is allowed, " +
1801 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1802 return;
1803 }
1804
1805 Interface intf = intfs.stream().findFirst().get();
1806 Interface prevIntf = prevIntfs.stream().findFirst().get();
1807
1808 DeviceId deviceId = intf.connectPoint().deviceId();
1809 PortNumber portNum = intf.connectPoint().port();
1810
1811 if (!mastershipService.isLocalMaster(deviceId)) {
1812 log.debug("CONFIG_UPDATED event for interfaces should be " +
1813 "handled by master node for device {}", deviceId);
1814 return;
1815 }
1816
1817 removeSubnetConfig(prevIntf.connectPoint(),
1818 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1819 new HashSet<>(intf.ipAddressesList())));
1820
1821 if (prevIntf.vlanNative() != VlanId.NONE && !intf.vlanNative().equals(prevIntf.vlanNative())) {
1822 // RemoveVlanNative
1823 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1824 }
1825
1826 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1827 // RemoveVlanTagged
1828 prevIntf.vlanTagged().stream().filter(i -> !intf.vlanTagged().contains(i)).forEach(
1829 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, false)
1830 );
1831 }
1832
1833 if (prevIntf.vlanUntagged() != VlanId.NONE && !intf.vlanUntagged().equals(prevIntf.vlanUntagged())) {
1834 // RemoveVlanUntagged
1835 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1836 }
1837
1838 if (intf.vlanNative() != VlanId.NONE && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1839 // AddVlanNative
1840 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1841 }
1842
1843 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1844 // AddVlanTagged
1845 intf.vlanTagged().stream().filter(i -> !prevIntf.vlanTagged().contains(i)).forEach(
1846 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, true)
1847 );
1848 }
1849
1850 if (intf.vlanUntagged() != VlanId.NONE && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())) {
1851 // AddVlanUntagged
1852 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1853 }
1854 addSubnetConfig(prevIntf.connectPoint(),
1855 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1856 new HashSet<>(prevIntf.ipAddressesList())));
1857 } catch (ConfigException e) {
1858 log.error("Error in configuration");
1859 }
1860 }
1861
1862 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1863 VlanId vlanId, boolean pushVlan, boolean install) {
1864 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1865 if (grpHandler == null) {
1866 log.warn("Failed to retrieve group handler for device {}", deviceId);
1867 return;
1868 }
1869
1870 // Update filtering objective for a single port
1871 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1872
1873 // Update filtering objective for multicast ingress port
1874 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1875
1876 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1877
1878 if (nextId != -1 && !install) {
1879 // Update next objective for a single port as an output port
1880 // Remove a single port from L2FG
1881 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1882 // Remove L2 Bridging rule and L3 Unicast rule to the host
1883 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1884 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1885 // only if there is no port configured on that VLAN ID
1886 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1887 // Remove broadcast forwarding rule for the VLAN
1888 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1889 // Remove L2FG for VLAN
1890 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1891 } else {
1892 // Remove L2IG of the port
1893 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1894 }
1895 } else if (install) {
1896 if (nextId != -1) {
1897 // Add a single port to L2FG
1898 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1899 } else {
1900 // Create L2FG for VLAN
1901 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1902 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1903 }
1904 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1905 } else {
1906 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1907 }
1908 }
1909
1910 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1911 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1912 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1913
1914 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1915 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1916 .filter(intf -> !intf.connectPoint().equals(cp))
1917 .flatMap(intf -> intf.ipAddressesList().stream())
1918 .collect(Collectors.toSet());
1919 // 1. Partial subnet population
1920 // Remove routing rules for removed subnet from previous configuration,
1921 // which does not also exist in other interfaces in the same device
1922 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1923 .map(InterfaceIpAddress::subnetAddress)
1924 .collect(Collectors.toSet());
1925
1926 defaultRoutingHandler.revokeSubnet(
1927 ipPrefixSet.stream()
1928 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1929 .collect(Collectors.toSet()));
1930
1931 // 2. Interface IP punts
1932 // Remove IP punts for old Intf address
1933 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1934 .map(InterfaceIpAddress::ipAddress)
1935 .collect(Collectors.toSet());
1936 ipAddressSet.stream()
1937 .map(InterfaceIpAddress::ipAddress)
1938 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1939 .forEach(interfaceIpAddress ->
1940 routingRulePopulator.revokeSingleIpPunts(
1941 cp.deviceId(), interfaceIpAddress));
1942
1943 // 3. Host unicast routing rule
1944 // Remove unicast routing rule
1945 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1946 }
1947
1948 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1949 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1950 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1951
1952 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1953 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1954 .filter(intf -> !intf.connectPoint().equals(cp))
1955 .flatMap(intf -> intf.ipAddressesList().stream())
1956 .collect(Collectors.toSet());
1957 // 1. Partial subnet population
1958 // Add routing rules for newly added subnet, which does not also exist in
1959 // other interfaces in the same device
1960 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1961 .map(InterfaceIpAddress::subnetAddress)
1962 .collect(Collectors.toSet());
1963
1964 defaultRoutingHandler.populateSubnet(
1965 Collections.singleton(cp),
1966 ipPrefixSet.stream()
1967 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1968 .collect(Collectors.toSet()));
1969
1970 // 2. Interface IP punts
1971 // Add IP punts for new Intf address
1972 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1973 .map(InterfaceIpAddress::ipAddress)
1974 .collect(Collectors.toSet());
1975 ipAddressSet.stream()
1976 .map(InterfaceIpAddress::ipAddress)
1977 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1978 .forEach(interfaceIpAddress ->
1979 routingRulePopulator.populateSingleIpPunts(
1980 cp.deviceId(), interfaceIpAddress));
1981
1982 // 3. Host unicast routing rule
1983 // Add unicast routing rule
1984 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1985 }
sangho80f11cb2015-04-01 13:05:26 -07001986}