blob: 4f853686f6e54a70df54a2c7e2bdab4c4476d098 [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;
Pier Luigieba73a02018-01-16 10:47:50 +010043import org.onosproject.cluster.ClusterService;
44import org.onosproject.cluster.LeadershipService;
sanghob35a6192015-04-01 13:05:26 -070045import org.onosproject.core.ApplicationId;
46import org.onosproject.core.CoreService;
47import org.onosproject.event.Event;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070048import org.onosproject.mastership.MastershipService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080049import org.onosproject.net.ConnectPoint;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070050import org.onosproject.net.Device;
51import org.onosproject.net.DeviceId;
Charles Chan9640c812017-08-23 13:55:39 -070052import org.onosproject.net.Host;
53import org.onosproject.net.HostId;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070054import org.onosproject.net.Link;
55import org.onosproject.net.Port;
Charles Chan68aa62d2015-11-09 16:37:23 -080056import org.onosproject.net.PortNumber;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070057import org.onosproject.net.config.ConfigException;
Charles Chand6832882015-10-05 17:50:33 -070058import org.onosproject.net.config.ConfigFactory;
59import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070060import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070061import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070062import org.onosproject.net.config.basics.InterfaceConfig;
63import org.onosproject.net.config.basics.McastConfig;
Charles Chand6832882015-10-05 17:50:33 -070064import org.onosproject.net.config.basics.SubjectFactories;
Saurav Das45f48152018-01-18 12:07:33 -080065import org.onosproject.net.device.DeviceAdminService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070066import org.onosproject.net.device.DeviceEvent;
67import org.onosproject.net.device.DeviceListener;
68import org.onosproject.net.device.DeviceService;
Charles Chan68aa62d2015-11-09 16:37:23 -080069import org.onosproject.net.flow.TrafficSelector;
70import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070071import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan68aa62d2015-11-09 16:37:23 -080072import org.onosproject.net.host.HostEvent;
73import org.onosproject.net.host.HostListener;
Charles Chan47933752017-11-30 15:37:50 -080074import org.onosproject.net.host.HostLocationProbingService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080075import org.onosproject.net.host.HostService;
Jonghwan Hyun42fe1052017-08-25 17:48:36 -070076import org.onosproject.net.host.InterfaceIpAddress;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070077import org.onosproject.net.intf.Interface;
78import org.onosproject.net.intf.InterfaceService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080079import org.onosproject.net.link.LinkEvent;
80import org.onosproject.net.link.LinkListener;
81import org.onosproject.net.link.LinkService;
Charles Chand55e84d2016-03-30 17:54:24 -070082import org.onosproject.net.mcast.McastEvent;
83import org.onosproject.net.mcast.McastListener;
84import org.onosproject.net.mcast.MulticastRouteService;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070085import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080086import org.onosproject.net.packet.InboundPacket;
87import org.onosproject.net.packet.PacketContext;
88import org.onosproject.net.packet.PacketProcessor;
89import org.onosproject.net.packet.PacketService;
Charles Chand55e84d2016-03-30 17:54:24 -070090import org.onosproject.net.topology.TopologyService;
Charles Chan9640c812017-08-23 13:55:39 -070091import org.onosproject.routeservice.ResolvedRoute;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070092import org.onosproject.routeservice.RouteEvent;
93import org.onosproject.routeservice.RouteListener;
94import org.onosproject.routeservice.RouteService;
Charles Chand55e84d2016-03-30 17:54:24 -070095import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
96import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventref34966c2016-11-07 16:21:04 -080097import org.onosproject.segmentrouting.config.PwaasConfig;
Pier Ventref34966c2016-11-07 16:21:04 -080098import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkey6c1f0f02017-08-15 11:02:29 -070099import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -0700100import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -0700101import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700102import org.onosproject.segmentrouting.grouphandler.DestinationSet;
103import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700104import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
105import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Ray Milkey6c1f0f02017-08-15 11:02:29 -0700106import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Saurav Das7bcbe702017-06-13 15:35:54 -0700107import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Charles Chand2990362016-04-18 13:44:03 -0700108import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -0800109import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -0700110import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700111import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700112import org.onosproject.store.service.EventuallyConsistentMap;
113import org.onosproject.store.service.EventuallyConsistentMapBuilder;
114import org.onosproject.store.service.StorageService;
115import org.onosproject.store.service.WallClockTimestamp;
Charles Chan47933752017-11-30 15:37:50 -0800116import org.osgi.service.component.ComponentContext;
sanghob35a6192015-04-01 13:05:26 -0700117import org.slf4j.Logger;
118import org.slf4j.LoggerFactory;
119
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800120import java.util.Collections;
Charles Chan47933752017-11-30 15:37:50 -0800121import java.util.Dictionary;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800122import java.util.HashSet;
123import java.util.List;
124import java.util.Map;
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800125import java.util.Optional;
126import java.util.Set;
127import java.util.concurrent.ConcurrentHashMap;
128import java.util.concurrent.ConcurrentLinkedQueue;
129import java.util.concurrent.Executors;
130import java.util.concurrent.ScheduledExecutorService;
131import java.util.concurrent.ScheduledFuture;
132import java.util.concurrent.TimeUnit;
133import java.util.concurrent.atomic.AtomicBoolean;
134import java.util.stream.Collectors;
sanghob35a6192015-04-01 13:05:26 -0700135
Charles Chan3e783d02016-02-26 22:19:52 -0800136import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800137import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700138import static org.onlab.util.Tools.groupedThreads;
Saurav Dase7f51012018-02-09 17:26:45 -0800139import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
140import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Charles Chan3e783d02016-02-26 22:19:52 -0800141
Charles Chane849c192016-01-11 18:28:54 -0800142/**
143 * Segment routing manager.
144 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700145@Service
146@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700147public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700148
Charles Chan2c15aca2016-11-09 20:51:44 -0800149 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan2fde6d42017-08-23 14:46:43 -0700150 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sanghob35a6192015-04-01 13:05:26 -0700151
152 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700153 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700154
155 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre10bd8d12016-11-26 21:05:22 -0800156 private NeighbourResolutionService neighbourResolutionService;
157
158 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700159 CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700160
161 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700162 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700163
164 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700165 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700166
167 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan47933752017-11-30 15:37:50 -0800168 HostLocationProbingService probingService;
169
170 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700171 DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700172
173 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Saurav Das45f48152018-01-18 12:07:33 -0800174 DeviceAdminService deviceAdminService;
175
176 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800177 public FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700178
179 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700180 LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700181
Charles Chan5270ed02016-01-30 23:22:37 -0800182 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800183 public MastershipService mastershipService;
Charles Chan03a73e02016-10-24 14:52:01 -0700184
185 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800186 public StorageService storageService;
Charles Chan03a73e02016-10-24 14:52:01 -0700187
188 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
189 MulticastRouteService multicastRouteService;
190
191 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chanf237e1b2018-01-09 13:45:07 -0800192 public TopologyService topologyService;
Charles Chan03a73e02016-10-24 14:52:01 -0700193
194 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700195 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800196
197 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800198 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800199
Saurav Das80980c72016-03-23 11:22:49 -0700200 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800201 public InterfaceService interfaceService;
202
Pier Luigieba73a02018-01-16 10:47:50 +0100203 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
204 public ClusterService clusterService;
205
206 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
207 public LeadershipService leadershipService;
208
Charles Chan47933752017-11-30 15:37:50 -0800209 @Property(name = "activeProbing", boolValue = true,
210 label = "Enable active probing to discover dual-homed hosts.")
211 boolean activeProbing = true;
212
Charles Chan03a73e02016-10-24 14:52:01 -0700213 ArpHandler arpHandler = null;
214 IcmpHandler icmpHandler = null;
215 IpHandler ipHandler = null;
216 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700217 ApplicationId appId;
218 DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700219
Charles Chan03a73e02016-10-24 14:52:01 -0700220 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700221 private TunnelHandler tunnelHandler = null;
222 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700223 private InternalPacketProcessor processor = null;
224 private InternalLinkListener linkListener = null;
225 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700226 private AppConfigHandler appCfgHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700227 XConnectHandler xConnectHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800228 McastHandler mcastHandler = null;
229 HostHandler hostHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800230 private RouteHandler routeHandler = null;
Saurav Das45f48152018-01-18 12:07:33 -0800231 LinkHandler linkHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800232 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Pier Ventref34966c2016-11-07 16:21:04 -0800233 private L2TunnelHandler l2TunnelHandler = null;
sanghob35a6192015-04-01 13:05:26 -0700234 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan5270ed02016-01-30 23:22:37 -0800235 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700236 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
237 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700238 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sanghob35a6192015-04-01 13:05:26 -0700239
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700240 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700241 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sanghob35a6192015-04-01 13:05:26 -0700242
Saurav Das4ce45962015-11-24 23:21:05 -0800243 @SuppressWarnings("unused")
sanghob35a6192015-04-01 13:05:26 -0700244 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800245 @SuppressWarnings("rawtypes")
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700246 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Saurav Das45f48152018-01-18 12:07:33 -0800247 Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chane849c192016-01-11 18:28:54 -0800248 new ConcurrentHashMap<>();
249 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700250 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700251 * Used to keep track on MPLS group information.
Charles Chane849c192016-01-11 18:28:54 -0800252 */
Charles Chan47933752017-11-30 15:37:50 -0800253 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das7bcbe702017-06-13 15:35:54 -0700254 dsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800255 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700256 * Per device next objective ID store with (device id + vlanid) as key.
257 * Used to keep track on L2 flood group information.
Charles Chane849c192016-01-11 18:28:54 -0800258 */
Charles Chan47933752017-11-30 15:37:50 -0800259 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan59cc16d2017-02-02 16:20:42 -0800260 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800261 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700262 * Per device next objective ID store with (device id + port + treatment + meta) as key.
263 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chane849c192016-01-11 18:28:54 -0800264 */
Charles Chan47933752017-11-30 15:37:50 -0800265 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800266 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800267
Saurav Das4ce45962015-11-24 23:21:05 -0800268 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
269 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700270
Saurav Das7bcbe702017-06-13 15:35:54 -0700271 private AtomicBoolean programmingScheduled = new AtomicBoolean();
272
Charles Chand55e84d2016-03-30 17:54:24 -0700273 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700274 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
275 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700276 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700277 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800278 public SegmentRoutingDeviceConfig createConfig() {
279 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700280 }
281 };
Pier Ventref34966c2016-11-07 16:21:04 -0800282
Charles Chand55e84d2016-03-30 17:54:24 -0700283 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700284 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
285 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700286 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800287 @Override
288 public SegmentRoutingAppConfig createConfig() {
289 return new SegmentRoutingAppConfig();
290 }
291 };
Pier Ventref34966c2016-11-07 16:21:04 -0800292
Charles Chanfc5c7802016-05-17 13:13:55 -0700293 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
294 new ConfigFactory<ApplicationId, XConnectConfig>(
295 SubjectFactories.APP_SUBJECT_FACTORY,
296 XConnectConfig.class, "xconnect") {
297 @Override
298 public XConnectConfig createConfig() {
299 return new XConnectConfig();
300 }
301 };
Pier Ventref34966c2016-11-07 16:21:04 -0800302
Charles Chand55e84d2016-03-30 17:54:24 -0700303 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700304 new ConfigFactory<ApplicationId, McastConfig>(
305 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700306 McastConfig.class, "multicast") {
307 @Override
308 public McastConfig createConfig() {
309 return new McastConfig();
310 }
311 };
312
Pier Ventref34966c2016-11-07 16:21:04 -0800313 private final ConfigFactory<ApplicationId, PwaasConfig> pwaasConfigFactory =
314 new ConfigFactory<ApplicationId, PwaasConfig>(
315 SubjectFactories.APP_SUBJECT_FACTORY,
316 PwaasConfig.class, "pwaas") {
317 @Override
318 public PwaasConfig createConfig() {
319 return new PwaasConfig();
320 }
321 };
322
Charles Chan65238242017-06-22 18:03:14 -0700323 private static final Object THREAD_SCHED_LOCK = new Object();
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700324 private static int numOfEventsQueued = 0;
325 private static int numOfEventsExecuted = 0;
sanghob35a6192015-04-01 13:05:26 -0700326 private static int numOfHandlerExecution = 0;
327 private static int numOfHandlerScheduled = 0;
328
Charles Chan116188d2016-02-18 14:22:42 -0800329 /**
330 * Segment Routing App ID.
331 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800332 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan59cc16d2017-02-02 16:20:42 -0800333
Charles Chane849c192016-01-11 18:28:54 -0800334 /**
335 * The default VLAN ID assigned to the interfaces without subnet config.
336 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800337 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700338
sanghob35a6192015-04-01 13:05:26 -0700339 @Activate
Charles Chan47933752017-11-30 15:37:50 -0800340 protected void activate(ComponentContext context) {
Charles Chan2c15aca2016-11-09 20:51:44 -0800341 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700342
343 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das7bcbe702017-06-13 15:35:54 -0700344 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700345 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7bcbe702017-06-13 15:35:54 -0700346 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700347 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700348 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700349 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700350 .build();
Saurav Das7bcbe702017-06-13 15:35:54 -0700351 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700352
Charles Chan59cc16d2017-02-02 16:20:42 -0800353 log.debug("Creating EC map vlannextobjectivestore");
354 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
355 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
356 vlanNextObjStore = vlanNextObjMapBuilder
357 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700358 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700359 .withTimestampProvider((k, v) -> new WallClockTimestamp())
360 .build();
361
Saurav Das4ce45962015-11-24 23:21:05 -0800362 log.debug("Creating EC map subnetnextobjectivestore");
363 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
364 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
365 portNextObjStore = portNextObjMapBuilder
366 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700367 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800368 .withTimestampProvider((k, v) -> new WallClockTimestamp())
369 .build();
370
sangho0b2b6d12015-05-20 22:16:38 -0700371 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
372 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700373 tunnelStore = tunnelMapBuilder
374 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700375 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700376 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700377 .build();
378
379 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
380 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700381 policyStore = policyMapBuilder
382 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700383 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700384 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700385 .build();
386
Saurav Das80980c72016-03-23 11:22:49 -0700387 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800388 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700389 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800390 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800391 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
392 "requestInterceptsEnabled", "false");
Charles Chand3baaba2017-08-08 15:13:37 -0700393 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800394 "requestInterceptsEnabled", "false");
Charles Chanc760f382017-07-24 15:56:10 -0700395 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigi7e415132017-01-12 22:46:39 -0800396 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800397 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
398 "greedyLearningIpv6", "true");
Charles Chanc6d227e2017-02-28 15:15:17 -0800399 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
400 "forceUnprovision", "true");
Charles Chanef624ed2017-08-10 16:57:28 -0700401 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan73316522017-07-20 16:16:25 -0700402 "distributed", "true");
Charles Chan35a32322017-08-14 11:42:11 -0700403 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
404 "multihomingEnabled", "true");
Charles Chan807d87a2017-11-29 19:54:20 -0800405 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
406 "staleLinkAge", "15000");
407 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
408 "allowDuplicateIps", "false");
Charles Chan47933752017-11-30 15:37:50 -0800409 compCfgService.registerProperties(getClass());
410 modified(context);
Saurav Das80980c72016-03-23 11:22:49 -0700411
Charles Chanb8e10c82015-10-14 11:24:40 -0700412 processor = new InternalPacketProcessor();
413 linkListener = new InternalLinkListener();
414 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700415 appCfgHandler = new AppConfigHandler(this);
416 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700417 mcastHandler = new McastHandler(this);
418 hostHandler = new HostHandler(this);
Saurav Das45f48152018-01-18 12:07:33 -0800419 linkHandler = new LinkHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700420 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800421 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Pier Ventref34966c2016-11-07 16:21:04 -0800422 l2TunnelHandler = new L2TunnelHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700423
Charles Chan3e783d02016-02-26 22:19:52 -0800424 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700425 cfgService.registerConfigFactory(deviceConfigFactory);
426 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700427 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700428 cfgService.registerConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800429 cfgService.registerConfigFactory(pwaasConfigFactory);
Saurav Dase7f51012018-02-09 17:26:45 -0800430 log.info("Configuring network before adding listeners");
Charles Chan132393a2018-01-04 14:26:07 -0800431 cfgListener.configureNetwork();
432
Charles Chan5270ed02016-01-30 23:22:37 -0800433 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700434 packetService.addProcessor(processor, PacketProcessor.director(2));
435 linkService.addListener(linkListener);
436 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700437 multicastRouteService.addListener(mcastListener);
Charles Chanc7a8a682017-06-19 00:43:31 -0700438 routeService.addListener(routeListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700439
Andreas Pantelopoulos4a768c02018-01-11 07:53:48 -0800440 l2TunnelHandler.init();
441
sanghob35a6192015-04-01 13:05:26 -0700442 log.info("Started");
443 }
444
Saurav Das45f48152018-01-18 12:07:33 -0800445 KryoNamespace.Builder createSerializer() {
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700446 return new KryoNamespace.Builder()
447 .register(KryoNamespaces.API)
Saurav Das7bcbe702017-06-13 15:35:54 -0700448 .register(DestinationSetNextObjectiveStoreKey.class,
Charles Chan59cc16d2017-02-02 16:20:42 -0800449 VlanNextObjectiveStoreKey.class,
Saurav Das7bcbe702017-06-13 15:35:54 -0700450 DestinationSet.class,
451 NextNeighbors.class,
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700452 Tunnel.class,
453 DefaultTunnel.class,
454 Policy.class,
455 TunnelPolicy.class,
456 Policy.Type.class,
457 PortNextObjectiveStoreKey.class,
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700458 XConnectStoreKey.class,
459 DefaultL2Tunnel.class,
460 DefaultL2TunnelPolicy.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700461 );
462 }
463
sanghob35a6192015-04-01 13:05:26 -0700464 @Deactivate
465 protected void deactivate() {
Charles Chand6832882015-10-05 17:50:33 -0700466 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700467 cfgService.unregisterConfigFactory(deviceConfigFactory);
468 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700469 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700470 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800471 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chan47933752017-11-30 15:37:50 -0800472 compCfgService.unregisterProperties(getClass(), false);
Charles Chand6832882015-10-05 17:50:33 -0700473
Charles Chanc7a8a682017-06-19 00:43:31 -0700474 hostService.removeListener(hostListener);
sanghob35a6192015-04-01 13:05:26 -0700475 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700476 linkService.removeListener(linkListener);
477 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700478 multicastRouteService.removeListener(mcastListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700479 routeService.removeListener(routeListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700480
Charles Chan0aa674e2017-02-23 15:44:08 -0800481 neighbourResolutionService.unregisterNeighbourHandlers(appId);
482
sanghob35a6192015-04-01 13:05:26 -0700483 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700484 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700485 deviceListener = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700486 groupHandlerMap.clear();
487
Saurav Das7bcbe702017-06-13 15:35:54 -0700488 dsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800489 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700490 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700491 tunnelStore.destroy();
492 policyStore.destroy();
Pier Luigib72201b2018-01-25 16:16:02 +0100493
494 mcastHandler.terminate();
sanghob35a6192015-04-01 13:05:26 -0700495 log.info("Stopped");
496 }
497
Charles Chan47933752017-11-30 15:37:50 -0800498 @Modified
499 private void modified(ComponentContext context) {
500 Dictionary<?, ?> properties = context.getProperties();
501 if (properties == null) {
502 return;
503 }
504
505 String strActiveProving = Tools.get(properties, "activeProbing");
506 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
507
508 if (expectActiveProbing != activeProbing) {
509 activeProbing = expectActiveProbing;
510 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
511 }
512 }
513
sangho1e575652015-05-14 00:39:53 -0700514 @Override
515 public List<Tunnel> getTunnels() {
516 return tunnelHandler.getTunnels();
517 }
518
519 @Override
sangho71abe1b2015-06-29 14:58:47 -0700520 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
521 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700522 }
523
524 @Override
sangho71abe1b2015-06-29 14:58:47 -0700525 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700526 for (Policy policy: policyHandler.getPolicies()) {
527 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
528 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
529 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
530 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700531 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700532 }
533 }
534 }
sangho71abe1b2015-06-29 14:58:47 -0700535 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700536 }
537
538 @Override
sangho71abe1b2015-06-29 14:58:47 -0700539 public PolicyHandler.Result removePolicy(Policy policy) {
540 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700541 }
542
543 @Override
sangho71abe1b2015-06-29 14:58:47 -0700544 public PolicyHandler.Result createPolicy(Policy policy) {
545 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700546 }
547
548 @Override
549 public List<Policy> getPolicies() {
550 return policyHandler.getPolicies();
551 }
552
Saurav Das59232cf2016-04-27 18:35:50 -0700553 @Override
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700554 public List<DefaultL2Tunnel> getL2Tunnels() {
555 return l2TunnelHandler.getL2Tunnels();
556 }
557
558 @Override
559 public List<DefaultL2TunnelPolicy> getL2Policies() {
560 return l2TunnelHandler.getL2Policies();
561 }
562
563 @Override
564 public L2TunnelHandler.Result addPseudowire(String tunnelId, String pwLabel, String cP1,
565 String cP1InnerVlan, String cP1OuterVlan, String cP2,
566 String cP2InnerVlan, String cP2OuterVlan,
567 String mode, String sdTag) {
Andreas Pantelopoulos20474e02017-12-20 18:04:27 -0800568 // Try to inject an empty Pwaas config if it is not found for the first time
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700569 PwaasConfig config = cfgService.getConfig(appId(), PwaasConfig.class);
570 if (config == null) {
Andreas Pantelopoulos20474e02017-12-20 18:04:27 -0800571 log.debug("Pwaas config not found. Try to create an empty one.");
572 cfgService.applyConfig(appId(), PwaasConfig.class, new ObjectMapper().createObjectNode());
573 config = cfgService.getConfig(appId(), PwaasConfig.class);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700574 }
575
576 ObjectNode object = config.addPseudowire(tunnelId, pwLabel,
577 cP1, cP1InnerVlan, cP1OuterVlan,
578 cP2, cP2InnerVlan, cP2OuterVlan,
579 mode, sdTag);
580 if (object == null) {
581 log.warn("Could not add pseudowire to the configuration!");
582 return L2TunnelHandler.Result.ADDITION_ERROR;
583 }
584
585 // inform everyone about the valid change in the pw configuration
586 cfgService.applyConfig(appId(), PwaasConfig.class, object);
587 return L2TunnelHandler.Result.SUCCESS;
588 }
589
590 @Override
591 public L2TunnelHandler.Result removePseudowire(String pwId) {
592
593 PwaasConfig config = cfgService.getConfig(appId(), PwaasConfig.class);
594 if (config == null) {
595 log.warn("Configuration for Pwaas class could not be found!");
596 return L2TunnelHandler.Result.CONFIG_NOT_FOUND;
597 }
598
599 ObjectNode object = config.removePseudowire(pwId);
600 if (object == null) {
601 log.warn("Could not delete pseudowire from configuration!");
602 return L2TunnelHandler.Result.REMOVAL_ERROR;
603 }
604
605 // sanity check, this should never fail since we removed a pw
606 // and we always check when we update the configuration
607 config.isValid();
608
609 // inform everyone
610 cfgService.applyConfig(appId(), PwaasConfig.class, object);
611
612 return L2TunnelHandler.Result.SUCCESS;
613 }
614
615 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700616 public void rerouteNetwork() {
617 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700618 }
619
Charles Chanc81c45b2016-10-20 17:02:44 -0700620 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800621 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
622 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800623 deviceConfiguration.getRouters().forEach(device ->
624 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chanc81c45b2016-10-20 17:02:44 -0700625 return deviceSubnetMap;
626 }
627
Saurav Dasc88d4662017-05-15 15:34:25 -0700628
629 @Override
630 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
631 if (defaultRoutingHandler != null) {
632 return defaultRoutingHandler.getCurrentEmcpSpgMap();
633 } else {
634 return null;
635 }
636 }
637
638 @Override
Saurav Das7bcbe702017-06-13 15:35:54 -0700639 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
640 if (dsNextObjStore != null) {
641 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700642 } else {
643 return ImmutableMap.of();
644 }
645 }
646
Saurav Dasceccf242017-08-03 18:30:35 -0700647 @Override
648 public void verifyGroups(DeviceId id) {
649 DefaultGroupHandler gh = groupHandlerMap.get(id);
650 if (gh != null) {
651 gh.triggerBucketCorrector();
652 }
653 }
654
Saurav Dasc568c342018-01-25 09:49:01 -0800655 @Override
656 public ImmutableMap<Link, Boolean> getSeenLinks() {
657 return linkHandler.getSeenLinks();
658 }
659
660 @Override
661 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
662 return linkHandler.getDownedPorts();
663 }
664
sanghof9d0bf12015-05-19 11:57:42 -0700665 /**
Ray Milkeye4afdb52017-04-05 09:42:04 -0700666 * Extracts the application ID from the manager.
667 *
668 * @return application ID
669 */
670 public ApplicationId appId() {
671 return appId;
672 }
673
674 /**
675 * Returns the device configuration.
676 *
677 * @return device configuration
678 */
679 public DeviceConfiguration deviceConfiguration() {
680 return deviceConfiguration;
681 }
682
683 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700684 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700685 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700686 *
687 * @return next objective ID store
688 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700689 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
690 dsNextObjStore() {
691 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700692 }
693
694 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700695 * Per device next objective ID store with (device id + vlanid) as key.
696 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700697 *
698 * @return vlan next object store
699 */
700 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
701 return vlanNextObjStore;
702 }
703
704 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700705 * Per device next objective ID store with (device id + port + treatment + meta) as key.
706 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700707 *
708 * @return port next object store.
709 */
710 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
711 return portNextObjStore;
712 }
713
714 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700715 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
716 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -0700717 *
718 * @return MPLS-ECMP value
719 */
720 public boolean getMplsEcmp() {
721 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
722 .getConfig(this.appId, SegmentRoutingAppConfig.class);
723 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
724 }
725
726 /**
sanghof9d0bf12015-05-19 11:57:42 -0700727 * Returns the tunnel object with the tunnel ID.
728 *
729 * @param tunnelId Tunnel ID
730 * @return Tunnel reference
731 */
sangho1e575652015-05-14 00:39:53 -0700732 public Tunnel getTunnel(String tunnelId) {
733 return tunnelHandler.getTunnel(tunnelId);
734 }
735
Charles Chan7ffd81f2017-02-08 15:52:08 -0800736 // TODO Consider moving these to InterfaceService
sanghob35a6192015-04-01 13:05:26 -0700737 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800738 * Returns untagged VLAN configured on given connect point.
Charles Chan7ffd81f2017-02-08 15:52:08 -0800739 * <p>
740 * Only returns the first match if there are multiple untagged VLAN configured
741 * on the connect point.
sanghob35a6192015-04-01 13:05:26 -0700742 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800743 * @param connectPoint connect point
744 * @return untagged VLAN or null if not configured
sanghob35a6192015-04-01 13:05:26 -0700745 */
Charles Chan65238242017-06-22 18:03:14 -0700746 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800747 return interfaceService.getInterfacesByPort(connectPoint).stream()
748 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
749 .map(Interface::vlanUntagged)
750 .findFirst().orElse(null);
sanghob35a6192015-04-01 13:05:26 -0700751 }
752
sangho1e575652015-05-14 00:39:53 -0700753 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800754 * Returns tagged VLAN configured on given connect point.
755 * <p>
756 * Returns all matches if there are multiple tagged VLAN configured
757 * on the connect point.
758 *
759 * @param connectPoint connect point
760 * @return tagged VLAN or empty set if not configured
761 */
Charles Chan65238242017-06-22 18:03:14 -0700762 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800763 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
764 return interfaces.stream()
765 .map(Interface::vlanTagged)
Charles Chan65238242017-06-22 18:03:14 -0700766 .flatMap(Set::stream)
Charles Chan7ffd81f2017-02-08 15:52:08 -0800767 .collect(Collectors.toSet());
768 }
769
770 /**
771 * Returns native VLAN configured on given connect point.
772 * <p>
773 * Only returns the first match if there are multiple native VLAN configured
774 * on the connect point.
775 *
776 * @param connectPoint connect point
777 * @return native VLAN or null if not configured
778 */
Charles Chan65238242017-06-22 18:03:14 -0700779 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800780 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
781 return interfaces.stream()
782 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
783 .map(Interface::vlanNative)
784 .findFirst()
785 .orElse(null);
786 }
787
788 /**
Charles Chanf9a52702017-06-16 15:19:24 -0700789 * Returns internal VLAN for untagged hosts on given connect point.
790 * <p>
791 * The internal VLAN is either vlan-untagged for an access port,
792 * or vlan-native for a trunk port.
793 *
794 * @param connectPoint connect point
795 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
796 */
Charles Chan65238242017-06-22 18:03:14 -0700797 VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chanf9a52702017-06-16 15:19:24 -0700798 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
799 VlanId nativeVlanId = getNativeVlanId(connectPoint);
800 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
801 }
802
803 /**
Charles Chan65238242017-06-22 18:03:14 -0700804 * Returns optional pair device ID of given device.
805 *
806 * @param deviceId device ID
807 * @return optional pair device ID. Might be empty if pair device is not configured
808 */
809 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
810 SegmentRoutingDeviceConfig deviceConfig =
811 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
812 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
813 }
814 /**
815 * Returns optional pair device local port of given device.
816 *
817 * @param deviceId device ID
818 * @return optional pair device ID. Might be empty if pair device is not configured
819 */
820 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
821 SegmentRoutingDeviceConfig deviceConfig =
822 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
823 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
824 }
825
826 /**
Charles Chan2fde6d42017-08-23 14:46:43 -0700827 * Determine if current instance is the master of given connect point.
828 *
829 * @param cp connect point
830 * @return true if current instance is the master of given connect point
831 */
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700832 public boolean isMasterOf(ConnectPoint cp) {
Charles Chan2fde6d42017-08-23 14:46:43 -0700833 boolean isMaster = mastershipService.isLocalMaster(cp.deviceId());
834 if (!isMaster) {
835 log.debug(NOT_MASTER, cp);
836 }
837 return isMaster;
838 }
839
840 /**
Charles Chan9640c812017-08-23 13:55:39 -0700841 * Returns locations of given resolved route.
842 *
843 * @param resolvedRoute resolved route
844 * @return locations of nexthop. Might be empty if next hop is not found
845 */
846 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
847 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
848 return Optional.ofNullable(hostService.getHost(hostId))
849 .map(Host::locations).orElse(Sets.newHashSet())
850 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
851 }
852
853 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800854 * Returns vlan port map of given device.
855 *
856 * @param deviceId device id
857 * @return vlan-port multimap
858 */
859 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
860 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
861
862 interfaceService.getInterfaces().stream()
863 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
864 .forEach(intf -> {
865 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -0700866 intf.vlanTagged().forEach(vlanTagged ->
867 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
868 );
Charles Chan7ffd81f2017-02-08 15:52:08 -0800869 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
870 });
871 vlanPortMap.removeAll(VlanId.NONE);
872
873 return vlanPortMap;
874 }
875
876 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800877 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -0800878 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700879 *
880 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -0800881 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -0800882 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700883 */
Charles Chan65238242017-06-22 18:03:14 -0700884 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -0700885 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800886 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
887 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700888 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -0800889 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -0800890 + "device {} not found", deviceId);
891 return -1;
892 }
893 }
894
895 /**
896 * Returns the next objective ID for the given portNumber, given the treatment.
897 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -0700898 * would result in different objectives. If the next object does not exist,
899 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -0800900 *
901 * @param deviceId Device ID
902 * @param portNum port number on device for which NextObjective is queried
903 * @param treatment the actions to apply on the packets (should include outport)
904 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -0700905 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -0700906 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800907 */
908 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
909 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700910 TrafficSelector meta,
911 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -0800912 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
913 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -0700914 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -0800915 } else {
Charles Chane849c192016-01-11 18:28:54 -0800916 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
917 + " not found", deviceId);
918 return -1;
919 }
920 }
921
Saurav Dasc88d4662017-05-15 15:34:25 -0700922 /**
923 * Returns the group handler object for the specified device id.
924 *
925 * @param devId the device identifier
926 * @return the groupHandler object for the device id, or null if not found
927 */
Charles Chan65238242017-06-22 18:03:14 -0700928 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700929 return groupHandlerMap.get(devId);
930 }
931
932 /**
Saurav Dasceccf242017-08-03 18:30:35 -0700933 * Returns the default routing handler object.
934 *
935 * @return the default routing handler object
936 */
937 public DefaultRoutingHandler getRoutingHandler() {
938 return defaultRoutingHandler;
939 }
940
sanghob35a6192015-04-01 13:05:26 -0700941 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700942 @Override
943 public void process(PacketContext context) {
944
945 if (context.isHandled()) {
946 return;
947 }
948
949 InboundPacket pkt = context.inPacket();
950 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -0800951
952 if (ethernet == null) {
953 return;
954 }
955
Saurav Das7bcbe702017-06-13 15:35:54 -0700956 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
957 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800958 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700959 log.warn("Received unexpected ARP packet on {}",
960 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -0700961 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -0800962 return;
sanghob35a6192015-04-01 13:05:26 -0700963 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800964 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
965 //ipHandler.addToPacketBuffer(ipv4Packet);
966 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
967 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700968 } else {
Charles Chan50035632017-01-13 17:20:44 -0800969 // NOTE: We don't support IP learning at this moment so this
970 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -0800971 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700972 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800973 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
974 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -0800975 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -0800976 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -0800977 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
978 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
979 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
980 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
981 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
982 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -0700983 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -0700984 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -0800985 }
986 } else {
987 // NOTE: We don't support IP learning at this moment so this
988 // is not necessary. Also it causes duplication of DHCPv6 packets.
989 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
990 }
sanghob35a6192015-04-01 13:05:26 -0700991 }
992 }
993 }
994
995 private class InternalLinkListener implements LinkListener {
996 @Override
997 public void event(LinkEvent event) {
Charles Chanb1f8c762017-03-29 16:39:05 -0700998 if (event.type() == LinkEvent.Type.LINK_ADDED ||
999 event.type() == LinkEvent.Type.LINK_UPDATED ||
1000 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001001 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -07001002 scheduleEventHandlerIfNotScheduled(event);
1003 }
1004 }
1005 }
1006
1007 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -07001008 @Override
1009 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -07001010 switch (event.type()) {
1011 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -08001012 case PORT_UPDATED:
1013 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -07001014 case DEVICE_UPDATED:
1015 case DEVICE_AVAILABILITY_CHANGED:
Saurav Dasc88d4662017-05-15 15:34:25 -07001016 log.trace("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -07001017 scheduleEventHandlerIfNotScheduled(event);
1018 break;
1019 default:
1020 }
1021 }
1022 }
1023
Saurav Das4ce45962015-11-24 23:21:05 -08001024 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -07001025 private void scheduleEventHandlerIfNotScheduled(Event event) {
Charles Chan65238242017-06-22 18:03:14 -07001026 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001027 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001028 numOfEventsQueued++;
1029
1030 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
1031 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001032 eventHandlerFuture = executorService
1033 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
1034 numOfHandlerScheduled++;
1035 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -07001036 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001037 numOfEventsQueued,
1038 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -07001039 }
sanghob35a6192015-04-01 13:05:26 -07001040 }
1041
1042 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -07001043 @Override
sanghob35a6192015-04-01 13:05:26 -07001044 public void run() {
Pier Luigieefa2762018-02-12 09:56:20 +01001045 while (true) {
1046 try {
Saurav Das4ce45962015-11-24 23:21:05 -08001047 @SuppressWarnings("rawtypes")
Charles Chan65238242017-06-22 18:03:14 -07001048 Event event;
1049 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001050 if (!eventQueue.isEmpty()) {
1051 event = eventQueue.poll();
1052 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001053 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001054 numOfHandlerExecution++;
1055 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
1056 numOfHandlerExecution, numOfEventsExecuted);
1057 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001058 }
sangho20eff1d2015-04-13 15:15:58 -07001059 }
Charles Chanb1f8c762017-03-29 16:39:05 -07001060 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1061 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Das45f48152018-01-18 12:07:33 -08001062 linkHandler.processLinkAdded((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001063 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Saurav Das45f48152018-01-18 12:07:33 -08001064 linkHandler.processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001065 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1066 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1067 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001068 DeviceId deviceId = ((Device) event.subject()).id();
1069 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001070 log.info("Processing device event {} for available device {}",
1071 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001072 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -07001073 } else {
1074 log.info("Processing device event {} for unavailable device {}",
Pier Luigieefa2762018-02-12 09:56:20 +01001075 event.type(), ((Device) event.subject()).id());
Saurav Das80980c72016-03-23 11:22:49 -07001076 processDeviceRemoved((Device) event.subject());
1077 }
Saurav Das1a129a02016-11-18 15:21:57 -08001078 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001079 // typically these calls come when device is added first time
1080 // so port filtering rules are handled at the device_added event.
1081 // port added calls represent all ports on the device,
1082 // enabled or not.
Saurav Dasc88d4662017-05-15 15:34:25 -07001083 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001084 ((DeviceEvent) event).subject().id(),
1085 ((DeviceEvent) event).port().number(),
1086 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -08001087 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001088 // these calls happen for every subsequent event
1089 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -08001090 log.info("** PORT UPDATED {}/{} -> {}",
1091 event.subject(),
1092 ((DeviceEvent) event).port(),
1093 event.type());
1094 processPortUpdated(((Device) event.subject()),
1095 ((DeviceEvent) event).port());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001096 } else {
1097 log.warn("Unhandled event type: {}", event.type());
1098 }
Pier Luigieefa2762018-02-12 09:56:20 +01001099 } catch (Exception e) {
1100 log.error("SegmentRouting event handler thread thrown an exception: {}",
1101 e.getMessage(), e);
sanghob35a6192015-04-01 13:05:26 -07001102 }
1103 }
sanghob35a6192015-04-01 13:05:26 -07001104 }
1105 }
1106
Saurav Das45f48152018-01-18 12:07:33 -08001107 void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001108 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001109
1110 // NOTE: Punt ARP/NDP even when the device is not configured.
1111 // Host learning without network config is required for CORD config generator.
1112 routingRulePopulator.populateIpPunts(device.id());
1113 routingRulePopulator.populateArpNdpPunts(device.id());
1114
Charles Chan0b4e6182015-11-03 10:42:14 -08001115 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001116 log.warn("Device configuration unavailable. Device {} will be "
1117 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001118 return;
1119 }
Charles Chan2199c302016-04-23 17:36:10 -07001120 processDeviceAddedInternal(device.id());
1121 }
1122
1123 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001124 // Irrespective of whether the local is a MASTER or not for this device,
1125 // we need to create a SR-group-handler instance. This is because in a
1126 // multi-instance setup, any instance can initiate forwarding/next-objectives
1127 // for any switch (even if this instance is a SLAVE or not even connected
1128 // to the switch). To handle this, a default-group-handler instance is necessary
1129 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001130 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1131 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001132 DefaultGroupHandler groupHandler;
1133 try {
1134 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001135 createGroupHandler(deviceId,
1136 appId,
1137 deviceConfiguration,
1138 linkService,
1139 flowObjectiveService,
1140 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001141 } catch (DeviceConfigNotFoundException e) {
1142 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1143 return;
1144 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001145 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001146 deviceId);
1147 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001148 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001149
Charles Chan2199c302016-04-23 17:36:10 -07001150 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001151 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan03a73e02016-10-24 14:52:01 -07001152 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001153 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001154 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001155 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001156 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001157 }
Charles Chan5270ed02016-01-30 23:22:37 -08001158
Charles Chan03a73e02016-10-24 14:52:01 -07001159 appCfgHandler.init(deviceId);
1160 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001161 }
1162
Saurav Das80980c72016-03-23 11:22:49 -07001163 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001164 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001165 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan47933752017-11-30 15:37:50 -08001166 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001167 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001168 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001169 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001170 portNextObjStore.entrySet().stream()
1171 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001172 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Das45f48152018-01-18 12:07:33 -08001173 linkHandler.processDeviceRemoved(device);
Charles Chaned3742352017-06-15 00:44:51 -07001174
Saurav Dasceccf242017-08-03 18:30:35 -07001175 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1176 if (gh != null) {
1177 gh.shutdown();
1178 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001179 // Note that a switch going down is associated with all of its links
1180 // going down as well, but it is treated as a single switch down event
1181 // while the link-downs are ignored.
1182 defaultRoutingHandler
1183 .populateRoutingRulesForLinkStatusChange(null, null, device.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001184 defaultRoutingHandler.purgeEcmpGraph(device.id());
Pier Luigieba73a02018-01-16 10:47:50 +01001185 mcastHandler.processDeviceDown(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001186 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -07001187 }
1188
Saurav Das1a129a02016-11-18 15:21:57 -08001189 private void processPortUpdated(Device device, Port port) {
1190 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1191 log.warn("Device configuration uploading. Not handling port event for"
1192 + "dev: {} port: {}", device.id(), port.number());
1193 return;
1194 }
Saurav Das018605f2017-02-18 14:05:44 -08001195
1196 if (!mastershipService.isLocalMaster(device.id())) {
1197 log.debug("Not master for dev:{} .. not handling port updated event"
1198 + "for port {}", device.id(), port.number());
1199 return;
1200 }
1201
1202 // first we handle filtering rules associated with the port
1203 if (port.isEnabled()) {
1204 log.info("Switchport {}/{} enabled..programming filters",
1205 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001206 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001207 } else {
1208 log.info("Switchport {}/{} disabled..removing filters",
1209 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001210 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001211 }
Saurav Das1a129a02016-11-18 15:21:57 -08001212
1213 // portUpdated calls are for ports that have gone down or up. For switch
1214 // to switch ports, link-events should take care of any re-routing or
1215 // group editing necessary for port up/down. Here we only process edge ports
1216 // that are already configured.
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001217 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1218 VlanId untaggedVlan = getUntaggedVlanId(cp);
1219 VlanId nativeVlan = getNativeVlanId(cp);
1220 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001221
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001222 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001223 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das1a129a02016-11-18 15:21:57 -08001224 + "dev/port: {}/{}", device.id(), port.number());
1225 return;
1226 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001227 if (untaggedVlan != null) {
1228 processEdgePort(device, port, untaggedVlan, true);
1229 }
1230 if (nativeVlan != null) {
1231 processEdgePort(device, port, nativeVlan, true);
1232 }
1233 if (!taggedVlans.isEmpty()) {
1234 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1235 }
Saurav Das1a129a02016-11-18 15:21:57 -08001236 }
1237
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001238 private void processEdgePort(Device device, Port port, VlanId vlanId,
1239 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001240 boolean portUp = port.isEnabled();
1241 if (portUp) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001242 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001243 port.number(), vlanId);
Charles Chan47933752017-11-30 15:37:50 -08001244 hostHandler.processPortUp(new ConnectPoint(device.id(), port.number()));
Saurav Das1a129a02016-11-18 15:21:57 -08001245 } else {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001246 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001247 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001248 }
1249
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001250 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001251 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001252 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001253 } else {
1254 log.warn("Group handler not found for dev:{}. Not handling edge port"
1255 + " {} event for port:{}", device.id(),
1256 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001257 }
1258 }
sangho1e575652015-05-14 00:39:53 -07001259
Charles Chan27fe1a52017-10-20 16:06:55 -07001260 private void createOrUpdateDeviceConfiguration() {
1261 if (deviceConfiguration == null) {
Saurav Dase7f51012018-02-09 17:26:45 -08001262 log.info("Creating new DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001263 deviceConfiguration = new DeviceConfiguration(this);
1264 } else {
Saurav Dase7f51012018-02-09 17:26:45 -08001265 log.info("Updating DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001266 deviceConfiguration.updateConfig();
1267 }
1268 }
1269
Pier Ventre10bd8d12016-11-26 21:05:22 -08001270 /**
1271 * Registers the given connect point with the NRS, this is necessary
1272 * to receive the NDP and ARP packets from the NRS.
1273 *
1274 * @param portToRegister connect point to register
1275 */
1276 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001277 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001278 portToRegister,
1279 neighbourHandler,
1280 appId
1281 );
1282 }
1283
Charles Chand6832882015-10-05 17:50:33 -07001284 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001285 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001286 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001287
Charles Chane849c192016-01-11 18:28:54 -08001288 /**
1289 * Constructs the internal network config listener.
1290 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001291 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001292 */
Charles Chan65238242017-06-22 18:03:14 -07001293 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001294 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001295 }
1296
Charles Chane849c192016-01-11 18:28:54 -08001297 /**
1298 * Reads network config and initializes related data structure accordingly.
1299 */
Charles Chan47933752017-11-30 15:37:50 -08001300 void configureNetwork() {
Saurav Dase7f51012018-02-09 17:26:45 -08001301 log.info("Configuring network ...");
Charles Chan27fe1a52017-10-20 16:06:55 -07001302 createOrUpdateDeviceConfiguration();
Charles Chan4636be02015-10-07 14:21:45 -07001303
Charles Chan2c15aca2016-11-09 20:51:44 -08001304 arpHandler = new ArpHandler(srManager);
1305 icmpHandler = new IcmpHandler(srManager);
1306 ipHandler = new IpHandler(srManager);
1307 routingRulePopulator = new RoutingRulePopulator(srManager);
1308 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001309
1310 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1311 groupHandlerMap, tunnelStore);
1312 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1313 flowObjectiveService,
1314 tunnelHandler, policyStore);
Saurav Das7bcbe702017-06-13 15:35:54 -07001315 // add a small delay to absorb multiple network config added notifications
1316 if (!programmingScheduled.get()) {
Saurav Dase7f51012018-02-09 17:26:45 -08001317 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
Saurav Das7bcbe702017-06-13 15:35:54 -07001318 programmingScheduled.set(true);
1319 executorService.schedule(new ConfigChange(), PROGRAM_DELAY,
1320 TimeUnit.SECONDS);
Charles Chan4636be02015-10-07 14:21:45 -07001321 }
Charles Chan2199c302016-04-23 17:36:10 -07001322 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001323 }
1324
Charles Chand6832882015-10-05 17:50:33 -07001325 @Override
1326 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001327 // TODO move this part to NetworkConfigEventHandler
1328 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1329 switch (event.type()) {
1330 case CONFIG_ADDED:
Charles Chan278ce832017-06-26 15:25:09 -07001331 log.info("Segment Routing Device Config added for {}", event.subject());
Charles Chan5270ed02016-01-30 23:22:37 -08001332 configureNetwork();
1333 break;
1334 case CONFIG_UPDATED:
Charles Chan278ce832017-06-26 15:25:09 -07001335 log.info("Segment Routing Config updated for {}", event.subject());
Charles Chan27fe1a52017-10-20 16:06:55 -07001336 createOrUpdateDeviceConfiguration();
Charles Chan278ce832017-06-26 15:25:09 -07001337 // TODO support dynamic configuration
1338 break;
1339 default:
1340 break;
1341 }
1342 } else if (event.configClass().equals(InterfaceConfig.class)) {
1343 switch (event.type()) {
1344 case CONFIG_ADDED:
1345 log.info("Interface Config added for {}", event.subject());
1346 configureNetwork();
1347 break;
1348 case CONFIG_UPDATED:
1349 log.info("Interface Config updated for {}", event.subject());
Charles Chan27fe1a52017-10-20 16:06:55 -07001350 createOrUpdateDeviceConfiguration();
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001351
1352 // Following code will be uncommented when [CORD-634] is fully implemented.
1353 // [CORD-634] Add dynamic config support for interfaces
1354 updateInterface((InterfaceConfig) event.config().get(),
1355 (InterfaceConfig) event.prevConfig().get());
Charles Chan5270ed02016-01-30 23:22:37 -08001356 // TODO support dynamic configuration
1357 break;
1358 default:
1359 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001360 }
Charles Chan5270ed02016-01-30 23:22:37 -08001361 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001362 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001363 switch (event.type()) {
1364 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001365 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001366 break;
1367 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001368 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001369 break;
1370 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001371 appCfgHandler.processAppConfigRemoved(event);
1372 break;
1373 default:
1374 break;
1375 }
Saurav Dase7f51012018-02-09 17:26:45 -08001376 log.info("App config event .. configuring network");
Charles Chan03a73e02016-10-24 14:52:01 -07001377 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001378 } else if (event.configClass().equals(XConnectConfig.class)) {
1379 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1380 switch (event.type()) {
1381 case CONFIG_ADDED:
1382 xConnectHandler.processXConnectConfigAdded(event);
1383 break;
1384 case CONFIG_UPDATED:
1385 xConnectHandler.processXConnectConfigUpdated(event);
1386 break;
1387 case CONFIG_REMOVED:
1388 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001389 break;
1390 default:
1391 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001392 }
Pier Ventref34966c2016-11-07 16:21:04 -08001393 } else if (event.configClass().equals(PwaasConfig.class)) {
1394 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1395 switch (event.type()) {
1396 case CONFIG_ADDED:
1397 l2TunnelHandler.processPwaasConfigAdded(event);
1398 break;
1399 case CONFIG_UPDATED:
1400 l2TunnelHandler.processPwaasConfigUpdated(event);
1401 break;
1402 case CONFIG_REMOVED:
1403 l2TunnelHandler.processPwaasConfigRemoved(event);
1404 break;
1405 default:
1406 break;
1407 }
Charles Chand6832882015-10-05 17:50:33 -07001408 }
1409 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001410
Charles Chan50443e82018-01-03 16:26:32 -08001411 @Override
1412 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase7f51012018-02-09 17:26:45 -08001413 if (event.type() == CONFIG_REGISTERED ||
1414 event.type() == CONFIG_UNREGISTERED) {
1415 log.debug("Ignore event {} due to type mismatch", event);
1416 return false;
Charles Chan50443e82018-01-03 16:26:32 -08001417 }
Saurav Dase7f51012018-02-09 17:26:45 -08001418
1419 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1420 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1421 !event.configClass().equals(InterfaceConfig.class) &&
1422 !event.configClass().equals(XConnectConfig.class) &&
1423 !event.configClass().equals(PwaasConfig.class)) {
1424 log.debug("Ignore event {} due to class mismatch", event);
1425 return false;
1426 }
1427
1428 return true;
Charles Chan50443e82018-01-03 16:26:32 -08001429 }
1430
Saurav Das7bcbe702017-06-13 15:35:54 -07001431 private final class ConfigChange implements Runnable {
1432 @Override
1433 public void run() {
1434 programmingScheduled.set(false);
Saurav Dase7f51012018-02-09 17:26:45 -08001435 log.info("Reacting to config changes after buffer delay");
Saurav Das7bcbe702017-06-13 15:35:54 -07001436 for (Device device : deviceService.getDevices()) {
1437 processDeviceAdded(device);
1438 }
1439 defaultRoutingHandler.startPopulationProcess();
1440 }
1441 }
Charles Chand6832882015-10-05 17:50:33 -07001442 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001443
1444 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001445 @Override
1446 public void event(HostEvent event) {
Charles Chan68aa62d2015-11-09 16:37:23 -08001447 switch (event.type()) {
1448 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001449 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001450 break;
1451 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001452 hostHandler.processHostMovedEvent(event);
Charles Chan2fde6d42017-08-23 14:46:43 -07001453 routeHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001454 break;
1455 case HOST_REMOVED:
Charles Chanf9a52702017-06-16 15:19:24 -07001456 hostHandler.processHostRemovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001457 break;
1458 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001459 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001460 break;
1461 default:
1462 log.warn("Unsupported host event type: {}", event.type());
1463 break;
1464 }
1465 }
1466 }
1467
Charles Chand55e84d2016-03-30 17:54:24 -07001468 private class InternalMcastListener implements McastListener {
1469 @Override
1470 public void event(McastEvent event) {
1471 switch (event.type()) {
1472 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001473 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001474 break;
1475 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001476 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001477 break;
1478 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001479 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001480 break;
Charles Chand55e84d2016-03-30 17:54:24 -07001481 case ROUTE_REMOVED:
Pier Luigi9930da52018-02-02 16:19:11 +01001482 mcastHandler.processRouteRemoved(event);
1483 break;
1484 case ROUTE_ADDED:
Charles Chand55e84d2016-03-30 17:54:24 -07001485 default:
1486 break;
1487 }
1488 }
1489 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001490
Charles Chan03a73e02016-10-24 14:52:01 -07001491 private class InternalRouteEventListener implements RouteListener {
1492 @Override
1493 public void event(RouteEvent event) {
1494 switch (event.type()) {
1495 case ROUTE_ADDED:
1496 routeHandler.processRouteAdded(event);
1497 break;
1498 case ROUTE_UPDATED:
1499 routeHandler.processRouteUpdated(event);
1500 break;
1501 case ROUTE_REMOVED:
1502 routeHandler.processRouteRemoved(event);
1503 break;
Charles Chan2fde6d42017-08-23 14:46:43 -07001504 case ALTERNATIVE_ROUTES_CHANGED:
1505 routeHandler.processAlternativeRoutesChanged(event);
1506 break;
Charles Chan03a73e02016-10-24 14:52:01 -07001507 default:
1508 break;
1509 }
1510 }
1511 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001512
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001513 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1514 try {
1515 Set<Interface> intfs = conf.getInterfaces();
1516 Set<Interface> prevIntfs = prevConf.getInterfaces();
1517
1518 // Now we only handle one interface config at each port.
1519 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1520 log.warn("Interface update aborted - one at a time is allowed, " +
1521 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1522 return;
1523 }
1524
1525 Interface intf = intfs.stream().findFirst().get();
1526 Interface prevIntf = prevIntfs.stream().findFirst().get();
1527
1528 DeviceId deviceId = intf.connectPoint().deviceId();
1529 PortNumber portNum = intf.connectPoint().port();
1530
1531 if (!mastershipService.isLocalMaster(deviceId)) {
1532 log.debug("CONFIG_UPDATED event for interfaces should be " +
1533 "handled by master node for device {}", deviceId);
1534 return;
1535 }
1536
1537 removeSubnetConfig(prevIntf.connectPoint(),
1538 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1539 new HashSet<>(intf.ipAddressesList())));
1540
1541 if (prevIntf.vlanNative() != VlanId.NONE && !intf.vlanNative().equals(prevIntf.vlanNative())) {
1542 // RemoveVlanNative
1543 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1544 }
1545
1546 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1547 // RemoveVlanTagged
1548 prevIntf.vlanTagged().stream().filter(i -> !intf.vlanTagged().contains(i)).forEach(
1549 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, false)
1550 );
1551 }
1552
1553 if (prevIntf.vlanUntagged() != VlanId.NONE && !intf.vlanUntagged().equals(prevIntf.vlanUntagged())) {
1554 // RemoveVlanUntagged
1555 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1556 }
1557
1558 if (intf.vlanNative() != VlanId.NONE && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1559 // AddVlanNative
1560 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1561 }
1562
1563 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1564 // AddVlanTagged
1565 intf.vlanTagged().stream().filter(i -> !prevIntf.vlanTagged().contains(i)).forEach(
1566 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, true)
1567 );
1568 }
1569
1570 if (intf.vlanUntagged() != VlanId.NONE && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())) {
1571 // AddVlanUntagged
1572 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1573 }
1574 addSubnetConfig(prevIntf.connectPoint(),
1575 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1576 new HashSet<>(prevIntf.ipAddressesList())));
1577 } catch (ConfigException e) {
1578 log.error("Error in configuration");
1579 }
1580 }
1581
1582 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1583 VlanId vlanId, boolean pushVlan, boolean install) {
1584 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1585 if (grpHandler == null) {
1586 log.warn("Failed to retrieve group handler for device {}", deviceId);
1587 return;
1588 }
1589
1590 // Update filtering objective for a single port
1591 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1592
1593 // Update filtering objective for multicast ingress port
1594 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1595
1596 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1597
1598 if (nextId != -1 && !install) {
1599 // Update next objective for a single port as an output port
1600 // Remove a single port from L2FG
1601 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1602 // Remove L2 Bridging rule and L3 Unicast rule to the host
1603 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1604 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1605 // only if there is no port configured on that VLAN ID
1606 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1607 // Remove broadcast forwarding rule for the VLAN
1608 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1609 // Remove L2FG for VLAN
1610 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1611 } else {
1612 // Remove L2IG of the port
1613 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1614 }
1615 } else if (install) {
1616 if (nextId != -1) {
1617 // Add a single port to L2FG
1618 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1619 } else {
1620 // Create L2FG for VLAN
1621 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1622 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1623 }
1624 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1625 } else {
1626 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1627 }
1628 }
1629
1630 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1631 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1632 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1633
1634 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1635 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1636 .filter(intf -> !intf.connectPoint().equals(cp))
1637 .flatMap(intf -> intf.ipAddressesList().stream())
1638 .collect(Collectors.toSet());
1639 // 1. Partial subnet population
1640 // Remove routing rules for removed subnet from previous configuration,
1641 // which does not also exist in other interfaces in the same device
1642 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1643 .map(InterfaceIpAddress::subnetAddress)
1644 .collect(Collectors.toSet());
1645
1646 defaultRoutingHandler.revokeSubnet(
1647 ipPrefixSet.stream()
1648 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1649 .collect(Collectors.toSet()));
1650
1651 // 2. Interface IP punts
1652 // Remove IP punts for old Intf address
1653 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1654 .map(InterfaceIpAddress::ipAddress)
1655 .collect(Collectors.toSet());
1656 ipAddressSet.stream()
1657 .map(InterfaceIpAddress::ipAddress)
1658 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1659 .forEach(interfaceIpAddress ->
1660 routingRulePopulator.revokeSingleIpPunts(
1661 cp.deviceId(), interfaceIpAddress));
1662
1663 // 3. Host unicast routing rule
1664 // Remove unicast routing rule
1665 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1666 }
1667
1668 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1669 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1670 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1671
1672 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1673 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1674 .filter(intf -> !intf.connectPoint().equals(cp))
1675 .flatMap(intf -> intf.ipAddressesList().stream())
1676 .collect(Collectors.toSet());
1677 // 1. Partial subnet population
1678 // Add routing rules for newly added subnet, which does not also exist in
1679 // other interfaces in the same device
1680 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1681 .map(InterfaceIpAddress::subnetAddress)
1682 .collect(Collectors.toSet());
1683
1684 defaultRoutingHandler.populateSubnet(
1685 Collections.singleton(cp),
1686 ipPrefixSet.stream()
1687 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1688 .collect(Collectors.toSet()));
1689
1690 // 2. Interface IP punts
1691 // Add IP punts for new Intf address
1692 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1693 .map(InterfaceIpAddress::ipAddress)
1694 .collect(Collectors.toSet());
1695 ipAddressSet.stream()
1696 .map(InterfaceIpAddress::ipAddress)
1697 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1698 .forEach(interfaceIpAddress ->
1699 routingRulePopulator.populateSingleIpPunts(
1700 cp.deviceId(), interfaceIpAddress));
1701
1702 // 3. Host unicast routing rule
1703 // Add unicast routing rule
1704 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1705 }
sanghob35a6192015-04-01 13:05:26 -07001706}