blob: 4b86d13b76f731c257e6a46eea95de2a7b49d756 [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sanghob35a6192015-04-01 13:05:26 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.segmentrouting;
17
Jonghwan Hyun5f1def82017-08-25 17:48:36 -070018import java.util.Collections;
Charles Chanba90df12017-11-30 15:37:50 -080019import java.util.Dictionary;
Jonghwan Hyun5f1def82017-08-25 17:48:36 -070020import java.util.HashSet;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070021import java.util.List;
22import java.util.Map;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070023import java.util.Optional;
24import java.util.Set;
25import java.util.concurrent.ConcurrentHashMap;
26import java.util.concurrent.ConcurrentLinkedQueue;
27import java.util.concurrent.Executors;
28import java.util.concurrent.ScheduledExecutorService;
29import java.util.concurrent.ScheduledFuture;
30import java.util.concurrent.TimeUnit;
31import java.util.concurrent.atomic.AtomicBoolean;
32import java.util.stream.Collectors;
33
Charles Chanb15d0102017-08-23 13:55:39 -070034import com.google.common.collect.Sets;
sanghob35a6192015-04-01 13:05:26 -070035import org.apache.felix.scr.annotations.Activate;
36import org.apache.felix.scr.annotations.Component;
37import org.apache.felix.scr.annotations.Deactivate;
Charles Chanba90df12017-11-30 15:37:50 -080038import org.apache.felix.scr.annotations.Modified;
39import org.apache.felix.scr.annotations.Property;
sanghob35a6192015-04-01 13:05:26 -070040import org.apache.felix.scr.annotations.Reference;
41import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho1e575652015-05-14 00:39:53 -070042import org.apache.felix.scr.annotations.Service;
sanghob35a6192015-04-01 13:05:26 -070043import org.onlab.packet.Ethernet;
Pier Ventre735b8c82016-12-02 08:16:05 -080044import org.onlab.packet.ICMP6;
Charles Chanc42e84e2015-10-20 16:24:19 -070045import org.onlab.packet.IPv4;
Pier Ventre10bd8d12016-11-26 21:05:22 -080046import org.onlab.packet.IPv6;
Jonghwan Hyun5f1def82017-08-25 17:48:36 -070047import org.onlab.packet.IpAddress;
Charles Chanc42e84e2015-10-20 16:24:19 -070048import org.onlab.packet.IpPrefix;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070049import org.onlab.packet.VlanId;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070050import org.onlab.util.KryoNamespace;
Charles Chanba90df12017-11-30 15:37:50 -080051import org.onlab.util.Tools;
Saurav Das80980c72016-03-23 11:22:49 -070052import org.onosproject.cfg.ComponentConfigService;
sanghob35a6192015-04-01 13:05:26 -070053import org.onosproject.core.ApplicationId;
54import org.onosproject.core.CoreService;
55import org.onosproject.event.Event;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070056import org.onosproject.mastership.MastershipService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080057import org.onosproject.net.ConnectPoint;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070058import org.onosproject.net.Device;
59import org.onosproject.net.DeviceId;
Charles Chanb15d0102017-08-23 13:55:39 -070060import org.onosproject.net.Host;
61import org.onosproject.net.HostId;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070062import org.onosproject.net.Link;
63import org.onosproject.net.Port;
Charles Chan68aa62d2015-11-09 16:37:23 -080064import org.onosproject.net.PortNumber;
Jonghwan Hyun5f1def82017-08-25 17:48:36 -070065import org.onosproject.net.config.ConfigException;
Charles Chand6832882015-10-05 17:50:33 -070066import org.onosproject.net.config.ConfigFactory;
67import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070068import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070069import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070070import org.onosproject.net.config.basics.InterfaceConfig;
71import org.onosproject.net.config.basics.McastConfig;
Charles Chand6832882015-10-05 17:50:33 -070072import org.onosproject.net.config.basics.SubjectFactories;
Saurav Dase9c8971e2018-01-18 12:07:33 -080073import org.onosproject.net.device.DeviceAdminService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070074import org.onosproject.net.device.DeviceEvent;
75import org.onosproject.net.device.DeviceListener;
76import org.onosproject.net.device.DeviceService;
Charles Chan68aa62d2015-11-09 16:37:23 -080077import org.onosproject.net.flow.TrafficSelector;
78import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070079import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan68aa62d2015-11-09 16:37:23 -080080import org.onosproject.net.host.HostEvent;
81import org.onosproject.net.host.HostListener;
Charles Chanba90df12017-11-30 15:37:50 -080082import org.onosproject.net.host.HostLocationProbingService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080083import org.onosproject.net.host.HostService;
Jonghwan Hyun5f1def82017-08-25 17:48:36 -070084import org.onosproject.net.host.InterfaceIpAddress;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070085import org.onosproject.net.intf.Interface;
86import org.onosproject.net.intf.InterfaceService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080087import org.onosproject.net.link.LinkEvent;
88import org.onosproject.net.link.LinkListener;
89import org.onosproject.net.link.LinkService;
Charles Chand55e84d2016-03-30 17:54:24 -070090import org.onosproject.net.mcast.McastEvent;
91import org.onosproject.net.mcast.McastListener;
92import org.onosproject.net.mcast.MulticastRouteService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070093import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080094import org.onosproject.net.packet.InboundPacket;
95import org.onosproject.net.packet.PacketContext;
96import org.onosproject.net.packet.PacketProcessor;
97import org.onosproject.net.packet.PacketService;
Pier Ventre42287df2016-11-09 14:17:26 -080098import org.onosproject.net.topology.PathService;
Charles Chand55e84d2016-03-30 17:54:24 -070099import org.onosproject.net.topology.TopologyService;
Charles Chanb15d0102017-08-23 13:55:39 -0700100import org.onosproject.routeservice.ResolvedRoute;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700101import org.onosproject.routeservice.RouteEvent;
102import org.onosproject.routeservice.RouteListener;
103import org.onosproject.routeservice.RouteService;
Charles Chand55e84d2016-03-30 17:54:24 -0700104import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
105import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventref34966c2016-11-07 16:21:04 -0800106import org.onosproject.segmentrouting.config.PwaasConfig;
Pier Ventref34966c2016-11-07 16:21:04 -0800107import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700108import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -0700109import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -0700110import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700111import org.onosproject.segmentrouting.grouphandler.DestinationSet;
112import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700113import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700114import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Charles Chand2990362016-04-18 13:44:03 -0700115import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -0800116import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -0700117import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700118import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700119import org.onosproject.store.service.EventuallyConsistentMap;
120import org.onosproject.store.service.EventuallyConsistentMapBuilder;
121import org.onosproject.store.service.StorageService;
122import org.onosproject.store.service.WallClockTimestamp;
Charles Chanba90df12017-11-30 15:37:50 -0800123import org.osgi.service.component.ComponentContext;
sanghob35a6192015-04-01 13:05:26 -0700124import org.slf4j.Logger;
125import org.slf4j.LoggerFactory;
126
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700127import com.google.common.collect.HashMultimap;
128import com.google.common.collect.ImmutableMap;
129import com.google.common.collect.Maps;
130import com.google.common.collect.Multimap;
sanghob35a6192015-04-01 13:05:26 -0700131
Charles Chan3e783d02016-02-26 22:19:52 -0800132import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800133import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700134import static org.onlab.util.Tools.groupedThreads;
Charles Chan3e783d02016-02-26 22:19:52 -0800135
Charles Chane849c192016-01-11 18:28:54 -0800136/**
137 * Segment routing manager.
138 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700139@Service
140@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700141public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700142
Charles Chan2c15aca2016-11-09 20:51:44 -0800143 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan23686832017-08-23 14:46:43 -0700144 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sanghob35a6192015-04-01 13:05:26 -0700145
146 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700147 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700148
149 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre10bd8d12016-11-26 21:05:22 -0800150 private NeighbourResolutionService neighbourResolutionService;
151
152 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800153 public PathService pathService;
154
155 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700156 CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700157
158 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700159 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700160
161 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700162 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700163
164 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chanba90df12017-11-30 15:37:50 -0800165 HostLocationProbingService probingService;
166
167 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700168 DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700169
170 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Saurav Dase9c8971e2018-01-18 12:07:33 -0800171 DeviceAdminService deviceAdminService;
172
173 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800174 public FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700175
176 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700177 LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700178
Charles Chan5270ed02016-01-30 23:22:37 -0800179 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800180 public MastershipService mastershipService;
Charles Chan03a73e02016-10-24 14:52:01 -0700181
182 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800183 public StorageService storageService;
Charles Chan03a73e02016-10-24 14:52:01 -0700184
185 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
186 MulticastRouteService multicastRouteService;
187
188 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
189 TopologyService topologyService;
190
191 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700192 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800193
194 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800195 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800196
Saurav Das80980c72016-03-23 11:22:49 -0700197 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800198 public InterfaceService interfaceService;
199
Charles Chanba90df12017-11-30 15:37:50 -0800200 @Property(name = "activeProbing", boolValue = true,
201 label = "Enable active probing to discover dual-homed hosts.")
202 boolean activeProbing = true;
203
Charles Chan03a73e02016-10-24 14:52:01 -0700204 ArpHandler arpHandler = null;
205 IcmpHandler icmpHandler = null;
206 IpHandler ipHandler = null;
207 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700208 ApplicationId appId;
209 DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700210
Charles Chan03a73e02016-10-24 14:52:01 -0700211 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700212 private TunnelHandler tunnelHandler = null;
213 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700214 private InternalPacketProcessor processor = null;
215 private InternalLinkListener linkListener = null;
216 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700217 private AppConfigHandler appCfgHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700218 XConnectHandler xConnectHandler = null;
Saurav Dase9c8971e2018-01-18 12:07:33 -0800219 McastHandler mcastHandler = null;
220 HostHandler hostHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800221 private RouteHandler routeHandler = null;
Saurav Dase9c8971e2018-01-18 12:07:33 -0800222 LinkHandler linkHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800223 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Pier Ventref34966c2016-11-07 16:21:04 -0800224 private L2TunnelHandler l2TunnelHandler = null;
sanghob35a6192015-04-01 13:05:26 -0700225 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan5270ed02016-01-30 23:22:37 -0800226 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700227 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
228 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700229 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sanghob35a6192015-04-01 13:05:26 -0700230
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700231 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700232 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sanghob35a6192015-04-01 13:05:26 -0700233
Saurav Das4ce45962015-11-24 23:21:05 -0800234 @SuppressWarnings("unused")
sanghob35a6192015-04-01 13:05:26 -0700235 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800236 @SuppressWarnings("rawtypes")
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700237 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Saurav Dase9c8971e2018-01-18 12:07:33 -0800238 Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chane849c192016-01-11 18:28:54 -0800239 new ConcurrentHashMap<>();
240 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700241 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5deb3232017-08-22 15:07:34 -0700242 * Used to keep track on MPLS group information.
Charles Chane849c192016-01-11 18:28:54 -0800243 */
Charles Chanba90df12017-11-30 15:37:50 -0800244 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das7bcbe702017-06-13 15:35:54 -0700245 dsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800246 /**
Charles Chan5deb3232017-08-22 15:07:34 -0700247 * Per device next objective ID store with (device id + vlanid) as key.
248 * Used to keep track on L2 flood group information.
Charles Chane849c192016-01-11 18:28:54 -0800249 */
Charles Chanba90df12017-11-30 15:37:50 -0800250 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan59cc16d2017-02-02 16:20:42 -0800251 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800252 /**
Charles Chan5deb3232017-08-22 15:07:34 -0700253 * Per device next objective ID store with (device id + port + treatment + meta) as key.
254 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chane849c192016-01-11 18:28:54 -0800255 */
Charles Chanba90df12017-11-30 15:37:50 -0800256 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800257 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800258
Saurav Das4ce45962015-11-24 23:21:05 -0800259 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
260 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700261
Saurav Das7bcbe702017-06-13 15:35:54 -0700262 private AtomicBoolean programmingScheduled = new AtomicBoolean();
263
Charles Chand55e84d2016-03-30 17:54:24 -0700264 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700265 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
266 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700267 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700268 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800269 public SegmentRoutingDeviceConfig createConfig() {
270 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700271 }
272 };
Pier Ventref34966c2016-11-07 16:21:04 -0800273
Charles Chand55e84d2016-03-30 17:54:24 -0700274 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700275 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
276 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700277 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800278 @Override
279 public SegmentRoutingAppConfig createConfig() {
280 return new SegmentRoutingAppConfig();
281 }
282 };
Pier Ventref34966c2016-11-07 16:21:04 -0800283
Charles Chanfc5c7802016-05-17 13:13:55 -0700284 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
285 new ConfigFactory<ApplicationId, XConnectConfig>(
286 SubjectFactories.APP_SUBJECT_FACTORY,
287 XConnectConfig.class, "xconnect") {
288 @Override
289 public XConnectConfig createConfig() {
290 return new XConnectConfig();
291 }
292 };
Pier Ventref34966c2016-11-07 16:21:04 -0800293
Charles Chand55e84d2016-03-30 17:54:24 -0700294 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700295 new ConfigFactory<ApplicationId, McastConfig>(
296 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700297 McastConfig.class, "multicast") {
298 @Override
299 public McastConfig createConfig() {
300 return new McastConfig();
301 }
302 };
303
Pier Ventref34966c2016-11-07 16:21:04 -0800304 private final ConfigFactory<ApplicationId, PwaasConfig> pwaasConfigFactory =
305 new ConfigFactory<ApplicationId, PwaasConfig>(
306 SubjectFactories.APP_SUBJECT_FACTORY,
307 PwaasConfig.class, "pwaas") {
308 @Override
309 public PwaasConfig createConfig() {
310 return new PwaasConfig();
311 }
312 };
313
Charles Chan65238242017-06-22 18:03:14 -0700314 private static final Object THREAD_SCHED_LOCK = new Object();
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700315 private static int numOfEventsQueued = 0;
316 private static int numOfEventsExecuted = 0;
sanghob35a6192015-04-01 13:05:26 -0700317 private static int numOfHandlerExecution = 0;
318 private static int numOfHandlerScheduled = 0;
319
Charles Chan116188d2016-02-18 14:22:42 -0800320 /**
321 * Segment Routing App ID.
322 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800323 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan59cc16d2017-02-02 16:20:42 -0800324
Charles Chane849c192016-01-11 18:28:54 -0800325 /**
326 * The default VLAN ID assigned to the interfaces without subnet config.
327 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800328 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700329
sanghob35a6192015-04-01 13:05:26 -0700330 @Activate
Charles Chanba90df12017-11-30 15:37:50 -0800331 protected void activate(ComponentContext context) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800332 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700333
334 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das7bcbe702017-06-13 15:35:54 -0700335 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700336 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700337 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700338 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700339 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700340 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700341 .build();
Saurav Das7bcbe702017-06-13 15:35:54 -0700342 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700343
Charles Chan59cc16d2017-02-02 16:20:42 -0800344 log.debug("Creating EC map vlannextobjectivestore");
345 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
346 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
347 vlanNextObjStore = vlanNextObjMapBuilder
348 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700349 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700350 .withTimestampProvider((k, v) -> new WallClockTimestamp())
351 .build();
352
Saurav Das4ce45962015-11-24 23:21:05 -0800353 log.debug("Creating EC map subnetnextobjectivestore");
354 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
355 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
356 portNextObjStore = portNextObjMapBuilder
357 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700358 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800359 .withTimestampProvider((k, v) -> new WallClockTimestamp())
360 .build();
361
sangho0b2b6d12015-05-20 22:16:38 -0700362 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
363 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700364 tunnelStore = tunnelMapBuilder
365 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700366 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700367 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700368 .build();
369
370 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
371 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700372 policyStore = policyMapBuilder
373 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700374 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700375 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700376 .build();
377
Saurav Das80980c72016-03-23 11:22:49 -0700378 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800379 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700380 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800381 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800382 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
383 "requestInterceptsEnabled", "false");
Charles Chand3baaba2017-08-08 15:13:37 -0700384 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800385 "requestInterceptsEnabled", "false");
Charles Chanc760f382017-07-24 15:56:10 -0700386 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800387 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800388 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
389 "greedyLearningIpv6", "true");
Charles Chanc6d227e2017-02-28 15:15:17 -0800390 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
391 "forceUnprovision", "true");
Charles Chanef624ed2017-08-10 16:57:28 -0700392 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan73316522017-07-20 16:16:25 -0700393 "distributed", "true");
Charles Chan35a32322017-08-14 11:42:11 -0700394 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
395 "multihomingEnabled", "true");
Charles Chanfd892012017-11-29 19:54:20 -0800396 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
397 "staleLinkAge", "15000");
398 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
399 "allowDuplicateIps", "false");
Charles Chanba90df12017-11-30 15:37:50 -0800400 compCfgService.registerProperties(getClass());
401 modified(context);
Saurav Das80980c72016-03-23 11:22:49 -0700402
Charles Chanb8e10c82015-10-14 11:24:40 -0700403 processor = new InternalPacketProcessor();
404 linkListener = new InternalLinkListener();
405 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700406 appCfgHandler = new AppConfigHandler(this);
407 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700408 mcastHandler = new McastHandler(this);
409 hostHandler = new HostHandler(this);
Saurav Dase9c8971e2018-01-18 12:07:33 -0800410 linkHandler = new LinkHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700411 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800412 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Pier Ventref34966c2016-11-07 16:21:04 -0800413 l2TunnelHandler = new L2TunnelHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700414
Charles Chan3e783d02016-02-26 22:19:52 -0800415 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700416 cfgService.registerConfigFactory(deviceConfigFactory);
417 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700418 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700419 cfgService.registerConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800420 cfgService.registerConfigFactory(pwaasConfigFactory);
Charles Chan8d055212018-01-04 14:26:07 -0800421
422 cfgListener.configureNetwork();
423
Charles Chan5270ed02016-01-30 23:22:37 -0800424 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700425 packetService.addProcessor(processor, PacketProcessor.director(2));
426 linkService.addListener(linkListener);
427 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700428 multicastRouteService.addListener(mcastListener);
Charles Chanc7a8a682017-06-19 00:43:31 -0700429 routeService.addListener(routeListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700430
sanghob35a6192015-04-01 13:05:26 -0700431 log.info("Started");
432 }
433
Saurav Dase9c8971e2018-01-18 12:07:33 -0800434 KryoNamespace.Builder createSerializer() {
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700435 return new KryoNamespace.Builder()
436 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700437 .register(DestinationSetNextObjectiveStoreKey.class,
Charles Chan59cc16d2017-02-02 16:20:42 -0800438 VlanNextObjectiveStoreKey.class,
Saurav Das7bcbe702017-06-13 15:35:54 -0700439 DestinationSet.class,
440 NextNeighbors.class,
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700441 Tunnel.class,
442 DefaultTunnel.class,
443 Policy.class,
444 TunnelPolicy.class,
445 Policy.Type.class,
446 PortNextObjectiveStoreKey.class,
Charles Chanfc5c7802016-05-17 13:13:55 -0700447 XConnectStoreKey.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700448 );
449 }
450
sanghob35a6192015-04-01 13:05:26 -0700451 @Deactivate
452 protected void deactivate() {
Charles Chand6832882015-10-05 17:50:33 -0700453 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700454 cfgService.unregisterConfigFactory(deviceConfigFactory);
455 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700456 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700457 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800458 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chanba90df12017-11-30 15:37:50 -0800459 compCfgService.unregisterProperties(getClass(), false);
Charles Chand6832882015-10-05 17:50:33 -0700460
Charles Chanc7a8a682017-06-19 00:43:31 -0700461 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700462 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700463 linkService.removeListener(linkListener);
464 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700465 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700466 routeService.removeListener(routeListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700467
Charles Chan0aa674e2017-02-23 15:44:08 -0800468 neighbourResolutionService.unregisterNeighbourHandlers(appId);
469
sanghob35a6192015-04-01 13:05:26 -0700470 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700471 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700472 deviceListener = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700473 groupHandlerMap.clear();
474
Saurav Das7bcbe702017-06-13 15:35:54 -0700475 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800476 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700477 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700478 tunnelStore.destroy();
479 policyStore.destroy();
sanghob35a6192015-04-01 13:05:26 -0700480 log.info("Stopped");
481 }
482
Charles Chanba90df12017-11-30 15:37:50 -0800483 @Modified
484 private void modified(ComponentContext context) {
485 Dictionary<?, ?> properties = context.getProperties();
486 if (properties == null) {
487 return;
488 }
489
490 String strActiveProving = Tools.get(properties, "activeProbing");
491 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
492
493 if (expectActiveProbing != activeProbing) {
494 activeProbing = expectActiveProbing;
495 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
496 }
497 }
498
sangho1e575652015-05-14 00:39:53 -0700499 @Override
500 public List<Tunnel> getTunnels() {
501 return tunnelHandler.getTunnels();
502 }
503
504 @Override
sangho71abe1b2015-06-29 14:58:47 -0700505 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
506 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700507 }
508
509 @Override
sangho71abe1b2015-06-29 14:58:47 -0700510 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700511 for (Policy policy: policyHandler.getPolicies()) {
512 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
513 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
514 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
515 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700516 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700517 }
518 }
519 }
sangho71abe1b2015-06-29 14:58:47 -0700520 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700521 }
522
523 @Override
sangho71abe1b2015-06-29 14:58:47 -0700524 public PolicyHandler.Result removePolicy(Policy policy) {
525 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700526 }
527
528 @Override
sangho71abe1b2015-06-29 14:58:47 -0700529 public PolicyHandler.Result createPolicy(Policy policy) {
530 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700531 }
532
533 @Override
534 public List<Policy> getPolicies() {
535 return policyHandler.getPolicies();
536 }
537
Saurav Das59232cf2016-04-27 18:35:50 -0700538 @Override
539 public void rerouteNetwork() {
540 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700541 }
542
Charles Chanc81c45b2016-10-20 17:02:44 -0700543 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800544 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
545 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Charles Chanc81c45b2016-10-20 17:02:44 -0700546 deviceService.getAvailableDevices().forEach(device -> {
547 deviceSubnetMap.put(device.id(), deviceConfiguration.getSubnets(device.id()));
548 });
549 return deviceSubnetMap;
550 }
551
Saurav Dasc88d4662017-05-15 15:34:25 -0700552
553 @Override
554 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
555 if (defaultRoutingHandler != null) {
556 return defaultRoutingHandler.getCurrentEmcpSpgMap();
557 } else {
558 return null;
559 }
560 }
561
562 @Override
Saurav Das7bcbe702017-06-13 15:35:54 -0700563 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
564 if (dsNextObjStore != null) {
565 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700566 } else {
567 return ImmutableMap.of();
568 }
569 }
570
Saurav Dasceccf242017-08-03 18:30:35 -0700571 @Override
572 public void verifyGroups(DeviceId id) {
573 DefaultGroupHandler gh = groupHandlerMap.get(id);
574 if (gh != null) {
575 gh.triggerBucketCorrector();
576 }
577 }
578
sanghof9d0bf12015-05-19 11:57:42 -0700579 /**
Ray Milkeye4afdb52017-04-05 09:42:04 -0700580 * Extracts the application ID from the manager.
581 *
582 * @return application ID
583 */
584 public ApplicationId appId() {
585 return appId;
586 }
587
588 /**
589 * Returns the device configuration.
590 *
591 * @return device configuration
592 */
593 public DeviceConfiguration deviceConfiguration() {
594 return deviceConfiguration;
595 }
596
597 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700598 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5deb3232017-08-22 15:07:34 -0700599 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700600 *
601 * @return next objective ID store
602 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700603 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
604 dsNextObjStore() {
605 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700606 }
607
608 /**
Charles Chan5deb3232017-08-22 15:07:34 -0700609 * Per device next objective ID store with (device id + vlanid) as key.
610 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700611 *
612 * @return vlan next object store
613 */
614 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
615 return vlanNextObjStore;
616 }
617
618 /**
Charles Chan5deb3232017-08-22 15:07:34 -0700619 * Per device next objective ID store with (device id + port + treatment + meta) as key.
620 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700621 *
622 * @return port next object store.
623 */
624 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
625 return portNextObjStore;
626 }
627
628 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700629 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
630 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -0700631 *
632 * @return MPLS-ECMP value
633 */
634 public boolean getMplsEcmp() {
635 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
636 .getConfig(this.appId, SegmentRoutingAppConfig.class);
637 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
638 }
639
640 /**
sanghof9d0bf12015-05-19 11:57:42 -0700641 * Returns the tunnel object with the tunnel ID.
642 *
643 * @param tunnelId Tunnel ID
644 * @return Tunnel reference
645 */
sangho1e575652015-05-14 00:39:53 -0700646 public Tunnel getTunnel(String tunnelId) {
647 return tunnelHandler.getTunnel(tunnelId);
648 }
649
Charles Chan7ffd81f2017-02-08 15:52:08 -0800650 // TODO Consider moving these to InterfaceService
sanghob35a6192015-04-01 13:05:26 -0700651 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800652 * Returns untagged VLAN configured on given connect point.
Charles Chan7ffd81f2017-02-08 15:52:08 -0800653 * <p>
654 * Only returns the first match if there are multiple untagged VLAN configured
655 * on the connect point.
sanghob35a6192015-04-01 13:05:26 -0700656 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800657 * @param connectPoint connect point
658 * @return untagged VLAN or null if not configured
sanghob35a6192015-04-01 13:05:26 -0700659 */
Charles Chan65238242017-06-22 18:03:14 -0700660 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800661 return interfaceService.getInterfacesByPort(connectPoint).stream()
662 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
663 .map(Interface::vlanUntagged)
664 .findFirst().orElse(null);
sanghob35a6192015-04-01 13:05:26 -0700665 }
666
sangho1e575652015-05-14 00:39:53 -0700667 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800668 * Returns tagged VLAN configured on given connect point.
669 * <p>
670 * Returns all matches if there are multiple tagged VLAN configured
671 * on the connect point.
672 *
673 * @param connectPoint connect point
674 * @return tagged VLAN or empty set if not configured
675 */
Charles Chan65238242017-06-22 18:03:14 -0700676 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800677 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
678 return interfaces.stream()
679 .map(Interface::vlanTagged)
Charles Chan65238242017-06-22 18:03:14 -0700680 .flatMap(Set::stream)
Charles Chan7ffd81f2017-02-08 15:52:08 -0800681 .collect(Collectors.toSet());
682 }
683
684 /**
685 * Returns native VLAN configured on given connect point.
686 * <p>
687 * Only returns the first match if there are multiple native VLAN configured
688 * on the connect point.
689 *
690 * @param connectPoint connect point
691 * @return native VLAN or null if not configured
692 */
Charles Chan65238242017-06-22 18:03:14 -0700693 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800694 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
695 return interfaces.stream()
696 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
697 .map(Interface::vlanNative)
698 .findFirst()
699 .orElse(null);
700 }
701
702 /**
Charles Chanf9a52702017-06-16 15:19:24 -0700703 * Returns internal VLAN for untagged hosts on given connect point.
704 * <p>
705 * The internal VLAN is either vlan-untagged for an access port,
706 * or vlan-native for a trunk port.
707 *
708 * @param connectPoint connect point
709 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
710 */
Charles Chan65238242017-06-22 18:03:14 -0700711 VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chanf9a52702017-06-16 15:19:24 -0700712 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
713 VlanId nativeVlanId = getNativeVlanId(connectPoint);
714 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
715 }
716
717 /**
Charles Chan65238242017-06-22 18:03:14 -0700718 * Returns optional pair device ID of given device.
719 *
720 * @param deviceId device ID
721 * @return optional pair device ID. Might be empty if pair device is not configured
722 */
723 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
724 SegmentRoutingDeviceConfig deviceConfig =
725 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
726 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
727 }
728 /**
729 * Returns optional pair device local port of given device.
730 *
731 * @param deviceId device ID
732 * @return optional pair device ID. Might be empty if pair device is not configured
733 */
734 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
735 SegmentRoutingDeviceConfig deviceConfig =
736 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
737 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
738 }
739
740 /**
Charles Chan23686832017-08-23 14:46:43 -0700741 * Determine if current instance is the master of given connect point.
742 *
743 * @param cp connect point
744 * @return true if current instance is the master of given connect point
745 */
746 boolean isMasterOf(ConnectPoint cp) {
747 boolean isMaster = mastershipService.isLocalMaster(cp.deviceId());
748 if (!isMaster) {
749 log.debug(NOT_MASTER, cp);
750 }
751 return isMaster;
752 }
753
754 /**
Charles Chanb15d0102017-08-23 13:55:39 -0700755 * Returns locations of given resolved route.
756 *
757 * @param resolvedRoute resolved route
758 * @return locations of nexthop. Might be empty if next hop is not found
759 */
760 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
761 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
762 return Optional.ofNullable(hostService.getHost(hostId))
763 .map(Host::locations).orElse(Sets.newHashSet())
764 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
765 }
766
767 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800768 * Returns vlan port map of given device.
769 *
770 * @param deviceId device id
771 * @return vlan-port multimap
772 */
773 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
774 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
775
776 interfaceService.getInterfaces().stream()
777 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
778 .forEach(intf -> {
779 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -0700780 intf.vlanTagged().forEach(vlanTagged ->
781 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
782 );
Charles Chan7ffd81f2017-02-08 15:52:08 -0800783 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
784 });
785 vlanPortMap.removeAll(VlanId.NONE);
786
787 return vlanPortMap;
788 }
789
790 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800791 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -0800792 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700793 *
794 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -0800795 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -0800796 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700797 */
Charles Chan65238242017-06-22 18:03:14 -0700798 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -0700799 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800800 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
801 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700802 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -0800803 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -0800804 + "device {} not found", deviceId);
805 return -1;
806 }
807 }
808
809 /**
810 * Returns the next objective ID for the given portNumber, given the treatment.
811 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -0700812 * would result in different objectives. If the next object does not exist,
813 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -0800814 *
815 * @param deviceId Device ID
816 * @param portNum port number on device for which NextObjective is queried
817 * @param treatment the actions to apply on the packets (should include outport)
818 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -0700819 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -0700820 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800821 */
822 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
823 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700824 TrafficSelector meta,
825 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -0800826 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
827 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -0700828 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -0800829 } else {
Charles Chane849c192016-01-11 18:28:54 -0800830 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
831 + " not found", deviceId);
832 return -1;
833 }
834 }
835
Saurav Dasc88d4662017-05-15 15:34:25 -0700836 /**
837 * Returns the group handler object for the specified device id.
838 *
839 * @param devId the device identifier
840 * @return the groupHandler object for the device id, or null if not found
841 */
Charles Chan65238242017-06-22 18:03:14 -0700842 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700843 return groupHandlerMap.get(devId);
844 }
845
846 /**
Saurav Dasceccf242017-08-03 18:30:35 -0700847 * Returns the default routing handler object.
848 *
849 * @return the default routing handler object
850 */
851 public DefaultRoutingHandler getRoutingHandler() {
852 return defaultRoutingHandler;
853 }
854
sanghob35a6192015-04-01 13:05:26 -0700855 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700856 @Override
857 public void process(PacketContext context) {
858
859 if (context.isHandled()) {
860 return;
861 }
862
863 InboundPacket pkt = context.inPacket();
864 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -0800865
866 if (ethernet == null) {
867 return;
868 }
869
Saurav Das7bcbe702017-06-13 15:35:54 -0700870 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
871 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800872 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700873 log.warn("Received unexpected ARP packet on {}",
874 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -0700875 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -0800876 return;
sanghob35a6192015-04-01 13:05:26 -0700877 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800878 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
879 //ipHandler.addToPacketBuffer(ipv4Packet);
880 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
881 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700882 } else {
Charles Chan50035632017-01-13 17:20:44 -0800883 // NOTE: We don't support IP learning at this moment so this
884 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -0800885 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700886 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800887 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
888 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -0800889 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -0800890 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -0800891 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
892 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
893 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
894 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
895 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
896 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -0700897 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -0700898 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -0800899 }
900 } else {
901 // NOTE: We don't support IP learning at this moment so this
902 // is not necessary. Also it causes duplication of DHCPv6 packets.
903 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
904 }
sanghob35a6192015-04-01 13:05:26 -0700905 }
906 }
907 }
908
909 private class InternalLinkListener implements LinkListener {
910 @Override
911 public void event(LinkEvent event) {
Charles Chanb1f8c762017-03-29 16:39:05 -0700912 if (event.type() == LinkEvent.Type.LINK_ADDED ||
913 event.type() == LinkEvent.Type.LINK_UPDATED ||
914 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700915 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700916 scheduleEventHandlerIfNotScheduled(event);
917 }
918 }
919 }
920
921 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -0700922 @Override
923 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -0700924 switch (event.type()) {
925 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -0800926 case PORT_UPDATED:
927 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -0700928 case DEVICE_UPDATED:
929 case DEVICE_AVAILABILITY_CHANGED:
Saurav Dasc88d4662017-05-15 15:34:25 -0700930 log.trace("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700931 scheduleEventHandlerIfNotScheduled(event);
932 break;
933 default:
934 }
935 }
936 }
937
Saurav Das4ce45962015-11-24 23:21:05 -0800938 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -0700939 private void scheduleEventHandlerIfNotScheduled(Event event) {
Charles Chan65238242017-06-22 18:03:14 -0700940 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700941 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700942 numOfEventsQueued++;
943
944 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
945 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700946 eventHandlerFuture = executorService
947 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
948 numOfHandlerScheduled++;
949 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700950 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700951 numOfEventsQueued,
952 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -0700953 }
sanghob35a6192015-04-01 13:05:26 -0700954 }
955
956 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700957 @Override
sanghob35a6192015-04-01 13:05:26 -0700958 public void run() {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700959 try {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700960 while (true) {
Saurav Das4ce45962015-11-24 23:21:05 -0800961 @SuppressWarnings("rawtypes")
Charles Chan65238242017-06-22 18:03:14 -0700962 Event event;
963 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700964 if (!eventQueue.isEmpty()) {
965 event = eventQueue.poll();
966 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700967 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700968 numOfHandlerExecution++;
969 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
970 numOfHandlerExecution, numOfEventsExecuted);
971 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700972 }
sangho20eff1d2015-04-13 15:15:58 -0700973 }
Charles Chanb1f8c762017-03-29 16:39:05 -0700974 if (event.type() == LinkEvent.Type.LINK_ADDED ||
975 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700976 // Note: do not update seenLinks here, otherwise every
977 // link, even one seen for the first time, will be appear
978 // to be a previously seen link
Saurav Dase9c8971e2018-01-18 12:07:33 -0800979 linkHandler.processLinkAdded((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700980 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Saurav Dase9c8971e2018-01-18 12:07:33 -0800981 linkHandler.processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700982 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
983 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
984 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800985 DeviceId deviceId = ((Device) event.subject()).id();
986 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -0700987 log.info("Processing device event {} for available device {}",
988 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700989 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -0700990 } else {
991 log.info("Processing device event {} for unavailable device {}",
992 event.type(), ((Device) event.subject()).id());
993 processDeviceRemoved((Device) event.subject());
994 }
Saurav Das1a129a02016-11-18 15:21:57 -0800995 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800996 // typically these calls come when device is added first time
997 // so port filtering rules are handled at the device_added event.
998 // port added calls represent all ports on the device,
999 // enabled or not.
Saurav Dasc88d4662017-05-15 15:34:25 -07001000 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001001 ((DeviceEvent) event).subject().id(),
1002 ((DeviceEvent) event).port().number(),
1003 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -08001004 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001005 // these calls happen for every subsequent event
1006 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -08001007 log.info("** PORT UPDATED {}/{} -> {}",
1008 event.subject(),
1009 ((DeviceEvent) event).port(),
1010 event.type());
1011 processPortUpdated(((Device) event.subject()),
1012 ((DeviceEvent) event).port());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001013 } else {
1014 log.warn("Unhandled event type: {}", event.type());
1015 }
sanghob35a6192015-04-01 13:05:26 -07001016 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001017 } catch (Exception e) {
1018 log.error("SegmentRouting event handler "
1019 + "thread thrown an exception: {}", e);
sanghob35a6192015-04-01 13:05:26 -07001020 }
sanghob35a6192015-04-01 13:05:26 -07001021 }
1022 }
1023
Saurav Dase9c8971e2018-01-18 12:07:33 -08001024 void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001025 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001026
1027 // NOTE: Punt ARP/NDP even when the device is not configured.
1028 // Host learning without network config is required for CORD config generator.
1029 routingRulePopulator.populateIpPunts(device.id());
1030 routingRulePopulator.populateArpNdpPunts(device.id());
1031
Charles Chan0b4e6182015-11-03 10:42:14 -08001032 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001033 log.warn("Device configuration unavailable. Device {} will be "
1034 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001035 return;
1036 }
Charles Chan2199c302016-04-23 17:36:10 -07001037 processDeviceAddedInternal(device.id());
1038 }
1039
1040 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001041 // Irrespective of whether the local is a MASTER or not for this device,
1042 // we need to create a SR-group-handler instance. This is because in a
1043 // multi-instance setup, any instance can initiate forwarding/next-objectives
1044 // for any switch (even if this instance is a SLAVE or not even connected
1045 // to the switch). To handle this, a default-group-handler instance is necessary
1046 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001047 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1048 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001049 DefaultGroupHandler groupHandler;
1050 try {
1051 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001052 createGroupHandler(deviceId,
1053 appId,
1054 deviceConfiguration,
1055 linkService,
1056 flowObjectiveService,
1057 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001058 } catch (DeviceConfigNotFoundException e) {
1059 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1060 return;
1061 }
Saurav Das9aa28bd2017-12-05 15:00:23 -08001062 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001063 deviceId);
1064 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001065 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001066
Charles Chan2199c302016-04-23 17:36:10 -07001067 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001068 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan03a73e02016-10-24 14:52:01 -07001069 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001070 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001071 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001072 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001073 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001074 }
Charles Chan5270ed02016-01-30 23:22:37 -08001075
Charles Chan03a73e02016-10-24 14:52:01 -07001076 appCfgHandler.init(deviceId);
1077 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001078 }
1079
Saurav Das80980c72016-03-23 11:22:49 -07001080 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001081 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001082 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chanba90df12017-11-30 15:37:50 -08001083 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001084 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001085 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001086 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001087 portNextObjStore.entrySet().stream()
1088 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001089 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Dase9c8971e2018-01-18 12:07:33 -08001090 linkHandler.processDeviceRemoved(device);
Charles Chaned3742352017-06-15 00:44:51 -07001091
Saurav Dasceccf242017-08-03 18:30:35 -07001092 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1093 if (gh != null) {
1094 gh.shutdown();
1095 }
Saurav Das80980c72016-03-23 11:22:49 -07001096 defaultRoutingHandler.purgeEcmpGraph(device.id());
Saurav Dasc88d4662017-05-15 15:34:25 -07001097 // Note that a switch going down is associated with all of its links
1098 // going down as well, but it is treated as a single switch down event
1099 // while the link-downs are ignored.
1100 defaultRoutingHandler
1101 .populateRoutingRulesForLinkStatusChange(null, null, device.id());
Charles Chan2199c302016-04-23 17:36:10 -07001102 mcastHandler.removeDevice(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001103 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -07001104 }
1105
Saurav Das1a129a02016-11-18 15:21:57 -08001106 private void processPortUpdated(Device device, Port port) {
1107 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1108 log.warn("Device configuration uploading. Not handling port event for"
1109 + "dev: {} port: {}", device.id(), port.number());
1110 return;
1111 }
Saurav Das018605f2017-02-18 14:05:44 -08001112
1113 if (!mastershipService.isLocalMaster(device.id())) {
1114 log.debug("Not master for dev:{} .. not handling port updated event"
1115 + "for port {}", device.id(), port.number());
1116 return;
1117 }
1118
1119 // first we handle filtering rules associated with the port
1120 if (port.isEnabled()) {
1121 log.info("Switchport {}/{} enabled..programming filters",
1122 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001123 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001124 } else {
1125 log.info("Switchport {}/{} disabled..removing filters",
1126 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001127 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001128 }
Saurav Das1a129a02016-11-18 15:21:57 -08001129
1130 // portUpdated calls are for ports that have gone down or up. For switch
1131 // to switch ports, link-events should take care of any re-routing or
1132 // group editing necessary for port up/down. Here we only process edge ports
1133 // that are already configured.
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001134 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1135 VlanId untaggedVlan = getUntaggedVlanId(cp);
1136 VlanId nativeVlan = getNativeVlanId(cp);
1137 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001138
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001139 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001140 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das1a129a02016-11-18 15:21:57 -08001141 + "dev/port: {}/{}", device.id(), port.number());
1142 return;
1143 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001144 if (untaggedVlan != null) {
1145 processEdgePort(device, port, untaggedVlan, true);
1146 }
1147 if (nativeVlan != null) {
1148 processEdgePort(device, port, nativeVlan, true);
1149 }
1150 if (!taggedVlans.isEmpty()) {
1151 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1152 }
Saurav Das1a129a02016-11-18 15:21:57 -08001153 }
1154
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001155 private void processEdgePort(Device device, Port port, VlanId vlanId,
1156 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001157 boolean portUp = port.isEnabled();
1158 if (portUp) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001159 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001160 port.number(), vlanId);
Charles Chanba90df12017-11-30 15:37:50 -08001161 hostHandler.processPortUp(new ConnectPoint(device.id(), port.number()));
Saurav Das1a129a02016-11-18 15:21:57 -08001162 } else {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001163 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001164 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001165 }
1166
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001167 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001168 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001169 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001170 } else {
1171 log.warn("Group handler not found for dev:{}. Not handling edge port"
1172 + " {} event for port:{}", device.id(),
1173 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001174 }
1175 }
sangho1e575652015-05-14 00:39:53 -07001176
Charles Chan463c6442017-10-20 16:06:55 -07001177 private void createOrUpdateDeviceConfiguration() {
1178 if (deviceConfiguration == null) {
1179 deviceConfiguration = new DeviceConfiguration(this);
1180 } else {
1181 deviceConfiguration.updateConfig();
1182 }
1183 }
1184
Pier Ventre10bd8d12016-11-26 21:05:22 -08001185 /**
1186 * Registers the given connect point with the NRS, this is necessary
1187 * to receive the NDP and ARP packets from the NRS.
1188 *
1189 * @param portToRegister connect point to register
1190 */
1191 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001192 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001193 portToRegister,
1194 neighbourHandler,
1195 appId
1196 );
1197 }
1198
Charles Chand6832882015-10-05 17:50:33 -07001199 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001200 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001201 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001202
Charles Chane849c192016-01-11 18:28:54 -08001203 /**
1204 * Constructs the internal network config listener.
1205 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001206 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001207 */
Charles Chan65238242017-06-22 18:03:14 -07001208 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001209 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001210 }
1211
Charles Chane849c192016-01-11 18:28:54 -08001212 /**
1213 * Reads network config and initializes related data structure accordingly.
1214 */
Charles Chanba90df12017-11-30 15:37:50 -08001215 void configureNetwork() {
Charles Chan463c6442017-10-20 16:06:55 -07001216 createOrUpdateDeviceConfiguration();
Charles Chan4636be02015-10-07 14:21:45 -07001217
Charles Chan2c15aca2016-11-09 20:51:44 -08001218 arpHandler = new ArpHandler(srManager);
1219 icmpHandler = new IcmpHandler(srManager);
1220 ipHandler = new IpHandler(srManager);
1221 routingRulePopulator = new RoutingRulePopulator(srManager);
1222 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001223
1224 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1225 groupHandlerMap, tunnelStore);
1226 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1227 flowObjectiveService,
1228 tunnelHandler, policyStore);
Saurav Das7bcbe702017-06-13 15:35:54 -07001229 // add a small delay to absorb multiple network config added notifications
1230 if (!programmingScheduled.get()) {
1231 programmingScheduled.set(true);
1232 executorService.schedule(new ConfigChange(), PROGRAM_DELAY,
1233 TimeUnit.SECONDS);
Charles Chan4636be02015-10-07 14:21:45 -07001234 }
Charles Chan2199c302016-04-23 17:36:10 -07001235 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001236 }
1237
Charles Chand6832882015-10-05 17:50:33 -07001238 @Override
1239 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001240 // TODO move this part to NetworkConfigEventHandler
1241 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1242 switch (event.type()) {
1243 case CONFIG_ADDED:
Charles Chan278ce832017-06-26 15:25:09 -07001244 log.info("Segment Routing Device Config added for {}", event.subject());
Charles Chan5270ed02016-01-30 23:22:37 -08001245 configureNetwork();
1246 break;
1247 case CONFIG_UPDATED:
Charles Chan278ce832017-06-26 15:25:09 -07001248 log.info("Segment Routing Config updated for {}", event.subject());
Charles Chan463c6442017-10-20 16:06:55 -07001249 createOrUpdateDeviceConfiguration();
Charles Chan278ce832017-06-26 15:25:09 -07001250 // TODO support dynamic configuration
1251 break;
1252 default:
1253 break;
1254 }
1255 } else if (event.configClass().equals(InterfaceConfig.class)) {
1256 switch (event.type()) {
1257 case CONFIG_ADDED:
1258 log.info("Interface Config added for {}", event.subject());
1259 configureNetwork();
1260 break;
1261 case CONFIG_UPDATED:
1262 log.info("Interface Config updated for {}", event.subject());
Charles Chan463c6442017-10-20 16:06:55 -07001263 createOrUpdateDeviceConfiguration();
Jonghwan Hyun5f1def82017-08-25 17:48:36 -07001264
1265 // Following code will be uncommented when [CORD-634] is fully implemented.
1266 // [CORD-634] Add dynamic config support for interfaces
1267 updateInterface((InterfaceConfig) event.config().get(),
1268 (InterfaceConfig) event.prevConfig().get());
Charles Chan5270ed02016-01-30 23:22:37 -08001269 // TODO support dynamic configuration
1270 break;
1271 default:
1272 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001273 }
Charles Chan5270ed02016-01-30 23:22:37 -08001274 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001275 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001276 switch (event.type()) {
1277 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001278 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001279 break;
1280 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001281 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001282 break;
1283 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001284 appCfgHandler.processAppConfigRemoved(event);
1285 break;
1286 default:
1287 break;
1288 }
Charles Chan03a73e02016-10-24 14:52:01 -07001289 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001290 } else if (event.configClass().equals(XConnectConfig.class)) {
1291 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1292 switch (event.type()) {
1293 case CONFIG_ADDED:
1294 xConnectHandler.processXConnectConfigAdded(event);
1295 break;
1296 case CONFIG_UPDATED:
1297 xConnectHandler.processXConnectConfigUpdated(event);
1298 break;
1299 case CONFIG_REMOVED:
1300 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001301 break;
1302 default:
1303 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001304 }
Pier Ventref34966c2016-11-07 16:21:04 -08001305 } else if (event.configClass().equals(PwaasConfig.class)) {
1306 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1307 switch (event.type()) {
1308 case CONFIG_ADDED:
1309 l2TunnelHandler.processPwaasConfigAdded(event);
1310 break;
1311 case CONFIG_UPDATED:
1312 l2TunnelHandler.processPwaasConfigUpdated(event);
1313 break;
1314 case CONFIG_REMOVED:
1315 l2TunnelHandler.processPwaasConfigRemoved(event);
1316 break;
1317 default:
1318 break;
1319 }
Charles Chand6832882015-10-05 17:50:33 -07001320 }
1321 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001322
Charles Chan91b371b2018-01-03 16:26:32 -08001323 @Override
1324 public boolean isRelevant(NetworkConfigEvent event) {
1325 if (event.configClass().equals(SegmentRoutingDeviceConfig.class) ||
1326 event.configClass().equals(SegmentRoutingAppConfig.class) ||
1327 event.configClass().equals(InterfaceConfig.class) ||
1328 event.configClass().equals(XConnectConfig.class) ||
1329 event.configClass().equals(PwaasConfig.class)) {
1330 return true;
1331 }
1332 log.debug("Ignore irrelevant event class {}", event.configClass().getName());
1333 return false;
1334 }
1335
Saurav Das7bcbe702017-06-13 15:35:54 -07001336 private final class ConfigChange implements Runnable {
1337 @Override
1338 public void run() {
1339 programmingScheduled.set(false);
1340 for (Device device : deviceService.getDevices()) {
1341 processDeviceAdded(device);
1342 }
1343 defaultRoutingHandler.startPopulationProcess();
1344 }
1345 }
Charles Chand6832882015-10-05 17:50:33 -07001346 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001347
1348 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001349 @Override
1350 public void event(HostEvent event) {
Charles Chan68aa62d2015-11-09 16:37:23 -08001351 switch (event.type()) {
1352 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001353 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001354 break;
1355 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001356 hostHandler.processHostMovedEvent(event);
Charles Chan23686832017-08-23 14:46:43 -07001357 routeHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001358 break;
1359 case HOST_REMOVED:
Charles Chanf9a52702017-06-16 15:19:24 -07001360 hostHandler.processHostRemovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001361 break;
1362 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001363 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001364 break;
1365 default:
1366 log.warn("Unsupported host event type: {}", event.type());
1367 break;
1368 }
1369 }
1370 }
1371
Charles Chand55e84d2016-03-30 17:54:24 -07001372 private class InternalMcastListener implements McastListener {
1373 @Override
1374 public void event(McastEvent event) {
1375 switch (event.type()) {
1376 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001377 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001378 break;
1379 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001380 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001381 break;
1382 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001383 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001384 break;
1385 case ROUTE_ADDED:
1386 case ROUTE_REMOVED:
1387 default:
1388 break;
1389 }
1390 }
1391 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001392
Charles Chan03a73e02016-10-24 14:52:01 -07001393 private class InternalRouteEventListener implements RouteListener {
1394 @Override
1395 public void event(RouteEvent event) {
1396 switch (event.type()) {
1397 case ROUTE_ADDED:
1398 routeHandler.processRouteAdded(event);
1399 break;
1400 case ROUTE_UPDATED:
1401 routeHandler.processRouteUpdated(event);
1402 break;
1403 case ROUTE_REMOVED:
1404 routeHandler.processRouteRemoved(event);
1405 break;
Charles Chan23686832017-08-23 14:46:43 -07001406 case ALTERNATIVE_ROUTES_CHANGED:
1407 routeHandler.processAlternativeRoutesChanged(event);
1408 break;
Charles Chan03a73e02016-10-24 14:52:01 -07001409 default:
1410 break;
1411 }
1412 }
1413 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001414
Jonghwan Hyun5f1def82017-08-25 17:48:36 -07001415 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1416 try {
1417 Set<Interface> intfs = conf.getInterfaces();
1418 Set<Interface> prevIntfs = prevConf.getInterfaces();
1419
1420 // Now we only handle one interface config at each port.
1421 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1422 log.warn("Interface update aborted - one at a time is allowed, " +
1423 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1424 return;
1425 }
1426
1427 Interface intf = intfs.stream().findFirst().get();
1428 Interface prevIntf = prevIntfs.stream().findFirst().get();
1429
1430 DeviceId deviceId = intf.connectPoint().deviceId();
1431 PortNumber portNum = intf.connectPoint().port();
1432
1433 if (!mastershipService.isLocalMaster(deviceId)) {
1434 log.debug("CONFIG_UPDATED event for interfaces should be " +
1435 "handled by master node for device {}", deviceId);
1436 return;
1437 }
1438
1439 removeSubnetConfig(prevIntf.connectPoint(),
1440 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1441 new HashSet<>(intf.ipAddressesList())));
1442
1443 if (prevIntf.vlanNative() != VlanId.NONE && !intf.vlanNative().equals(prevIntf.vlanNative())) {
1444 // RemoveVlanNative
1445 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1446 }
1447
1448 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1449 // RemoveVlanTagged
1450 prevIntf.vlanTagged().stream().filter(i -> !intf.vlanTagged().contains(i)).forEach(
1451 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, false)
1452 );
1453 }
1454
1455 if (prevIntf.vlanUntagged() != VlanId.NONE && !intf.vlanUntagged().equals(prevIntf.vlanUntagged())) {
1456 // RemoveVlanUntagged
1457 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1458 }
1459
1460 if (intf.vlanNative() != VlanId.NONE && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1461 // AddVlanNative
1462 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1463 }
1464
1465 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1466 // AddVlanTagged
1467 intf.vlanTagged().stream().filter(i -> !prevIntf.vlanTagged().contains(i)).forEach(
1468 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, true)
1469 );
1470 }
1471
1472 if (intf.vlanUntagged() != VlanId.NONE && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())) {
1473 // AddVlanUntagged
1474 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1475 }
1476 addSubnetConfig(prevIntf.connectPoint(),
1477 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1478 new HashSet<>(prevIntf.ipAddressesList())));
1479 } catch (ConfigException e) {
1480 log.error("Error in configuration");
1481 }
1482 }
1483
1484 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1485 VlanId vlanId, boolean pushVlan, boolean install) {
1486 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1487 if (grpHandler == null) {
1488 log.warn("Failed to retrieve group handler for device {}", deviceId);
1489 return;
1490 }
1491
1492 // Update filtering objective for a single port
1493 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1494
1495 // Update filtering objective for multicast ingress port
1496 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1497
1498 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1499
1500 if (nextId != -1 && !install) {
1501 // Update next objective for a single port as an output port
1502 // Remove a single port from L2FG
1503 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1504 // Remove L2 Bridging rule and L3 Unicast rule to the host
1505 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1506 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1507 // only if there is no port configured on that VLAN ID
1508 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1509 // Remove broadcast forwarding rule for the VLAN
1510 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1511 // Remove L2FG for VLAN
1512 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1513 } else {
1514 // Remove L2IG of the port
1515 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1516 }
1517 } else if (install) {
1518 if (nextId != -1) {
1519 // Add a single port to L2FG
1520 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1521 } else {
1522 // Create L2FG for VLAN
1523 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1524 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1525 }
1526 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1527 } else {
1528 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1529 }
1530 }
1531
1532 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1533 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1534 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1535
1536 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1537 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1538 .filter(intf -> !intf.connectPoint().equals(cp))
1539 .flatMap(intf -> intf.ipAddressesList().stream())
1540 .collect(Collectors.toSet());
1541 // 1. Partial subnet population
1542 // Remove routing rules for removed subnet from previous configuration,
1543 // which does not also exist in other interfaces in the same device
1544 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1545 .map(InterfaceIpAddress::subnetAddress)
1546 .collect(Collectors.toSet());
1547
1548 defaultRoutingHandler.revokeSubnet(
1549 ipPrefixSet.stream()
1550 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1551 .collect(Collectors.toSet()));
1552
1553 // 2. Interface IP punts
1554 // Remove IP punts for old Intf address
1555 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1556 .map(InterfaceIpAddress::ipAddress)
1557 .collect(Collectors.toSet());
1558 ipAddressSet.stream()
1559 .map(InterfaceIpAddress::ipAddress)
1560 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1561 .forEach(interfaceIpAddress ->
1562 routingRulePopulator.revokeSingleIpPunts(
1563 cp.deviceId(), interfaceIpAddress));
1564
1565 // 3. Host unicast routing rule
1566 // Remove unicast routing rule
1567 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1568 }
1569
1570 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1571 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1572 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1573
1574 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1575 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1576 .filter(intf -> !intf.connectPoint().equals(cp))
1577 .flatMap(intf -> intf.ipAddressesList().stream())
1578 .collect(Collectors.toSet());
1579 // 1. Partial subnet population
1580 // Add routing rules for newly added subnet, which does not also exist in
1581 // other interfaces in the same device
1582 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1583 .map(InterfaceIpAddress::subnetAddress)
1584 .collect(Collectors.toSet());
1585
1586 defaultRoutingHandler.populateSubnet(
1587 Collections.singleton(cp),
1588 ipPrefixSet.stream()
1589 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1590 .collect(Collectors.toSet()));
1591
1592 // 2. Interface IP punts
1593 // Add IP punts for new Intf address
1594 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1595 .map(InterfaceIpAddress::ipAddress)
1596 .collect(Collectors.toSet());
1597 ipAddressSet.stream()
1598 .map(InterfaceIpAddress::ipAddress)
1599 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1600 .forEach(interfaceIpAddress ->
1601 routingRulePopulator.populateSingleIpPunts(
1602 cp.deviceId(), interfaceIpAddress));
1603
1604 // 3. Host unicast routing rule
1605 // Add unicast routing rule
1606 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1607 }
Saurav Dase9c8971e2018-01-18 12:07:33 -08001608
sanghob35a6192015-04-01 13:05:26 -07001609}