blob: 7f44d2d9165f9457c7ef8df98b738ab4c5339f13 [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
sanghob35a6192015-04-01 13:05:26 -0700433 log.info("Started");
434 }
435
Saurav Dase9c8971e2018-01-18 12:07:33 -0800436 KryoNamespace.Builder createSerializer() {
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700437 return new KryoNamespace.Builder()
438 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700439 .register(DestinationSetNextObjectiveStoreKey.class,
Charles Chan59cc16d2017-02-02 16:20:42 -0800440 VlanNextObjectiveStoreKey.class,
Saurav Das7bcbe702017-06-13 15:35:54 -0700441 DestinationSet.class,
442 NextNeighbors.class,
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700443 Tunnel.class,
444 DefaultTunnel.class,
445 Policy.class,
446 TunnelPolicy.class,
447 Policy.Type.class,
448 PortNextObjectiveStoreKey.class,
Charles Chanfc5c7802016-05-17 13:13:55 -0700449 XConnectStoreKey.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700450 );
451 }
452
sanghob35a6192015-04-01 13:05:26 -0700453 @Deactivate
454 protected void deactivate() {
Charles Chand6832882015-10-05 17:50:33 -0700455 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700456 cfgService.unregisterConfigFactory(deviceConfigFactory);
457 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700458 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700459 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800460 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chanba90df12017-11-30 15:37:50 -0800461 compCfgService.unregisterProperties(getClass(), false);
Charles Chand6832882015-10-05 17:50:33 -0700462
Charles Chanc7a8a682017-06-19 00:43:31 -0700463 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700464 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700465 linkService.removeListener(linkListener);
466 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700467 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700468 routeService.removeListener(routeListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700469
Charles Chan0aa674e2017-02-23 15:44:08 -0800470 neighbourResolutionService.unregisterNeighbourHandlers(appId);
471
sanghob35a6192015-04-01 13:05:26 -0700472 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700473 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700474 deviceListener = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700475 groupHandlerMap.clear();
476
Saurav Das7bcbe702017-06-13 15:35:54 -0700477 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800478 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700479 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700480 tunnelStore.destroy();
481 policyStore.destroy();
sanghob35a6192015-04-01 13:05:26 -0700482 log.info("Stopped");
483 }
484
Charles Chanba90df12017-11-30 15:37:50 -0800485 @Modified
486 private void modified(ComponentContext context) {
487 Dictionary<?, ?> properties = context.getProperties();
488 if (properties == null) {
489 return;
490 }
491
492 String strActiveProving = Tools.get(properties, "activeProbing");
493 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
494
495 if (expectActiveProbing != activeProbing) {
496 activeProbing = expectActiveProbing;
497 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
498 }
499 }
500
sangho1e575652015-05-14 00:39:53 -0700501 @Override
502 public List<Tunnel> getTunnels() {
503 return tunnelHandler.getTunnels();
504 }
505
506 @Override
sangho71abe1b2015-06-29 14:58:47 -0700507 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
508 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700509 }
510
511 @Override
sangho71abe1b2015-06-29 14:58:47 -0700512 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700513 for (Policy policy: policyHandler.getPolicies()) {
514 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
515 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
516 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
517 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700518 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700519 }
520 }
521 }
sangho71abe1b2015-06-29 14:58:47 -0700522 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700523 }
524
525 @Override
sangho71abe1b2015-06-29 14:58:47 -0700526 public PolicyHandler.Result removePolicy(Policy policy) {
527 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700528 }
529
530 @Override
sangho71abe1b2015-06-29 14:58:47 -0700531 public PolicyHandler.Result createPolicy(Policy policy) {
532 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700533 }
534
535 @Override
536 public List<Policy> getPolicies() {
537 return policyHandler.getPolicies();
538 }
539
Saurav Das59232cf2016-04-27 18:35:50 -0700540 @Override
541 public void rerouteNetwork() {
542 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700543 }
544
Charles Chanc81c45b2016-10-20 17:02:44 -0700545 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800546 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
547 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Charles Chanc81c45b2016-10-20 17:02:44 -0700548 deviceService.getAvailableDevices().forEach(device -> {
549 deviceSubnetMap.put(device.id(), deviceConfiguration.getSubnets(device.id()));
550 });
551 return deviceSubnetMap;
552 }
553
Saurav Dasc88d4662017-05-15 15:34:25 -0700554
555 @Override
556 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
557 if (defaultRoutingHandler != null) {
558 return defaultRoutingHandler.getCurrentEmcpSpgMap();
559 } else {
560 return null;
561 }
562 }
563
564 @Override
Saurav Das7bcbe702017-06-13 15:35:54 -0700565 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
566 if (dsNextObjStore != null) {
567 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700568 } else {
569 return ImmutableMap.of();
570 }
571 }
572
Saurav Dasceccf242017-08-03 18:30:35 -0700573 @Override
574 public void verifyGroups(DeviceId id) {
575 DefaultGroupHandler gh = groupHandlerMap.get(id);
576 if (gh != null) {
577 gh.triggerBucketCorrector();
578 }
579 }
580
Saurav Das95047002018-01-25 09:49:01 -0800581 @Override
582 public ImmutableMap<Link, Boolean> getSeenLinks() {
583 return linkHandler.getSeenLinks();
584 }
585
586 @Override
587 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
588 return linkHandler.getDownedPorts();
589 }
590
sanghof9d0bf12015-05-19 11:57:42 -0700591 /**
Ray Milkeye4afdb52017-04-05 09:42:04 -0700592 * Extracts the application ID from the manager.
593 *
594 * @return application ID
595 */
596 public ApplicationId appId() {
597 return appId;
598 }
599
600 /**
601 * Returns the device configuration.
602 *
603 * @return device configuration
604 */
605 public DeviceConfiguration deviceConfiguration() {
606 return deviceConfiguration;
607 }
608
609 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700610 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5deb3232017-08-22 15:07:34 -0700611 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700612 *
613 * @return next objective ID store
614 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700615 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
616 dsNextObjStore() {
617 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700618 }
619
620 /**
Charles Chan5deb3232017-08-22 15:07:34 -0700621 * Per device next objective ID store with (device id + vlanid) as key.
622 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700623 *
624 * @return vlan next object store
625 */
626 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
627 return vlanNextObjStore;
628 }
629
630 /**
Charles Chan5deb3232017-08-22 15:07:34 -0700631 * Per device next objective ID store with (device id + port + treatment + meta) as key.
632 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700633 *
634 * @return port next object store.
635 */
636 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
637 return portNextObjStore;
638 }
639
640 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700641 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
642 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -0700643 *
644 * @return MPLS-ECMP value
645 */
646 public boolean getMplsEcmp() {
647 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
648 .getConfig(this.appId, SegmentRoutingAppConfig.class);
649 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
650 }
651
652 /**
sanghof9d0bf12015-05-19 11:57:42 -0700653 * Returns the tunnel object with the tunnel ID.
654 *
655 * @param tunnelId Tunnel ID
656 * @return Tunnel reference
657 */
sangho1e575652015-05-14 00:39:53 -0700658 public Tunnel getTunnel(String tunnelId) {
659 return tunnelHandler.getTunnel(tunnelId);
660 }
661
Charles Chan7ffd81f2017-02-08 15:52:08 -0800662 // TODO Consider moving these to InterfaceService
sanghob35a6192015-04-01 13:05:26 -0700663 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800664 * Returns untagged VLAN configured on given connect point.
Charles Chan7ffd81f2017-02-08 15:52:08 -0800665 * <p>
666 * Only returns the first match if there are multiple untagged VLAN configured
667 * on the connect point.
sanghob35a6192015-04-01 13:05:26 -0700668 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800669 * @param connectPoint connect point
670 * @return untagged VLAN or null if not configured
sanghob35a6192015-04-01 13:05:26 -0700671 */
Charles Chan65238242017-06-22 18:03:14 -0700672 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800673 return interfaceService.getInterfacesByPort(connectPoint).stream()
674 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
675 .map(Interface::vlanUntagged)
676 .findFirst().orElse(null);
sanghob35a6192015-04-01 13:05:26 -0700677 }
678
sangho1e575652015-05-14 00:39:53 -0700679 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800680 * Returns tagged VLAN configured on given connect point.
681 * <p>
682 * Returns all matches if there are multiple tagged VLAN configured
683 * on the connect point.
684 *
685 * @param connectPoint connect point
686 * @return tagged VLAN or empty set if not configured
687 */
Charles Chan65238242017-06-22 18:03:14 -0700688 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800689 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
690 return interfaces.stream()
691 .map(Interface::vlanTagged)
Charles Chan65238242017-06-22 18:03:14 -0700692 .flatMap(Set::stream)
Charles Chan7ffd81f2017-02-08 15:52:08 -0800693 .collect(Collectors.toSet());
694 }
695
696 /**
697 * Returns native VLAN configured on given connect point.
698 * <p>
699 * Only returns the first match if there are multiple native VLAN configured
700 * on the connect point.
701 *
702 * @param connectPoint connect point
703 * @return native VLAN or null if not configured
704 */
Charles Chan65238242017-06-22 18:03:14 -0700705 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800706 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
707 return interfaces.stream()
708 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
709 .map(Interface::vlanNative)
710 .findFirst()
711 .orElse(null);
712 }
713
714 /**
Charles Chanf9a52702017-06-16 15:19:24 -0700715 * Returns internal VLAN for untagged hosts on given connect point.
716 * <p>
717 * The internal VLAN is either vlan-untagged for an access port,
718 * or vlan-native for a trunk port.
719 *
720 * @param connectPoint connect point
721 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
722 */
Charles Chan65238242017-06-22 18:03:14 -0700723 VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chanf9a52702017-06-16 15:19:24 -0700724 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
725 VlanId nativeVlanId = getNativeVlanId(connectPoint);
726 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
727 }
728
729 /**
Charles Chan65238242017-06-22 18:03:14 -0700730 * Returns optional pair device ID of given device.
731 *
732 * @param deviceId device ID
733 * @return optional pair device ID. Might be empty if pair device is not configured
734 */
735 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
736 SegmentRoutingDeviceConfig deviceConfig =
737 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
738 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
739 }
740 /**
741 * Returns optional pair device local port of given device.
742 *
743 * @param deviceId device ID
744 * @return optional pair device ID. Might be empty if pair device is not configured
745 */
746 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
747 SegmentRoutingDeviceConfig deviceConfig =
748 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
749 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
750 }
751
752 /**
Charles Chan23686832017-08-23 14:46:43 -0700753 * Determine if current instance is the master of given connect point.
754 *
755 * @param cp connect point
756 * @return true if current instance is the master of given connect point
757 */
758 boolean isMasterOf(ConnectPoint cp) {
759 boolean isMaster = mastershipService.isLocalMaster(cp.deviceId());
760 if (!isMaster) {
761 log.debug(NOT_MASTER, cp);
762 }
763 return isMaster;
764 }
765
766 /**
Charles Chanb15d0102017-08-23 13:55:39 -0700767 * Returns locations of given resolved route.
768 *
769 * @param resolvedRoute resolved route
770 * @return locations of nexthop. Might be empty if next hop is not found
771 */
772 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
773 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
774 return Optional.ofNullable(hostService.getHost(hostId))
775 .map(Host::locations).orElse(Sets.newHashSet())
776 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
777 }
778
779 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800780 * Returns vlan port map of given device.
781 *
782 * @param deviceId device id
783 * @return vlan-port multimap
784 */
785 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
786 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
787
788 interfaceService.getInterfaces().stream()
789 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
790 .forEach(intf -> {
791 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -0700792 intf.vlanTagged().forEach(vlanTagged ->
793 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
794 );
Charles Chan7ffd81f2017-02-08 15:52:08 -0800795 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
796 });
797 vlanPortMap.removeAll(VlanId.NONE);
798
799 return vlanPortMap;
800 }
801
802 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800803 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -0800804 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700805 *
806 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -0800807 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -0800808 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700809 */
Charles Chan65238242017-06-22 18:03:14 -0700810 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -0700811 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800812 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
813 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700814 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -0800815 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -0800816 + "device {} not found", deviceId);
817 return -1;
818 }
819 }
820
821 /**
822 * Returns the next objective ID for the given portNumber, given the treatment.
823 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -0700824 * would result in different objectives. If the next object does not exist,
825 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -0800826 *
827 * @param deviceId Device ID
828 * @param portNum port number on device for which NextObjective is queried
829 * @param treatment the actions to apply on the packets (should include outport)
830 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -0700831 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -0700832 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800833 */
834 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
835 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700836 TrafficSelector meta,
837 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -0800838 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
839 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -0700840 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -0800841 } else {
Charles Chane849c192016-01-11 18:28:54 -0800842 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
843 + " not found", deviceId);
844 return -1;
845 }
846 }
847
Saurav Dasc88d4662017-05-15 15:34:25 -0700848 /**
849 * Returns the group handler object for the specified device id.
850 *
851 * @param devId the device identifier
852 * @return the groupHandler object for the device id, or null if not found
853 */
Charles Chan65238242017-06-22 18:03:14 -0700854 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700855 return groupHandlerMap.get(devId);
856 }
857
858 /**
Saurav Dasceccf242017-08-03 18:30:35 -0700859 * Returns the default routing handler object.
860 *
861 * @return the default routing handler object
862 */
863 public DefaultRoutingHandler getRoutingHandler() {
864 return defaultRoutingHandler;
865 }
866
sanghob35a6192015-04-01 13:05:26 -0700867 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700868 @Override
869 public void process(PacketContext context) {
870
871 if (context.isHandled()) {
872 return;
873 }
874
875 InboundPacket pkt = context.inPacket();
876 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -0800877
878 if (ethernet == null) {
879 return;
880 }
881
Saurav Das7bcbe702017-06-13 15:35:54 -0700882 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
883 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800884 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700885 log.warn("Received unexpected ARP packet on {}",
886 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -0700887 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -0800888 return;
sanghob35a6192015-04-01 13:05:26 -0700889 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800890 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
891 //ipHandler.addToPacketBuffer(ipv4Packet);
892 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
893 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700894 } else {
Charles Chan50035632017-01-13 17:20:44 -0800895 // NOTE: We don't support IP learning at this moment so this
896 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -0800897 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700898 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800899 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
900 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -0800901 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -0800902 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -0800903 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
904 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
905 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
906 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
907 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
908 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -0700909 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -0700910 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -0800911 }
912 } else {
913 // NOTE: We don't support IP learning at this moment so this
914 // is not necessary. Also it causes duplication of DHCPv6 packets.
915 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
916 }
sanghob35a6192015-04-01 13:05:26 -0700917 }
918 }
919 }
920
921 private class InternalLinkListener implements LinkListener {
922 @Override
923 public void event(LinkEvent event) {
Charles Chanb1f8c762017-03-29 16:39:05 -0700924 if (event.type() == LinkEvent.Type.LINK_ADDED ||
925 event.type() == LinkEvent.Type.LINK_UPDATED ||
926 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700927 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700928 scheduleEventHandlerIfNotScheduled(event);
929 }
930 }
931 }
932
933 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -0700934 @Override
935 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -0700936 switch (event.type()) {
937 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -0800938 case PORT_UPDATED:
939 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -0700940 case DEVICE_UPDATED:
941 case DEVICE_AVAILABILITY_CHANGED:
Saurav Dasc88d4662017-05-15 15:34:25 -0700942 log.trace("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700943 scheduleEventHandlerIfNotScheduled(event);
944 break;
945 default:
946 }
947 }
948 }
949
Saurav Das4ce45962015-11-24 23:21:05 -0800950 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -0700951 private void scheduleEventHandlerIfNotScheduled(Event event) {
Charles Chan65238242017-06-22 18:03:14 -0700952 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700953 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700954 numOfEventsQueued++;
955
956 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
957 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700958 eventHandlerFuture = executorService
959 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
960 numOfHandlerScheduled++;
961 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700962 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700963 numOfEventsQueued,
964 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -0700965 }
sanghob35a6192015-04-01 13:05:26 -0700966 }
967
968 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700969 @Override
sanghob35a6192015-04-01 13:05:26 -0700970 public void run() {
Pier Luigi657ddfc2018-02-12 09:56:20 +0100971 while (true) {
972 try {
Saurav Das4ce45962015-11-24 23:21:05 -0800973 @SuppressWarnings("rawtypes")
Charles Chan65238242017-06-22 18:03:14 -0700974 Event event;
975 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700976 if (!eventQueue.isEmpty()) {
977 event = eventQueue.poll();
978 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700979 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700980 numOfHandlerExecution++;
981 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
982 numOfHandlerExecution, numOfEventsExecuted);
983 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700984 }
sangho20eff1d2015-04-13 15:15:58 -0700985 }
Charles Chanb1f8c762017-03-29 16:39:05 -0700986 if (event.type() == LinkEvent.Type.LINK_ADDED ||
987 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Dase9c8971e2018-01-18 12:07:33 -0800988 linkHandler.processLinkAdded((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700989 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Saurav Dase9c8971e2018-01-18 12:07:33 -0800990 linkHandler.processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700991 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
992 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
993 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800994 DeviceId deviceId = ((Device) event.subject()).id();
995 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -0700996 log.info("Processing device event {} for available device {}",
997 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700998 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -0700999 } else {
1000 log.info("Processing device event {} for unavailable device {}",
Pier Luigi657ddfc2018-02-12 09:56:20 +01001001 event.type(), ((Device) event.subject()).id());
Saurav Das80980c72016-03-23 11:22:49 -07001002 processDeviceRemoved((Device) event.subject());
1003 }
Saurav Das1a129a02016-11-18 15:21:57 -08001004 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001005 // typically these calls come when device is added first time
1006 // so port filtering rules are handled at the device_added event.
1007 // port added calls represent all ports on the device,
1008 // enabled or not.
Saurav Dasc88d4662017-05-15 15:34:25 -07001009 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001010 ((DeviceEvent) event).subject().id(),
1011 ((DeviceEvent) event).port().number(),
1012 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -08001013 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001014 // these calls happen for every subsequent event
1015 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -08001016 log.info("** PORT UPDATED {}/{} -> {}",
1017 event.subject(),
1018 ((DeviceEvent) event).port(),
1019 event.type());
1020 processPortUpdated(((Device) event.subject()),
1021 ((DeviceEvent) event).port());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001022 } else {
1023 log.warn("Unhandled event type: {}", event.type());
1024 }
Pier Luigi657ddfc2018-02-12 09:56:20 +01001025 } catch (Exception e) {
1026 log.error("SegmentRouting event handler thread thrown an exception: {}",
1027 e.getMessage(), e);
sanghob35a6192015-04-01 13:05:26 -07001028 }
1029 }
sanghob35a6192015-04-01 13:05:26 -07001030 }
1031 }
1032
Saurav Dase9c8971e2018-01-18 12:07:33 -08001033 void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001034 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001035
1036 // NOTE: Punt ARP/NDP even when the device is not configured.
1037 // Host learning without network config is required for CORD config generator.
1038 routingRulePopulator.populateIpPunts(device.id());
1039 routingRulePopulator.populateArpNdpPunts(device.id());
1040
Charles Chan0b4e6182015-11-03 10:42:14 -08001041 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001042 log.warn("Device configuration unavailable. Device {} will be "
1043 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001044 return;
1045 }
Charles Chan2199c302016-04-23 17:36:10 -07001046 processDeviceAddedInternal(device.id());
1047 }
1048
1049 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001050 // Irrespective of whether the local is a MASTER or not for this device,
1051 // we need to create a SR-group-handler instance. This is because in a
1052 // multi-instance setup, any instance can initiate forwarding/next-objectives
1053 // for any switch (even if this instance is a SLAVE or not even connected
1054 // to the switch). To handle this, a default-group-handler instance is necessary
1055 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001056 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1057 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001058 DefaultGroupHandler groupHandler;
1059 try {
1060 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001061 createGroupHandler(deviceId,
1062 appId,
1063 deviceConfiguration,
1064 linkService,
1065 flowObjectiveService,
1066 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001067 } catch (DeviceConfigNotFoundException e) {
1068 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1069 return;
1070 }
Saurav Das9aa28bd2017-12-05 15:00:23 -08001071 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001072 deviceId);
1073 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001074 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001075
Charles Chan2199c302016-04-23 17:36:10 -07001076 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001077 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan03a73e02016-10-24 14:52:01 -07001078 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001079 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001080 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001081 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001082 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001083 }
Charles Chan5270ed02016-01-30 23:22:37 -08001084
Charles Chan03a73e02016-10-24 14:52:01 -07001085 appCfgHandler.init(deviceId);
1086 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001087 }
1088
Saurav Das80980c72016-03-23 11:22:49 -07001089 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001090 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001091 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chanba90df12017-11-30 15:37:50 -08001092 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001093 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001094 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001095 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001096 portNextObjStore.entrySet().stream()
1097 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001098 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Dase9c8971e2018-01-18 12:07:33 -08001099 linkHandler.processDeviceRemoved(device);
Charles Chaned3742352017-06-15 00:44:51 -07001100
Saurav Dasceccf242017-08-03 18:30:35 -07001101 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1102 if (gh != null) {
1103 gh.shutdown();
1104 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001105 // Note that a switch going down is associated with all of its links
1106 // going down as well, but it is treated as a single switch down event
1107 // while the link-downs are ignored.
1108 defaultRoutingHandler
1109 .populateRoutingRulesForLinkStatusChange(null, null, device.id());
Saurav Das95047002018-01-25 09:49:01 -08001110 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan2199c302016-04-23 17:36:10 -07001111 mcastHandler.removeDevice(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001112 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -07001113 }
1114
Saurav Das1a129a02016-11-18 15:21:57 -08001115 private void processPortUpdated(Device device, Port port) {
1116 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1117 log.warn("Device configuration uploading. Not handling port event for"
1118 + "dev: {} port: {}", device.id(), port.number());
1119 return;
1120 }
Saurav Das018605f2017-02-18 14:05:44 -08001121
1122 if (!mastershipService.isLocalMaster(device.id())) {
1123 log.debug("Not master for dev:{} .. not handling port updated event"
1124 + "for port {}", device.id(), port.number());
1125 return;
1126 }
1127
1128 // first we handle filtering rules associated with the port
1129 if (port.isEnabled()) {
1130 log.info("Switchport {}/{} enabled..programming filters",
1131 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001132 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001133 } else {
1134 log.info("Switchport {}/{} disabled..removing filters",
1135 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001136 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001137 }
Saurav Das1a129a02016-11-18 15:21:57 -08001138
1139 // portUpdated calls are for ports that have gone down or up. For switch
1140 // to switch ports, link-events should take care of any re-routing or
1141 // group editing necessary for port up/down. Here we only process edge ports
1142 // that are already configured.
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001143 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1144 VlanId untaggedVlan = getUntaggedVlanId(cp);
1145 VlanId nativeVlan = getNativeVlanId(cp);
1146 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001147
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001148 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001149 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das1a129a02016-11-18 15:21:57 -08001150 + "dev/port: {}/{}", device.id(), port.number());
1151 return;
1152 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001153 if (untaggedVlan != null) {
1154 processEdgePort(device, port, untaggedVlan, true);
1155 }
1156 if (nativeVlan != null) {
1157 processEdgePort(device, port, nativeVlan, true);
1158 }
1159 if (!taggedVlans.isEmpty()) {
1160 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1161 }
Saurav Das1a129a02016-11-18 15:21:57 -08001162 }
1163
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001164 private void processEdgePort(Device device, Port port, VlanId vlanId,
1165 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001166 boolean portUp = port.isEnabled();
1167 if (portUp) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001168 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001169 port.number(), vlanId);
Charles Chanba90df12017-11-30 15:37:50 -08001170 hostHandler.processPortUp(new ConnectPoint(device.id(), port.number()));
Saurav Das1a129a02016-11-18 15:21:57 -08001171 } else {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001172 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001173 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001174 }
1175
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001176 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001177 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001178 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001179 } else {
1180 log.warn("Group handler not found for dev:{}. Not handling edge port"
1181 + " {} event for port:{}", device.id(),
1182 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001183 }
1184 }
sangho1e575652015-05-14 00:39:53 -07001185
Charles Chan463c6442017-10-20 16:06:55 -07001186 private void createOrUpdateDeviceConfiguration() {
1187 if (deviceConfiguration == null) {
Saurav Das07a34aa2018-02-09 17:26:45 -08001188 log.info("Creating new DeviceConfiguration");
Charles Chan463c6442017-10-20 16:06:55 -07001189 deviceConfiguration = new DeviceConfiguration(this);
1190 } else {
Saurav Das07a34aa2018-02-09 17:26:45 -08001191 log.info("Updating DeviceConfiguration");
Charles Chan463c6442017-10-20 16:06:55 -07001192 deviceConfiguration.updateConfig();
1193 }
1194 }
1195
Pier Ventre10bd8d12016-11-26 21:05:22 -08001196 /**
1197 * Registers the given connect point with the NRS, this is necessary
1198 * to receive the NDP and ARP packets from the NRS.
1199 *
1200 * @param portToRegister connect point to register
1201 */
1202 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001203 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001204 portToRegister,
1205 neighbourHandler,
1206 appId
1207 );
1208 }
1209
Charles Chand6832882015-10-05 17:50:33 -07001210 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001211 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001212 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001213
Charles Chane849c192016-01-11 18:28:54 -08001214 /**
1215 * Constructs the internal network config listener.
1216 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001217 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001218 */
Charles Chan65238242017-06-22 18:03:14 -07001219 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001220 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001221 }
1222
Charles Chane849c192016-01-11 18:28:54 -08001223 /**
1224 * Reads network config and initializes related data structure accordingly.
1225 */
Charles Chanba90df12017-11-30 15:37:50 -08001226 void configureNetwork() {
Saurav Das07a34aa2018-02-09 17:26:45 -08001227 log.info("Configuring network ...");
Charles Chan463c6442017-10-20 16:06:55 -07001228 createOrUpdateDeviceConfiguration();
Charles Chan4636be02015-10-07 14:21:45 -07001229
Charles Chan2c15aca2016-11-09 20:51:44 -08001230 arpHandler = new ArpHandler(srManager);
1231 icmpHandler = new IcmpHandler(srManager);
1232 ipHandler = new IpHandler(srManager);
1233 routingRulePopulator = new RoutingRulePopulator(srManager);
1234 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001235
1236 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1237 groupHandlerMap, tunnelStore);
1238 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1239 flowObjectiveService,
1240 tunnelHandler, policyStore);
Saurav Das7bcbe702017-06-13 15:35:54 -07001241 // add a small delay to absorb multiple network config added notifications
1242 if (!programmingScheduled.get()) {
Saurav Das07a34aa2018-02-09 17:26:45 -08001243 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
Saurav Das7bcbe702017-06-13 15:35:54 -07001244 programmingScheduled.set(true);
1245 executorService.schedule(new ConfigChange(), PROGRAM_DELAY,
1246 TimeUnit.SECONDS);
Charles Chan4636be02015-10-07 14:21:45 -07001247 }
Charles Chan2199c302016-04-23 17:36:10 -07001248 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001249 }
1250
Charles Chand6832882015-10-05 17:50:33 -07001251 @Override
1252 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001253 // TODO move this part to NetworkConfigEventHandler
1254 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1255 switch (event.type()) {
1256 case CONFIG_ADDED:
Charles Chan278ce832017-06-26 15:25:09 -07001257 log.info("Segment Routing Device Config added for {}", event.subject());
Charles Chan5270ed02016-01-30 23:22:37 -08001258 configureNetwork();
1259 break;
1260 case CONFIG_UPDATED:
Charles Chan278ce832017-06-26 15:25:09 -07001261 log.info("Segment Routing Config updated for {}", event.subject());
Charles Chan463c6442017-10-20 16:06:55 -07001262 createOrUpdateDeviceConfiguration();
Charles Chan278ce832017-06-26 15:25:09 -07001263 // TODO support dynamic configuration
1264 break;
1265 default:
1266 break;
1267 }
1268 } else if (event.configClass().equals(InterfaceConfig.class)) {
1269 switch (event.type()) {
1270 case CONFIG_ADDED:
1271 log.info("Interface Config added for {}", event.subject());
1272 configureNetwork();
1273 break;
1274 case CONFIG_UPDATED:
1275 log.info("Interface Config updated for {}", event.subject());
Charles Chan463c6442017-10-20 16:06:55 -07001276 createOrUpdateDeviceConfiguration();
Jonghwan Hyun5f1def82017-08-25 17:48:36 -07001277
1278 // Following code will be uncommented when [CORD-634] is fully implemented.
1279 // [CORD-634] Add dynamic config support for interfaces
1280 updateInterface((InterfaceConfig) event.config().get(),
1281 (InterfaceConfig) event.prevConfig().get());
Charles Chan5270ed02016-01-30 23:22:37 -08001282 // TODO support dynamic configuration
1283 break;
1284 default:
1285 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001286 }
Charles Chan5270ed02016-01-30 23:22:37 -08001287 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001288 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001289 switch (event.type()) {
1290 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001291 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001292 break;
1293 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001294 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001295 break;
1296 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001297 appCfgHandler.processAppConfigRemoved(event);
1298 break;
1299 default:
1300 break;
1301 }
Saurav Das07a34aa2018-02-09 17:26:45 -08001302 log.info("App config event .. configuring network");
Charles Chan03a73e02016-10-24 14:52:01 -07001303 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001304 } else if (event.configClass().equals(XConnectConfig.class)) {
1305 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1306 switch (event.type()) {
1307 case CONFIG_ADDED:
1308 xConnectHandler.processXConnectConfigAdded(event);
1309 break;
1310 case CONFIG_UPDATED:
1311 xConnectHandler.processXConnectConfigUpdated(event);
1312 break;
1313 case CONFIG_REMOVED:
1314 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001315 break;
1316 default:
1317 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001318 }
Pier Ventref34966c2016-11-07 16:21:04 -08001319 } else if (event.configClass().equals(PwaasConfig.class)) {
1320 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1321 switch (event.type()) {
1322 case CONFIG_ADDED:
1323 l2TunnelHandler.processPwaasConfigAdded(event);
1324 break;
1325 case CONFIG_UPDATED:
1326 l2TunnelHandler.processPwaasConfigUpdated(event);
1327 break;
1328 case CONFIG_REMOVED:
1329 l2TunnelHandler.processPwaasConfigRemoved(event);
1330 break;
1331 default:
1332 break;
1333 }
Charles Chand6832882015-10-05 17:50:33 -07001334 }
1335 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001336
Charles Chan91b371b2018-01-03 16:26:32 -08001337 @Override
1338 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Das07a34aa2018-02-09 17:26:45 -08001339 if (event.type() == CONFIG_REGISTERED ||
1340 event.type() == CONFIG_UNREGISTERED) {
1341 log.debug("Ignore event {} due to type mismatch", event);
1342 return false;
Charles Chan91b371b2018-01-03 16:26:32 -08001343 }
Saurav Das07a34aa2018-02-09 17:26:45 -08001344
1345 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1346 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1347 !event.configClass().equals(InterfaceConfig.class) &&
1348 !event.configClass().equals(XConnectConfig.class) &&
1349 !event.configClass().equals(PwaasConfig.class)) {
1350 log.debug("Ignore event {} due to class mismatch", event);
1351 return false;
1352 }
1353
1354 return true;
Charles Chan91b371b2018-01-03 16:26:32 -08001355 }
1356
Saurav Das7bcbe702017-06-13 15:35:54 -07001357 private final class ConfigChange implements Runnable {
1358 @Override
1359 public void run() {
1360 programmingScheduled.set(false);
Saurav Das07a34aa2018-02-09 17:26:45 -08001361 log.info("Reacting to config changes after buffer delay");
Saurav Das7bcbe702017-06-13 15:35:54 -07001362 for (Device device : deviceService.getDevices()) {
1363 processDeviceAdded(device);
1364 }
1365 defaultRoutingHandler.startPopulationProcess();
1366 }
1367 }
Charles Chand6832882015-10-05 17:50:33 -07001368 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001369
1370 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001371 @Override
1372 public void event(HostEvent event) {
Charles Chan68aa62d2015-11-09 16:37:23 -08001373 switch (event.type()) {
1374 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001375 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001376 break;
1377 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001378 hostHandler.processHostMovedEvent(event);
Charles Chan23686832017-08-23 14:46:43 -07001379 routeHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001380 break;
1381 case HOST_REMOVED:
Charles Chanf9a52702017-06-16 15:19:24 -07001382 hostHandler.processHostRemovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001383 break;
1384 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001385 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001386 break;
1387 default:
1388 log.warn("Unsupported host event type: {}", event.type());
1389 break;
1390 }
1391 }
1392 }
1393
Charles Chand55e84d2016-03-30 17:54:24 -07001394 private class InternalMcastListener implements McastListener {
1395 @Override
1396 public void event(McastEvent event) {
1397 switch (event.type()) {
1398 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001399 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001400 break;
1401 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001402 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001403 break;
1404 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001405 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001406 break;
1407 case ROUTE_ADDED:
1408 case ROUTE_REMOVED:
1409 default:
1410 break;
1411 }
1412 }
1413 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001414
Charles Chan03a73e02016-10-24 14:52:01 -07001415 private class InternalRouteEventListener implements RouteListener {
1416 @Override
1417 public void event(RouteEvent event) {
1418 switch (event.type()) {
1419 case ROUTE_ADDED:
1420 routeHandler.processRouteAdded(event);
1421 break;
1422 case ROUTE_UPDATED:
1423 routeHandler.processRouteUpdated(event);
1424 break;
1425 case ROUTE_REMOVED:
1426 routeHandler.processRouteRemoved(event);
1427 break;
Charles Chan23686832017-08-23 14:46:43 -07001428 case ALTERNATIVE_ROUTES_CHANGED:
1429 routeHandler.processAlternativeRoutesChanged(event);
1430 break;
Charles Chan03a73e02016-10-24 14:52:01 -07001431 default:
1432 break;
1433 }
1434 }
1435 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001436
Jonghwan Hyun5f1def82017-08-25 17:48:36 -07001437 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1438 try {
1439 Set<Interface> intfs = conf.getInterfaces();
1440 Set<Interface> prevIntfs = prevConf.getInterfaces();
1441
1442 // Now we only handle one interface config at each port.
1443 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1444 log.warn("Interface update aborted - one at a time is allowed, " +
1445 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1446 return;
1447 }
1448
1449 Interface intf = intfs.stream().findFirst().get();
1450 Interface prevIntf = prevIntfs.stream().findFirst().get();
1451
1452 DeviceId deviceId = intf.connectPoint().deviceId();
1453 PortNumber portNum = intf.connectPoint().port();
1454
1455 if (!mastershipService.isLocalMaster(deviceId)) {
1456 log.debug("CONFIG_UPDATED event for interfaces should be " +
1457 "handled by master node for device {}", deviceId);
1458 return;
1459 }
1460
1461 removeSubnetConfig(prevIntf.connectPoint(),
1462 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1463 new HashSet<>(intf.ipAddressesList())));
1464
1465 if (prevIntf.vlanNative() != VlanId.NONE && !intf.vlanNative().equals(prevIntf.vlanNative())) {
1466 // RemoveVlanNative
1467 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1468 }
1469
1470 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1471 // RemoveVlanTagged
1472 prevIntf.vlanTagged().stream().filter(i -> !intf.vlanTagged().contains(i)).forEach(
1473 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, false)
1474 );
1475 }
1476
1477 if (prevIntf.vlanUntagged() != VlanId.NONE && !intf.vlanUntagged().equals(prevIntf.vlanUntagged())) {
1478 // RemoveVlanUntagged
1479 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1480 }
1481
1482 if (intf.vlanNative() != VlanId.NONE && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1483 // AddVlanNative
1484 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1485 }
1486
1487 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1488 // AddVlanTagged
1489 intf.vlanTagged().stream().filter(i -> !prevIntf.vlanTagged().contains(i)).forEach(
1490 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, true)
1491 );
1492 }
1493
1494 if (intf.vlanUntagged() != VlanId.NONE && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())) {
1495 // AddVlanUntagged
1496 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1497 }
1498 addSubnetConfig(prevIntf.connectPoint(),
1499 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1500 new HashSet<>(prevIntf.ipAddressesList())));
1501 } catch (ConfigException e) {
1502 log.error("Error in configuration");
1503 }
1504 }
1505
1506 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1507 VlanId vlanId, boolean pushVlan, boolean install) {
1508 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1509 if (grpHandler == null) {
1510 log.warn("Failed to retrieve group handler for device {}", deviceId);
1511 return;
1512 }
1513
1514 // Update filtering objective for a single port
1515 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1516
1517 // Update filtering objective for multicast ingress port
1518 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1519
1520 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1521
1522 if (nextId != -1 && !install) {
1523 // Update next objective for a single port as an output port
1524 // Remove a single port from L2FG
1525 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1526 // Remove L2 Bridging rule and L3 Unicast rule to the host
1527 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1528 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1529 // only if there is no port configured on that VLAN ID
1530 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1531 // Remove broadcast forwarding rule for the VLAN
1532 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1533 // Remove L2FG for VLAN
1534 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1535 } else {
1536 // Remove L2IG of the port
1537 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1538 }
1539 } else if (install) {
1540 if (nextId != -1) {
1541 // Add a single port to L2FG
1542 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1543 } else {
1544 // Create L2FG for VLAN
1545 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1546 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1547 }
1548 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1549 } else {
1550 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1551 }
1552 }
1553
1554 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1555 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1556 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1557
1558 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1559 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1560 .filter(intf -> !intf.connectPoint().equals(cp))
1561 .flatMap(intf -> intf.ipAddressesList().stream())
1562 .collect(Collectors.toSet());
1563 // 1. Partial subnet population
1564 // Remove routing rules for removed subnet from previous configuration,
1565 // which does not also exist in other interfaces in the same device
1566 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1567 .map(InterfaceIpAddress::subnetAddress)
1568 .collect(Collectors.toSet());
1569
1570 defaultRoutingHandler.revokeSubnet(
1571 ipPrefixSet.stream()
1572 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1573 .collect(Collectors.toSet()));
1574
1575 // 2. Interface IP punts
1576 // Remove IP punts for old Intf address
1577 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1578 .map(InterfaceIpAddress::ipAddress)
1579 .collect(Collectors.toSet());
1580 ipAddressSet.stream()
1581 .map(InterfaceIpAddress::ipAddress)
1582 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1583 .forEach(interfaceIpAddress ->
1584 routingRulePopulator.revokeSingleIpPunts(
1585 cp.deviceId(), interfaceIpAddress));
1586
1587 // 3. Host unicast routing rule
1588 // Remove unicast routing rule
1589 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1590 }
1591
1592 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1593 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1594 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1595
1596 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1597 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1598 .filter(intf -> !intf.connectPoint().equals(cp))
1599 .flatMap(intf -> intf.ipAddressesList().stream())
1600 .collect(Collectors.toSet());
1601 // 1. Partial subnet population
1602 // Add routing rules for newly added subnet, which does not also exist in
1603 // other interfaces in the same device
1604 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1605 .map(InterfaceIpAddress::subnetAddress)
1606 .collect(Collectors.toSet());
1607
1608 defaultRoutingHandler.populateSubnet(
1609 Collections.singleton(cp),
1610 ipPrefixSet.stream()
1611 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1612 .collect(Collectors.toSet()));
1613
1614 // 2. Interface IP punts
1615 // Add IP punts for new Intf address
1616 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1617 .map(InterfaceIpAddress::ipAddress)
1618 .collect(Collectors.toSet());
1619 ipAddressSet.stream()
1620 .map(InterfaceIpAddress::ipAddress)
1621 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1622 .forEach(interfaceIpAddress ->
1623 routingRulePopulator.populateSingleIpPunts(
1624 cp.deviceId(), interfaceIpAddress));
1625
1626 // 3. Host unicast routing rule
1627 // Add unicast routing rule
1628 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1629 }
Saurav Dase9c8971e2018-01-18 12:07:33 -08001630
sanghob35a6192015-04-01 13:05:26 -07001631}