blob: f661addce05a7e7a46bdfe385d6e57fae1385c24 [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();
sanghob35a6192015-04-01 13:05:26 -0700493 log.info("Stopped");
494 }
495
Charles Chan47933752017-11-30 15:37:50 -0800496 @Modified
497 private void modified(ComponentContext context) {
498 Dictionary<?, ?> properties = context.getProperties();
499 if (properties == null) {
500 return;
501 }
502
503 String strActiveProving = Tools.get(properties, "activeProbing");
504 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
505
506 if (expectActiveProbing != activeProbing) {
507 activeProbing = expectActiveProbing;
508 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
509 }
510 }
511
sangho1e575652015-05-14 00:39:53 -0700512 @Override
513 public List<Tunnel> getTunnels() {
514 return tunnelHandler.getTunnels();
515 }
516
517 @Override
sangho71abe1b2015-06-29 14:58:47 -0700518 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
519 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700520 }
521
522 @Override
sangho71abe1b2015-06-29 14:58:47 -0700523 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700524 for (Policy policy: policyHandler.getPolicies()) {
525 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
526 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
527 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
528 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700529 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700530 }
531 }
532 }
sangho71abe1b2015-06-29 14:58:47 -0700533 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700534 }
535
536 @Override
sangho71abe1b2015-06-29 14:58:47 -0700537 public PolicyHandler.Result removePolicy(Policy policy) {
538 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700539 }
540
541 @Override
sangho71abe1b2015-06-29 14:58:47 -0700542 public PolicyHandler.Result createPolicy(Policy policy) {
543 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700544 }
545
546 @Override
547 public List<Policy> getPolicies() {
548 return policyHandler.getPolicies();
549 }
550
Saurav Das59232cf2016-04-27 18:35:50 -0700551 @Override
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700552 public List<DefaultL2Tunnel> getL2Tunnels() {
553 return l2TunnelHandler.getL2Tunnels();
554 }
555
556 @Override
557 public List<DefaultL2TunnelPolicy> getL2Policies() {
558 return l2TunnelHandler.getL2Policies();
559 }
560
561 @Override
562 public L2TunnelHandler.Result addPseudowire(String tunnelId, String pwLabel, String cP1,
563 String cP1InnerVlan, String cP1OuterVlan, String cP2,
564 String cP2InnerVlan, String cP2OuterVlan,
565 String mode, String sdTag) {
Andreas Pantelopoulos20474e02017-12-20 18:04:27 -0800566 // Try to inject an empty Pwaas config if it is not found for the first time
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700567 PwaasConfig config = cfgService.getConfig(appId(), PwaasConfig.class);
568 if (config == null) {
Andreas Pantelopoulos20474e02017-12-20 18:04:27 -0800569 log.debug("Pwaas config not found. Try to create an empty one.");
570 cfgService.applyConfig(appId(), PwaasConfig.class, new ObjectMapper().createObjectNode());
571 config = cfgService.getConfig(appId(), PwaasConfig.class);
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700572 }
573
574 ObjectNode object = config.addPseudowire(tunnelId, pwLabel,
575 cP1, cP1InnerVlan, cP1OuterVlan,
576 cP2, cP2InnerVlan, cP2OuterVlan,
577 mode, sdTag);
578 if (object == null) {
579 log.warn("Could not add pseudowire to the configuration!");
580 return L2TunnelHandler.Result.ADDITION_ERROR;
581 }
582
583 // inform everyone about the valid change in the pw configuration
584 cfgService.applyConfig(appId(), PwaasConfig.class, object);
585 return L2TunnelHandler.Result.SUCCESS;
586 }
587
588 @Override
589 public L2TunnelHandler.Result removePseudowire(String pwId) {
590
591 PwaasConfig config = cfgService.getConfig(appId(), PwaasConfig.class);
592 if (config == null) {
593 log.warn("Configuration for Pwaas class could not be found!");
594 return L2TunnelHandler.Result.CONFIG_NOT_FOUND;
595 }
596
597 ObjectNode object = config.removePseudowire(pwId);
598 if (object == null) {
599 log.warn("Could not delete pseudowire from configuration!");
600 return L2TunnelHandler.Result.REMOVAL_ERROR;
601 }
602
603 // sanity check, this should never fail since we removed a pw
604 // and we always check when we update the configuration
605 config.isValid();
606
607 // inform everyone
608 cfgService.applyConfig(appId(), PwaasConfig.class, object);
609
610 return L2TunnelHandler.Result.SUCCESS;
611 }
612
613 @Override
Saurav Das59232cf2016-04-27 18:35:50 -0700614 public void rerouteNetwork() {
615 cfgListener.configureNetwork();
Saurav Das59232cf2016-04-27 18:35:50 -0700616 }
617
Charles Chanc81c45b2016-10-20 17:02:44 -0700618 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800619 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
620 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart8ca2bc02017-11-30 18:23:42 -0800621 deviceConfiguration.getRouters().forEach(device ->
622 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chanc81c45b2016-10-20 17:02:44 -0700623 return deviceSubnetMap;
624 }
625
Saurav Dasc88d4662017-05-15 15:34:25 -0700626
627 @Override
628 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
629 if (defaultRoutingHandler != null) {
630 return defaultRoutingHandler.getCurrentEmcpSpgMap();
631 } else {
632 return null;
633 }
634 }
635
636 @Override
Saurav Das7bcbe702017-06-13 15:35:54 -0700637 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
638 if (dsNextObjStore != null) {
639 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Dasc88d4662017-05-15 15:34:25 -0700640 } else {
641 return ImmutableMap.of();
642 }
643 }
644
Saurav Dasceccf242017-08-03 18:30:35 -0700645 @Override
646 public void verifyGroups(DeviceId id) {
647 DefaultGroupHandler gh = groupHandlerMap.get(id);
648 if (gh != null) {
649 gh.triggerBucketCorrector();
650 }
651 }
652
Saurav Dasc568c342018-01-25 09:49:01 -0800653 @Override
654 public ImmutableMap<Link, Boolean> getSeenLinks() {
655 return linkHandler.getSeenLinks();
656 }
657
658 @Override
659 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
660 return linkHandler.getDownedPorts();
661 }
662
sanghof9d0bf12015-05-19 11:57:42 -0700663 /**
Ray Milkeye4afdb52017-04-05 09:42:04 -0700664 * Extracts the application ID from the manager.
665 *
666 * @return application ID
667 */
668 public ApplicationId appId() {
669 return appId;
670 }
671
672 /**
673 * Returns the device configuration.
674 *
675 * @return device configuration
676 */
677 public DeviceConfiguration deviceConfiguration() {
678 return deviceConfiguration;
679 }
680
681 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700682 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan5bc32632017-08-22 15:07:34 -0700683 * Used to keep track on MPLS group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700684 *
685 * @return next objective ID store
686 */
Saurav Das7bcbe702017-06-13 15:35:54 -0700687 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
688 dsNextObjStore() {
689 return dsNextObjStore;
Ray Milkeye4afdb52017-04-05 09:42:04 -0700690 }
691
692 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700693 * Per device next objective ID store with (device id + vlanid) as key.
694 * Used to keep track on L2 flood group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700695 *
696 * @return vlan next object store
697 */
698 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
699 return vlanNextObjStore;
700 }
701
702 /**
Charles Chan5bc32632017-08-22 15:07:34 -0700703 * Per device next objective ID store with (device id + port + treatment + meta) as key.
704 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeye4afdb52017-04-05 09:42:04 -0700705 *
706 * @return port next object store.
707 */
708 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
709 return portNextObjStore;
710 }
711
712 /**
Saurav Das7bcbe702017-06-13 15:35:54 -0700713 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
714 * labeled packets should be programmed or not.
Pier Ventre98161782016-10-31 15:00:01 -0700715 *
716 * @return MPLS-ECMP value
717 */
718 public boolean getMplsEcmp() {
719 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
720 .getConfig(this.appId, SegmentRoutingAppConfig.class);
721 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
722 }
723
724 /**
sanghof9d0bf12015-05-19 11:57:42 -0700725 * Returns the tunnel object with the tunnel ID.
726 *
727 * @param tunnelId Tunnel ID
728 * @return Tunnel reference
729 */
sangho1e575652015-05-14 00:39:53 -0700730 public Tunnel getTunnel(String tunnelId) {
731 return tunnelHandler.getTunnel(tunnelId);
732 }
733
Charles Chan7ffd81f2017-02-08 15:52:08 -0800734 // TODO Consider moving these to InterfaceService
sanghob35a6192015-04-01 13:05:26 -0700735 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800736 * Returns untagged VLAN configured on given connect point.
Charles Chan7ffd81f2017-02-08 15:52:08 -0800737 * <p>
738 * Only returns the first match if there are multiple untagged VLAN configured
739 * on the connect point.
sanghob35a6192015-04-01 13:05:26 -0700740 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800741 * @param connectPoint connect point
742 * @return untagged VLAN or null if not configured
sanghob35a6192015-04-01 13:05:26 -0700743 */
Charles Chan65238242017-06-22 18:03:14 -0700744 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800745 return interfaceService.getInterfacesByPort(connectPoint).stream()
746 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
747 .map(Interface::vlanUntagged)
748 .findFirst().orElse(null);
sanghob35a6192015-04-01 13:05:26 -0700749 }
750
sangho1e575652015-05-14 00:39:53 -0700751 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800752 * Returns tagged VLAN configured on given connect point.
753 * <p>
754 * Returns all matches if there are multiple tagged VLAN configured
755 * on the connect point.
756 *
757 * @param connectPoint connect point
758 * @return tagged VLAN or empty set if not configured
759 */
Charles Chan65238242017-06-22 18:03:14 -0700760 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800761 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
762 return interfaces.stream()
763 .map(Interface::vlanTagged)
Charles Chan65238242017-06-22 18:03:14 -0700764 .flatMap(Set::stream)
Charles Chan7ffd81f2017-02-08 15:52:08 -0800765 .collect(Collectors.toSet());
766 }
767
768 /**
769 * Returns native VLAN configured on given connect point.
770 * <p>
771 * Only returns the first match if there are multiple native VLAN configured
772 * on the connect point.
773 *
774 * @param connectPoint connect point
775 * @return native VLAN or null if not configured
776 */
Charles Chan65238242017-06-22 18:03:14 -0700777 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan7ffd81f2017-02-08 15:52:08 -0800778 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
779 return interfaces.stream()
780 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
781 .map(Interface::vlanNative)
782 .findFirst()
783 .orElse(null);
784 }
785
786 /**
Charles Chanf9a52702017-06-16 15:19:24 -0700787 * Returns internal VLAN for untagged hosts on given connect point.
788 * <p>
789 * The internal VLAN is either vlan-untagged for an access port,
790 * or vlan-native for a trunk port.
791 *
792 * @param connectPoint connect point
793 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
794 */
Charles Chan65238242017-06-22 18:03:14 -0700795 VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chanf9a52702017-06-16 15:19:24 -0700796 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
797 VlanId nativeVlanId = getNativeVlanId(connectPoint);
798 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
799 }
800
801 /**
Charles Chan65238242017-06-22 18:03:14 -0700802 * Returns optional pair device ID of given device.
803 *
804 * @param deviceId device ID
805 * @return optional pair device ID. Might be empty if pair device is not configured
806 */
807 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
808 SegmentRoutingDeviceConfig deviceConfig =
809 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
810 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
811 }
812 /**
813 * Returns optional pair device local port of given device.
814 *
815 * @param deviceId device ID
816 * @return optional pair device ID. Might be empty if pair device is not configured
817 */
818 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
819 SegmentRoutingDeviceConfig deviceConfig =
820 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
821 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
822 }
823
824 /**
Charles Chan2fde6d42017-08-23 14:46:43 -0700825 * Determine if current instance is the master of given connect point.
826 *
827 * @param cp connect point
828 * @return true if current instance is the master of given connect point
829 */
Andreas Pantelopoulos27532cd2017-10-23 12:18:25 -0700830 public boolean isMasterOf(ConnectPoint cp) {
Charles Chan2fde6d42017-08-23 14:46:43 -0700831 boolean isMaster = mastershipService.isLocalMaster(cp.deviceId());
832 if (!isMaster) {
833 log.debug(NOT_MASTER, cp);
834 }
835 return isMaster;
836 }
837
838 /**
Charles Chan9640c812017-08-23 13:55:39 -0700839 * Returns locations of given resolved route.
840 *
841 * @param resolvedRoute resolved route
842 * @return locations of nexthop. Might be empty if next hop is not found
843 */
844 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
845 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
846 return Optional.ofNullable(hostService.getHost(hostId))
847 .map(Host::locations).orElse(Sets.newHashSet())
848 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
849 }
850
851 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800852 * Returns vlan port map of given device.
853 *
854 * @param deviceId device id
855 * @return vlan-port multimap
856 */
857 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
858 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
859
860 interfaceService.getInterfaces().stream()
861 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
862 .forEach(intf -> {
863 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan65238242017-06-22 18:03:14 -0700864 intf.vlanTagged().forEach(vlanTagged ->
865 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
866 );
Charles Chan7ffd81f2017-02-08 15:52:08 -0800867 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
868 });
869 vlanPortMap.removeAll(VlanId.NONE);
870
871 return vlanPortMap;
872 }
873
874 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800875 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -0800876 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700877 *
878 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -0800879 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -0800880 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700881 */
Charles Chan65238242017-06-22 18:03:14 -0700882 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -0700883 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800884 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
885 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700886 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -0800887 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -0800888 + "device {} not found", deviceId);
889 return -1;
890 }
891 }
892
893 /**
894 * Returns the next objective ID for the given portNumber, given the treatment.
895 * There could be multiple different treatments to the same outport, which
Saurav Das961beb22017-03-29 19:09:17 -0700896 * would result in different objectives. If the next object does not exist,
897 * and should be created, a new one is created and its id is returned.
Saurav Das4ce45962015-11-24 23:21:05 -0800898 *
899 * @param deviceId Device ID
900 * @param portNum port number on device for which NextObjective is queried
901 * @param treatment the actions to apply on the packets (should include outport)
902 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das961beb22017-03-29 19:09:17 -0700903 * @param createIfMissing true if a next object should be created if not found
Saurav Das59232cf2016-04-27 18:35:50 -0700904 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800905 */
906 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
907 TrafficTreatment treatment,
Saurav Das961beb22017-03-29 19:09:17 -0700908 TrafficSelector meta,
909 boolean createIfMissing) {
Saurav Das4ce45962015-11-24 23:21:05 -0800910 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
911 if (ghdlr != null) {
Saurav Das961beb22017-03-29 19:09:17 -0700912 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das4ce45962015-11-24 23:21:05 -0800913 } else {
Charles Chane849c192016-01-11 18:28:54 -0800914 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
915 + " not found", deviceId);
916 return -1;
917 }
918 }
919
Saurav Dasc88d4662017-05-15 15:34:25 -0700920 /**
921 * Returns the group handler object for the specified device id.
922 *
923 * @param devId the device identifier
924 * @return the groupHandler object for the device id, or null if not found
925 */
Charles Chan65238242017-06-22 18:03:14 -0700926 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700927 return groupHandlerMap.get(devId);
928 }
929
930 /**
Saurav Dasceccf242017-08-03 18:30:35 -0700931 * Returns the default routing handler object.
932 *
933 * @return the default routing handler object
934 */
935 public DefaultRoutingHandler getRoutingHandler() {
936 return defaultRoutingHandler;
937 }
938
sanghob35a6192015-04-01 13:05:26 -0700939 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700940 @Override
941 public void process(PacketContext context) {
942
943 if (context.isHandled()) {
944 return;
945 }
946
947 InboundPacket pkt = context.inPacket();
948 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -0800949
950 if (ethernet == null) {
951 return;
952 }
953
Saurav Das7bcbe702017-06-13 15:35:54 -0700954 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
955 ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800956 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Dasc88d4662017-05-15 15:34:25 -0700957 log.warn("Received unexpected ARP packet on {}",
958 context.inPacket().receivedFrom());
Saurav Das76ae6812017-03-15 15:15:14 -0700959 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -0800960 return;
sanghob35a6192015-04-01 13:05:26 -0700961 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800962 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
963 //ipHandler.addToPacketBuffer(ipv4Packet);
964 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
965 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700966 } else {
Charles Chan50035632017-01-13 17:20:44 -0800967 // NOTE: We don't support IP learning at this moment so this
968 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -0800969 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700970 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800971 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
972 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -0800973 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -0800974 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -0800975 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
976 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
977 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
978 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
979 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
980 } else {
Saurav Dasc88d4662017-05-15 15:34:25 -0700981 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chan0ed44fb2017-03-13 13:10:30 -0700982 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -0800983 }
984 } else {
985 // NOTE: We don't support IP learning at this moment so this
986 // is not necessary. Also it causes duplication of DHCPv6 packets.
987 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
988 }
sanghob35a6192015-04-01 13:05:26 -0700989 }
990 }
991 }
992
993 private class InternalLinkListener implements LinkListener {
994 @Override
995 public void event(LinkEvent event) {
Charles Chanb1f8c762017-03-29 16:39:05 -0700996 if (event.type() == LinkEvent.Type.LINK_ADDED ||
997 event.type() == LinkEvent.Type.LINK_UPDATED ||
998 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700999 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -07001000 scheduleEventHandlerIfNotScheduled(event);
1001 }
1002 }
1003 }
1004
1005 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -07001006 @Override
1007 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -07001008 switch (event.type()) {
1009 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -08001010 case PORT_UPDATED:
1011 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -07001012 case DEVICE_UPDATED:
1013 case DEVICE_AVAILABILITY_CHANGED:
Saurav Dasc88d4662017-05-15 15:34:25 -07001014 log.trace("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -07001015 scheduleEventHandlerIfNotScheduled(event);
1016 break;
1017 default:
1018 }
1019 }
1020 }
1021
Saurav Das4ce45962015-11-24 23:21:05 -08001022 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -07001023 private void scheduleEventHandlerIfNotScheduled(Event event) {
Charles Chan65238242017-06-22 18:03:14 -07001024 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001025 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001026 numOfEventsQueued++;
1027
1028 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
1029 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001030 eventHandlerFuture = executorService
1031 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
1032 numOfHandlerScheduled++;
1033 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -07001034 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001035 numOfEventsQueued,
1036 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -07001037 }
sanghob35a6192015-04-01 13:05:26 -07001038 }
1039
1040 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -07001041 @Override
sanghob35a6192015-04-01 13:05:26 -07001042 public void run() {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001043 try {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001044 while (true) {
Saurav Das4ce45962015-11-24 23:21:05 -08001045 @SuppressWarnings("rawtypes")
Charles Chan65238242017-06-22 18:03:14 -07001046 Event event;
1047 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001048 if (!eventQueue.isEmpty()) {
1049 event = eventQueue.poll();
1050 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001051 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001052 numOfHandlerExecution++;
1053 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
1054 numOfHandlerExecution, numOfEventsExecuted);
1055 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001056 }
sangho20eff1d2015-04-13 15:15:58 -07001057 }
Charles Chanb1f8c762017-03-29 16:39:05 -07001058 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1059 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Das45f48152018-01-18 12:07:33 -08001060 linkHandler.processLinkAdded((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001061 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Saurav Das45f48152018-01-18 12:07:33 -08001062 linkHandler.processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001063 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1064 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1065 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -08001066 DeviceId deviceId = ((Device) event.subject()).id();
1067 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001068 log.info("Processing device event {} for available device {}",
1069 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001070 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -07001071 } else {
1072 log.info("Processing device event {} for unavailable device {}",
1073 event.type(), ((Device) event.subject()).id());
1074 processDeviceRemoved((Device) event.subject());
1075 }
Saurav Das1a129a02016-11-18 15:21:57 -08001076 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001077 // typically these calls come when device is added first time
1078 // so port filtering rules are handled at the device_added event.
1079 // port added calls represent all ports on the device,
1080 // enabled or not.
Saurav Dasc88d4662017-05-15 15:34:25 -07001081 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -08001082 ((DeviceEvent) event).subject().id(),
1083 ((DeviceEvent) event).port().number(),
1084 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -08001085 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -08001086 // these calls happen for every subsequent event
1087 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -08001088 log.info("** PORT UPDATED {}/{} -> {}",
1089 event.subject(),
1090 ((DeviceEvent) event).port(),
1091 event.type());
1092 processPortUpdated(((Device) event.subject()),
1093 ((DeviceEvent) event).port());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -07001094 } else {
1095 log.warn("Unhandled event type: {}", event.type());
1096 }
sanghob35a6192015-04-01 13:05:26 -07001097 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -07001098 } catch (Exception e) {
1099 log.error("SegmentRouting event handler "
1100 + "thread thrown an exception: {}", e);
sanghob35a6192015-04-01 13:05:26 -07001101 }
sanghob35a6192015-04-01 13:05:26 -07001102 }
1103 }
1104
Saurav Das45f48152018-01-18 12:07:33 -08001105 void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -07001106 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan91ccbf72017-06-27 18:48:32 -07001107
1108 // NOTE: Punt ARP/NDP even when the device is not configured.
1109 // Host learning without network config is required for CORD config generator.
1110 routingRulePopulator.populateIpPunts(device.id());
1111 routingRulePopulator.populateArpNdpPunts(device.id());
1112
Charles Chan0b4e6182015-11-03 10:42:14 -08001113 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001114 log.warn("Device configuration unavailable. Device {} will be "
1115 + "processed after configuration.", device.id());
Saurav Das2857f382015-11-03 14:39:27 -08001116 return;
1117 }
Charles Chan2199c302016-04-23 17:36:10 -07001118 processDeviceAddedInternal(device.id());
1119 }
1120
1121 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -07001122 // Irrespective of whether the local is a MASTER or not for this device,
1123 // we need to create a SR-group-handler instance. This is because in a
1124 // multi-instance setup, any instance can initiate forwarding/next-objectives
1125 // for any switch (even if this instance is a SLAVE or not even connected
1126 // to the switch). To handle this, a default-group-handler instance is necessary
1127 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -07001128 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1129 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -08001130 DefaultGroupHandler groupHandler;
1131 try {
1132 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -07001133 createGroupHandler(deviceId,
1134 appId,
1135 deviceConfiguration,
1136 linkService,
1137 flowObjectiveService,
1138 this);
Charles Chan0b4e6182015-11-03 10:42:14 -08001139 } catch (DeviceConfigNotFoundException e) {
1140 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1141 return;
1142 }
Saurav Dasf14d9ef2017-12-05 15:00:23 -08001143 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan2199c302016-04-23 17:36:10 -07001144 deviceId);
1145 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -08001146 }
Saurav Dasb5c236e2016-06-07 10:08:06 -07001147
Charles Chan2199c302016-04-23 17:36:10 -07001148 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -08001149 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan03a73e02016-10-24 14:52:01 -07001150 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -07001151 xConnectHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -07001152 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -08001153 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -07001154 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -07001155 }
Charles Chan5270ed02016-01-30 23:22:37 -08001156
Charles Chan03a73e02016-10-24 14:52:01 -07001157 appCfgHandler.init(deviceId);
1158 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -07001159 }
1160
Saurav Das80980c72016-03-23 11:22:49 -07001161 private void processDeviceRemoved(Device device) {
Saurav Das7bcbe702017-06-13 15:35:54 -07001162 dsNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001163 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan47933752017-11-30 15:37:50 -08001164 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan59cc16d2017-02-02 16:20:42 -08001165 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -07001166 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001167 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Das80980c72016-03-23 11:22:49 -07001168 portNextObjStore.entrySet().stream()
1169 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan65238242017-06-22 18:03:14 -07001170 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Das45f48152018-01-18 12:07:33 -08001171 linkHandler.processDeviceRemoved(device);
Charles Chaned3742352017-06-15 00:44:51 -07001172
Saurav Dasceccf242017-08-03 18:30:35 -07001173 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1174 if (gh != null) {
1175 gh.shutdown();
1176 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001177 // Note that a switch going down is associated with all of its links
1178 // going down as well, but it is treated as a single switch down event
1179 // while the link-downs are ignored.
1180 defaultRoutingHandler
1181 .populateRoutingRulesForLinkStatusChange(null, null, device.id());
Saurav Dasc568c342018-01-25 09:49:01 -08001182 defaultRoutingHandler.purgeEcmpGraph(device.id());
Pier Luigieba73a02018-01-16 10:47:50 +01001183 mcastHandler.processDeviceDown(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -07001184 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -07001185 }
1186
Saurav Das1a129a02016-11-18 15:21:57 -08001187 private void processPortUpdated(Device device, Port port) {
1188 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1189 log.warn("Device configuration uploading. Not handling port event for"
1190 + "dev: {} port: {}", device.id(), port.number());
1191 return;
1192 }
Saurav Das018605f2017-02-18 14:05:44 -08001193
1194 if (!mastershipService.isLocalMaster(device.id())) {
1195 log.debug("Not master for dev:{} .. not handling port updated event"
1196 + "for port {}", device.id(), port.number());
1197 return;
1198 }
1199
1200 // first we handle filtering rules associated with the port
1201 if (port.isEnabled()) {
1202 log.info("Switchport {}/{} enabled..programming filters",
1203 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001204 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -08001205 } else {
1206 log.info("Switchport {}/{} disabled..removing filters",
1207 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001208 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001209 }
Saurav Das1a129a02016-11-18 15:21:57 -08001210
1211 // portUpdated calls are for ports that have gone down or up. For switch
1212 // to switch ports, link-events should take care of any re-routing or
1213 // group editing necessary for port up/down. Here we only process edge ports
1214 // that are already configured.
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001215 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1216 VlanId untaggedVlan = getUntaggedVlanId(cp);
1217 VlanId nativeVlan = getNativeVlanId(cp);
1218 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001219
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001220 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasc88d4662017-05-15 15:34:25 -07001221 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Das1a129a02016-11-18 15:21:57 -08001222 + "dev/port: {}/{}", device.id(), port.number());
1223 return;
1224 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001225 if (untaggedVlan != null) {
1226 processEdgePort(device, port, untaggedVlan, true);
1227 }
1228 if (nativeVlan != null) {
1229 processEdgePort(device, port, nativeVlan, true);
1230 }
1231 if (!taggedVlans.isEmpty()) {
1232 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1233 }
Saurav Das1a129a02016-11-18 15:21:57 -08001234 }
1235
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001236 private void processEdgePort(Device device, Port port, VlanId vlanId,
1237 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001238 boolean portUp = port.isEnabled();
1239 if (portUp) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001240 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001241 port.number(), vlanId);
Charles Chan47933752017-11-30 15:37:50 -08001242 hostHandler.processPortUp(new ConnectPoint(device.id(), port.number()));
Saurav Das1a129a02016-11-18 15:21:57 -08001243 } else {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001244 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001245 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001246 }
1247
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001248 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001249 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001250 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001251 } else {
1252 log.warn("Group handler not found for dev:{}. Not handling edge port"
1253 + " {} event for port:{}", device.id(),
1254 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001255 }
1256 }
sangho1e575652015-05-14 00:39:53 -07001257
Charles Chan27fe1a52017-10-20 16:06:55 -07001258 private void createOrUpdateDeviceConfiguration() {
1259 if (deviceConfiguration == null) {
Saurav Dase7f51012018-02-09 17:26:45 -08001260 log.info("Creating new DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001261 deviceConfiguration = new DeviceConfiguration(this);
1262 } else {
Saurav Dase7f51012018-02-09 17:26:45 -08001263 log.info("Updating DeviceConfiguration");
Charles Chan27fe1a52017-10-20 16:06:55 -07001264 deviceConfiguration.updateConfig();
1265 }
1266 }
1267
Pier Ventre10bd8d12016-11-26 21:05:22 -08001268 /**
1269 * Registers the given connect point with the NRS, this is necessary
1270 * to receive the NDP and ARP packets from the NRS.
1271 *
1272 * @param portToRegister connect point to register
1273 */
1274 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001275 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001276 portToRegister,
1277 neighbourHandler,
1278 appId
1279 );
1280 }
1281
Charles Chand6832882015-10-05 17:50:33 -07001282 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das7bcbe702017-06-13 15:35:54 -07001283 private static final long PROGRAM_DELAY = 2;
Charles Chan2c15aca2016-11-09 20:51:44 -08001284 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001285
Charles Chane849c192016-01-11 18:28:54 -08001286 /**
1287 * Constructs the internal network config listener.
1288 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001289 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001290 */
Charles Chan65238242017-06-22 18:03:14 -07001291 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan2c15aca2016-11-09 20:51:44 -08001292 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001293 }
1294
Charles Chane849c192016-01-11 18:28:54 -08001295 /**
1296 * Reads network config and initializes related data structure accordingly.
1297 */
Charles Chan47933752017-11-30 15:37:50 -08001298 void configureNetwork() {
Saurav Dase7f51012018-02-09 17:26:45 -08001299 log.info("Configuring network ...");
Charles Chan27fe1a52017-10-20 16:06:55 -07001300 createOrUpdateDeviceConfiguration();
Charles Chan4636be02015-10-07 14:21:45 -07001301
Charles Chan2c15aca2016-11-09 20:51:44 -08001302 arpHandler = new ArpHandler(srManager);
1303 icmpHandler = new IcmpHandler(srManager);
1304 ipHandler = new IpHandler(srManager);
1305 routingRulePopulator = new RoutingRulePopulator(srManager);
1306 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001307
1308 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1309 groupHandlerMap, tunnelStore);
1310 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1311 flowObjectiveService,
1312 tunnelHandler, policyStore);
Saurav Das7bcbe702017-06-13 15:35:54 -07001313 // add a small delay to absorb multiple network config added notifications
1314 if (!programmingScheduled.get()) {
Saurav Dase7f51012018-02-09 17:26:45 -08001315 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
Saurav Das7bcbe702017-06-13 15:35:54 -07001316 programmingScheduled.set(true);
1317 executorService.schedule(new ConfigChange(), PROGRAM_DELAY,
1318 TimeUnit.SECONDS);
Charles Chan4636be02015-10-07 14:21:45 -07001319 }
Charles Chan2199c302016-04-23 17:36:10 -07001320 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001321 }
1322
Charles Chand6832882015-10-05 17:50:33 -07001323 @Override
1324 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001325 // TODO move this part to NetworkConfigEventHandler
1326 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1327 switch (event.type()) {
1328 case CONFIG_ADDED:
Charles Chan278ce832017-06-26 15:25:09 -07001329 log.info("Segment Routing Device Config added for {}", event.subject());
Charles Chan5270ed02016-01-30 23:22:37 -08001330 configureNetwork();
1331 break;
1332 case CONFIG_UPDATED:
Charles Chan278ce832017-06-26 15:25:09 -07001333 log.info("Segment Routing Config updated for {}", event.subject());
Charles Chan27fe1a52017-10-20 16:06:55 -07001334 createOrUpdateDeviceConfiguration();
Charles Chan278ce832017-06-26 15:25:09 -07001335 // TODO support dynamic configuration
1336 break;
1337 default:
1338 break;
1339 }
1340 } else if (event.configClass().equals(InterfaceConfig.class)) {
1341 switch (event.type()) {
1342 case CONFIG_ADDED:
1343 log.info("Interface Config added for {}", event.subject());
1344 configureNetwork();
1345 break;
1346 case CONFIG_UPDATED:
1347 log.info("Interface Config updated for {}", event.subject());
Charles Chan27fe1a52017-10-20 16:06:55 -07001348 createOrUpdateDeviceConfiguration();
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001349
1350 // Following code will be uncommented when [CORD-634] is fully implemented.
1351 // [CORD-634] Add dynamic config support for interfaces
1352 updateInterface((InterfaceConfig) event.config().get(),
1353 (InterfaceConfig) event.prevConfig().get());
Charles Chan5270ed02016-01-30 23:22:37 -08001354 // TODO support dynamic configuration
1355 break;
1356 default:
1357 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001358 }
Charles Chan5270ed02016-01-30 23:22:37 -08001359 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001360 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001361 switch (event.type()) {
1362 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001363 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001364 break;
1365 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001366 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001367 break;
1368 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001369 appCfgHandler.processAppConfigRemoved(event);
1370 break;
1371 default:
1372 break;
1373 }
Saurav Dase7f51012018-02-09 17:26:45 -08001374 log.info("App config event .. configuring network");
Charles Chan03a73e02016-10-24 14:52:01 -07001375 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001376 } else if (event.configClass().equals(XConnectConfig.class)) {
1377 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1378 switch (event.type()) {
1379 case CONFIG_ADDED:
1380 xConnectHandler.processXConnectConfigAdded(event);
1381 break;
1382 case CONFIG_UPDATED:
1383 xConnectHandler.processXConnectConfigUpdated(event);
1384 break;
1385 case CONFIG_REMOVED:
1386 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001387 break;
1388 default:
1389 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001390 }
Pier Ventref34966c2016-11-07 16:21:04 -08001391 } else if (event.configClass().equals(PwaasConfig.class)) {
1392 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1393 switch (event.type()) {
1394 case CONFIG_ADDED:
1395 l2TunnelHandler.processPwaasConfigAdded(event);
1396 break;
1397 case CONFIG_UPDATED:
1398 l2TunnelHandler.processPwaasConfigUpdated(event);
1399 break;
1400 case CONFIG_REMOVED:
1401 l2TunnelHandler.processPwaasConfigRemoved(event);
1402 break;
1403 default:
1404 break;
1405 }
Charles Chand6832882015-10-05 17:50:33 -07001406 }
1407 }
Saurav Das7bcbe702017-06-13 15:35:54 -07001408
Charles Chan50443e82018-01-03 16:26:32 -08001409 @Override
1410 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase7f51012018-02-09 17:26:45 -08001411 if (event.type() == CONFIG_REGISTERED ||
1412 event.type() == CONFIG_UNREGISTERED) {
1413 log.debug("Ignore event {} due to type mismatch", event);
1414 return false;
Charles Chan50443e82018-01-03 16:26:32 -08001415 }
Saurav Dase7f51012018-02-09 17:26:45 -08001416
1417 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1418 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1419 !event.configClass().equals(InterfaceConfig.class) &&
1420 !event.configClass().equals(XConnectConfig.class) &&
1421 !event.configClass().equals(PwaasConfig.class)) {
1422 log.debug("Ignore event {} due to class mismatch", event);
1423 return false;
1424 }
1425
1426 return true;
Charles Chan50443e82018-01-03 16:26:32 -08001427 }
1428
Saurav Das7bcbe702017-06-13 15:35:54 -07001429 private final class ConfigChange implements Runnable {
1430 @Override
1431 public void run() {
1432 programmingScheduled.set(false);
Saurav Dase7f51012018-02-09 17:26:45 -08001433 log.info("Reacting to config changes after buffer delay");
Saurav Das7bcbe702017-06-13 15:35:54 -07001434 for (Device device : deviceService.getDevices()) {
1435 processDeviceAdded(device);
1436 }
1437 defaultRoutingHandler.startPopulationProcess();
1438 }
1439 }
Charles Chand6832882015-10-05 17:50:33 -07001440 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001441
1442 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001443 @Override
1444 public void event(HostEvent event) {
Charles Chan68aa62d2015-11-09 16:37:23 -08001445 switch (event.type()) {
1446 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001447 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001448 break;
1449 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001450 hostHandler.processHostMovedEvent(event);
Charles Chan2fde6d42017-08-23 14:46:43 -07001451 routeHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001452 break;
1453 case HOST_REMOVED:
Charles Chanf9a52702017-06-16 15:19:24 -07001454 hostHandler.processHostRemovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001455 break;
1456 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001457 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001458 break;
1459 default:
1460 log.warn("Unsupported host event type: {}", event.type());
1461 break;
1462 }
1463 }
1464 }
1465
Charles Chand55e84d2016-03-30 17:54:24 -07001466 private class InternalMcastListener implements McastListener {
1467 @Override
1468 public void event(McastEvent event) {
1469 switch (event.type()) {
1470 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001471 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001472 break;
1473 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001474 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001475 break;
1476 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001477 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001478 break;
1479 case ROUTE_ADDED:
1480 case ROUTE_REMOVED:
1481 default:
1482 break;
1483 }
1484 }
1485 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001486
Charles Chan03a73e02016-10-24 14:52:01 -07001487 private class InternalRouteEventListener implements RouteListener {
1488 @Override
1489 public void event(RouteEvent event) {
1490 switch (event.type()) {
1491 case ROUTE_ADDED:
1492 routeHandler.processRouteAdded(event);
1493 break;
1494 case ROUTE_UPDATED:
1495 routeHandler.processRouteUpdated(event);
1496 break;
1497 case ROUTE_REMOVED:
1498 routeHandler.processRouteRemoved(event);
1499 break;
Charles Chan2fde6d42017-08-23 14:46:43 -07001500 case ALTERNATIVE_ROUTES_CHANGED:
1501 routeHandler.processAlternativeRoutesChanged(event);
1502 break;
Charles Chan03a73e02016-10-24 14:52:01 -07001503 default:
1504 break;
1505 }
1506 }
1507 }
Saurav Dasc88d4662017-05-15 15:34:25 -07001508
Jonghwan Hyun42fe1052017-08-25 17:48:36 -07001509 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1510 try {
1511 Set<Interface> intfs = conf.getInterfaces();
1512 Set<Interface> prevIntfs = prevConf.getInterfaces();
1513
1514 // Now we only handle one interface config at each port.
1515 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1516 log.warn("Interface update aborted - one at a time is allowed, " +
1517 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1518 return;
1519 }
1520
1521 Interface intf = intfs.stream().findFirst().get();
1522 Interface prevIntf = prevIntfs.stream().findFirst().get();
1523
1524 DeviceId deviceId = intf.connectPoint().deviceId();
1525 PortNumber portNum = intf.connectPoint().port();
1526
1527 if (!mastershipService.isLocalMaster(deviceId)) {
1528 log.debug("CONFIG_UPDATED event for interfaces should be " +
1529 "handled by master node for device {}", deviceId);
1530 return;
1531 }
1532
1533 removeSubnetConfig(prevIntf.connectPoint(),
1534 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1535 new HashSet<>(intf.ipAddressesList())));
1536
1537 if (prevIntf.vlanNative() != VlanId.NONE && !intf.vlanNative().equals(prevIntf.vlanNative())) {
1538 // RemoveVlanNative
1539 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1540 }
1541
1542 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1543 // RemoveVlanTagged
1544 prevIntf.vlanTagged().stream().filter(i -> !intf.vlanTagged().contains(i)).forEach(
1545 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, false)
1546 );
1547 }
1548
1549 if (prevIntf.vlanUntagged() != VlanId.NONE && !intf.vlanUntagged().equals(prevIntf.vlanUntagged())) {
1550 // RemoveVlanUntagged
1551 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1552 }
1553
1554 if (intf.vlanNative() != VlanId.NONE && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1555 // AddVlanNative
1556 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1557 }
1558
1559 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1560 // AddVlanTagged
1561 intf.vlanTagged().stream().filter(i -> !prevIntf.vlanTagged().contains(i)).forEach(
1562 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, true)
1563 );
1564 }
1565
1566 if (intf.vlanUntagged() != VlanId.NONE && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())) {
1567 // AddVlanUntagged
1568 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1569 }
1570 addSubnetConfig(prevIntf.connectPoint(),
1571 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1572 new HashSet<>(prevIntf.ipAddressesList())));
1573 } catch (ConfigException e) {
1574 log.error("Error in configuration");
1575 }
1576 }
1577
1578 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1579 VlanId vlanId, boolean pushVlan, boolean install) {
1580 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1581 if (grpHandler == null) {
1582 log.warn("Failed to retrieve group handler for device {}", deviceId);
1583 return;
1584 }
1585
1586 // Update filtering objective for a single port
1587 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1588
1589 // Update filtering objective for multicast ingress port
1590 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1591
1592 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1593
1594 if (nextId != -1 && !install) {
1595 // Update next objective for a single port as an output port
1596 // Remove a single port from L2FG
1597 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1598 // Remove L2 Bridging rule and L3 Unicast rule to the host
1599 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1600 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1601 // only if there is no port configured on that VLAN ID
1602 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1603 // Remove broadcast forwarding rule for the VLAN
1604 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1605 // Remove L2FG for VLAN
1606 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1607 } else {
1608 // Remove L2IG of the port
1609 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1610 }
1611 } else if (install) {
1612 if (nextId != -1) {
1613 // Add a single port to L2FG
1614 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1615 } else {
1616 // Create L2FG for VLAN
1617 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1618 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1619 }
1620 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1621 } else {
1622 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1623 }
1624 }
1625
1626 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1627 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1628 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1629
1630 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1631 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1632 .filter(intf -> !intf.connectPoint().equals(cp))
1633 .flatMap(intf -> intf.ipAddressesList().stream())
1634 .collect(Collectors.toSet());
1635 // 1. Partial subnet population
1636 // Remove routing rules for removed subnet from previous configuration,
1637 // which does not also exist in other interfaces in the same device
1638 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1639 .map(InterfaceIpAddress::subnetAddress)
1640 .collect(Collectors.toSet());
1641
1642 defaultRoutingHandler.revokeSubnet(
1643 ipPrefixSet.stream()
1644 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1645 .collect(Collectors.toSet()));
1646
1647 // 2. Interface IP punts
1648 // Remove IP punts for old Intf address
1649 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1650 .map(InterfaceIpAddress::ipAddress)
1651 .collect(Collectors.toSet());
1652 ipAddressSet.stream()
1653 .map(InterfaceIpAddress::ipAddress)
1654 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1655 .forEach(interfaceIpAddress ->
1656 routingRulePopulator.revokeSingleIpPunts(
1657 cp.deviceId(), interfaceIpAddress));
1658
1659 // 3. Host unicast routing rule
1660 // Remove unicast routing rule
1661 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1662 }
1663
1664 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1665 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1666 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1667
1668 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1669 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1670 .filter(intf -> !intf.connectPoint().equals(cp))
1671 .flatMap(intf -> intf.ipAddressesList().stream())
1672 .collect(Collectors.toSet());
1673 // 1. Partial subnet population
1674 // Add routing rules for newly added subnet, which does not also exist in
1675 // other interfaces in the same device
1676 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1677 .map(InterfaceIpAddress::subnetAddress)
1678 .collect(Collectors.toSet());
1679
1680 defaultRoutingHandler.populateSubnet(
1681 Collections.singleton(cp),
1682 ipPrefixSet.stream()
1683 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1684 .collect(Collectors.toSet()));
1685
1686 // 2. Interface IP punts
1687 // Add IP punts for new Intf address
1688 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1689 .map(InterfaceIpAddress::ipAddress)
1690 .collect(Collectors.toSet());
1691 ipAddressSet.stream()
1692 .map(InterfaceIpAddress::ipAddress)
1693 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1694 .forEach(interfaceIpAddress ->
1695 routingRulePopulator.populateSingleIpPunts(
1696 cp.deviceId(), interfaceIpAddress));
1697
1698 // 3. Host unicast routing rule
1699 // Add unicast routing rule
1700 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1701 }
sanghob35a6192015-04-01 13:05:26 -07001702}