blob: 32428661a86832036a322ea54af2fc2cc07dcbb3 [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sanghob35a6192015-04-01 13:05:26 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.segmentrouting;
17
Andreas Pantelopoulos20474e02017-12-20 18:04:27 -080018import com.fasterxml.jackson.databind.ObjectMapper;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -070019import com.fasterxml.jackson.databind.node.ObjectNode;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -080020import com.google.common.collect.HashMultimap;
21import com.google.common.collect.ImmutableMap;
22import com.google.common.collect.Maps;
23import com.google.common.collect.Multimap;
Charles Chan9640c812017-08-23 13:55:39 -070024import com.google.common.collect.Sets;
sanghob35a6192015-04-01 13:05:26 -070025import org.apache.felix.scr.annotations.Activate;
26import org.apache.felix.scr.annotations.Component;
27import org.apache.felix.scr.annotations.Deactivate;
Charles Chan47933752017-11-30 15:37:50 -080028import org.apache.felix.scr.annotations.Modified;
29import org.apache.felix.scr.annotations.Property;
sanghob35a6192015-04-01 13:05:26 -070030import org.apache.felix.scr.annotations.Reference;
31import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho1e575652015-05-14 00:39:53 -070032import org.apache.felix.scr.annotations.Service;
sanghob35a6192015-04-01 13:05:26 -070033import org.onlab.packet.Ethernet;
Pier Ventre735b8c82016-12-02 08:16:05 -080034import org.onlab.packet.ICMP6;
Charles Chanc42e84e2015-10-20 16:24:19 -070035import org.onlab.packet.IPv4;
Pier Ventre10bd8d12016-11-26 21:05:22 -080036import org.onlab.packet.IPv6;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070037import org.onlab.packet.IpAddress;
Charles Chanc42e84e2015-10-20 16:24:19 -070038import org.onlab.packet.IpPrefix;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070039import org.onlab.packet.VlanId;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070040import org.onlab.util.KryoNamespace;
Charles Chan47933752017-11-30 15:37:50 -080041import org.onlab.util.Tools;
Saurav Das80980c72016-03-23 11:22:49 -070042import org.onosproject.cfg.ComponentConfigService;
sanghob35a6192015-04-01 13:05:26 -070043import org.onosproject.core.ApplicationId;
44import org.onosproject.core.CoreService;
45import org.onosproject.event.Event;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070046import org.onosproject.mastership.MastershipService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080047import org.onosproject.net.ConnectPoint;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070048import org.onosproject.net.Device;
49import org.onosproject.net.DeviceId;
Charles Chan9640c812017-08-23 13:55:39 -070050import org.onosproject.net.Host;
51import org.onosproject.net.HostId;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070052import org.onosproject.net.Link;
53import org.onosproject.net.Port;
Charles Chan68aa62d2015-11-09 16:37:23 -080054import org.onosproject.net.PortNumber;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070055import org.onosproject.net.config.ConfigException;
Charles Chand6832882015-10-05 17:50:33 -070056import org.onosproject.net.config.ConfigFactory;
57import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070058import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070059import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070060import org.onosproject.net.config.basics.InterfaceConfig;
61import org.onosproject.net.config.basics.McastConfig;
Charles Chand6832882015-10-05 17:50:33 -070062import org.onosproject.net.config.basics.SubjectFactories;
Saurav Das45f48152018-01-18 12:07:33 -080063import org.onosproject.net.device.DeviceAdminService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070064import org.onosproject.net.device.DeviceEvent;
65import org.onosproject.net.device.DeviceListener;
66import org.onosproject.net.device.DeviceService;
Charles Chan68aa62d2015-11-09 16:37:23 -080067import org.onosproject.net.flow.TrafficSelector;
68import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070069import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan68aa62d2015-11-09 16:37:23 -080070import org.onosproject.net.host.HostEvent;
71import org.onosproject.net.host.HostListener;
Charles Chan47933752017-11-30 15:37:50 -080072import org.onosproject.net.host.HostLocationProbingService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080073import org.onosproject.net.host.HostService;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070074import org.onosproject.net.host.InterfaceIpAddress;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070075import org.onosproject.net.intf.Interface;
76import org.onosproject.net.intf.InterfaceService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080077import org.onosproject.net.link.LinkEvent;
78import org.onosproject.net.link.LinkListener;
79import org.onosproject.net.link.LinkService;
Charles Chand55e84d2016-03-30 17:54:24 -070080import org.onosproject.net.mcast.McastEvent;
81import org.onosproject.net.mcast.McastListener;
82import org.onosproject.net.mcast.MulticastRouteService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070083import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080084import org.onosproject.net.packet.InboundPacket;
85import org.onosproject.net.packet.PacketContext;
86import org.onosproject.net.packet.PacketProcessor;
87import org.onosproject.net.packet.PacketService;
Charles Chand55e84d2016-03-30 17:54:24 -070088import org.onosproject.net.topology.TopologyService;
Charles Chan9640c812017-08-23 13:55:39 -070089import org.onosproject.routeservice.ResolvedRoute;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070090import org.onosproject.routeservice.RouteEvent;
91import org.onosproject.routeservice.RouteListener;
92import org.onosproject.routeservice.RouteService;
Charles Chand55e84d2016-03-30 17:54:24 -070093import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
94import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventref34966c2016-11-07 16:21:04 -080095import org.onosproject.segmentrouting.config.PwaasConfig;
Pier Ventref34966c2016-11-07 16:21:04 -080096import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070097import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -070098import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -070099import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700100import org.onosproject.segmentrouting.grouphandler.DestinationSet;
101import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700102import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
103import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700104import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700105import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Charles Chand2990362016-04-18 13:44:03 -0700106import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -0800107import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -0700108import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700109import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700110import org.onosproject.store.service.EventuallyConsistentMap;
111import org.onosproject.store.service.EventuallyConsistentMapBuilder;
112import org.onosproject.store.service.StorageService;
113import org.onosproject.store.service.WallClockTimestamp;
Charles Chan47933752017-11-30 15:37:50 -0800114import org.osgi.service.component.ComponentContext;
sanghob35a6192015-04-01 13:05:26 -0700115import org.slf4j.Logger;
116import org.slf4j.LoggerFactory;
117
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800118import java.util.Collections;
Charles Chan47933752017-11-30 15:37:50 -0800119import java.util.Dictionary;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800120import java.util.HashSet;
121import java.util.List;
122import java.util.Map;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800123import java.util.Optional;
124import java.util.Set;
125import java.util.concurrent.ConcurrentHashMap;
126import java.util.concurrent.ConcurrentLinkedQueue;
127import java.util.concurrent.Executors;
128import java.util.concurrent.ScheduledExecutorService;
129import java.util.concurrent.ScheduledFuture;
130import java.util.concurrent.TimeUnit;
131import java.util.concurrent.atomic.AtomicBoolean;
132import java.util.stream.Collectors;
sanghob35a6192015-04-01 13:05:26 -0700133
Charles Chan3e783d02016-02-26 22:19:52 -0800134import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800135import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700136import static org.onlab.util.Tools.groupedThreads;
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 Chan2fde6d42017-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)
Charles Chan03a73e02016-10-24 14:52:01 -0700155 CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700156
157 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700158 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700159
160 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700161 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700162
163 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan47933752017-11-30 15:37:50 -0800164 HostLocationProbingService probingService;
165
166 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700167 DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700168
169 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Saurav Das45f48152018-01-18 12:07:33 -0800170 DeviceAdminService deviceAdminService;
171
172 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800173 public FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700174
175 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700176 LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700177
Charles Chan5270ed02016-01-30 23:22:37 -0800178 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800179 public MastershipService mastershipService;
Charles Chan03a73e02016-10-24 14:52:01 -0700180
181 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800182 public StorageService storageService;
Charles Chan03a73e02016-10-24 14:52:01 -0700183
184 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
185 MulticastRouteService multicastRouteService;
186
187 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chanf237e1b2018-01-09 13:45:07 -0800188 public TopologyService topologyService;
Charles Chan03a73e02016-10-24 14:52:01 -0700189
190 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700191 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800192
193 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800194 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800195
Saurav Das80980c72016-03-23 11:22:49 -0700196 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800197 public InterfaceService interfaceService;
198
Charles Chan47933752017-11-30 15:37:50 -0800199 @Property(name = "activeProbing", boolValue = true,
200 label = "Enable active probing to discover dual-homed hosts.")
201 boolean activeProbing = true;
202
Charles Chan03a73e02016-10-24 14:52:01 -0700203 ArpHandler arpHandler = null;
204 IcmpHandler icmpHandler = null;
205 IpHandler ipHandler = null;
206 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700207 ApplicationId appId;
208 DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700209
Charles Chan03a73e02016-10-24 14:52:01 -0700210 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700211 private TunnelHandler tunnelHandler = null;
212 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700213 private InternalPacketProcessor processor = null;
214 private InternalLinkListener linkListener = null;
215 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700216 private AppConfigHandler appCfgHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700217 XConnectHandler xConnectHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800218 McastHandler mcastHandler = null;
219 HostHandler hostHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800220 private RouteHandler routeHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800221 LinkHandler linkHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800222 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Pier Ventref34966c2016-11-07 16:21:04 -0800223 private L2TunnelHandler l2TunnelHandler = null;
sanghob35a6192015-04-01 13:05:26 -0700224 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan5270ed02016-01-30 23:22:37 -0800225 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700226 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
227 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700228 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sanghob35a6192015-04-01 13:05:26 -0700229
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700230 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700231 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sanghob35a6192015-04-01 13:05:26 -0700232
Saurav Das4ce45962015-11-24 23:21:05 -0800233 @SuppressWarnings("unused")
sanghob35a6192015-04-01 13:05:26 -0700234 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800235 @SuppressWarnings("rawtypes")
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700236 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Saurav Das45f48152018-01-18 12:07:33 -0800237 Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chane849c192016-01-11 18:28:54 -0800238 new ConcurrentHashMap<>();
239 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700240 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700241 * Used to keep track on MPLS group information.
Charles Chane849c192016-01-11 18:28:54 -0800242 */
Charles Chan47933752017-11-30 15:37:50 -0800243 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das7bcbe702017-06-13 15:35:54 -0700244 dsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800245 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700246 * Per device next objective ID store with (device id + vlanid) as key.
247 * Used to keep track on L2 flood group information.
Charles Chane849c192016-01-11 18:28:54 -0800248 */
Charles Chan47933752017-11-30 15:37:50 -0800249 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan59cc16d2017-02-02 16:20:42 -0800250 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800251 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700252 * Per device next objective ID store with (device id + port + treatment + meta) as key.
253 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chane849c192016-01-11 18:28:54 -0800254 */
Charles Chan47933752017-11-30 15:37:50 -0800255 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800256 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800257
Saurav Das4ce45962015-11-24 23:21:05 -0800258 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
259 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700260
Saurav Das7bcbe702017-06-13 15:35:54 -0700261 private AtomicBoolean programmingScheduled = new AtomicBoolean();
262
Charles Chand55e84d2016-03-30 17:54:24 -0700263 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700264 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
265 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700266 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700267 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800268 public SegmentRoutingDeviceConfig createConfig() {
269 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700270 }
271 };
Pier Ventref34966c2016-11-07 16:21:04 -0800272
Charles Chand55e84d2016-03-30 17:54:24 -0700273 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700274 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
275 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700276 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800277 @Override
278 public SegmentRoutingAppConfig createConfig() {
279 return new SegmentRoutingAppConfig();
280 }
281 };
Pier Ventref34966c2016-11-07 16:21:04 -0800282
Charles Chanfc5c7802016-05-17 13:13:55 -0700283 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
284 new ConfigFactory<ApplicationId, XConnectConfig>(
285 SubjectFactories.APP_SUBJECT_FACTORY,
286 XConnectConfig.class, "xconnect") {
287 @Override
288 public XConnectConfig createConfig() {
289 return new XConnectConfig();
290 }
291 };
Pier Ventref34966c2016-11-07 16:21:04 -0800292
Charles Chand55e84d2016-03-30 17:54:24 -0700293 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700294 new ConfigFactory<ApplicationId, McastConfig>(
295 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700296 McastConfig.class, "multicast") {
297 @Override
298 public McastConfig createConfig() {
299 return new McastConfig();
300 }
301 };
302
Pier Ventref34966c2016-11-07 16:21:04 -0800303 private final ConfigFactory<ApplicationId, PwaasConfig> pwaasConfigFactory =
304 new ConfigFactory<ApplicationId, PwaasConfig>(
305 SubjectFactories.APP_SUBJECT_FACTORY,
306 PwaasConfig.class, "pwaas") {
307 @Override
308 public PwaasConfig createConfig() {
309 return new PwaasConfig();
310 }
311 };
312
Charles Chan65238242017-06-22 18:03:14 -0700313 private static final Object THREAD_SCHED_LOCK = new Object();
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700314 private static int numOfEventsQueued = 0;
315 private static int numOfEventsExecuted = 0;
sanghob35a6192015-04-01 13:05:26 -0700316 private static int numOfHandlerExecution = 0;
317 private static int numOfHandlerScheduled = 0;
318
Charles Chan116188d2016-02-18 14:22:42 -0800319 /**
320 * Segment Routing App ID.
321 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800322 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan59cc16d2017-02-02 16:20:42 -0800323
Charles Chane849c192016-01-11 18:28:54 -0800324 /**
325 * The default VLAN ID assigned to the interfaces without subnet config.
326 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800327 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700328
sanghob35a6192015-04-01 13:05:26 -0700329 @Activate
Charles Chan47933752017-11-30 15:37:50 -0800330 protected void activate(ComponentContext context) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800331 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700332
333 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das7bcbe702017-06-13 15:35:54 -0700334 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700335 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700336 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700337 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700338 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700339 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700340 .build();
Saurav Das7bcbe702017-06-13 15:35:54 -0700341 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700342
Charles Chan59cc16d2017-02-02 16:20:42 -0800343 log.debug("Creating EC map vlannextobjectivestore");
344 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
345 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
346 vlanNextObjStore = vlanNextObjMapBuilder
347 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700348 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700349 .withTimestampProvider((k, v) -> new WallClockTimestamp())
350 .build();
351
Saurav Das4ce45962015-11-24 23:21:05 -0800352 log.debug("Creating EC map subnetnextobjectivestore");
353 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
354 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
355 portNextObjStore = portNextObjMapBuilder
356 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700357 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800358 .withTimestampProvider((k, v) -> new WallClockTimestamp())
359 .build();
360
sangho0b2b6d12015-05-20 22:16:38 -0700361 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
362 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700363 tunnelStore = tunnelMapBuilder
364 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700365 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700366 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700367 .build();
368
369 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
370 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700371 policyStore = policyMapBuilder
372 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700373 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700374 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700375 .build();
376
Saurav Das80980c72016-03-23 11:22:49 -0700377 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800378 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700379 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800380 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800381 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
382 "requestInterceptsEnabled", "false");
Charles Chand3baaba2017-08-08 15:13:37 -0700383 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800384 "requestInterceptsEnabled", "false");
Charles Chanc760f382017-07-24 15:56:10 -0700385 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800386 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800387 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
388 "greedyLearningIpv6", "true");
Charles Chanc6d227e2017-02-28 15:15:17 -0800389 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
390 "forceUnprovision", "true");
Charles Chanef624ed2017-08-10 16:57:28 -0700391 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan73316522017-07-20 16:16:25 -0700392 "distributed", "true");
Charles Chan35a32322017-08-14 11:42:11 -0700393 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
394 "multihomingEnabled", "true");
Charles Chan807d87a2017-11-29 19:54:20 -0800395 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
396 "staleLinkAge", "15000");
397 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
398 "allowDuplicateIps", "false");
Charles Chan47933752017-11-30 15:37:50 -0800399 compCfgService.registerProperties(getClass());
400 modified(context);
Saurav Das80980c72016-03-23 11:22:49 -0700401
Charles Chanb8e10c82015-10-14 11:24:40 -0700402 processor = new InternalPacketProcessor();
403 linkListener = new InternalLinkListener();
404 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700405 appCfgHandler = new AppConfigHandler(this);
406 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700407 mcastHandler = new McastHandler(this);
408 hostHandler = new HostHandler(this);
Saurav Das45f48152018-01-18 12:07:33 -0800409 linkHandler = new LinkHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700410 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800411 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Pier Ventref34966c2016-11-07 16:21:04 -0800412 l2TunnelHandler = new L2TunnelHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700413
Charles Chan3e783d02016-02-26 22:19:52 -0800414 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700415 cfgService.registerConfigFactory(deviceConfigFactory);
416 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700417 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700418 cfgService.registerConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800419 cfgService.registerConfigFactory(pwaasConfigFactory);
Charles Chan132393a2018-01-04 14:26:07 -0800420
421 cfgListener.configureNetwork();
422
Charles Chan5270ed02016-01-30 23:22:37 -0800423 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700424 packetService.addProcessor(processor, PacketProcessor.director(2));
425 linkService.addListener(linkListener);
426 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700427 multicastRouteService.addListener(mcastListener);
Charles Chanc7a8a682017-06-19 00:43:31 -0700428 routeService.addListener(routeListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700429
Andreas Pantelopoulos4a768c02018-01-11 07:53:48 -0800430 l2TunnelHandler.init();
431
sanghob35a6192015-04-01 13:05:26 -0700432 log.info("Started");
433 }
434
Saurav Das45f48152018-01-18 12:07:33 -0800435 KryoNamespace.Builder createSerializer() {
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700436 return new KryoNamespace.Builder()
437 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700438 .register(DestinationSetNextObjectiveStoreKey.class,
Charles Chan59cc16d2017-02-02 16:20:42 -0800439 VlanNextObjectiveStoreKey.class,
Saurav Das7bcbe702017-06-13 15:35:54 -0700440 DestinationSet.class,
441 NextNeighbors.class,
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700442 Tunnel.class,
443 DefaultTunnel.class,
444 Policy.class,
445 TunnelPolicy.class,
446 Policy.Type.class,
447 PortNextObjectiveStoreKey.class,
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700448 XConnectStoreKey.class,
449 DefaultL2Tunnel.class,
450 DefaultL2TunnelPolicy.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700451 );
452 }
453
sanghob35a6192015-04-01 13:05:26 -0700454 @Deactivate
455 protected void deactivate() {
Charles Chand6832882015-10-05 17:50:33 -0700456 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700457 cfgService.unregisterConfigFactory(deviceConfigFactory);
458 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700459 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700460 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800461 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chan47933752017-11-30 15:37:50 -0800462 compCfgService.unregisterProperties(getClass(), false);
Charles Chand6832882015-10-05 17:50:33 -0700463
Charles Chanc7a8a682017-06-19 00:43:31 -0700464 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700465 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700466 linkService.removeListener(linkListener);
467 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700468 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700469 routeService.removeListener(routeListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700470
Charles Chan0aa674e2017-02-23 15:44:08 -0800471 neighbourResolutionService.unregisterNeighbourHandlers(appId);
472
sanghob35a6192015-04-01 13:05:26 -0700473 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700474 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700475 deviceListener = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700476 groupHandlerMap.clear();
477
Saurav Das7bcbe702017-06-13 15:35:54 -0700478 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800479 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700480 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700481 tunnelStore.destroy();
482 policyStore.destroy();
sanghob35a6192015-04-01 13:05:26 -0700483 log.info("Stopped");
484 }
485
Charles Chan47933752017-11-30 15:37:50 -0800486 @Modified
487 private void modified(ComponentContext context) {
488 Dictionary<?, ?> properties = context.getProperties();
489 if (properties == null) {
490 return;
491 }
492
493 String strActiveProving = Tools.get(properties, "activeProbing");
494 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
495
496 if (expectActiveProbing != activeProbing) {
497 activeProbing = expectActiveProbing;
498 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
499 }
500 }
501
sangho1e575652015-05-14 00:39:53 -0700502 @Override
503 public List<Tunnel> getTunnels() {
504 return tunnelHandler.getTunnels();
505 }
506
507 @Override
sangho71abe1b2015-06-29 14:58:47 -0700508 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
509 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700510 }
511
512 @Override
sangho71abe1b2015-06-29 14:58:47 -0700513 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700514 for (Policy policy: policyHandler.getPolicies()) {
515 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
516 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
517 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
518 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700519 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700520 }
521 }
522 }
sangho71abe1b2015-06-29 14:58:47 -0700523 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700524 }
525
526 @Override
sangho71abe1b2015-06-29 14:58:47 -0700527 public PolicyHandler.Result removePolicy(Policy policy) {
528 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700529 }
530
531 @Override
sangho71abe1b2015-06-29 14:58:47 -0700532 public PolicyHandler.Result createPolicy(Policy policy) {
533 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700534 }
535
536 @Override
537 public List<Policy> getPolicies() {
538 return policyHandler.getPolicies();
539 }
540
Saurav Das59232cf2016-04-27 18:35:50 -0700541 @Override
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700542 public List<DefaultL2Tunnel> getL2Tunnels() {
543 return l2TunnelHandler.getL2Tunnels();
544 }
545
546 @Override
547 public List<DefaultL2TunnelPolicy> getL2Policies() {
548 return l2TunnelHandler.getL2Policies();
549 }
550
551 @Override
552 public L2TunnelHandler.Result addPseudowire(String tunnelId, String pwLabel, String cP1,
553 String cP1InnerVlan, String cP1OuterVlan, String cP2,
554 String cP2InnerVlan, String cP2OuterVlan,
555 String mode, String sdTag) {
Andreas Pantelopoulos20474e02017-12-20 18:04:27 -0800556 // Try to inject an empty Pwaas config if it is not found for the first time
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700557 PwaasConfig config = cfgService.getConfig(appId(), PwaasConfig.class);
558 if (config == null) {
Andreas Pantelopoulos20474e02017-12-20 18:04:27 -0800559 log.debug("Pwaas config not found. Try to create an empty one.");
560 cfgService.applyConfig(appId(), PwaasConfig.class, new ObjectMapper().createObjectNode());
561 config = cfgService.getConfig(appId(), PwaasConfig.class);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700562 }
563
564 ObjectNode object = config.addPseudowire(tunnelId, pwLabel,
565 cP1, cP1InnerVlan, cP1OuterVlan,
566 cP2, cP2InnerVlan, cP2OuterVlan,
567 mode, sdTag);
568 if (object == null) {
569 log.warn("Could not add pseudowire to the configuration!");
570 return L2TunnelHandler.Result.ADDITION_ERROR;
571 }
572
573 // inform everyone about the valid change in the pw configuration
574 cfgService.applyConfig(appId(), PwaasConfig.class, object);
575 return L2TunnelHandler.Result.SUCCESS;
576 }
577
578 @Override
579 public L2TunnelHandler.Result removePseudowire(String pwId) {
580
581 PwaasConfig config = cfgService.getConfig(appId(), PwaasConfig.class);
582 if (config == null) {
583 log.warn("Configuration for Pwaas class could not be found!");
584 return L2TunnelHandler.Result.CONFIG_NOT_FOUND;
585 }
586
587 ObjectNode object = config.removePseudowire(pwId);
588 if (object == null) {
589 log.warn("Could not delete pseudowire from configuration!");
590 return L2TunnelHandler.Result.REMOVAL_ERROR;
591 }
592
593 // sanity check, this should never fail since we removed a pw
594 // and we always check when we update the configuration
595 config.isValid();
596
597 // inform everyone
598 cfgService.applyConfig(appId(), PwaasConfig.class, object);
599
600 return L2TunnelHandler.Result.SUCCESS;
601 }
602
603 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700604 public void rerouteNetwork() {
605 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700606 }
607
Charles Chanc81c45b2016-10-20 17:02:44 -0700608 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800609 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
610 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800611 deviceConfiguration.getRouters().forEach(device ->
612 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chanc81c45b2016-10-20 17:02:44 -0700613 return deviceSubnetMap;
614 }
615
Saurav Dasc88d4662017-05-15 15:34:25 -0700616
617 @Override
618 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
619 if (defaultRoutingHandler != null) {
620 return defaultRoutingHandler.getCurrentEmcpSpgMap();
621 } else {
622 return null;
623 }
624 }
625
626 @Override
Saurav Das7bcbe702017-06-13 15:35:54 -0700627 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
628 if (dsNextObjStore != null) {
629 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700630 } else {
631 return ImmutableMap.of();
632 }
633 }
634
Saurav Dasceccf242017-08-03 18:30:35 -0700635 @Override
636 public void verifyGroups(DeviceId id) {
637 DefaultGroupHandler gh = groupHandlerMap.get(id);
638 if (gh != null) {
639 gh.triggerBucketCorrector();
640 }
641 }
642
sanghof9d0bf12015-05-19 11:57:42 -0700643 /**
Ray Milkeye4afdb52017-04-05 09:42:04 -0700644 * Extracts the application ID from the manager.
645 *
646 * @return application ID
647 */
648 public ApplicationId appId() {
649 return appId;
650 }
651
652 /**
653 * Returns the device configuration.
654 *
655 * @return device configuration
656 */
657 public DeviceConfiguration deviceConfiguration() {
658 return deviceConfiguration;
659 }
660
661 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700662 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700663 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700664 *
665 * @return next objective ID store
666 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700667 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
668 dsNextObjStore() {
669 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700670 }
671
672 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700673 * Per device next objective ID store with (device id + vlanid) as key.
674 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700675 *
676 * @return vlan next object store
677 */
678 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
679 return vlanNextObjStore;
680 }
681
682 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700683 * Per device next objective ID store with (device id + port + treatment + meta) as key.
684 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700685 *
686 * @return port next object store.
687 */
688 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
689 return portNextObjStore;
690 }
691
692 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700693 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
694 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -0700695 *
696 * @return MPLS-ECMP value
697 */
698 public boolean getMplsEcmp() {
699 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
700 .getConfig(this.appId, SegmentRoutingAppConfig.class);
701 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
702 }
703
704 /**
sanghof9d0bf12015-05-19 11:57:42 -0700705 * Returns the tunnel object with the tunnel ID.
706 *
707 * @param tunnelId Tunnel ID
708 * @return Tunnel reference
709 */
sangho1e575652015-05-14 00:39:53 -0700710 public Tunnel getTunnel(String tunnelId) {
711 return tunnelHandler.getTunnel(tunnelId);
712 }
713
Charles Chan7ffd81f2017-02-08 15:52:08 -0800714 // TODO Consider moving these to InterfaceService
sanghob35a6192015-04-01 13:05:26 -0700715 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800716 * Returns untagged VLAN configured on given connect point.
Charles Chan7ffd81f2017-02-08 15:52:08 -0800717 * <p>
718 * Only returns the first match if there are multiple untagged VLAN configured
719 * on the connect point.
sanghob35a6192015-04-01 13:05:26 -0700720 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800721 * @param connectPoint connect point
722 * @return untagged VLAN or null if not configured
sanghob35a6192015-04-01 13:05:26 -0700723 */
Charles Chan65238242017-06-22 18:03:14 -0700724 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800725 return interfaceService.getInterfacesByPort(connectPoint).stream()
726 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
727 .map(Interface::vlanUntagged)
728 .findFirst().orElse(null);
sanghob35a6192015-04-01 13:05:26 -0700729 }
730
sangho1e575652015-05-14 00:39:53 -0700731 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800732 * Returns tagged VLAN configured on given connect point.
733 * <p>
734 * Returns all matches if there are multiple tagged VLAN configured
735 * on the connect point.
736 *
737 * @param connectPoint connect point
738 * @return tagged VLAN or empty set if not configured
739 */
Charles Chan65238242017-06-22 18:03:14 -0700740 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800741 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
742 return interfaces.stream()
743 .map(Interface::vlanTagged)
Charles Chan65238242017-06-22 18:03:14 -0700744 .flatMap(Set::stream)
Charles Chan7ffd81f2017-02-08 15:52:08 -0800745 .collect(Collectors.toSet());
746 }
747
748 /**
749 * Returns native VLAN configured on given connect point.
750 * <p>
751 * Only returns the first match if there are multiple native VLAN configured
752 * on the connect point.
753 *
754 * @param connectPoint connect point
755 * @return native VLAN or null if not configured
756 */
Charles Chan65238242017-06-22 18:03:14 -0700757 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800758 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
759 return interfaces.stream()
760 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
761 .map(Interface::vlanNative)
762 .findFirst()
763 .orElse(null);
764 }
765
766 /**
Charles Chanf9a52702017-06-16 15:19:24 -0700767 * Returns internal VLAN for untagged hosts on given connect point.
768 * <p>
769 * The internal VLAN is either vlan-untagged for an access port,
770 * or vlan-native for a trunk port.
771 *
772 * @param connectPoint connect point
773 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
774 */
Charles Chan65238242017-06-22 18:03:14 -0700775 VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chanf9a52702017-06-16 15:19:24 -0700776 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
777 VlanId nativeVlanId = getNativeVlanId(connectPoint);
778 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
779 }
780
781 /**
Charles Chan65238242017-06-22 18:03:14 -0700782 * Returns optional pair device ID of given device.
783 *
784 * @param deviceId device ID
785 * @return optional pair device ID. Might be empty if pair device is not configured
786 */
787 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
788 SegmentRoutingDeviceConfig deviceConfig =
789 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
790 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
791 }
792 /**
793 * Returns optional pair device local port of given device.
794 *
795 * @param deviceId device ID
796 * @return optional pair device ID. Might be empty if pair device is not configured
797 */
798 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
799 SegmentRoutingDeviceConfig deviceConfig =
800 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
801 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
802 }
803
804 /**
Charles Chan2fde6d42017-08-23 14:46:43 -0700805 * Determine if current instance is the master of given connect point.
806 *
807 * @param cp connect point
808 * @return true if current instance is the master of given connect point
809 */
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700810 public boolean isMasterOf(ConnectPoint cp) {
Charles Chan2fde6d42017-08-23 14:46:43 -0700811 boolean isMaster = mastershipService.isLocalMaster(cp.deviceId());
812 if (!isMaster) {
813 log.debug(NOT_MASTER, cp);
814 }
815 return isMaster;
816 }
817
818 /**
Charles Chan9640c812017-08-23 13:55:39 -0700819 * Returns locations of given resolved route.
820 *
821 * @param resolvedRoute resolved route
822 * @return locations of nexthop. Might be empty if next hop is not found
823 */
824 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
825 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
826 return Optional.ofNullable(hostService.getHost(hostId))
827 .map(Host::locations).orElse(Sets.newHashSet())
828 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
829 }
830
831 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800832 * Returns vlan port map of given device.
833 *
834 * @param deviceId device id
835 * @return vlan-port multimap
836 */
837 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
838 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
839
840 interfaceService.getInterfaces().stream()
841 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
842 .forEach(intf -> {
843 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -0700844 intf.vlanTagged().forEach(vlanTagged ->
845 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
846 );
Charles Chan7ffd81f2017-02-08 15:52:08 -0800847 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
848 });
849 vlanPortMap.removeAll(VlanId.NONE);
850
851 return vlanPortMap;
852 }
853
854 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800855 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -0800856 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700857 *
858 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -0800859 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -0800860 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700861 */
Charles Chan65238242017-06-22 18:03:14 -0700862 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -0700863 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800864 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
865 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700866 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -0800867 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -0800868 + "device {} not found", deviceId);
869 return -1;
870 }
871 }
872
873 /**
874 * Returns the next objective ID for the given portNumber, given the treatment.
875 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -0700876 * would result in different objectives. If the next object does not exist,
877 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -0800878 *
879 * @param deviceId Device ID
880 * @param portNum port number on device for which NextObjective is queried
881 * @param treatment the actions to apply on the packets (should include outport)
882 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -0700883 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -0700884 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800885 */
886 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
887 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700888 TrafficSelector meta,
889 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -0800890 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
891 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -0700892 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -0800893 } else {
Charles Chane849c192016-01-11 18:28:54 -0800894 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
895 + " not found", deviceId);
896 return -1;
897 }
898 }
899
Saurav Dasc88d4662017-05-15 15:34:25 -0700900 /**
901 * Returns the group handler object for the specified device id.
902 *
903 * @param devId the device identifier
904 * @return the groupHandler object for the device id, or null if not found
905 */
Charles Chan65238242017-06-22 18:03:14 -0700906 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700907 return groupHandlerMap.get(devId);
908 }
909
910 /**
Saurav Dasceccf242017-08-03 18:30:35 -0700911 * Returns the default routing handler object.
912 *
913 * @return the default routing handler object
914 */
915 public DefaultRoutingHandler getRoutingHandler() {
916 return defaultRoutingHandler;
917 }
918
sanghob35a6192015-04-01 13:05:26 -0700919 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700920 @Override
921 public void process(PacketContext context) {
922
923 if (context.isHandled()) {
924 return;
925 }
926
927 InboundPacket pkt = context.inPacket();
928 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -0800929
930 if (ethernet == null) {
931 return;
932 }
933
Saurav Das7bcbe702017-06-13 15:35:54 -0700934 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
935 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800936 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700937 log.warn("Received unexpected ARP packet on {}",
938 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -0700939 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -0800940 return;
sanghob35a6192015-04-01 13:05:26 -0700941 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800942 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
943 //ipHandler.addToPacketBuffer(ipv4Packet);
944 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
945 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700946 } else {
Charles Chan50035632017-01-13 17:20:44 -0800947 // NOTE: We don't support IP learning at this moment so this
948 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -0800949 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700950 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800951 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
952 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -0800953 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -0800954 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -0800955 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
956 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
957 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
958 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
959 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
960 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -0700961 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -0700962 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -0800963 }
964 } else {
965 // NOTE: We don't support IP learning at this moment so this
966 // is not necessary. Also it causes duplication of DHCPv6 packets.
967 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
968 }
sanghob35a6192015-04-01 13:05:26 -0700969 }
970 }
971 }
972
973 private class InternalLinkListener implements LinkListener {
974 @Override
975 public void event(LinkEvent event) {
Charles Chanb1f8c762017-03-29 16:39:05 -0700976 if (event.type() == LinkEvent.Type.LINK_ADDED ||
977 event.type() == LinkEvent.Type.LINK_UPDATED ||
978 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700979 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700980 scheduleEventHandlerIfNotScheduled(event);
981 }
982 }
983 }
984
985 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -0700986 @Override
987 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -0700988 switch (event.type()) {
989 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -0800990 case PORT_UPDATED:
991 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -0700992 case DEVICE_UPDATED:
993 case DEVICE_AVAILABILITY_CHANGED:
Saurav Dasc88d4662017-05-15 15:34:25 -0700994 log.trace("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700995 scheduleEventHandlerIfNotScheduled(event);
996 break;
997 default:
998 }
999 }
1000 }
1001
Saurav Das4ce45962015-11-24 23:21:05 -08001002 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -07001003 private void scheduleEventHandlerIfNotScheduled(Event event) {
Charles Chan65238242017-06-22 18:03:14 -07001004 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001005 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001006 numOfEventsQueued++;
1007
1008 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
1009 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001010 eventHandlerFuture = executorService
1011 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
1012 numOfHandlerScheduled++;
1013 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -07001014 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001015 numOfEventsQueued,
1016 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -07001017 }
sanghob35a6192015-04-01 13:05:26 -07001018 }
1019
1020 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -07001021 @Override
sanghob35a6192015-04-01 13:05:26 -07001022 public void run() {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001023 try {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001024 while (true) {
Saurav Das4ce45962015-11-24 23:21:05 -08001025 @SuppressWarnings("rawtypes")
Charles Chan65238242017-06-22 18:03:14 -07001026 Event event;
1027 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001028 if (!eventQueue.isEmpty()) {
1029 event = eventQueue.poll();
1030 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001031 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001032 numOfHandlerExecution++;
1033 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
1034 numOfHandlerExecution, numOfEventsExecuted);
1035 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001036 }
sangho20eff1d2015-04-13 15:15:58 -07001037 }
Charles Chanb1f8c762017-03-29 16:39:05 -07001038 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1039 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001040 // Note: do not update seenLinks here, otherwise every
1041 // link, even one seen for the first time, will be appear
1042 // to be a previously seen link
Saurav Das45f48152018-01-18 12:07:33 -08001043 linkHandler.processLinkAdded((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001044 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Saurav Das45f48152018-01-18 12:07:33 -08001045 linkHandler.processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001046 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1047 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1048 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001049 DeviceId deviceId = ((Device) event.subject()).id();
1050 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001051 log.info("Processing device event {} for available device {}",
1052 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001053 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -07001054 } else {
1055 log.info("Processing device event {} for unavailable device {}",
1056 event.type(), ((Device) event.subject()).id());
1057 processDeviceRemoved((Device) event.subject());
1058 }
Saurav Das1a129a02016-11-18 15:21:57 -08001059 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001060 // typically these calls come when device is added first time
1061 // so port filtering rules are handled at the device_added event.
1062 // port added calls represent all ports on the device,
1063 // enabled or not.
Saurav Dasc88d4662017-05-15 15:34:25 -07001064 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001065 ((DeviceEvent) event).subject().id(),
1066 ((DeviceEvent) event).port().number(),
1067 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -08001068 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001069 // these calls happen for every subsequent event
1070 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -08001071 log.info("** PORT UPDATED {}/{} -> {}",
1072 event.subject(),
1073 ((DeviceEvent) event).port(),
1074 event.type());
1075 processPortUpdated(((Device) event.subject()),
1076 ((DeviceEvent) event).port());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001077 } else {
1078 log.warn("Unhandled event type: {}", event.type());
1079 }
sanghob35a6192015-04-01 13:05:26 -07001080 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001081 } catch (Exception e) {
1082 log.error("SegmentRouting event handler "
1083 + "thread thrown an exception: {}", e);
sanghob35a6192015-04-01 13:05:26 -07001084 }
sanghob35a6192015-04-01 13:05:26 -07001085 }
1086 }
1087
Saurav Das45f48152018-01-18 12:07:33 -08001088 void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001089 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001090
1091 // NOTE: Punt ARP/NDP even when the device is not configured.
1092 // Host learning without network config is required for CORD config generator.
1093 routingRulePopulator.populateIpPunts(device.id());
1094 routingRulePopulator.populateArpNdpPunts(device.id());
1095
Charles Chan0b4e6182015-11-03 10:42:14 -08001096 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001097 log.warn("Device configuration unavailable. Device {} will be "
1098 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001099 return;
1100 }
Charles Chan2199c302016-04-23 17:36:10 -07001101 processDeviceAddedInternal(device.id());
1102 }
1103
1104 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001105 // Irrespective of whether the local is a MASTER or not for this device,
1106 // we need to create a SR-group-handler instance. This is because in a
1107 // multi-instance setup, any instance can initiate forwarding/next-objectives
1108 // for any switch (even if this instance is a SLAVE or not even connected
1109 // to the switch). To handle this, a default-group-handler instance is necessary
1110 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001111 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1112 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001113 DefaultGroupHandler groupHandler;
1114 try {
1115 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001116 createGroupHandler(deviceId,
1117 appId,
1118 deviceConfiguration,
1119 linkService,
1120 flowObjectiveService,
1121 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001122 } catch (DeviceConfigNotFoundException e) {
1123 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1124 return;
1125 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001126 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001127 deviceId);
1128 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001129 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001130
Charles Chan2199c302016-04-23 17:36:10 -07001131 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001132 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan03a73e02016-10-24 14:52:01 -07001133 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001134 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001135 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001136 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001137 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001138 }
Charles Chan5270ed02016-01-30 23:22:37 -08001139
Charles Chan03a73e02016-10-24 14:52:01 -07001140 appCfgHandler.init(deviceId);
1141 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001142 }
1143
Saurav Das80980c72016-03-23 11:22:49 -07001144 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001145 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001146 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan47933752017-11-30 15:37:50 -08001147 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001148 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001149 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001150 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001151 portNextObjStore.entrySet().stream()
1152 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001153 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Das45f48152018-01-18 12:07:33 -08001154 linkHandler.processDeviceRemoved(device);
Charles Chaned3742352017-06-15 00:44:51 -07001155
Saurav Dasceccf242017-08-03 18:30:35 -07001156 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1157 if (gh != null) {
1158 gh.shutdown();
1159 }
Saurav Das80980c72016-03-23 11:22:49 -07001160 defaultRoutingHandler.purgeEcmpGraph(device.id());
Saurav Dasc88d4662017-05-15 15:34:25 -07001161 // Note that a switch going down is associated with all of its links
1162 // going down as well, but it is treated as a single switch down event
1163 // while the link-downs are ignored.
1164 defaultRoutingHandler
1165 .populateRoutingRulesForLinkStatusChange(null, null, device.id());
Charles Chan2199c302016-04-23 17:36:10 -07001166 mcastHandler.removeDevice(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001167 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -07001168 }
1169
Saurav Das1a129a02016-11-18 15:21:57 -08001170 private void processPortUpdated(Device device, Port port) {
1171 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1172 log.warn("Device configuration uploading. Not handling port event for"
1173 + "dev: {} port: {}", device.id(), port.number());
1174 return;
1175 }
Saurav Das018605f2017-02-18 14:05:44 -08001176
1177 if (!mastershipService.isLocalMaster(device.id())) {
1178 log.debug("Not master for dev:{} .. not handling port updated event"
1179 + "for port {}", device.id(), port.number());
1180 return;
1181 }
1182
1183 // first we handle filtering rules associated with the port
1184 if (port.isEnabled()) {
1185 log.info("Switchport {}/{} enabled..programming filters",
1186 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001187 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001188 } else {
1189 log.info("Switchport {}/{} disabled..removing filters",
1190 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001191 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001192 }
Saurav Das1a129a02016-11-18 15:21:57 -08001193
1194 // portUpdated calls are for ports that have gone down or up. For switch
1195 // to switch ports, link-events should take care of any re-routing or
1196 // group editing necessary for port up/down. Here we only process edge ports
1197 // that are already configured.
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001198 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1199 VlanId untaggedVlan = getUntaggedVlanId(cp);
1200 VlanId nativeVlan = getNativeVlanId(cp);
1201 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001202
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001203 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001204 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das1a129a02016-11-18 15:21:57 -08001205 + "dev/port: {}/{}", device.id(), port.number());
1206 return;
1207 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001208 if (untaggedVlan != null) {
1209 processEdgePort(device, port, untaggedVlan, true);
1210 }
1211 if (nativeVlan != null) {
1212 processEdgePort(device, port, nativeVlan, true);
1213 }
1214 if (!taggedVlans.isEmpty()) {
1215 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1216 }
Saurav Das1a129a02016-11-18 15:21:57 -08001217 }
1218
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001219 private void processEdgePort(Device device, Port port, VlanId vlanId,
1220 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001221 boolean portUp = port.isEnabled();
1222 if (portUp) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001223 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001224 port.number(), vlanId);
Charles Chan47933752017-11-30 15:37:50 -08001225 hostHandler.processPortUp(new ConnectPoint(device.id(), port.number()));
Saurav Das1a129a02016-11-18 15:21:57 -08001226 } else {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001227 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001228 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001229 }
1230
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001231 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001232 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001233 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001234 } else {
1235 log.warn("Group handler not found for dev:{}. Not handling edge port"
1236 + " {} event for port:{}", device.id(),
1237 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001238 }
1239 }
sangho1e575652015-05-14 00:39:53 -07001240
Charles Chan27fe1a52017-10-20 16:06:55 -07001241 private void createOrUpdateDeviceConfiguration() {
1242 if (deviceConfiguration == null) {
1243 deviceConfiguration = new DeviceConfiguration(this);
1244 } else {
1245 deviceConfiguration.updateConfig();
1246 }
1247 }
1248
Pier Ventre10bd8d12016-11-26 21:05:22 -08001249 /**
1250 * Registers the given connect point with the NRS, this is necessary
1251 * to receive the NDP and ARP packets from the NRS.
1252 *
1253 * @param portToRegister connect point to register
1254 */
1255 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001256 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001257 portToRegister,
1258 neighbourHandler,
1259 appId
1260 );
1261 }
1262
Charles Chand6832882015-10-05 17:50:33 -07001263 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001264 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001265 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001266
Charles Chane849c192016-01-11 18:28:54 -08001267 /**
1268 * Constructs the internal network config listener.
1269 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001270 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001271 */
Charles Chan65238242017-06-22 18:03:14 -07001272 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001273 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001274 }
1275
Charles Chane849c192016-01-11 18:28:54 -08001276 /**
1277 * Reads network config and initializes related data structure accordingly.
1278 */
Charles Chan47933752017-11-30 15:37:50 -08001279 void configureNetwork() {
Charles Chan27fe1a52017-10-20 16:06:55 -07001280 createOrUpdateDeviceConfiguration();
Charles Chan4636be02015-10-07 14:21:45 -07001281
Charles Chan2c15aca2016-11-09 20:51:44 -08001282 arpHandler = new ArpHandler(srManager);
1283 icmpHandler = new IcmpHandler(srManager);
1284 ipHandler = new IpHandler(srManager);
1285 routingRulePopulator = new RoutingRulePopulator(srManager);
1286 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001287
1288 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1289 groupHandlerMap, tunnelStore);
1290 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1291 flowObjectiveService,
1292 tunnelHandler, policyStore);
Saurav Das7bcbe702017-06-13 15:35:54 -07001293 // add a small delay to absorb multiple network config added notifications
1294 if (!programmingScheduled.get()) {
1295 programmingScheduled.set(true);
1296 executorService.schedule(new ConfigChange(), PROGRAM_DELAY,
1297 TimeUnit.SECONDS);
Charles Chan4636be02015-10-07 14:21:45 -07001298 }
Charles Chan2199c302016-04-23 17:36:10 -07001299 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001300 }
1301
Charles Chand6832882015-10-05 17:50:33 -07001302 @Override
1303 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001304 // TODO move this part to NetworkConfigEventHandler
1305 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1306 switch (event.type()) {
1307 case CONFIG_ADDED:
Charles Chan278ce832017-06-26 15:25:09 -07001308 log.info("Segment Routing Device Config added for {}", event.subject());
Charles Chan5270ed02016-01-30 23:22:37 -08001309 configureNetwork();
1310 break;
1311 case CONFIG_UPDATED:
Charles Chan278ce832017-06-26 15:25:09 -07001312 log.info("Segment Routing Config updated for {}", event.subject());
Charles Chan27fe1a52017-10-20 16:06:55 -07001313 createOrUpdateDeviceConfiguration();
Charles Chan278ce832017-06-26 15:25:09 -07001314 // TODO support dynamic configuration
1315 break;
1316 default:
1317 break;
1318 }
1319 } else if (event.configClass().equals(InterfaceConfig.class)) {
1320 switch (event.type()) {
1321 case CONFIG_ADDED:
1322 log.info("Interface Config added for {}", event.subject());
1323 configureNetwork();
1324 break;
1325 case CONFIG_UPDATED:
1326 log.info("Interface Config updated for {}", event.subject());
Charles Chan27fe1a52017-10-20 16:06:55 -07001327 createOrUpdateDeviceConfiguration();
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001328
1329 // Following code will be uncommented when [CORD-634] is fully implemented.
1330 // [CORD-634] Add dynamic config support for interfaces
1331 updateInterface((InterfaceConfig) event.config().get(),
1332 (InterfaceConfig) event.prevConfig().get());
Charles Chan5270ed02016-01-30 23:22:37 -08001333 // TODO support dynamic configuration
1334 break;
1335 default:
1336 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001337 }
Charles Chan5270ed02016-01-30 23:22:37 -08001338 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001339 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001340 switch (event.type()) {
1341 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001342 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001343 break;
1344 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001345 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001346 break;
1347 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001348 appCfgHandler.processAppConfigRemoved(event);
1349 break;
1350 default:
1351 break;
1352 }
Charles Chan03a73e02016-10-24 14:52:01 -07001353 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001354 } else if (event.configClass().equals(XConnectConfig.class)) {
1355 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1356 switch (event.type()) {
1357 case CONFIG_ADDED:
1358 xConnectHandler.processXConnectConfigAdded(event);
1359 break;
1360 case CONFIG_UPDATED:
1361 xConnectHandler.processXConnectConfigUpdated(event);
1362 break;
1363 case CONFIG_REMOVED:
1364 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001365 break;
1366 default:
1367 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001368 }
Pier Ventref34966c2016-11-07 16:21:04 -08001369 } else if (event.configClass().equals(PwaasConfig.class)) {
1370 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1371 switch (event.type()) {
1372 case CONFIG_ADDED:
1373 l2TunnelHandler.processPwaasConfigAdded(event);
1374 break;
1375 case CONFIG_UPDATED:
1376 l2TunnelHandler.processPwaasConfigUpdated(event);
1377 break;
1378 case CONFIG_REMOVED:
1379 l2TunnelHandler.processPwaasConfigRemoved(event);
1380 break;
1381 default:
1382 break;
1383 }
Charles Chand6832882015-10-05 17:50:33 -07001384 }
1385 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001386
Charles Chan50443e82018-01-03 16:26:32 -08001387 @Override
1388 public boolean isRelevant(NetworkConfigEvent event) {
1389 if (event.configClass().equals(SegmentRoutingDeviceConfig.class) ||
1390 event.configClass().equals(SegmentRoutingAppConfig.class) ||
1391 event.configClass().equals(InterfaceConfig.class) ||
1392 event.configClass().equals(XConnectConfig.class) ||
1393 event.configClass().equals(PwaasConfig.class)) {
1394 return true;
1395 }
1396 log.debug("Ignore irrelevant event class {}", event.configClass().getName());
1397 return false;
1398 }
1399
Saurav Das7bcbe702017-06-13 15:35:54 -07001400 private final class ConfigChange implements Runnable {
1401 @Override
1402 public void run() {
1403 programmingScheduled.set(false);
1404 for (Device device : deviceService.getDevices()) {
1405 processDeviceAdded(device);
1406 }
1407 defaultRoutingHandler.startPopulationProcess();
1408 }
1409 }
Charles Chand6832882015-10-05 17:50:33 -07001410 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001411
1412 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001413 @Override
1414 public void event(HostEvent event) {
Charles Chan68aa62d2015-11-09 16:37:23 -08001415 switch (event.type()) {
1416 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001417 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001418 break;
1419 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001420 hostHandler.processHostMovedEvent(event);
Charles Chan2fde6d42017-08-23 14:46:43 -07001421 routeHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001422 break;
1423 case HOST_REMOVED:
Charles Chanf9a52702017-06-16 15:19:24 -07001424 hostHandler.processHostRemovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001425 break;
1426 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001427 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001428 break;
1429 default:
1430 log.warn("Unsupported host event type: {}", event.type());
1431 break;
1432 }
1433 }
1434 }
1435
Charles Chand55e84d2016-03-30 17:54:24 -07001436 private class InternalMcastListener implements McastListener {
1437 @Override
1438 public void event(McastEvent event) {
1439 switch (event.type()) {
1440 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001441 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001442 break;
1443 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001444 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001445 break;
1446 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001447 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001448 break;
1449 case ROUTE_ADDED:
1450 case ROUTE_REMOVED:
1451 default:
1452 break;
1453 }
1454 }
1455 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001456
Charles Chan03a73e02016-10-24 14:52:01 -07001457 private class InternalRouteEventListener implements RouteListener {
1458 @Override
1459 public void event(RouteEvent event) {
1460 switch (event.type()) {
1461 case ROUTE_ADDED:
1462 routeHandler.processRouteAdded(event);
1463 break;
1464 case ROUTE_UPDATED:
1465 routeHandler.processRouteUpdated(event);
1466 break;
1467 case ROUTE_REMOVED:
1468 routeHandler.processRouteRemoved(event);
1469 break;
Charles Chan2fde6d42017-08-23 14:46:43 -07001470 case ALTERNATIVE_ROUTES_CHANGED:
1471 routeHandler.processAlternativeRoutesChanged(event);
1472 break;
Charles Chan03a73e02016-10-24 14:52:01 -07001473 default:
1474 break;
1475 }
1476 }
1477 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001478
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001479 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1480 try {
1481 Set<Interface> intfs = conf.getInterfaces();
1482 Set<Interface> prevIntfs = prevConf.getInterfaces();
1483
1484 // Now we only handle one interface config at each port.
1485 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1486 log.warn("Interface update aborted - one at a time is allowed, " +
1487 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1488 return;
1489 }
1490
1491 Interface intf = intfs.stream().findFirst().get();
1492 Interface prevIntf = prevIntfs.stream().findFirst().get();
1493
1494 DeviceId deviceId = intf.connectPoint().deviceId();
1495 PortNumber portNum = intf.connectPoint().port();
1496
1497 if (!mastershipService.isLocalMaster(deviceId)) {
1498 log.debug("CONFIG_UPDATED event for interfaces should be " +
1499 "handled by master node for device {}", deviceId);
1500 return;
1501 }
1502
1503 removeSubnetConfig(prevIntf.connectPoint(),
1504 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1505 new HashSet<>(intf.ipAddressesList())));
1506
1507 if (prevIntf.vlanNative() != VlanId.NONE && !intf.vlanNative().equals(prevIntf.vlanNative())) {
1508 // RemoveVlanNative
1509 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1510 }
1511
1512 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1513 // RemoveVlanTagged
1514 prevIntf.vlanTagged().stream().filter(i -> !intf.vlanTagged().contains(i)).forEach(
1515 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, false)
1516 );
1517 }
1518
1519 if (prevIntf.vlanUntagged() != VlanId.NONE && !intf.vlanUntagged().equals(prevIntf.vlanUntagged())) {
1520 // RemoveVlanUntagged
1521 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1522 }
1523
1524 if (intf.vlanNative() != VlanId.NONE && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1525 // AddVlanNative
1526 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1527 }
1528
1529 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1530 // AddVlanTagged
1531 intf.vlanTagged().stream().filter(i -> !prevIntf.vlanTagged().contains(i)).forEach(
1532 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, true)
1533 );
1534 }
1535
1536 if (intf.vlanUntagged() != VlanId.NONE && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())) {
1537 // AddVlanUntagged
1538 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1539 }
1540 addSubnetConfig(prevIntf.connectPoint(),
1541 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1542 new HashSet<>(prevIntf.ipAddressesList())));
1543 } catch (ConfigException e) {
1544 log.error("Error in configuration");
1545 }
1546 }
1547
1548 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1549 VlanId vlanId, boolean pushVlan, boolean install) {
1550 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1551 if (grpHandler == null) {
1552 log.warn("Failed to retrieve group handler for device {}", deviceId);
1553 return;
1554 }
1555
1556 // Update filtering objective for a single port
1557 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1558
1559 // Update filtering objective for multicast ingress port
1560 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1561
1562 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1563
1564 if (nextId != -1 && !install) {
1565 // Update next objective for a single port as an output port
1566 // Remove a single port from L2FG
1567 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1568 // Remove L2 Bridging rule and L3 Unicast rule to the host
1569 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1570 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1571 // only if there is no port configured on that VLAN ID
1572 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1573 // Remove broadcast forwarding rule for the VLAN
1574 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1575 // Remove L2FG for VLAN
1576 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1577 } else {
1578 // Remove L2IG of the port
1579 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1580 }
1581 } else if (install) {
1582 if (nextId != -1) {
1583 // Add a single port to L2FG
1584 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1585 } else {
1586 // Create L2FG for VLAN
1587 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1588 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1589 }
1590 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1591 } else {
1592 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1593 }
1594 }
1595
1596 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1597 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1598 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1599
1600 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1601 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1602 .filter(intf -> !intf.connectPoint().equals(cp))
1603 .flatMap(intf -> intf.ipAddressesList().stream())
1604 .collect(Collectors.toSet());
1605 // 1. Partial subnet population
1606 // Remove routing rules for removed subnet from previous configuration,
1607 // which does not also exist in other interfaces in the same device
1608 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1609 .map(InterfaceIpAddress::subnetAddress)
1610 .collect(Collectors.toSet());
1611
1612 defaultRoutingHandler.revokeSubnet(
1613 ipPrefixSet.stream()
1614 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1615 .collect(Collectors.toSet()));
1616
1617 // 2. Interface IP punts
1618 // Remove IP punts for old Intf address
1619 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1620 .map(InterfaceIpAddress::ipAddress)
1621 .collect(Collectors.toSet());
1622 ipAddressSet.stream()
1623 .map(InterfaceIpAddress::ipAddress)
1624 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1625 .forEach(interfaceIpAddress ->
1626 routingRulePopulator.revokeSingleIpPunts(
1627 cp.deviceId(), interfaceIpAddress));
1628
1629 // 3. Host unicast routing rule
1630 // Remove unicast routing rule
1631 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1632 }
1633
1634 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1635 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1636 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1637
1638 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1639 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1640 .filter(intf -> !intf.connectPoint().equals(cp))
1641 .flatMap(intf -> intf.ipAddressesList().stream())
1642 .collect(Collectors.toSet());
1643 // 1. Partial subnet population
1644 // Add routing rules for newly added subnet, which does not also exist in
1645 // other interfaces in the same device
1646 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1647 .map(InterfaceIpAddress::subnetAddress)
1648 .collect(Collectors.toSet());
1649
1650 defaultRoutingHandler.populateSubnet(
1651 Collections.singleton(cp),
1652 ipPrefixSet.stream()
1653 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1654 .collect(Collectors.toSet()));
1655
1656 // 2. Interface IP punts
1657 // Add IP punts for new Intf address
1658 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1659 .map(InterfaceIpAddress::ipAddress)
1660 .collect(Collectors.toSet());
1661 ipAddressSet.stream()
1662 .map(InterfaceIpAddress::ipAddress)
1663 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1664 .forEach(interfaceIpAddress ->
1665 routingRulePopulator.populateSingleIpPunts(
1666 cp.deviceId(), interfaceIpAddress));
1667
1668 // 3. Host unicast routing rule
1669 // Add unicast routing rule
1670 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1671 }
sanghob35a6192015-04-01 13:05:26 -07001672}