blob: 439a3d80f3bdf7d8d9d262f9d73d32dd510effb5 [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;
Saurav Das07a34aa2018-02-09 17:26:45 -0800135import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
136import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Charles Chan3e783d02016-02-26 22:19:52 -0800137
Charles Chane849c192016-01-11 18:28:54 -0800138/**
139 * Segment routing manager.
140 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700141@Service
142@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700143public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700144
Charles Chan2c15aca2016-11-09 20:51:44 -0800145 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan23686832017-08-23 14:46:43 -0700146 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sanghob35a6192015-04-01 13:05:26 -0700147
148 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700149 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700150
151 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre10bd8d12016-11-26 21:05:22 -0800152 private NeighbourResolutionService neighbourResolutionService;
153
154 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800155 public PathService pathService;
156
157 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700158 CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700159
160 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700161 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700162
163 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700164 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700165
166 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chanba90df12017-11-30 15:37:50 -0800167 HostLocationProbingService probingService;
168
169 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700170 DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700171
172 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Saurav Dase9c8971e2018-01-18 12:07:33 -0800173 DeviceAdminService deviceAdminService;
174
175 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800176 public FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700177
178 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700179 LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700180
Charles Chan5270ed02016-01-30 23:22:37 -0800181 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800182 public MastershipService mastershipService;
Charles Chan03a73e02016-10-24 14:52:01 -0700183
184 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800185 public StorageService storageService;
Charles Chan03a73e02016-10-24 14:52:01 -0700186
187 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
188 MulticastRouteService multicastRouteService;
189
190 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
191 TopologyService topologyService;
192
193 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700194 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800195
196 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800197 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800198
Saurav Das80980c72016-03-23 11:22:49 -0700199 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800200 public InterfaceService interfaceService;
201
Charles Chanba90df12017-11-30 15:37:50 -0800202 @Property(name = "activeProbing", boolValue = true,
203 label = "Enable active probing to discover dual-homed hosts.")
204 boolean activeProbing = true;
205
Charles Chan03a73e02016-10-24 14:52:01 -0700206 ArpHandler arpHandler = null;
207 IcmpHandler icmpHandler = null;
208 IpHandler ipHandler = null;
209 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700210 ApplicationId appId;
211 DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700212
Charles Chan03a73e02016-10-24 14:52:01 -0700213 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700214 private TunnelHandler tunnelHandler = null;
215 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700216 private InternalPacketProcessor processor = null;
217 private InternalLinkListener linkListener = null;
218 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700219 private AppConfigHandler appCfgHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700220 XConnectHandler xConnectHandler = null;
Saurav Dase9c8971e2018-01-18 12:07:33 -0800221 McastHandler mcastHandler = null;
222 HostHandler hostHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800223 private RouteHandler routeHandler = null;
Saurav Dase9c8971e2018-01-18 12:07:33 -0800224 LinkHandler linkHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800225 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Pier Ventref34966c2016-11-07 16:21:04 -0800226 private L2TunnelHandler l2TunnelHandler = null;
sanghob35a6192015-04-01 13:05:26 -0700227 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan5270ed02016-01-30 23:22:37 -0800228 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700229 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
230 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700231 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sanghob35a6192015-04-01 13:05:26 -0700232
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700233 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700234 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sanghob35a6192015-04-01 13:05:26 -0700235
Saurav Das4ce45962015-11-24 23:21:05 -0800236 @SuppressWarnings("unused")
sanghob35a6192015-04-01 13:05:26 -0700237 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800238 @SuppressWarnings("rawtypes")
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700239 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Saurav Dase9c8971e2018-01-18 12:07:33 -0800240 Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chane849c192016-01-11 18:28:54 -0800241 new ConcurrentHashMap<>();
242 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700243 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5deb3232017-08-22 15:07:34 -0700244 * Used to keep track on MPLS group information.
Charles Chane849c192016-01-11 18:28:54 -0800245 */
Charles Chanba90df12017-11-30 15:37:50 -0800246 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das7bcbe702017-06-13 15:35:54 -0700247 dsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800248 /**
Charles Chan5deb3232017-08-22 15:07:34 -0700249 * Per device next objective ID store with (device id + vlanid) as key.
250 * Used to keep track on L2 flood group information.
Charles Chane849c192016-01-11 18:28:54 -0800251 */
Charles Chanba90df12017-11-30 15:37:50 -0800252 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan59cc16d2017-02-02 16:20:42 -0800253 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800254 /**
Charles Chan5deb3232017-08-22 15:07:34 -0700255 * Per device next objective ID store with (device id + port + treatment + meta) as key.
256 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chane849c192016-01-11 18:28:54 -0800257 */
Charles Chanba90df12017-11-30 15:37:50 -0800258 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800259 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800260
Saurav Das4ce45962015-11-24 23:21:05 -0800261 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
262 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700263
Saurav Das7bcbe702017-06-13 15:35:54 -0700264 private AtomicBoolean programmingScheduled = new AtomicBoolean();
265
Charles Chand55e84d2016-03-30 17:54:24 -0700266 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700267 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
268 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700269 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700270 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800271 public SegmentRoutingDeviceConfig createConfig() {
272 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700273 }
274 };
Pier Ventref34966c2016-11-07 16:21:04 -0800275
Charles Chand55e84d2016-03-30 17:54:24 -0700276 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700277 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
278 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700279 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800280 @Override
281 public SegmentRoutingAppConfig createConfig() {
282 return new SegmentRoutingAppConfig();
283 }
284 };
Pier Ventref34966c2016-11-07 16:21:04 -0800285
Charles Chanfc5c7802016-05-17 13:13:55 -0700286 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
287 new ConfigFactory<ApplicationId, XConnectConfig>(
288 SubjectFactories.APP_SUBJECT_FACTORY,
289 XConnectConfig.class, "xconnect") {
290 @Override
291 public XConnectConfig createConfig() {
292 return new XConnectConfig();
293 }
294 };
Pier Ventref34966c2016-11-07 16:21:04 -0800295
Charles Chand55e84d2016-03-30 17:54:24 -0700296 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700297 new ConfigFactory<ApplicationId, McastConfig>(
298 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700299 McastConfig.class, "multicast") {
300 @Override
301 public McastConfig createConfig() {
302 return new McastConfig();
303 }
304 };
305
Pier Ventref34966c2016-11-07 16:21:04 -0800306 private final ConfigFactory<ApplicationId, PwaasConfig> pwaasConfigFactory =
307 new ConfigFactory<ApplicationId, PwaasConfig>(
308 SubjectFactories.APP_SUBJECT_FACTORY,
309 PwaasConfig.class, "pwaas") {
310 @Override
311 public PwaasConfig createConfig() {
312 return new PwaasConfig();
313 }
314 };
315
Charles Chan65238242017-06-22 18:03:14 -0700316 private static final Object THREAD_SCHED_LOCK = new Object();
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700317 private static int numOfEventsQueued = 0;
318 private static int numOfEventsExecuted = 0;
sanghob35a6192015-04-01 13:05:26 -0700319 private static int numOfHandlerExecution = 0;
320 private static int numOfHandlerScheduled = 0;
321
Charles Chan116188d2016-02-18 14:22:42 -0800322 /**
323 * Segment Routing App ID.
324 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800325 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan59cc16d2017-02-02 16:20:42 -0800326
Charles Chane849c192016-01-11 18:28:54 -0800327 /**
328 * The default VLAN ID assigned to the interfaces without subnet config.
329 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800330 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700331
sanghob35a6192015-04-01 13:05:26 -0700332 @Activate
Charles Chanba90df12017-11-30 15:37:50 -0800333 protected void activate(ComponentContext context) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800334 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700335
336 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das7bcbe702017-06-13 15:35:54 -0700337 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700338 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700339 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700340 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700341 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700342 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700343 .build();
Saurav Das7bcbe702017-06-13 15:35:54 -0700344 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700345
Charles Chan59cc16d2017-02-02 16:20:42 -0800346 log.debug("Creating EC map vlannextobjectivestore");
347 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
348 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
349 vlanNextObjStore = vlanNextObjMapBuilder
350 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700351 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700352 .withTimestampProvider((k, v) -> new WallClockTimestamp())
353 .build();
354
Saurav Das4ce45962015-11-24 23:21:05 -0800355 log.debug("Creating EC map subnetnextobjectivestore");
356 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
357 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
358 portNextObjStore = portNextObjMapBuilder
359 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700360 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800361 .withTimestampProvider((k, v) -> new WallClockTimestamp())
362 .build();
363
sangho0b2b6d12015-05-20 22:16:38 -0700364 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
365 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700366 tunnelStore = tunnelMapBuilder
367 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700368 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700369 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700370 .build();
371
372 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
373 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700374 policyStore = policyMapBuilder
375 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700376 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700377 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700378 .build();
379
Saurav Das80980c72016-03-23 11:22:49 -0700380 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800381 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700382 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800383 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800384 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
385 "requestInterceptsEnabled", "false");
Charles Chand3baaba2017-08-08 15:13:37 -0700386 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800387 "requestInterceptsEnabled", "false");
Charles Chanc760f382017-07-24 15:56:10 -0700388 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800389 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800390 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
391 "greedyLearningIpv6", "true");
Charles Chanc6d227e2017-02-28 15:15:17 -0800392 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
393 "forceUnprovision", "true");
Charles Chanef624ed2017-08-10 16:57:28 -0700394 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan73316522017-07-20 16:16:25 -0700395 "distributed", "true");
Charles Chan35a32322017-08-14 11:42:11 -0700396 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
397 "multihomingEnabled", "true");
Charles Chanfd892012017-11-29 19:54:20 -0800398 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
399 "staleLinkAge", "15000");
400 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
401 "allowDuplicateIps", "false");
Charles Chanba90df12017-11-30 15:37:50 -0800402 compCfgService.registerProperties(getClass());
403 modified(context);
Saurav Das80980c72016-03-23 11:22:49 -0700404
Charles Chanb8e10c82015-10-14 11:24:40 -0700405 processor = new InternalPacketProcessor();
406 linkListener = new InternalLinkListener();
407 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700408 appCfgHandler = new AppConfigHandler(this);
409 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700410 mcastHandler = new McastHandler(this);
411 hostHandler = new HostHandler(this);
Saurav Dase9c8971e2018-01-18 12:07:33 -0800412 linkHandler = new LinkHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700413 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800414 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Pier Ventref34966c2016-11-07 16:21:04 -0800415 l2TunnelHandler = new L2TunnelHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700416
Charles Chan3e783d02016-02-26 22:19:52 -0800417 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700418 cfgService.registerConfigFactory(deviceConfigFactory);
419 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700420 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700421 cfgService.registerConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800422 cfgService.registerConfigFactory(pwaasConfigFactory);
Saurav Das07a34aa2018-02-09 17:26:45 -0800423 log.info("Configuring network before adding listeners");
Charles Chan8d055212018-01-04 14:26:07 -0800424 cfgListener.configureNetwork();
425
Charles Chan5270ed02016-01-30 23:22:37 -0800426 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700427 packetService.addProcessor(processor, PacketProcessor.director(2));
428 linkService.addListener(linkListener);
429 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700430 multicastRouteService.addListener(mcastListener);
Charles Chanc7a8a682017-06-19 00:43:31 -0700431 routeService.addListener(routeListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700432
Charles Chan09c66992018-03-09 15:53:44 -0800433 linkHandler.init();
434
sanghob35a6192015-04-01 13:05:26 -0700435 log.info("Started");
436 }
437
Saurav Dase9c8971e2018-01-18 12:07:33 -0800438 KryoNamespace.Builder createSerializer() {
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700439 return new KryoNamespace.Builder()
440 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700441 .register(DestinationSetNextObjectiveStoreKey.class,
Charles Chan59cc16d2017-02-02 16:20:42 -0800442 VlanNextObjectiveStoreKey.class,
Saurav Das7bcbe702017-06-13 15:35:54 -0700443 DestinationSet.class,
444 NextNeighbors.class,
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700445 Tunnel.class,
446 DefaultTunnel.class,
447 Policy.class,
448 TunnelPolicy.class,
449 Policy.Type.class,
450 PortNextObjectiveStoreKey.class,
Charles Chanfc5c7802016-05-17 13:13:55 -0700451 XConnectStoreKey.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700452 );
453 }
454
sanghob35a6192015-04-01 13:05:26 -0700455 @Deactivate
456 protected void deactivate() {
Charles Chand6832882015-10-05 17:50:33 -0700457 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700458 cfgService.unregisterConfigFactory(deviceConfigFactory);
459 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700460 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700461 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800462 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chanba90df12017-11-30 15:37:50 -0800463 compCfgService.unregisterProperties(getClass(), false);
Charles Chand6832882015-10-05 17:50:33 -0700464
Charles Chanc7a8a682017-06-19 00:43:31 -0700465 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700466 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700467 linkService.removeListener(linkListener);
468 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700469 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700470 routeService.removeListener(routeListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700471
Charles Chan0aa674e2017-02-23 15:44:08 -0800472 neighbourResolutionService.unregisterNeighbourHandlers(appId);
473
sanghob35a6192015-04-01 13:05:26 -0700474 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700475 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700476 deviceListener = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700477 groupHandlerMap.clear();
478
Saurav Das7bcbe702017-06-13 15:35:54 -0700479 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800480 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700481 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700482 tunnelStore.destroy();
483 policyStore.destroy();
sanghob35a6192015-04-01 13:05:26 -0700484 log.info("Stopped");
485 }
486
Charles Chanba90df12017-11-30 15:37:50 -0800487 @Modified
488 private void modified(ComponentContext context) {
489 Dictionary<?, ?> properties = context.getProperties();
490 if (properties == null) {
491 return;
492 }
493
494 String strActiveProving = Tools.get(properties, "activeProbing");
495 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
496
497 if (expectActiveProbing != activeProbing) {
498 activeProbing = expectActiveProbing;
499 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
500 }
501 }
502
sangho1e575652015-05-14 00:39:53 -0700503 @Override
504 public List<Tunnel> getTunnels() {
505 return tunnelHandler.getTunnels();
506 }
507
508 @Override
sangho71abe1b2015-06-29 14:58:47 -0700509 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
510 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700511 }
512
513 @Override
sangho71abe1b2015-06-29 14:58:47 -0700514 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700515 for (Policy policy: policyHandler.getPolicies()) {
516 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
517 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
518 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
519 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700520 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700521 }
522 }
523 }
sangho71abe1b2015-06-29 14:58:47 -0700524 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700525 }
526
527 @Override
sangho71abe1b2015-06-29 14:58:47 -0700528 public PolicyHandler.Result removePolicy(Policy policy) {
529 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700530 }
531
532 @Override
sangho71abe1b2015-06-29 14:58:47 -0700533 public PolicyHandler.Result createPolicy(Policy policy) {
534 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700535 }
536
537 @Override
538 public List<Policy> getPolicies() {
539 return policyHandler.getPolicies();
540 }
541
Saurav Das59232cf2016-04-27 18:35:50 -0700542 @Override
543 public void rerouteNetwork() {
544 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700545 }
546
Charles Chanc81c45b2016-10-20 17:02:44 -0700547 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800548 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
549 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Charles Chanc81c45b2016-10-20 17:02:44 -0700550 deviceService.getAvailableDevices().forEach(device -> {
551 deviceSubnetMap.put(device.id(), deviceConfiguration.getSubnets(device.id()));
552 });
553 return deviceSubnetMap;
554 }
555
Saurav Dasc88d4662017-05-15 15:34:25 -0700556
557 @Override
558 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
559 if (defaultRoutingHandler != null) {
560 return defaultRoutingHandler.getCurrentEmcpSpgMap();
561 } else {
562 return null;
563 }
564 }
565
566 @Override
Saurav Das7bcbe702017-06-13 15:35:54 -0700567 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
568 if (dsNextObjStore != null) {
569 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700570 } else {
571 return ImmutableMap.of();
572 }
573 }
574
Saurav Dasceccf242017-08-03 18:30:35 -0700575 @Override
576 public void verifyGroups(DeviceId id) {
577 DefaultGroupHandler gh = groupHandlerMap.get(id);
578 if (gh != null) {
579 gh.triggerBucketCorrector();
580 }
581 }
582
Saurav Das95047002018-01-25 09:49:01 -0800583 @Override
584 public ImmutableMap<Link, Boolean> getSeenLinks() {
585 return linkHandler.getSeenLinks();
586 }
587
588 @Override
589 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
590 return linkHandler.getDownedPorts();
591 }
592
sanghof9d0bf12015-05-19 11:57:42 -0700593 /**
Ray Milkeye4afdb52017-04-05 09:42:04 -0700594 * Extracts the application ID from the manager.
595 *
596 * @return application ID
597 */
598 public ApplicationId appId() {
599 return appId;
600 }
601
602 /**
603 * Returns the device configuration.
604 *
605 * @return device configuration
606 */
607 public DeviceConfiguration deviceConfiguration() {
608 return deviceConfiguration;
609 }
610
611 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700612 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5deb3232017-08-22 15:07:34 -0700613 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700614 *
615 * @return next objective ID store
616 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700617 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
618 dsNextObjStore() {
619 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700620 }
621
622 /**
Charles Chan5deb3232017-08-22 15:07:34 -0700623 * Per device next objective ID store with (device id + vlanid) as key.
624 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700625 *
626 * @return vlan next object store
627 */
628 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
629 return vlanNextObjStore;
630 }
631
632 /**
Charles Chan5deb3232017-08-22 15:07:34 -0700633 * Per device next objective ID store with (device id + port + treatment + meta) as key.
634 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700635 *
636 * @return port next object store.
637 */
638 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
639 return portNextObjStore;
640 }
641
642 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700643 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
644 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -0700645 *
646 * @return MPLS-ECMP value
647 */
648 public boolean getMplsEcmp() {
649 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
650 .getConfig(this.appId, SegmentRoutingAppConfig.class);
651 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
652 }
653
654 /**
sanghof9d0bf12015-05-19 11:57:42 -0700655 * Returns the tunnel object with the tunnel ID.
656 *
657 * @param tunnelId Tunnel ID
658 * @return Tunnel reference
659 */
sangho1e575652015-05-14 00:39:53 -0700660 public Tunnel getTunnel(String tunnelId) {
661 return tunnelHandler.getTunnel(tunnelId);
662 }
663
Charles Chan7ffd81f2017-02-08 15:52:08 -0800664 // TODO Consider moving these to InterfaceService
sanghob35a6192015-04-01 13:05:26 -0700665 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800666 * Returns untagged VLAN configured on given connect point.
Charles Chan7ffd81f2017-02-08 15:52:08 -0800667 * <p>
668 * Only returns the first match if there are multiple untagged VLAN configured
669 * on the connect point.
sanghob35a6192015-04-01 13:05:26 -0700670 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800671 * @param connectPoint connect point
672 * @return untagged VLAN or null if not configured
sanghob35a6192015-04-01 13:05:26 -0700673 */
Charles Chan65238242017-06-22 18:03:14 -0700674 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800675 return interfaceService.getInterfacesByPort(connectPoint).stream()
676 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
677 .map(Interface::vlanUntagged)
678 .findFirst().orElse(null);
sanghob35a6192015-04-01 13:05:26 -0700679 }
680
sangho1e575652015-05-14 00:39:53 -0700681 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800682 * Returns tagged VLAN configured on given connect point.
683 * <p>
684 * Returns all matches if there are multiple tagged VLAN configured
685 * on the connect point.
686 *
687 * @param connectPoint connect point
688 * @return tagged VLAN or empty set if not configured
689 */
Charles Chan65238242017-06-22 18:03:14 -0700690 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800691 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
692 return interfaces.stream()
693 .map(Interface::vlanTagged)
Charles Chan65238242017-06-22 18:03:14 -0700694 .flatMap(Set::stream)
Charles Chan7ffd81f2017-02-08 15:52:08 -0800695 .collect(Collectors.toSet());
696 }
697
698 /**
699 * Returns native VLAN configured on given connect point.
700 * <p>
701 * Only returns the first match if there are multiple native VLAN configured
702 * on the connect point.
703 *
704 * @param connectPoint connect point
705 * @return native VLAN or null if not configured
706 */
Charles Chan65238242017-06-22 18:03:14 -0700707 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800708 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
709 return interfaces.stream()
710 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
711 .map(Interface::vlanNative)
712 .findFirst()
713 .orElse(null);
714 }
715
716 /**
Charles Chanf9a52702017-06-16 15:19:24 -0700717 * Returns internal VLAN for untagged hosts on given connect point.
718 * <p>
719 * The internal VLAN is either vlan-untagged for an access port,
720 * or vlan-native for a trunk port.
721 *
722 * @param connectPoint connect point
723 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
724 */
Charles Chan65238242017-06-22 18:03:14 -0700725 VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chanf9a52702017-06-16 15:19:24 -0700726 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
727 VlanId nativeVlanId = getNativeVlanId(connectPoint);
728 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
729 }
730
731 /**
Charles Chan65238242017-06-22 18:03:14 -0700732 * Returns optional pair device ID of given device.
733 *
734 * @param deviceId device ID
735 * @return optional pair device ID. Might be empty if pair device is not configured
736 */
737 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
738 SegmentRoutingDeviceConfig deviceConfig =
739 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
740 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
741 }
742 /**
743 * Returns optional pair device local port of given device.
744 *
745 * @param deviceId device ID
746 * @return optional pair device ID. Might be empty if pair device is not configured
747 */
748 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
749 SegmentRoutingDeviceConfig deviceConfig =
750 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
751 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
752 }
753
754 /**
Charles Chan23686832017-08-23 14:46:43 -0700755 * Determine if current instance is the master of given connect point.
756 *
757 * @param cp connect point
758 * @return true if current instance is the master of given connect point
759 */
760 boolean isMasterOf(ConnectPoint cp) {
761 boolean isMaster = mastershipService.isLocalMaster(cp.deviceId());
762 if (!isMaster) {
763 log.debug(NOT_MASTER, cp);
764 }
765 return isMaster;
766 }
767
768 /**
Charles Chanb15d0102017-08-23 13:55:39 -0700769 * Returns locations of given resolved route.
770 *
771 * @param resolvedRoute resolved route
772 * @return locations of nexthop. Might be empty if next hop is not found
773 */
774 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
775 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
776 return Optional.ofNullable(hostService.getHost(hostId))
777 .map(Host::locations).orElse(Sets.newHashSet())
778 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
779 }
780
781 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800782 * Returns vlan port map of given device.
783 *
784 * @param deviceId device id
785 * @return vlan-port multimap
786 */
787 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
788 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
789
790 interfaceService.getInterfaces().stream()
791 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
792 .forEach(intf -> {
793 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -0700794 intf.vlanTagged().forEach(vlanTagged ->
795 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
796 );
Charles Chan7ffd81f2017-02-08 15:52:08 -0800797 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
798 });
799 vlanPortMap.removeAll(VlanId.NONE);
800
801 return vlanPortMap;
802 }
803
804 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800805 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -0800806 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700807 *
808 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -0800809 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -0800810 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700811 */
Charles Chan65238242017-06-22 18:03:14 -0700812 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -0700813 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800814 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
815 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700816 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -0800817 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -0800818 + "device {} not found", deviceId);
819 return -1;
820 }
821 }
822
823 /**
824 * Returns the next objective ID for the given portNumber, given the treatment.
825 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -0700826 * would result in different objectives. If the next object does not exist,
827 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -0800828 *
829 * @param deviceId Device ID
830 * @param portNum port number on device for which NextObjective is queried
831 * @param treatment the actions to apply on the packets (should include outport)
832 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -0700833 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -0700834 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800835 */
836 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
837 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700838 TrafficSelector meta,
839 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -0800840 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
841 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -0700842 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -0800843 } else {
Charles Chane849c192016-01-11 18:28:54 -0800844 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
845 + " not found", deviceId);
846 return -1;
847 }
848 }
849
Saurav Dasc88d4662017-05-15 15:34:25 -0700850 /**
851 * Returns the group handler object for the specified device id.
852 *
853 * @param devId the device identifier
854 * @return the groupHandler object for the device id, or null if not found
855 */
Charles Chan65238242017-06-22 18:03:14 -0700856 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700857 return groupHandlerMap.get(devId);
858 }
859
860 /**
Saurav Dasceccf242017-08-03 18:30:35 -0700861 * Returns the default routing handler object.
862 *
863 * @return the default routing handler object
864 */
865 public DefaultRoutingHandler getRoutingHandler() {
866 return defaultRoutingHandler;
867 }
868
sanghob35a6192015-04-01 13:05:26 -0700869 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700870 @Override
871 public void process(PacketContext context) {
872
873 if (context.isHandled()) {
874 return;
875 }
876
877 InboundPacket pkt = context.inPacket();
878 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -0800879
880 if (ethernet == null) {
881 return;
882 }
883
Saurav Das7bcbe702017-06-13 15:35:54 -0700884 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
885 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800886 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700887 log.warn("Received unexpected ARP packet on {}",
888 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -0700889 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -0800890 return;
sanghob35a6192015-04-01 13:05:26 -0700891 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800892 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
893 //ipHandler.addToPacketBuffer(ipv4Packet);
894 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
895 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700896 } else {
Charles Chan50035632017-01-13 17:20:44 -0800897 // NOTE: We don't support IP learning at this moment so this
898 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -0800899 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700900 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800901 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
902 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -0800903 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -0800904 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -0800905 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
906 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
907 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
908 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
909 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
910 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -0700911 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -0700912 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -0800913 }
914 } else {
915 // NOTE: We don't support IP learning at this moment so this
916 // is not necessary. Also it causes duplication of DHCPv6 packets.
917 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
918 }
sanghob35a6192015-04-01 13:05:26 -0700919 }
920 }
921 }
922
923 private class InternalLinkListener implements LinkListener {
924 @Override
925 public void event(LinkEvent event) {
Charles Chanb1f8c762017-03-29 16:39:05 -0700926 if (event.type() == LinkEvent.Type.LINK_ADDED ||
927 event.type() == LinkEvent.Type.LINK_UPDATED ||
928 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700929 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700930 scheduleEventHandlerIfNotScheduled(event);
931 }
932 }
933 }
934
935 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -0700936 @Override
937 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -0700938 switch (event.type()) {
939 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -0800940 case PORT_UPDATED:
941 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -0700942 case DEVICE_UPDATED:
943 case DEVICE_AVAILABILITY_CHANGED:
Saurav Dasc88d4662017-05-15 15:34:25 -0700944 log.trace("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700945 scheduleEventHandlerIfNotScheduled(event);
946 break;
947 default:
948 }
949 }
950 }
951
Saurav Das4ce45962015-11-24 23:21:05 -0800952 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -0700953 private void scheduleEventHandlerIfNotScheduled(Event event) {
Charles Chan65238242017-06-22 18:03:14 -0700954 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700955 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700956 numOfEventsQueued++;
957
958 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
959 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700960 eventHandlerFuture = executorService
961 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
962 numOfHandlerScheduled++;
963 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700964 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700965 numOfEventsQueued,
966 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -0700967 }
sanghob35a6192015-04-01 13:05:26 -0700968 }
969
970 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700971 @Override
sanghob35a6192015-04-01 13:05:26 -0700972 public void run() {
Pier Luigi657ddfc2018-02-12 09:56:20 +0100973 while (true) {
974 try {
Saurav Das4ce45962015-11-24 23:21:05 -0800975 @SuppressWarnings("rawtypes")
Charles Chan65238242017-06-22 18:03:14 -0700976 Event event;
977 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700978 if (!eventQueue.isEmpty()) {
979 event = eventQueue.poll();
980 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700981 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700982 numOfHandlerExecution++;
983 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
984 numOfHandlerExecution, numOfEventsExecuted);
985 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700986 }
sangho20eff1d2015-04-13 15:15:58 -0700987 }
Charles Chanb1f8c762017-03-29 16:39:05 -0700988 if (event.type() == LinkEvent.Type.LINK_ADDED ||
989 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Dase9c8971e2018-01-18 12:07:33 -0800990 linkHandler.processLinkAdded((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700991 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Saurav Dase9c8971e2018-01-18 12:07:33 -0800992 linkHandler.processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700993 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
994 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
995 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800996 DeviceId deviceId = ((Device) event.subject()).id();
997 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -0700998 log.info("Processing device event {} for available device {}",
999 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001000 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -07001001 } else {
1002 log.info("Processing device event {} for unavailable device {}",
Pier Luigi657ddfc2018-02-12 09:56:20 +01001003 event.type(), ((Device) event.subject()).id());
Saurav Das80980c72016-03-23 11:22:49 -07001004 processDeviceRemoved((Device) event.subject());
1005 }
Saurav Das1a129a02016-11-18 15:21:57 -08001006 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001007 // typically these calls come when device is added first time
1008 // so port filtering rules are handled at the device_added event.
1009 // port added calls represent all ports on the device,
1010 // enabled or not.
Saurav Dasc88d4662017-05-15 15:34:25 -07001011 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001012 ((DeviceEvent) event).subject().id(),
1013 ((DeviceEvent) event).port().number(),
1014 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -08001015 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001016 // these calls happen for every subsequent event
1017 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -08001018 log.info("** PORT UPDATED {}/{} -> {}",
1019 event.subject(),
1020 ((DeviceEvent) event).port(),
1021 event.type());
1022 processPortUpdated(((Device) event.subject()),
1023 ((DeviceEvent) event).port());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001024 } else {
1025 log.warn("Unhandled event type: {}", event.type());
1026 }
Pier Luigi657ddfc2018-02-12 09:56:20 +01001027 } catch (Exception e) {
1028 log.error("SegmentRouting event handler thread thrown an exception: {}",
1029 e.getMessage(), e);
sanghob35a6192015-04-01 13:05:26 -07001030 }
1031 }
sanghob35a6192015-04-01 13:05:26 -07001032 }
1033 }
1034
Saurav Dase9c8971e2018-01-18 12:07:33 -08001035 void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001036 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001037
1038 // NOTE: Punt ARP/NDP even when the device is not configured.
1039 // Host learning without network config is required for CORD config generator.
1040 routingRulePopulator.populateIpPunts(device.id());
1041 routingRulePopulator.populateArpNdpPunts(device.id());
1042
Charles Chan0b4e6182015-11-03 10:42:14 -08001043 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001044 log.warn("Device configuration unavailable. Device {} will be "
1045 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001046 return;
1047 }
Charles Chan2199c302016-04-23 17:36:10 -07001048 processDeviceAddedInternal(device.id());
1049 }
1050
1051 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001052 // Irrespective of whether the local is a MASTER or not for this device,
1053 // we need to create a SR-group-handler instance. This is because in a
1054 // multi-instance setup, any instance can initiate forwarding/next-objectives
1055 // for any switch (even if this instance is a SLAVE or not even connected
1056 // to the switch). To handle this, a default-group-handler instance is necessary
1057 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001058 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1059 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001060 DefaultGroupHandler groupHandler;
1061 try {
1062 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001063 createGroupHandler(deviceId,
1064 appId,
1065 deviceConfiguration,
1066 linkService,
1067 flowObjectiveService,
1068 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001069 } catch (DeviceConfigNotFoundException e) {
1070 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1071 return;
1072 }
Saurav Das9aa28bd2017-12-05 15:00:23 -08001073 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001074 deviceId);
1075 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001076 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001077
Charles Chan2199c302016-04-23 17:36:10 -07001078 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001079 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan03a73e02016-10-24 14:52:01 -07001080 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001081 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001082 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001083 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001084 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001085 }
Charles Chan5270ed02016-01-30 23:22:37 -08001086
Charles Chan03a73e02016-10-24 14:52:01 -07001087 appCfgHandler.init(deviceId);
1088 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001089 }
1090
Saurav Das80980c72016-03-23 11:22:49 -07001091 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001092 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001093 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chanba90df12017-11-30 15:37:50 -08001094 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001095 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001096 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001097 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001098 portNextObjStore.entrySet().stream()
1099 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001100 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Dase9c8971e2018-01-18 12:07:33 -08001101 linkHandler.processDeviceRemoved(device);
Charles Chaned3742352017-06-15 00:44:51 -07001102
Saurav Dasceccf242017-08-03 18:30:35 -07001103 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1104 if (gh != null) {
1105 gh.shutdown();
1106 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001107 // Note that a switch going down is associated with all of its links
1108 // going down as well, but it is treated as a single switch down event
1109 // while the link-downs are ignored.
1110 defaultRoutingHandler
1111 .populateRoutingRulesForLinkStatusChange(null, null, device.id());
Saurav Das95047002018-01-25 09:49:01 -08001112 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan2199c302016-04-23 17:36:10 -07001113 mcastHandler.removeDevice(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001114 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -07001115 }
1116
Saurav Das1a129a02016-11-18 15:21:57 -08001117 private void processPortUpdated(Device device, Port port) {
1118 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1119 log.warn("Device configuration uploading. Not handling port event for"
1120 + "dev: {} port: {}", device.id(), port.number());
1121 return;
1122 }
Saurav Das018605f2017-02-18 14:05:44 -08001123
1124 if (!mastershipService.isLocalMaster(device.id())) {
1125 log.debug("Not master for dev:{} .. not handling port updated event"
1126 + "for port {}", device.id(), port.number());
1127 return;
1128 }
1129
1130 // first we handle filtering rules associated with the port
1131 if (port.isEnabled()) {
1132 log.info("Switchport {}/{} enabled..programming filters",
1133 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001134 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001135 } else {
1136 log.info("Switchport {}/{} disabled..removing filters",
1137 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001138 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001139 }
Saurav Das1a129a02016-11-18 15:21:57 -08001140
1141 // portUpdated calls are for ports that have gone down or up. For switch
1142 // to switch ports, link-events should take care of any re-routing or
1143 // group editing necessary for port up/down. Here we only process edge ports
1144 // that are already configured.
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001145 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1146 VlanId untaggedVlan = getUntaggedVlanId(cp);
1147 VlanId nativeVlan = getNativeVlanId(cp);
1148 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001149
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001150 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001151 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das1a129a02016-11-18 15:21:57 -08001152 + "dev/port: {}/{}", device.id(), port.number());
1153 return;
1154 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001155 if (untaggedVlan != null) {
1156 processEdgePort(device, port, untaggedVlan, true);
1157 }
1158 if (nativeVlan != null) {
1159 processEdgePort(device, port, nativeVlan, true);
1160 }
1161 if (!taggedVlans.isEmpty()) {
1162 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1163 }
Saurav Das1a129a02016-11-18 15:21:57 -08001164 }
1165
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001166 private void processEdgePort(Device device, Port port, VlanId vlanId,
1167 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001168 boolean portUp = port.isEnabled();
1169 if (portUp) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001170 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001171 port.number(), vlanId);
Charles Chanba90df12017-11-30 15:37:50 -08001172 hostHandler.processPortUp(new ConnectPoint(device.id(), port.number()));
Saurav Das1a129a02016-11-18 15:21:57 -08001173 } else {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001174 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001175 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001176 }
1177
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001178 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001179 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001180 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001181 } else {
1182 log.warn("Group handler not found for dev:{}. Not handling edge port"
1183 + " {} event for port:{}", device.id(),
1184 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001185 }
1186 }
sangho1e575652015-05-14 00:39:53 -07001187
Charles Chan463c6442017-10-20 16:06:55 -07001188 private void createOrUpdateDeviceConfiguration() {
1189 if (deviceConfiguration == null) {
Saurav Das07a34aa2018-02-09 17:26:45 -08001190 log.info("Creating new DeviceConfiguration");
Charles Chan463c6442017-10-20 16:06:55 -07001191 deviceConfiguration = new DeviceConfiguration(this);
1192 } else {
Saurav Das07a34aa2018-02-09 17:26:45 -08001193 log.info("Updating DeviceConfiguration");
Charles Chan463c6442017-10-20 16:06:55 -07001194 deviceConfiguration.updateConfig();
1195 }
1196 }
1197
Pier Ventre10bd8d12016-11-26 21:05:22 -08001198 /**
1199 * Registers the given connect point with the NRS, this is necessary
1200 * to receive the NDP and ARP packets from the NRS.
1201 *
1202 * @param portToRegister connect point to register
1203 */
1204 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001205 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001206 portToRegister,
1207 neighbourHandler,
1208 appId
1209 );
1210 }
1211
Charles Chand6832882015-10-05 17:50:33 -07001212 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001213 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001214 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001215
Charles Chane849c192016-01-11 18:28:54 -08001216 /**
1217 * Constructs the internal network config listener.
1218 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001219 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001220 */
Charles Chan65238242017-06-22 18:03:14 -07001221 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001222 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001223 }
1224
Charles Chane849c192016-01-11 18:28:54 -08001225 /**
1226 * Reads network config and initializes related data structure accordingly.
1227 */
Charles Chanba90df12017-11-30 15:37:50 -08001228 void configureNetwork() {
Saurav Das07a34aa2018-02-09 17:26:45 -08001229 log.info("Configuring network ...");
Charles Chan463c6442017-10-20 16:06:55 -07001230 createOrUpdateDeviceConfiguration();
Charles Chan4636be02015-10-07 14:21:45 -07001231
Charles Chan2c15aca2016-11-09 20:51:44 -08001232 arpHandler = new ArpHandler(srManager);
1233 icmpHandler = new IcmpHandler(srManager);
1234 ipHandler = new IpHandler(srManager);
1235 routingRulePopulator = new RoutingRulePopulator(srManager);
1236 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001237
1238 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1239 groupHandlerMap, tunnelStore);
1240 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1241 flowObjectiveService,
1242 tunnelHandler, policyStore);
Saurav Das7bcbe702017-06-13 15:35:54 -07001243 // add a small delay to absorb multiple network config added notifications
1244 if (!programmingScheduled.get()) {
Saurav Das07a34aa2018-02-09 17:26:45 -08001245 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
Saurav Das7bcbe702017-06-13 15:35:54 -07001246 programmingScheduled.set(true);
1247 executorService.schedule(new ConfigChange(), PROGRAM_DELAY,
1248 TimeUnit.SECONDS);
Charles Chan4636be02015-10-07 14:21:45 -07001249 }
Charles Chan2199c302016-04-23 17:36:10 -07001250 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001251 }
1252
Charles Chand6832882015-10-05 17:50:33 -07001253 @Override
1254 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001255 // TODO move this part to NetworkConfigEventHandler
1256 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1257 switch (event.type()) {
1258 case CONFIG_ADDED:
Charles Chan278ce832017-06-26 15:25:09 -07001259 log.info("Segment Routing Device Config added for {}", event.subject());
Charles Chan5270ed02016-01-30 23:22:37 -08001260 configureNetwork();
1261 break;
1262 case CONFIG_UPDATED:
Charles Chan278ce832017-06-26 15:25:09 -07001263 log.info("Segment Routing Config updated for {}", event.subject());
Charles Chan463c6442017-10-20 16:06:55 -07001264 createOrUpdateDeviceConfiguration();
Charles Chan278ce832017-06-26 15:25:09 -07001265 // TODO support dynamic configuration
1266 break;
1267 default:
1268 break;
1269 }
1270 } else if (event.configClass().equals(InterfaceConfig.class)) {
1271 switch (event.type()) {
1272 case CONFIG_ADDED:
1273 log.info("Interface Config added for {}", event.subject());
1274 configureNetwork();
1275 break;
1276 case CONFIG_UPDATED:
1277 log.info("Interface Config updated for {}", event.subject());
Charles Chan463c6442017-10-20 16:06:55 -07001278 createOrUpdateDeviceConfiguration();
Jonghwan Hyun5f1def82017-08-25 17:48:36 -07001279
1280 // Following code will be uncommented when [CORD-634] is fully implemented.
1281 // [CORD-634] Add dynamic config support for interfaces
1282 updateInterface((InterfaceConfig) event.config().get(),
1283 (InterfaceConfig) event.prevConfig().get());
Charles Chan5270ed02016-01-30 23:22:37 -08001284 // TODO support dynamic configuration
1285 break;
1286 default:
1287 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001288 }
Charles Chan5270ed02016-01-30 23:22:37 -08001289 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001290 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001291 switch (event.type()) {
1292 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001293 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001294 break;
1295 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001296 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001297 break;
1298 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001299 appCfgHandler.processAppConfigRemoved(event);
1300 break;
1301 default:
1302 break;
1303 }
Saurav Das07a34aa2018-02-09 17:26:45 -08001304 log.info("App config event .. configuring network");
Charles Chan03a73e02016-10-24 14:52:01 -07001305 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001306 } else if (event.configClass().equals(XConnectConfig.class)) {
1307 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1308 switch (event.type()) {
1309 case CONFIG_ADDED:
1310 xConnectHandler.processXConnectConfigAdded(event);
1311 break;
1312 case CONFIG_UPDATED:
1313 xConnectHandler.processXConnectConfigUpdated(event);
1314 break;
1315 case CONFIG_REMOVED:
1316 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001317 break;
1318 default:
1319 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001320 }
Pier Ventref34966c2016-11-07 16:21:04 -08001321 } else if (event.configClass().equals(PwaasConfig.class)) {
1322 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1323 switch (event.type()) {
1324 case CONFIG_ADDED:
1325 l2TunnelHandler.processPwaasConfigAdded(event);
1326 break;
1327 case CONFIG_UPDATED:
1328 l2TunnelHandler.processPwaasConfigUpdated(event);
1329 break;
1330 case CONFIG_REMOVED:
1331 l2TunnelHandler.processPwaasConfigRemoved(event);
1332 break;
1333 default:
1334 break;
1335 }
Charles Chand6832882015-10-05 17:50:33 -07001336 }
1337 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001338
Charles Chan91b371b2018-01-03 16:26:32 -08001339 @Override
1340 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Das07a34aa2018-02-09 17:26:45 -08001341 if (event.type() == CONFIG_REGISTERED ||
1342 event.type() == CONFIG_UNREGISTERED) {
1343 log.debug("Ignore event {} due to type mismatch", event);
1344 return false;
Charles Chan91b371b2018-01-03 16:26:32 -08001345 }
Saurav Das07a34aa2018-02-09 17:26:45 -08001346
1347 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1348 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1349 !event.configClass().equals(InterfaceConfig.class) &&
1350 !event.configClass().equals(XConnectConfig.class) &&
1351 !event.configClass().equals(PwaasConfig.class)) {
1352 log.debug("Ignore event {} due to class mismatch", event);
1353 return false;
1354 }
1355
1356 return true;
Charles Chan91b371b2018-01-03 16:26:32 -08001357 }
1358
Saurav Das7bcbe702017-06-13 15:35:54 -07001359 private final class ConfigChange implements Runnable {
1360 @Override
1361 public void run() {
1362 programmingScheduled.set(false);
Saurav Das07a34aa2018-02-09 17:26:45 -08001363 log.info("Reacting to config changes after buffer delay");
Saurav Das7bcbe702017-06-13 15:35:54 -07001364 for (Device device : deviceService.getDevices()) {
1365 processDeviceAdded(device);
1366 }
1367 defaultRoutingHandler.startPopulationProcess();
1368 }
1369 }
Charles Chand6832882015-10-05 17:50:33 -07001370 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001371
1372 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001373 @Override
1374 public void event(HostEvent event) {
Charles Chan68aa62d2015-11-09 16:37:23 -08001375 switch (event.type()) {
1376 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001377 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001378 break;
1379 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001380 hostHandler.processHostMovedEvent(event);
Charles Chan23686832017-08-23 14:46:43 -07001381 routeHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001382 break;
1383 case HOST_REMOVED:
Charles Chanf9a52702017-06-16 15:19:24 -07001384 hostHandler.processHostRemovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001385 break;
1386 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001387 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001388 break;
1389 default:
1390 log.warn("Unsupported host event type: {}", event.type());
1391 break;
1392 }
1393 }
1394 }
1395
Charles Chand55e84d2016-03-30 17:54:24 -07001396 private class InternalMcastListener implements McastListener {
1397 @Override
1398 public void event(McastEvent event) {
1399 switch (event.type()) {
1400 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001401 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001402 break;
1403 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001404 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001405 break;
1406 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001407 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001408 break;
1409 case ROUTE_ADDED:
1410 case ROUTE_REMOVED:
1411 default:
1412 break;
1413 }
1414 }
1415 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001416
Charles Chan03a73e02016-10-24 14:52:01 -07001417 private class InternalRouteEventListener implements RouteListener {
1418 @Override
1419 public void event(RouteEvent event) {
1420 switch (event.type()) {
1421 case ROUTE_ADDED:
1422 routeHandler.processRouteAdded(event);
1423 break;
1424 case ROUTE_UPDATED:
1425 routeHandler.processRouteUpdated(event);
1426 break;
1427 case ROUTE_REMOVED:
1428 routeHandler.processRouteRemoved(event);
1429 break;
Charles Chan23686832017-08-23 14:46:43 -07001430 case ALTERNATIVE_ROUTES_CHANGED:
1431 routeHandler.processAlternativeRoutesChanged(event);
1432 break;
Charles Chan03a73e02016-10-24 14:52:01 -07001433 default:
1434 break;
1435 }
1436 }
1437 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001438
Jonghwan Hyun5f1def82017-08-25 17:48:36 -07001439 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1440 try {
1441 Set<Interface> intfs = conf.getInterfaces();
1442 Set<Interface> prevIntfs = prevConf.getInterfaces();
1443
1444 // Now we only handle one interface config at each port.
1445 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1446 log.warn("Interface update aborted - one at a time is allowed, " +
1447 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1448 return;
1449 }
1450
1451 Interface intf = intfs.stream().findFirst().get();
1452 Interface prevIntf = prevIntfs.stream().findFirst().get();
1453
1454 DeviceId deviceId = intf.connectPoint().deviceId();
1455 PortNumber portNum = intf.connectPoint().port();
1456
1457 if (!mastershipService.isLocalMaster(deviceId)) {
1458 log.debug("CONFIG_UPDATED event for interfaces should be " +
1459 "handled by master node for device {}", deviceId);
1460 return;
1461 }
1462
1463 removeSubnetConfig(prevIntf.connectPoint(),
1464 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1465 new HashSet<>(intf.ipAddressesList())));
1466
1467 if (prevIntf.vlanNative() != VlanId.NONE && !intf.vlanNative().equals(prevIntf.vlanNative())) {
1468 // RemoveVlanNative
1469 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1470 }
1471
1472 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1473 // RemoveVlanTagged
1474 prevIntf.vlanTagged().stream().filter(i -> !intf.vlanTagged().contains(i)).forEach(
1475 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, false)
1476 );
1477 }
1478
1479 if (prevIntf.vlanUntagged() != VlanId.NONE && !intf.vlanUntagged().equals(prevIntf.vlanUntagged())) {
1480 // RemoveVlanUntagged
1481 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1482 }
1483
1484 if (intf.vlanNative() != VlanId.NONE && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1485 // AddVlanNative
1486 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1487 }
1488
1489 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1490 // AddVlanTagged
1491 intf.vlanTagged().stream().filter(i -> !prevIntf.vlanTagged().contains(i)).forEach(
1492 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, true)
1493 );
1494 }
1495
1496 if (intf.vlanUntagged() != VlanId.NONE && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())) {
1497 // AddVlanUntagged
1498 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1499 }
1500 addSubnetConfig(prevIntf.connectPoint(),
1501 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1502 new HashSet<>(prevIntf.ipAddressesList())));
1503 } catch (ConfigException e) {
1504 log.error("Error in configuration");
1505 }
1506 }
1507
1508 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1509 VlanId vlanId, boolean pushVlan, boolean install) {
1510 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1511 if (grpHandler == null) {
1512 log.warn("Failed to retrieve group handler for device {}", deviceId);
1513 return;
1514 }
1515
1516 // Update filtering objective for a single port
1517 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1518
1519 // Update filtering objective for multicast ingress port
1520 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1521
1522 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1523
1524 if (nextId != -1 && !install) {
1525 // Update next objective for a single port as an output port
1526 // Remove a single port from L2FG
1527 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1528 // Remove L2 Bridging rule and L3 Unicast rule to the host
1529 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1530 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1531 // only if there is no port configured on that VLAN ID
1532 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1533 // Remove broadcast forwarding rule for the VLAN
1534 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1535 // Remove L2FG for VLAN
1536 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1537 } else {
1538 // Remove L2IG of the port
1539 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1540 }
1541 } else if (install) {
1542 if (nextId != -1) {
1543 // Add a single port to L2FG
1544 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1545 } else {
1546 // Create L2FG for VLAN
1547 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1548 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1549 }
1550 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1551 } else {
1552 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1553 }
1554 }
1555
1556 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1557 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1558 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1559
1560 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1561 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1562 .filter(intf -> !intf.connectPoint().equals(cp))
1563 .flatMap(intf -> intf.ipAddressesList().stream())
1564 .collect(Collectors.toSet());
1565 // 1. Partial subnet population
1566 // Remove routing rules for removed subnet from previous configuration,
1567 // which does not also exist in other interfaces in the same device
1568 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1569 .map(InterfaceIpAddress::subnetAddress)
1570 .collect(Collectors.toSet());
1571
1572 defaultRoutingHandler.revokeSubnet(
1573 ipPrefixSet.stream()
1574 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1575 .collect(Collectors.toSet()));
1576
1577 // 2. Interface IP punts
1578 // Remove IP punts for old Intf address
1579 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1580 .map(InterfaceIpAddress::ipAddress)
1581 .collect(Collectors.toSet());
1582 ipAddressSet.stream()
1583 .map(InterfaceIpAddress::ipAddress)
1584 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1585 .forEach(interfaceIpAddress ->
1586 routingRulePopulator.revokeSingleIpPunts(
1587 cp.deviceId(), interfaceIpAddress));
1588
1589 // 3. Host unicast routing rule
1590 // Remove unicast routing rule
1591 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1592 }
1593
1594 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1595 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1596 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1597
1598 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1599 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1600 .filter(intf -> !intf.connectPoint().equals(cp))
1601 .flatMap(intf -> intf.ipAddressesList().stream())
1602 .collect(Collectors.toSet());
1603 // 1. Partial subnet population
1604 // Add routing rules for newly added subnet, which does not also exist in
1605 // other interfaces in the same device
1606 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1607 .map(InterfaceIpAddress::subnetAddress)
1608 .collect(Collectors.toSet());
1609
1610 defaultRoutingHandler.populateSubnet(
1611 Collections.singleton(cp),
1612 ipPrefixSet.stream()
1613 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1614 .collect(Collectors.toSet()));
1615
1616 // 2. Interface IP punts
1617 // Add IP punts for new Intf address
1618 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1619 .map(InterfaceIpAddress::ipAddress)
1620 .collect(Collectors.toSet());
1621 ipAddressSet.stream()
1622 .map(InterfaceIpAddress::ipAddress)
1623 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1624 .forEach(interfaceIpAddress ->
1625 routingRulePopulator.populateSingleIpPunts(
1626 cp.deviceId(), interfaceIpAddress));
1627
1628 // 3. Host unicast routing rule
1629 // Add unicast routing rule
1630 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1631 }
Saurav Dase9c8971e2018-01-18 12:07:33 -08001632
sanghob35a6192015-04-01 13:05:26 -07001633}