blob: d951b530336bcd6e713f14db09c8c5f98eefc429 [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor0947d7e2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
sangho80f11cb2015-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 Pantelopoulosafc637f2017-12-20 18:04:27 -080018import com.fasterxml.jackson.databind.ObjectMapper;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -070019import com.fasterxml.jackson.databind.node.ObjectNode;
Jonathan Hart61e24e12017-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 Chanf0ae41e2017-08-23 13:55:39 -070024import com.google.common.collect.Sets;
sangho80f11cb2015-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 Chan873661e2017-11-30 15:37:50 -080028import org.apache.felix.scr.annotations.Modified;
29import org.apache.felix.scr.annotations.Property;
sangho80f11cb2015-04-01 13:05:26 -070030import org.apache.felix.scr.annotations.Reference;
31import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho27462c62015-05-14 00:39:53 -070032import org.apache.felix.scr.annotations.Service;
sangho80f11cb2015-04-01 13:05:26 -070033import org.onlab.packet.Ethernet;
Pier Ventreb6b81d52016-12-02 08:16:05 -080034import org.onlab.packet.ICMP6;
Charles Chan77277672015-10-20 16:24:19 -070035import org.onlab.packet.IPv4;
Pier Ventreb6a7f342016-11-26 21:05:22 -080036import org.onlab.packet.IPv6;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070037import org.onlab.packet.IpAddress;
Charles Chan77277672015-10-20 16:24:19 -070038import org.onlab.packet.IpPrefix;
Jonathan Hart54541d12016-04-12 15:39:44 -070039import org.onlab.packet.VlanId;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070040import org.onlab.util.KryoNamespace;
Charles Chan873661e2017-11-30 15:37:50 -080041import org.onlab.util.Tools;
Saurav Dasc3604f12016-03-23 11:22:49 -070042import org.onosproject.cfg.ComponentConfigService;
Pier Luigi580fd8a2018-01-16 10:47:50 +010043import org.onosproject.cluster.ClusterService;
44import org.onosproject.cluster.LeadershipService;
sangho80f11cb2015-04-01 13:05:26 -070045import org.onosproject.core.ApplicationId;
46import org.onosproject.core.CoreService;
47import org.onosproject.event.Event;
Jonathan Hart54541d12016-04-12 15:39:44 -070048import org.onosproject.mastership.MastershipService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080049import org.onosproject.net.ConnectPoint;
Jonathan Hart54541d12016-04-12 15:39:44 -070050import org.onosproject.net.Device;
51import org.onosproject.net.DeviceId;
Charles Chanf0ae41e2017-08-23 13:55:39 -070052import org.onosproject.net.Host;
53import org.onosproject.net.HostId;
Jonathan Hart54541d12016-04-12 15:39:44 -070054import org.onosproject.net.Link;
55import org.onosproject.net.Port;
Charles Chanf4586112015-11-09 16:37:23 -080056import org.onosproject.net.PortNumber;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070057import org.onosproject.net.config.ConfigException;
Charles Chan72f556a2015-10-05 17:50:33 -070058import org.onosproject.net.config.ConfigFactory;
59import org.onosproject.net.config.NetworkConfigEvent;
Charles Chan72f556a2015-10-05 17:50:33 -070060import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070061import org.onosproject.net.config.NetworkConfigRegistry;
Ray Milkeyae0068a2017-08-15 11:02:29 -070062import org.onosproject.net.config.basics.InterfaceConfig;
63import org.onosproject.net.config.basics.McastConfig;
Charles Chan72f556a2015-10-05 17:50:33 -070064import org.onosproject.net.config.basics.SubjectFactories;
Saurav Dase6c448a2018-01-18 12:07:33 -080065import org.onosproject.net.device.DeviceAdminService;
Jonathan Hart54541d12016-04-12 15:39:44 -070066import org.onosproject.net.device.DeviceEvent;
67import org.onosproject.net.device.DeviceListener;
68import org.onosproject.net.device.DeviceService;
Charles Chanf4586112015-11-09 16:37:23 -080069import org.onosproject.net.flow.TrafficSelector;
70import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hart54541d12016-04-12 15:39:44 -070071import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chanf4586112015-11-09 16:37:23 -080072import org.onosproject.net.host.HostEvent;
73import org.onosproject.net.host.HostListener;
Charles Chan873661e2017-11-30 15:37:50 -080074import org.onosproject.net.host.HostLocationProbingService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080075import org.onosproject.net.host.HostService;
Jonghwan Hyune5ef7622017-08-25 17:48:36 -070076import org.onosproject.net.host.InterfaceIpAddress;
Ray Milkeyae0068a2017-08-15 11:02:29 -070077import org.onosproject.net.intf.Interface;
78import org.onosproject.net.intf.InterfaceService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080079import org.onosproject.net.link.LinkEvent;
80import org.onosproject.net.link.LinkListener;
81import org.onosproject.net.link.LinkService;
Charles Chanc91c8782016-03-30 17:54:24 -070082import org.onosproject.net.mcast.McastEvent;
83import org.onosproject.net.mcast.McastListener;
84import org.onosproject.net.mcast.MulticastRouteService;
Ray Milkeyae0068a2017-08-15 11:02:29 -070085import org.onosproject.net.neighbour.NeighbourResolutionService;
Pier Ventreb6a7f342016-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 Chanc91c8782016-03-30 17:54:24 -070090import org.onosproject.net.topology.TopologyService;
Charles Chanf0ae41e2017-08-23 13:55:39 -070091import org.onosproject.routeservice.ResolvedRoute;
Ray Milkeyae0068a2017-08-15 11:02:29 -070092import org.onosproject.routeservice.RouteEvent;
93import org.onosproject.routeservice.RouteListener;
94import org.onosproject.routeservice.RouteService;
Charles Chanc91c8782016-03-30 17:54:24 -070095import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
96import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventre6b19e482016-11-07 16:21:04 -080097import org.onosproject.segmentrouting.config.PwaasConfig;
Pier Ventre6b19e482016-11-07 16:21:04 -080098import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Ray Milkeyae0068a2017-08-15 11:02:29 -070099import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chan82f19972016-05-17 13:13:55 -0700100import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chanc91c8782016-03-30 17:54:24 -0700101import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
Saurav Das261c3002017-06-13 15:35:54 -0700102import org.onosproject.segmentrouting.grouphandler.DestinationSet;
103import org.onosproject.segmentrouting.grouphandler.NextNeighbors;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700104import org.onosproject.segmentrouting.pwaas.DefaultL2Tunnel;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800105import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelHandler;
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700106import org.onosproject.segmentrouting.pwaas.DefaultL2TunnelPolicy;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800107import org.onosproject.segmentrouting.pwaas.L2Tunnel;
Ray Milkeyae0068a2017-08-15 11:02:29 -0700108import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800109import org.onosproject.segmentrouting.pwaas.L2TunnelPolicy;
Saurav Das261c3002017-06-13 15:35:54 -0700110import org.onosproject.segmentrouting.storekey.DestinationSetNextObjectiveStoreKey;
Pier Luigi0f9635b2018-01-15 18:06:43 +0100111import org.onosproject.segmentrouting.storekey.McastStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -0700112import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan10b0fb72017-02-02 16:20:42 -0800113import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chan82f19972016-05-17 13:13:55 -0700114import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hart54541d12016-04-12 15:39:44 -0700115import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700116import org.onosproject.store.service.EventuallyConsistentMap;
117import org.onosproject.store.service.EventuallyConsistentMapBuilder;
118import org.onosproject.store.service.StorageService;
119import org.onosproject.store.service.WallClockTimestamp;
Charles Chan873661e2017-11-30 15:37:50 -0800120import org.osgi.service.component.ComponentContext;
sangho80f11cb2015-04-01 13:05:26 -0700121import org.slf4j.Logger;
122import org.slf4j.LoggerFactory;
123
Jonathan Hart61e24e12017-11-30 18:23:42 -0800124import java.util.Collections;
Charles Chan873661e2017-11-30 15:37:50 -0800125import java.util.Dictionary;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800126import java.util.HashSet;
127import java.util.List;
128import java.util.Map;
Jonathan Hart61e24e12017-11-30 18:23:42 -0800129import java.util.Optional;
130import java.util.Set;
131import java.util.concurrent.ConcurrentHashMap;
132import java.util.concurrent.ConcurrentLinkedQueue;
133import java.util.concurrent.Executors;
134import java.util.concurrent.ScheduledExecutorService;
135import java.util.concurrent.ScheduledFuture;
136import java.util.concurrent.TimeUnit;
137import java.util.concurrent.atomic.AtomicBoolean;
138import java.util.stream.Collectors;
sangho80f11cb2015-04-01 13:05:26 -0700139
Charles Chand6d25332016-02-26 22:19:52 -0800140import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800141import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700142import static org.onlab.util.Tools.groupedThreads;
Saurav Dase321cff2018-02-09 17:26:45 -0800143import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_REGISTERED;
144import static org.onosproject.net.config.NetworkConfigEvent.Type.CONFIG_UNREGISTERED;
Charles Chand6d25332016-02-26 22:19:52 -0800145
Charles Chanb7f75ac2016-01-11 18:28:54 -0800146/**
147 * Segment routing manager.
148 */
Jonathan Hart54541d12016-04-12 15:39:44 -0700149@Service
150@Component(immediate = true)
sangho27462c62015-05-14 00:39:53 -0700151public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700152
Charles Chan46fdfaf2016-11-09 20:51:44 -0800153 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
Charles Chan910be6a2017-08-23 14:46:43 -0700154 private static final String NOT_MASTER = "Current instance is not the master of {}. Ignore.";
sangho80f11cb2015-04-01 13:05:26 -0700155
156 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700157 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700158
159 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventreb6a7f342016-11-26 21:05:22 -0800160 private NeighbourResolutionService neighbourResolutionService;
161
162 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700163 CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700164
165 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700166 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700167
168 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700169 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700170
171 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan873661e2017-11-30 15:37:50 -0800172 HostLocationProbingService probingService;
173
174 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700175 DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700176
177 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Saurav Dase6c448a2018-01-18 12:07:33 -0800178 DeviceAdminService deviceAdminService;
179
180 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800181 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700182
183 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700184 LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700185
Charles Chan82ab1932016-01-30 23:22:37 -0800186 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800187 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700188
189 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800190 public StorageService storageService;
Charles Chandebfea32016-10-24 14:52:01 -0700191
192 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
193 MulticastRouteService multicastRouteService;
194
195 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan83163812018-01-09 13:45:07 -0800196 public TopologyService topologyService;
Charles Chandebfea32016-10-24 14:52:01 -0700197
198 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700199 RouteService routeService;
Charles Chan82ab1932016-01-30 23:22:37 -0800200
201 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800202 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800203
Saurav Dasc3604f12016-03-23 11:22:49 -0700204 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800205 public InterfaceService interfaceService;
206
Pier Luigi580fd8a2018-01-16 10:47:50 +0100207 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
208 public ClusterService clusterService;
209
210 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
211 public LeadershipService leadershipService;
212
Charles Chan873661e2017-11-30 15:37:50 -0800213 @Property(name = "activeProbing", boolValue = true,
214 label = "Enable active probing to discover dual-homed hosts.")
215 boolean activeProbing = true;
216
Charles Chandebfea32016-10-24 14:52:01 -0700217 ArpHandler arpHandler = null;
218 IcmpHandler icmpHandler = null;
219 IpHandler ipHandler = null;
220 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700221 ApplicationId appId;
222 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700223
Charles Chandebfea32016-10-24 14:52:01 -0700224 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700225 private TunnelHandler tunnelHandler = null;
226 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700227 private InternalPacketProcessor processor = null;
228 private InternalLinkListener linkListener = null;
229 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700230 private AppConfigHandler appCfgHandler = null;
Charles Chandebfea32016-10-24 14:52:01 -0700231 XConnectHandler xConnectHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800232 McastHandler mcastHandler = null;
233 HostHandler hostHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800234 private RouteHandler routeHandler = null;
Saurav Dase6c448a2018-01-18 12:07:33 -0800235 LinkHandler linkHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800236 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Pier Ventre6b19e482016-11-07 16:21:04 -0800237 private L2TunnelHandler l2TunnelHandler = null;
sangho80f11cb2015-04-01 13:05:26 -0700238 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan82ab1932016-01-30 23:22:37 -0800239 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700240 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
241 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chandebfea32016-10-24 14:52:01 -0700242 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sangho80f11cb2015-04-01 13:05:26 -0700243
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700244 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700245 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sangho80f11cb2015-04-01 13:05:26 -0700246
Saurav Das2d94d312015-11-24 23:21:05 -0800247 @SuppressWarnings("unused")
sangho80f11cb2015-04-01 13:05:26 -0700248 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das2d94d312015-11-24 23:21:05 -0800249 @SuppressWarnings("rawtypes")
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700250 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Saurav Dase6c448a2018-01-18 12:07:33 -0800251 Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chanb7f75ac2016-01-11 18:28:54 -0800252 new ConcurrentHashMap<>();
253 /**
Saurav Das261c3002017-06-13 15:35:54 -0700254 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700255 * Used to keep track on MPLS group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800256 */
Charles Chan873661e2017-11-30 15:37:50 -0800257 private EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Saurav Das261c3002017-06-13 15:35:54 -0700258 dsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800259 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700260 * Per device next objective ID store with (device id + vlanid) as key.
261 * Used to keep track on L2 flood group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800262 */
Charles Chan873661e2017-11-30 15:37:50 -0800263 private EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800264 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800265 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700266 * Per device next objective ID store with (device id + port + treatment + meta) as key.
267 * Used to keep track on L2 interface group and L3 unicast group information.
Charles Chanb7f75ac2016-01-11 18:28:54 -0800268 */
Charles Chan873661e2017-11-30 15:37:50 -0800269 private EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800270 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800271
Saurav Das2d94d312015-11-24 23:21:05 -0800272 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
273 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700274
Saurav Das261c3002017-06-13 15:35:54 -0700275 private AtomicBoolean programmingScheduled = new AtomicBoolean();
276
Charles Chanc91c8782016-03-30 17:54:24 -0700277 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700278 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
279 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700280 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700281 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800282 public SegmentRoutingDeviceConfig createConfig() {
283 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700284 }
285 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800286
Charles Chanc91c8782016-03-30 17:54:24 -0700287 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700288 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
289 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700290 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800291 @Override
292 public SegmentRoutingAppConfig createConfig() {
293 return new SegmentRoutingAppConfig();
294 }
295 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800296
Charles Chan82f19972016-05-17 13:13:55 -0700297 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
298 new ConfigFactory<ApplicationId, XConnectConfig>(
299 SubjectFactories.APP_SUBJECT_FACTORY,
300 XConnectConfig.class, "xconnect") {
301 @Override
302 public XConnectConfig createConfig() {
303 return new XConnectConfig();
304 }
305 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800306
Charles Chanc91c8782016-03-30 17:54:24 -0700307 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700308 new ConfigFactory<ApplicationId, McastConfig>(
309 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700310 McastConfig.class, "multicast") {
311 @Override
312 public McastConfig createConfig() {
313 return new McastConfig();
314 }
315 };
316
Pier Ventre6b19e482016-11-07 16:21:04 -0800317 private final ConfigFactory<ApplicationId, PwaasConfig> pwaasConfigFactory =
318 new ConfigFactory<ApplicationId, PwaasConfig>(
319 SubjectFactories.APP_SUBJECT_FACTORY,
320 PwaasConfig.class, "pwaas") {
321 @Override
322 public PwaasConfig createConfig() {
323 return new PwaasConfig();
324 }
325 };
326
Charles Chan3ed34d82017-06-22 18:03:14 -0700327 private static final Object THREAD_SCHED_LOCK = new Object();
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700328 private static int numOfEventsQueued = 0;
329 private static int numOfEventsExecuted = 0;
sangho80f11cb2015-04-01 13:05:26 -0700330 private static int numOfHandlerExecution = 0;
331 private static int numOfHandlerScheduled = 0;
332
Charles Chan1963f4f2016-02-18 14:22:42 -0800333 /**
334 * Segment Routing App ID.
335 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800336 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan10b0fb72017-02-02 16:20:42 -0800337
Charles Chanb7f75ac2016-01-11 18:28:54 -0800338 /**
339 * The default VLAN ID assigned to the interfaces without subnet config.
340 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800341 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das7c305372015-10-28 12:39:42 -0700342
sangho80f11cb2015-04-01 13:05:26 -0700343 @Activate
Charles Chan873661e2017-11-30 15:37:50 -0800344 protected void activate(ComponentContext context) {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800345 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700346
347 log.debug("Creating EC map nsnextobjectivestore");
Saurav Das261c3002017-06-13 15:35:54 -0700348 EventuallyConsistentMapBuilder<DestinationSetNextObjectiveStoreKey, NextNeighbors>
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700349 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das261c3002017-06-13 15:35:54 -0700350 dsNextObjStore = nsNextObjMapBuilder
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700351 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700352 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700353 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700354 .build();
Saurav Das261c3002017-06-13 15:35:54 -0700355 log.trace("Current size {}", dsNextObjStore.size());
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700356
Charles Chan10b0fb72017-02-02 16:20:42 -0800357 log.debug("Creating EC map vlannextobjectivestore");
358 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
359 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
360 vlanNextObjStore = vlanNextObjMapBuilder
361 .withName("vlannextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700362 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700363 .withTimestampProvider((k, v) -> new WallClockTimestamp())
364 .build();
365
Saurav Das2d94d312015-11-24 23:21:05 -0800366 log.debug("Creating EC map subnetnextobjectivestore");
367 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
368 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
369 portNextObjStore = portNextObjMapBuilder
370 .withName("portnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700371 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800372 .withTimestampProvider((k, v) -> new WallClockTimestamp())
373 .build();
374
sangho4a5c42a2015-05-20 22:16:38 -0700375 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
376 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700377 tunnelStore = tunnelMapBuilder
378 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700379 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700380 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700381 .build();
382
383 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
384 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700385 policyStore = policyMapBuilder
386 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700387 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700388 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700389 .build();
390
Saurav Dasc3604f12016-03-23 11:22:49 -0700391 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800392 "purgeOnDisconnection", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700393 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800394 "purgeOnDisconnection", "true");
Pier Luigib9632ba2017-01-12 18:14:58 -0800395 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
396 "requestInterceptsEnabled", "false");
Charles Chanc1909e12017-08-08 15:13:37 -0700397 compCfgService.preSetProperty("org.onosproject.net.neighbour.impl.NeighbourResolutionManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800398 "requestInterceptsEnabled", "false");
Charles Chan9597f8d2017-07-24 15:56:10 -0700399 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelayManager",
Pier Luigibc976df2017-01-12 22:46:39 -0800400 "arpEnabled", "false");
Pier Luigi0ebeb312017-02-02 22:31:34 -0800401 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
402 "greedyLearningIpv6", "true");
Charles Chancf8ea472017-02-28 15:15:17 -0800403 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
404 "forceUnprovision", "true");
Charles Chanc7b73c72017-08-10 16:57:28 -0700405 compCfgService.preSetProperty("org.onosproject.routeservice.store.RouteStoreImpl",
Charles Chan4c95c0d2017-07-20 16:16:25 -0700406 "distributed", "true");
Charles Chan804772f2017-08-14 11:42:11 -0700407 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
408 "multihomingEnabled", "true");
Charles Chan0c9c19f2017-11-29 19:54:20 -0800409 compCfgService.preSetProperty("org.onosproject.provider.lldp.impl.LldpLinkProvider",
410 "staleLinkAge", "15000");
411 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
412 "allowDuplicateIps", "false");
Charles Chan873661e2017-11-30 15:37:50 -0800413 compCfgService.registerProperties(getClass());
414 modified(context);
Saurav Dasc3604f12016-03-23 11:22:49 -0700415
Charles Chan2b078ae2015-10-14 11:24:40 -0700416 processor = new InternalPacketProcessor();
417 linkListener = new InternalLinkListener();
418 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700419 appCfgHandler = new AppConfigHandler(this);
420 xConnectHandler = new XConnectHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700421 mcastHandler = new McastHandler(this);
422 hostHandler = new HostHandler(this);
Saurav Dase6c448a2018-01-18 12:07:33 -0800423 linkHandler = new LinkHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700424 routeHandler = new RouteHandler(this);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800425 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800426 l2TunnelHandler = new DefaultL2TunnelHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700427
Charles Chand6d25332016-02-26 22:19:52 -0800428 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700429 cfgService.registerConfigFactory(deviceConfigFactory);
430 cfgService.registerConfigFactory(appConfigFactory);
Charles Chan82f19972016-05-17 13:13:55 -0700431 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700432 cfgService.registerConfigFactory(mcastConfigFactory);
Pier Ventre6b19e482016-11-07 16:21:04 -0800433 cfgService.registerConfigFactory(pwaasConfigFactory);
Saurav Dase321cff2018-02-09 17:26:45 -0800434 log.info("Configuring network before adding listeners");
Charles Chan6961f222018-01-04 14:26:07 -0800435 cfgListener.configureNetwork();
436
Charles Chan82ab1932016-01-30 23:22:37 -0800437 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700438 packetService.addProcessor(processor, PacketProcessor.director(2));
439 linkService.addListener(linkListener);
440 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700441 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700442 routeService.addListener(routeListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700443
Andreas Pantelopoulos75eef062018-01-11 07:53:48 -0800444 l2TunnelHandler.init();
445
sangho80f11cb2015-04-01 13:05:26 -0700446 log.info("Started");
447 }
448
Saurav Dase6c448a2018-01-18 12:07:33 -0800449 KryoNamespace.Builder createSerializer() {
Jonathan Hart54541d12016-04-12 15:39:44 -0700450 return new KryoNamespace.Builder()
451 .register(KryoNamespaces.API)
Saurav Das261c3002017-06-13 15:35:54 -0700452 .register(DestinationSetNextObjectiveStoreKey.class,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800453 VlanNextObjectiveStoreKey.class,
454 DestinationSet.class,
455 NextNeighbors.class,
456 Tunnel.class,
457 DefaultTunnel.class,
458 Policy.class,
459 TunnelPolicy.class,
460 Policy.Type.class,
461 PortNextObjectiveStoreKey.class,
462 XConnectStoreKey.class,
463 L2Tunnel.class,
464 L2TunnelPolicy.class,
465 DefaultL2Tunnel.class,
466 DefaultL2TunnelPolicy.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700467 );
468 }
469
sangho80f11cb2015-04-01 13:05:26 -0700470 @Deactivate
471 protected void deactivate() {
Charles Chan72f556a2015-10-05 17:50:33 -0700472 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700473 cfgService.unregisterConfigFactory(deviceConfigFactory);
474 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chandebfea32016-10-24 14:52:01 -0700475 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700476 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventre6b19e482016-11-07 16:21:04 -0800477 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chan873661e2017-11-30 15:37:50 -0800478 compCfgService.unregisterProperties(getClass(), false);
Charles Chan72f556a2015-10-05 17:50:33 -0700479
Charles Chanfd48c222017-06-19 00:43:31 -0700480 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700481 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700482 linkService.removeListener(linkListener);
483 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700484 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700485 routeService.removeListener(routeListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700486
Charles Chan2e71ef32017-02-23 15:44:08 -0800487 neighbourResolutionService.unregisterNeighbourHandlers(appId);
488
sangho80f11cb2015-04-01 13:05:26 -0700489 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700490 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700491 deviceListener = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700492 groupHandlerMap.clear();
493
Saurav Das261c3002017-06-13 15:35:54 -0700494 dsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800495 vlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700496 portNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700497 tunnelStore.destroy();
498 policyStore.destroy();
Pier Luigi35dab3f2018-01-25 16:16:02 +0100499
500 mcastHandler.terminate();
sangho80f11cb2015-04-01 13:05:26 -0700501 log.info("Stopped");
502 }
503
Charles Chan873661e2017-11-30 15:37:50 -0800504 @Modified
505 private void modified(ComponentContext context) {
506 Dictionary<?, ?> properties = context.getProperties();
507 if (properties == null) {
508 return;
509 }
510
511 String strActiveProving = Tools.get(properties, "activeProbing");
512 boolean expectActiveProbing = Boolean.parseBoolean(strActiveProving);
513
514 if (expectActiveProbing != activeProbing) {
515 activeProbing = expectActiveProbing;
516 log.info("{} active probing", activeProbing ? "Enabling" : "Disabling");
517 }
518 }
519
sangho27462c62015-05-14 00:39:53 -0700520 @Override
521 public List<Tunnel> getTunnels() {
522 return tunnelHandler.getTunnels();
523 }
524
525 @Override
sanghobd812f82015-06-29 14:58:47 -0700526 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
527 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700528 }
529
530 @Override
sanghobd812f82015-06-29 14:58:47 -0700531 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700532 for (Policy policy: policyHandler.getPolicies()) {
533 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
534 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
535 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
536 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700537 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700538 }
539 }
540 }
sanghobd812f82015-06-29 14:58:47 -0700541 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700542 }
543
544 @Override
sanghobd812f82015-06-29 14:58:47 -0700545 public PolicyHandler.Result removePolicy(Policy policy) {
546 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700547 }
548
549 @Override
sanghobd812f82015-06-29 14:58:47 -0700550 public PolicyHandler.Result createPolicy(Policy policy) {
551 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700552 }
553
554 @Override
555 public List<Policy> getPolicies() {
556 return policyHandler.getPolicies();
557 }
558
Saurav Das07c74602016-04-27 18:35:50 -0700559 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800560 public List<L2Tunnel> getL2Tunnels() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700561 return l2TunnelHandler.getL2Tunnels();
562 }
563
564 @Override
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800565 public List<L2TunnelPolicy> getL2Policies() {
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700566 return l2TunnelHandler.getL2Policies();
567 }
568
569 @Override
570 public L2TunnelHandler.Result addPseudowire(String tunnelId, String pwLabel, String cP1,
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -0800571 String cP1InnerVlan, String cP1OuterVlan, String cP2,
572 String cP2InnerVlan, String cP2OuterVlan,
573 String mode, String sdTag) {
Andreas Pantelopoulosafc637f2017-12-20 18:04:27 -0800574 // Try to inject an empty Pwaas config if it is not found for the first time
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700575 PwaasConfig config = cfgService.getConfig(appId(), PwaasConfig.class);
576 if (config == null) {
Andreas Pantelopoulosafc637f2017-12-20 18:04:27 -0800577 log.debug("Pwaas config not found. Try to create an empty one.");
578 cfgService.applyConfig(appId(), PwaasConfig.class, new ObjectMapper().createObjectNode());
579 config = cfgService.getConfig(appId(), PwaasConfig.class);
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700580 }
581
582 ObjectNode object = config.addPseudowire(tunnelId, pwLabel,
583 cP1, cP1InnerVlan, cP1OuterVlan,
584 cP2, cP2InnerVlan, cP2OuterVlan,
585 mode, sdTag);
586 if (object == null) {
587 log.warn("Could not add pseudowire to the configuration!");
588 return L2TunnelHandler.Result.ADDITION_ERROR;
589 }
590
591 // inform everyone about the valid change in the pw configuration
592 cfgService.applyConfig(appId(), PwaasConfig.class, object);
593 return L2TunnelHandler.Result.SUCCESS;
594 }
595
596 @Override
597 public L2TunnelHandler.Result removePseudowire(String pwId) {
598
599 PwaasConfig config = cfgService.getConfig(appId(), PwaasConfig.class);
600 if (config == null) {
601 log.warn("Configuration for Pwaas class could not be found!");
602 return L2TunnelHandler.Result.CONFIG_NOT_FOUND;
603 }
604
605 ObjectNode object = config.removePseudowire(pwId);
606 if (object == null) {
607 log.warn("Could not delete pseudowire from configuration!");
608 return L2TunnelHandler.Result.REMOVAL_ERROR;
609 }
610
611 // sanity check, this should never fail since we removed a pw
612 // and we always check when we update the configuration
613 config.isValid();
614
615 // inform everyone
616 cfgService.applyConfig(appId(), PwaasConfig.class, object);
617
618 return L2TunnelHandler.Result.SUCCESS;
619 }
620
621 @Override
Saurav Das07c74602016-04-27 18:35:50 -0700622 public void rerouteNetwork() {
623 cfgListener.configureNetwork();
Saurav Das07c74602016-04-27 18:35:50 -0700624 }
625
Charles Chand7844e52016-10-20 17:02:44 -0700626 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800627 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
628 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Jonathan Hart61e24e12017-11-30 18:23:42 -0800629 deviceConfiguration.getRouters().forEach(device ->
630 deviceSubnetMap.put(device, deviceConfiguration.getSubnets(device)));
Charles Chand7844e52016-10-20 17:02:44 -0700631 return deviceSubnetMap;
632 }
633
Saurav Das62ae6792017-05-15 15:34:25 -0700634
635 @Override
636 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
637 if (defaultRoutingHandler != null) {
638 return defaultRoutingHandler.getCurrentEmcpSpgMap();
639 } else {
640 return null;
641 }
642 }
643
644 @Override
Saurav Das261c3002017-06-13 15:35:54 -0700645 public ImmutableMap<DestinationSetNextObjectiveStoreKey, NextNeighbors> getDestinationSet() {
646 if (dsNextObjStore != null) {
647 return ImmutableMap.copyOf(dsNextObjStore.entrySet());
Saurav Das62ae6792017-05-15 15:34:25 -0700648 } else {
649 return ImmutableMap.of();
650 }
651 }
652
Saurav Dasfbe74572017-08-03 18:30:35 -0700653 @Override
654 public void verifyGroups(DeviceId id) {
655 DefaultGroupHandler gh = groupHandlerMap.get(id);
656 if (gh != null) {
657 gh.triggerBucketCorrector();
658 }
659 }
660
Saurav Das6430f412018-01-25 09:49:01 -0800661 @Override
662 public ImmutableMap<Link, Boolean> getSeenLinks() {
663 return linkHandler.getSeenLinks();
664 }
665
666 @Override
667 public ImmutableMap<DeviceId, Set<PortNumber>> getDownedPortState() {
668 return linkHandler.getDownedPorts();
669 }
670
Pier Luigi0f9635b2018-01-15 18:06:43 +0100671 @Override
672 public Map<McastStoreKey, Integer> getMcastNextIds(IpAddress mcastIp) {
673 return mcastHandler.getMcastNextIds(mcastIp);
674 }
675
676 @Override
677 public Map<McastStoreKey, McastHandler.McastRole> getMcastRoles(IpAddress mcastIp) {
678 return mcastHandler.getMcastRoles(mcastIp);
679 }
680
681 @Override
682 public Map<ConnectPoint, List<ConnectPoint>> getMcastPaths(IpAddress mcastIp) {
683 return mcastHandler.getMcastPaths(mcastIp);
684 }
685
sangho80f1f892015-05-19 11:57:42 -0700686 /**
Ray Milkeyb85de082017-04-05 09:42:04 -0700687 * Extracts the application ID from the manager.
688 *
689 * @return application ID
690 */
691 public ApplicationId appId() {
692 return appId;
693 }
694
695 /**
696 * Returns the device configuration.
697 *
698 * @return device configuration
699 */
700 public DeviceConfiguration deviceConfiguration() {
701 return deviceConfiguration;
702 }
703
704 /**
Saurav Das261c3002017-06-13 15:35:54 -0700705 * Per device next objective ID store with (device id + destination set) as key.
Charles Chan53de91f2017-08-22 15:07:34 -0700706 * Used to keep track on MPLS group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700707 *
708 * @return next objective ID store
709 */
Saurav Das261c3002017-06-13 15:35:54 -0700710 public EventuallyConsistentMap<DestinationSetNextObjectiveStoreKey, NextNeighbors>
711 dsNextObjStore() {
712 return dsNextObjStore;
Ray Milkeyb85de082017-04-05 09:42:04 -0700713 }
714
715 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700716 * Per device next objective ID store with (device id + vlanid) as key.
717 * Used to keep track on L2 flood group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700718 *
719 * @return vlan next object store
720 */
721 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
722 return vlanNextObjStore;
723 }
724
725 /**
Charles Chan53de91f2017-08-22 15:07:34 -0700726 * Per device next objective ID store with (device id + port + treatment + meta) as key.
727 * Used to keep track on L2 interface group and L3 unicast group information.
Ray Milkeyb85de082017-04-05 09:42:04 -0700728 *
729 * @return port next object store.
730 */
731 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
732 return portNextObjStore;
733 }
734
735 /**
Saurav Das261c3002017-06-13 15:35:54 -0700736 * Returns the MPLS-ECMP configuration which indicates whether ECMP on
737 * labeled packets should be programmed or not.
Pier Ventre7a78de22016-10-31 15:00:01 -0700738 *
739 * @return MPLS-ECMP value
740 */
741 public boolean getMplsEcmp() {
742 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
743 .getConfig(this.appId, SegmentRoutingAppConfig.class);
744 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
745 }
746
747 /**
sangho80f1f892015-05-19 11:57:42 -0700748 * Returns the tunnel object with the tunnel ID.
749 *
750 * @param tunnelId Tunnel ID
751 * @return Tunnel reference
752 */
sangho27462c62015-05-14 00:39:53 -0700753 public Tunnel getTunnel(String tunnelId) {
754 return tunnelHandler.getTunnel(tunnelId);
755 }
756
Charles Chan90772a72017-02-08 15:52:08 -0800757 // TODO Consider moving these to InterfaceService
sangho80f11cb2015-04-01 13:05:26 -0700758 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800759 * Returns untagged VLAN configured on given connect point.
Charles Chan90772a72017-02-08 15:52:08 -0800760 * <p>
761 * Only returns the first match if there are multiple untagged VLAN configured
762 * on the connect point.
sangho80f11cb2015-04-01 13:05:26 -0700763 *
Charles Chan10b0fb72017-02-02 16:20:42 -0800764 * @param connectPoint connect point
765 * @return untagged VLAN or null if not configured
sangho80f11cb2015-04-01 13:05:26 -0700766 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700767 VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800768 return interfaceService.getInterfacesByPort(connectPoint).stream()
769 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
770 .map(Interface::vlanUntagged)
771 .findFirst().orElse(null);
sangho80f11cb2015-04-01 13:05:26 -0700772 }
773
sangho27462c62015-05-14 00:39:53 -0700774 /**
Charles Chan90772a72017-02-08 15:52:08 -0800775 * Returns tagged VLAN configured on given connect point.
776 * <p>
777 * Returns all matches if there are multiple tagged VLAN configured
778 * on the connect point.
779 *
780 * @param connectPoint connect point
781 * @return tagged VLAN or empty set if not configured
782 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700783 Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
Charles Chan90772a72017-02-08 15:52:08 -0800784 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
785 return interfaces.stream()
786 .map(Interface::vlanTagged)
Charles Chan3ed34d82017-06-22 18:03:14 -0700787 .flatMap(Set::stream)
Charles Chan90772a72017-02-08 15:52:08 -0800788 .collect(Collectors.toSet());
789 }
790
791 /**
792 * Returns native VLAN configured on given connect point.
793 * <p>
794 * Only returns the first match if there are multiple native VLAN configured
795 * on the connect point.
796 *
797 * @param connectPoint connect point
798 * @return native VLAN or null if not configured
799 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700800 VlanId getNativeVlanId(ConnectPoint connectPoint) {
Charles Chan90772a72017-02-08 15:52:08 -0800801 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
802 return interfaces.stream()
803 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
804 .map(Interface::vlanNative)
805 .findFirst()
806 .orElse(null);
807 }
808
809 /**
Charles Chand9265a32017-06-16 15:19:24 -0700810 * Returns internal VLAN for untagged hosts on given connect point.
811 * <p>
812 * The internal VLAN is either vlan-untagged for an access port,
813 * or vlan-native for a trunk port.
814 *
815 * @param connectPoint connect point
816 * @return internal VLAN or null if both vlan-untagged and vlan-native are undefined
817 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700818 VlanId getInternalVlanId(ConnectPoint connectPoint) {
Charles Chand9265a32017-06-16 15:19:24 -0700819 VlanId untaggedVlanId = getUntaggedVlanId(connectPoint);
820 VlanId nativeVlanId = getNativeVlanId(connectPoint);
821 return untaggedVlanId != null ? untaggedVlanId : nativeVlanId;
822 }
823
824 /**
Charles Chan3ed34d82017-06-22 18:03:14 -0700825 * Returns optional pair device ID of given device.
826 *
827 * @param deviceId device ID
828 * @return optional pair device ID. Might be empty if pair device is not configured
829 */
830 Optional<DeviceId> getPairDeviceId(DeviceId deviceId) {
831 SegmentRoutingDeviceConfig deviceConfig =
832 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
833 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairDeviceId);
834 }
835 /**
836 * Returns optional pair device local port of given device.
837 *
838 * @param deviceId device ID
839 * @return optional pair device ID. Might be empty if pair device is not configured
840 */
841 Optional<PortNumber> getPairLocalPorts(DeviceId deviceId) {
842 SegmentRoutingDeviceConfig deviceConfig =
843 cfgService.getConfig(deviceId, SegmentRoutingDeviceConfig.class);
844 return Optional.ofNullable(deviceConfig).map(SegmentRoutingDeviceConfig::pairLocalPort);
845 }
846
847 /**
Charles Chan910be6a2017-08-23 14:46:43 -0700848 * Determine if current instance is the master of given connect point.
849 *
850 * @param cp connect point
851 * @return true if current instance is the master of given connect point
852 */
Andreas Pantelopoulos5e7be3d2017-10-23 12:18:25 -0700853 public boolean isMasterOf(ConnectPoint cp) {
Charles Chan910be6a2017-08-23 14:46:43 -0700854 boolean isMaster = mastershipService.isLocalMaster(cp.deviceId());
855 if (!isMaster) {
856 log.debug(NOT_MASTER, cp);
857 }
858 return isMaster;
859 }
860
861 /**
Charles Chanf0ae41e2017-08-23 13:55:39 -0700862 * Returns locations of given resolved route.
863 *
864 * @param resolvedRoute resolved route
865 * @return locations of nexthop. Might be empty if next hop is not found
866 */
867 Set<ConnectPoint> nextHopLocations(ResolvedRoute resolvedRoute) {
868 HostId hostId = HostId.hostId(resolvedRoute.nextHopMac(), resolvedRoute.nextHopVlan());
869 return Optional.ofNullable(hostService.getHost(hostId))
870 .map(Host::locations).orElse(Sets.newHashSet())
871 .stream().map(l -> (ConnectPoint) l).collect(Collectors.toSet());
872 }
873
874 /**
Charles Chan90772a72017-02-08 15:52:08 -0800875 * Returns vlan port map of given device.
876 *
877 * @param deviceId device id
878 * @return vlan-port multimap
879 */
880 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
881 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
882
883 interfaceService.getInterfaces().stream()
884 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
885 .forEach(intf -> {
886 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
Charles Chan3ed34d82017-06-22 18:03:14 -0700887 intf.vlanTagged().forEach(vlanTagged ->
888 vlanPortMap.put(vlanTagged, intf.connectPoint().port())
889 );
Charles Chan90772a72017-02-08 15:52:08 -0800890 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
891 });
892 vlanPortMap.removeAll(VlanId.NONE);
893
894 return vlanPortMap;
895 }
896
897 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800898 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -0800899 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -0700900 *
901 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -0800902 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -0800903 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -0700904 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700905 int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -0700906 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800907 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
908 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -0700909 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -0800910 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -0800911 + "device {} not found", deviceId);
912 return -1;
913 }
914 }
915
916 /**
917 * Returns the next objective ID for the given portNumber, given the treatment.
918 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -0700919 * would result in different objectives. If the next object does not exist,
920 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -0800921 *
922 * @param deviceId Device ID
923 * @param portNum port number on device for which NextObjective is queried
924 * @param treatment the actions to apply on the packets (should include outport)
925 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -0700926 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -0700927 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -0800928 */
929 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
930 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -0700931 TrafficSelector meta,
932 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -0800933 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
934 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -0700935 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -0800936 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -0800937 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
938 + " not found", deviceId);
939 return -1;
940 }
941 }
942
Saurav Das62ae6792017-05-15 15:34:25 -0700943 /**
944 * Returns the group handler object for the specified device id.
945 *
946 * @param devId the device identifier
947 * @return the groupHandler object for the device id, or null if not found
948 */
Charles Chan3ed34d82017-06-22 18:03:14 -0700949 DefaultGroupHandler getGroupHandler(DeviceId devId) {
Saurav Das62ae6792017-05-15 15:34:25 -0700950 return groupHandlerMap.get(devId);
951 }
952
953 /**
Saurav Dasfbe74572017-08-03 18:30:35 -0700954 * Returns the default routing handler object.
955 *
956 * @return the default routing handler object
957 */
958 public DefaultRoutingHandler getRoutingHandler() {
959 return defaultRoutingHandler;
960 }
961
sangho80f11cb2015-04-01 13:05:26 -0700962 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -0700963 @Override
964 public void process(PacketContext context) {
965
966 if (context.isHandled()) {
967 return;
968 }
969
970 InboundPacket pkt = context.inPacket();
971 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -0800972
973 if (ethernet == null) {
974 return;
975 }
976
Saurav Das261c3002017-06-13 15:35:54 -0700977 log.trace("Rcvd pktin from {}: {}", context.inPacket().receivedFrom(),
978 ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -0800979 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -0700980 log.warn("Received unexpected ARP packet on {}",
981 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -0700982 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800983 return;
sangho80f11cb2015-04-01 13:05:26 -0700984 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -0800985 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
986 //ipHandler.addToPacketBuffer(ipv4Packet);
987 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
988 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -0700989 } else {
Charles Chand041ad82017-01-13 17:20:44 -0800990 // NOTE: We don't support IP learning at this moment so this
991 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800992 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -0700993 }
Pier Ventreb6a7f342016-11-26 21:05:22 -0800994 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
995 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -0800996 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -0800997 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -0800998 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
999 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
1000 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
1001 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
1002 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
1003 } else {
Saurav Das62ae6792017-05-15 15:34:25 -07001004 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -07001005 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -08001006 }
1007 } else {
1008 // NOTE: We don't support IP learning at this moment so this
1009 // is not necessary. Also it causes duplication of DHCPv6 packets.
1010 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
1011 }
sangho80f11cb2015-04-01 13:05:26 -07001012 }
1013 }
1014 }
1015
1016 private class InternalLinkListener implements LinkListener {
1017 @Override
1018 public void event(LinkEvent event) {
Charles Chan0cff8aa2017-03-29 16:39:05 -07001019 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1020 event.type() == LinkEvent.Type.LINK_UPDATED ||
1021 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001022 log.debug("Event {} received from Link Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001023 scheduleEventHandlerIfNotScheduled(event);
1024 }
1025 }
1026 }
1027
1028 private class InternalDeviceListener implements DeviceListener {
sangho80f11cb2015-04-01 13:05:26 -07001029 @Override
1030 public void event(DeviceEvent event) {
sangho80f11cb2015-04-01 13:05:26 -07001031 switch (event.type()) {
1032 case DEVICE_ADDED:
Saurav Dasf0f592d2016-11-18 15:21:57 -08001033 case PORT_UPDATED:
1034 case PORT_ADDED:
sanghofb7c7292015-04-13 15:15:58 -07001035 case DEVICE_UPDATED:
1036 case DEVICE_AVAILABILITY_CHANGED:
Saurav Das62ae6792017-05-15 15:34:25 -07001037 log.trace("Event {} received from Device Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -07001038 scheduleEventHandlerIfNotScheduled(event);
1039 break;
1040 default:
1041 }
1042 }
1043 }
1044
Saurav Das2d94d312015-11-24 23:21:05 -08001045 @SuppressWarnings("rawtypes")
sangho80f11cb2015-04-01 13:05:26 -07001046 private void scheduleEventHandlerIfNotScheduled(Event event) {
Charles Chan3ed34d82017-06-22 18:03:14 -07001047 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001048 eventQueue.add(event);
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001049 numOfEventsQueued++;
1050
1051 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
1052 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001053 eventHandlerFuture = executorService
1054 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
1055 numOfHandlerScheduled++;
1056 }
Jonathan Hart54541d12016-04-12 15:39:44 -07001057 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001058 numOfEventsQueued,
1059 numOfHandlerScheduled);
sangho80f11cb2015-04-01 13:05:26 -07001060 }
sangho80f11cb2015-04-01 13:05:26 -07001061 }
1062
1063 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -07001064 @Override
sangho80f11cb2015-04-01 13:05:26 -07001065 public void run() {
Pier Luigi8f362362018-02-12 09:56:20 +01001066 while (true) {
1067 try {
Saurav Das2d94d312015-11-24 23:21:05 -08001068 @SuppressWarnings("rawtypes")
Charles Chan3ed34d82017-06-22 18:03:14 -07001069 Event event;
1070 synchronized (THREAD_SCHED_LOCK) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001071 if (!eventQueue.isEmpty()) {
1072 event = eventQueue.poll();
1073 numOfEventsExecuted++;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001074 } else {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001075 numOfHandlerExecution++;
1076 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
1077 numOfHandlerExecution, numOfEventsExecuted);
1078 break;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001079 }
sanghofb7c7292015-04-13 15:15:58 -07001080 }
Charles Chan0cff8aa2017-03-29 16:39:05 -07001081 if (event.type() == LinkEvent.Type.LINK_ADDED ||
1082 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Dase6c448a2018-01-18 12:07:33 -08001083 linkHandler.processLinkAdded((Link) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001084 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Saurav Dase6c448a2018-01-18 12:07:33 -08001085 linkHandler.processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001086 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
1087 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
1088 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das62af8802015-12-04 10:52:59 -08001089 DeviceId deviceId = ((Device) event.subject()).id();
1090 if (deviceService.isAvailable(deviceId)) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001091 log.info("Processing device event {} for available device {}",
1092 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001093 processDeviceAdded((Device) event.subject());
Saurav Dasc3604f12016-03-23 11:22:49 -07001094 } else {
1095 log.info("Processing device event {} for unavailable device {}",
Pier Luigi8f362362018-02-12 09:56:20 +01001096 event.type(), ((Device) event.subject()).id());
Saurav Dasc3604f12016-03-23 11:22:49 -07001097 processDeviceRemoved((Device) event.subject());
1098 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001099 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd1872b02016-12-02 15:43:47 -08001100 // typically these calls come when device is added first time
1101 // so port filtering rules are handled at the device_added event.
1102 // port added calls represent all ports on the device,
1103 // enabled or not.
Saurav Das62ae6792017-05-15 15:34:25 -07001104 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd1872b02016-12-02 15:43:47 -08001105 ((DeviceEvent) event).subject().id(),
1106 ((DeviceEvent) event).port().number(),
1107 event.type());
Saurav Dasf0f592d2016-11-18 15:21:57 -08001108 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd1872b02016-12-02 15:43:47 -08001109 // these calls happen for every subsequent event
1110 // ports enabled, disabled, switch goes away, comes back
Saurav Dasf0f592d2016-11-18 15:21:57 -08001111 log.info("** PORT UPDATED {}/{} -> {}",
1112 event.subject(),
1113 ((DeviceEvent) event).port(),
1114 event.type());
1115 processPortUpdated(((Device) event.subject()),
1116 ((DeviceEvent) event).port());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001117 } else {
1118 log.warn("Unhandled event type: {}", event.type());
1119 }
Pier Luigi8f362362018-02-12 09:56:20 +01001120 } catch (Exception e) {
1121 log.error("SegmentRouting event handler thread thrown an exception: {}",
1122 e.getMessage(), e);
sangho80f11cb2015-04-01 13:05:26 -07001123 }
1124 }
sangho80f11cb2015-04-01 13:05:26 -07001125 }
1126 }
1127
Saurav Dase6c448a2018-01-18 12:07:33 -08001128 void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001129 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan9bd6aea2017-06-27 18:48:32 -07001130
1131 // NOTE: Punt ARP/NDP even when the device is not configured.
1132 // Host learning without network config is required for CORD config generator.
1133 routingRulePopulator.populateIpPunts(device.id());
1134 routingRulePopulator.populateArpNdpPunts(device.id());
1135
Charles Chan319d1a22015-11-03 10:42:14 -08001136 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das261c3002017-06-13 15:35:54 -07001137 log.warn("Device configuration unavailable. Device {} will be "
1138 + "processed after configuration.", device.id());
Saurav Das8ec0ec42015-11-03 14:39:27 -08001139 return;
1140 }
Charles Chan72779502016-04-23 17:36:10 -07001141 processDeviceAddedInternal(device.id());
1142 }
1143
1144 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001145 // Irrespective of whether the local is a MASTER or not for this device,
1146 // we need to create a SR-group-handler instance. This is because in a
1147 // multi-instance setup, any instance can initiate forwarding/next-objectives
1148 // for any switch (even if this instance is a SLAVE or not even connected
1149 // to the switch). To handle this, a default-group-handler instance is necessary
1150 // per switch.
Charles Chan72779502016-04-23 17:36:10 -07001151 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1152 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001153 DefaultGroupHandler groupHandler;
1154 try {
1155 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001156 createGroupHandler(deviceId,
1157 appId,
1158 deviceConfiguration,
1159 linkService,
1160 flowObjectiveService,
1161 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001162 } catch (DeviceConfigNotFoundException e) {
1163 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1164 return;
1165 }
Saurav Das2b6a00f2017-12-05 15:00:23 -08001166 log.debug("updating groupHandlerMap with new grpHdlr for device: {}",
Charles Chan72779502016-04-23 17:36:10 -07001167 deviceId);
1168 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001169 }
Saurav Dasb149be12016-06-07 10:08:06 -07001170
Charles Chan72779502016-04-23 17:36:10 -07001171 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001172 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chandebfea32016-10-24 14:52:01 -07001173 hostHandler.init(deviceId);
Charles Chan82f19972016-05-17 13:13:55 -07001174 xConnectHandler.init(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001175 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001176 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001177 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001178 }
Charles Chan82ab1932016-01-30 23:22:37 -08001179
Charles Chandebfea32016-10-24 14:52:01 -07001180 appCfgHandler.init(deviceId);
1181 routeHandler.init(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001182 }
1183
Saurav Dasc3604f12016-03-23 11:22:49 -07001184 private void processDeviceRemoved(Device device) {
Saurav Das261c3002017-06-13 15:35:54 -07001185 dsNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001186 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan873661e2017-11-30 15:37:50 -08001187 .forEach(entry -> dsNextObjStore.remove(entry.getKey()));
Charles Chan10b0fb72017-02-02 16:20:42 -08001188 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001189 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001190 .forEach(entry -> vlanNextObjStore.remove(entry.getKey()));
Saurav Dasc3604f12016-03-23 11:22:49 -07001191 portNextObjStore.entrySet().stream()
1192 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
Charles Chan3ed34d82017-06-22 18:03:14 -07001193 .forEach(entry -> portNextObjStore.remove(entry.getKey()));
Saurav Dase6c448a2018-01-18 12:07:33 -08001194 linkHandler.processDeviceRemoved(device);
Charles Chan17d75d82017-06-15 00:44:51 -07001195
Saurav Dasfbe74572017-08-03 18:30:35 -07001196 DefaultGroupHandler gh = groupHandlerMap.remove(device.id());
1197 if (gh != null) {
1198 gh.shutdown();
1199 }
Saurav Das62ae6792017-05-15 15:34:25 -07001200 // Note that a switch going down is associated with all of its links
1201 // going down as well, but it is treated as a single switch down event
1202 // while the link-downs are ignored.
1203 defaultRoutingHandler
1204 .populateRoutingRulesForLinkStatusChange(null, null, device.id());
Saurav Das6430f412018-01-25 09:49:01 -08001205 defaultRoutingHandler.purgeEcmpGraph(device.id());
Pier Luigi580fd8a2018-01-16 10:47:50 +01001206 mcastHandler.processDeviceDown(device.id());
Charles Chan82f19972016-05-17 13:13:55 -07001207 xConnectHandler.removeDevice(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -07001208 }
1209
Saurav Dasf0f592d2016-11-18 15:21:57 -08001210 private void processPortUpdated(Device device, Port port) {
1211 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1212 log.warn("Device configuration uploading. Not handling port event for"
1213 + "dev: {} port: {}", device.id(), port.number());
1214 return;
1215 }
Saurav Dasf9332192017-02-18 14:05:44 -08001216
1217 if (!mastershipService.isLocalMaster(device.id())) {
1218 log.debug("Not master for dev:{} .. not handling port updated event"
1219 + "for port {}", device.id(), port.number());
1220 return;
1221 }
1222
1223 // first we handle filtering rules associated with the port
1224 if (port.isEnabled()) {
1225 log.info("Switchport {}/{} enabled..programming filters",
1226 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001227 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001228 } else {
1229 log.info("Switchport {}/{} disabled..removing filters",
1230 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001231 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001232 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001233
1234 // portUpdated calls are for ports that have gone down or up. For switch
1235 // to switch ports, link-events should take care of any re-routing or
1236 // group editing necessary for port up/down. Here we only process edge ports
1237 // that are already configured.
Saurav Das3fb28272017-03-04 16:08:47 -08001238 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1239 VlanId untaggedVlan = getUntaggedVlanId(cp);
1240 VlanId nativeVlan = getNativeVlanId(cp);
1241 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001242
Saurav Das3fb28272017-03-04 16:08:47 -08001243 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001244 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasf0f592d2016-11-18 15:21:57 -08001245 + "dev/port: {}/{}", device.id(), port.number());
1246 return;
1247 }
Saurav Das3fb28272017-03-04 16:08:47 -08001248 if (untaggedVlan != null) {
1249 processEdgePort(device, port, untaggedVlan, true);
1250 }
1251 if (nativeVlan != null) {
1252 processEdgePort(device, port, nativeVlan, true);
1253 }
1254 if (!taggedVlans.isEmpty()) {
1255 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1256 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001257 }
1258
Saurav Das3fb28272017-03-04 16:08:47 -08001259 private void processEdgePort(Device device, Port port, VlanId vlanId,
1260 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001261 boolean portUp = port.isEnabled();
1262 if (portUp) {
Saurav Das3fb28272017-03-04 16:08:47 -08001263 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001264 port.number(), vlanId);
Charles Chan873661e2017-11-30 15:37:50 -08001265 hostHandler.processPortUp(new ConnectPoint(device.id(), port.number()));
Saurav Dasf0f592d2016-11-18 15:21:57 -08001266 } else {
Saurav Das3fb28272017-03-04 16:08:47 -08001267 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001268 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001269 }
1270
Srikanth Vavilapalli64505482015-04-21 13:04:13 -07001271 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sangho80f11cb2015-04-01 13:05:26 -07001272 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001273 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001274 } else {
1275 log.warn("Group handler not found for dev:{}. Not handling edge port"
1276 + " {} event for port:{}", device.id(),
1277 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001278 }
1279 }
sangho27462c62015-05-14 00:39:53 -07001280
Charles Chan8ca5a122017-10-20 16:06:55 -07001281 private void createOrUpdateDeviceConfiguration() {
1282 if (deviceConfiguration == null) {
Saurav Dase321cff2018-02-09 17:26:45 -08001283 log.info("Creating new DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001284 deviceConfiguration = new DeviceConfiguration(this);
1285 } else {
Saurav Dase321cff2018-02-09 17:26:45 -08001286 log.info("Updating DeviceConfiguration");
Charles Chan8ca5a122017-10-20 16:06:55 -07001287 deviceConfiguration.updateConfig();
1288 }
1289 }
1290
Pier Ventreb6a7f342016-11-26 21:05:22 -08001291 /**
1292 * Registers the given connect point with the NRS, this is necessary
1293 * to receive the NDP and ARP packets from the NRS.
1294 *
1295 * @param portToRegister connect point to register
1296 */
1297 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001298 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001299 portToRegister,
1300 neighbourHandler,
1301 appId
1302 );
1303 }
1304
Charles Chan72f556a2015-10-05 17:50:33 -07001305 private class InternalConfigListener implements NetworkConfigListener {
Saurav Das261c3002017-06-13 15:35:54 -07001306 private static final long PROGRAM_DELAY = 2;
Charles Chan46fdfaf2016-11-09 20:51:44 -08001307 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001308
Charles Chanb7f75ac2016-01-11 18:28:54 -08001309 /**
1310 * Constructs the internal network config listener.
1311 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001312 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001313 */
Charles Chan3ed34d82017-06-22 18:03:14 -07001314 InternalConfigListener(SegmentRoutingManager srManager) {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001315 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001316 }
1317
Charles Chanb7f75ac2016-01-11 18:28:54 -08001318 /**
1319 * Reads network config and initializes related data structure accordingly.
1320 */
Charles Chan873661e2017-11-30 15:37:50 -08001321 void configureNetwork() {
Saurav Dase321cff2018-02-09 17:26:45 -08001322 log.info("Configuring network ...");
Charles Chan8ca5a122017-10-20 16:06:55 -07001323 createOrUpdateDeviceConfiguration();
Charles Chane7c61022015-10-07 14:21:45 -07001324
Charles Chan46fdfaf2016-11-09 20:51:44 -08001325 arpHandler = new ArpHandler(srManager);
1326 icmpHandler = new IcmpHandler(srManager);
1327 ipHandler = new IpHandler(srManager);
1328 routingRulePopulator = new RoutingRulePopulator(srManager);
1329 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chane7c61022015-10-07 14:21:45 -07001330
1331 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1332 groupHandlerMap, tunnelStore);
1333 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1334 flowObjectiveService,
1335 tunnelHandler, policyStore);
Saurav Das261c3002017-06-13 15:35:54 -07001336 // add a small delay to absorb multiple network config added notifications
1337 if (!programmingScheduled.get()) {
Saurav Dase321cff2018-02-09 17:26:45 -08001338 log.info("Buffering config calls for {} secs", PROGRAM_DELAY);
Saurav Das261c3002017-06-13 15:35:54 -07001339 programmingScheduled.set(true);
1340 executorService.schedule(new ConfigChange(), PROGRAM_DELAY,
1341 TimeUnit.SECONDS);
Charles Chane7c61022015-10-07 14:21:45 -07001342 }
Charles Chan72779502016-04-23 17:36:10 -07001343 mcastHandler.init();
Charles Chane7c61022015-10-07 14:21:45 -07001344 }
1345
Charles Chan72f556a2015-10-05 17:50:33 -07001346 @Override
1347 public void event(NetworkConfigEvent event) {
Charles Chan82ab1932016-01-30 23:22:37 -08001348 // TODO move this part to NetworkConfigEventHandler
1349 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1350 switch (event.type()) {
1351 case CONFIG_ADDED:
Charles Chan68363b12017-06-26 15:25:09 -07001352 log.info("Segment Routing Device Config added for {}", event.subject());
Charles Chan82ab1932016-01-30 23:22:37 -08001353 configureNetwork();
1354 break;
1355 case CONFIG_UPDATED:
Charles Chan68363b12017-06-26 15:25:09 -07001356 log.info("Segment Routing Config updated for {}", event.subject());
Charles Chan8ca5a122017-10-20 16:06:55 -07001357 createOrUpdateDeviceConfiguration();
Charles Chan68363b12017-06-26 15:25:09 -07001358 // TODO support dynamic configuration
1359 break;
1360 default:
1361 break;
1362 }
1363 } else if (event.configClass().equals(InterfaceConfig.class)) {
1364 switch (event.type()) {
1365 case CONFIG_ADDED:
1366 log.info("Interface Config added for {}", event.subject());
1367 configureNetwork();
1368 break;
1369 case CONFIG_UPDATED:
1370 log.info("Interface Config updated for {}", event.subject());
Charles Chan8ca5a122017-10-20 16:06:55 -07001371 createOrUpdateDeviceConfiguration();
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001372
1373 // Following code will be uncommented when [CORD-634] is fully implemented.
1374 // [CORD-634] Add dynamic config support for interfaces
1375 updateInterface((InterfaceConfig) event.config().get(),
1376 (InterfaceConfig) event.prevConfig().get());
Charles Chan82ab1932016-01-30 23:22:37 -08001377 // TODO support dynamic configuration
1378 break;
1379 default:
1380 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001381 }
Charles Chan82ab1932016-01-30 23:22:37 -08001382 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chan82f19972016-05-17 13:13:55 -07001383 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan82ab1932016-01-30 23:22:37 -08001384 switch (event.type()) {
1385 case CONFIG_ADDED:
Charles Chan82f19972016-05-17 13:13:55 -07001386 appCfgHandler.processAppConfigAdded(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001387 break;
1388 case CONFIG_UPDATED:
Charles Chan82f19972016-05-17 13:13:55 -07001389 appCfgHandler.processAppConfigUpdated(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001390 break;
1391 case CONFIG_REMOVED:
Charles Chan82f19972016-05-17 13:13:55 -07001392 appCfgHandler.processAppConfigRemoved(event);
1393 break;
1394 default:
1395 break;
1396 }
Saurav Dase321cff2018-02-09 17:26:45 -08001397 log.info("App config event .. configuring network");
Charles Chandebfea32016-10-24 14:52:01 -07001398 configureNetwork();
Charles Chan82f19972016-05-17 13:13:55 -07001399 } else if (event.configClass().equals(XConnectConfig.class)) {
1400 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1401 switch (event.type()) {
1402 case CONFIG_ADDED:
1403 xConnectHandler.processXConnectConfigAdded(event);
1404 break;
1405 case CONFIG_UPDATED:
1406 xConnectHandler.processXConnectConfigUpdated(event);
1407 break;
1408 case CONFIG_REMOVED:
1409 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001410 break;
1411 default:
1412 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001413 }
Pier Ventre6b19e482016-11-07 16:21:04 -08001414 } else if (event.configClass().equals(PwaasConfig.class)) {
Andreas Pantelopoulos4c7de132018-02-22 12:32:42 -08001415 checkState(l2TunnelHandler != null, "DefaultL2TunnelHandler is not initialized");
Pier Ventre6b19e482016-11-07 16:21:04 -08001416 switch (event.type()) {
1417 case CONFIG_ADDED:
1418 l2TunnelHandler.processPwaasConfigAdded(event);
1419 break;
1420 case CONFIG_UPDATED:
1421 l2TunnelHandler.processPwaasConfigUpdated(event);
1422 break;
1423 case CONFIG_REMOVED:
1424 l2TunnelHandler.processPwaasConfigRemoved(event);
1425 break;
1426 default:
1427 break;
1428 }
Charles Chan72f556a2015-10-05 17:50:33 -07001429 }
1430 }
Saurav Das261c3002017-06-13 15:35:54 -07001431
Charles Chanc4a68c32018-01-03 16:26:32 -08001432 @Override
1433 public boolean isRelevant(NetworkConfigEvent event) {
Saurav Dase321cff2018-02-09 17:26:45 -08001434 if (event.type() == CONFIG_REGISTERED ||
1435 event.type() == CONFIG_UNREGISTERED) {
1436 log.debug("Ignore event {} due to type mismatch", event);
1437 return false;
Charles Chanc4a68c32018-01-03 16:26:32 -08001438 }
Saurav Dase321cff2018-02-09 17:26:45 -08001439
1440 if (!event.configClass().equals(SegmentRoutingDeviceConfig.class) &&
1441 !event.configClass().equals(SegmentRoutingAppConfig.class) &&
1442 !event.configClass().equals(InterfaceConfig.class) &&
1443 !event.configClass().equals(XConnectConfig.class) &&
1444 !event.configClass().equals(PwaasConfig.class)) {
1445 log.debug("Ignore event {} due to class mismatch", event);
1446 return false;
1447 }
1448
1449 return true;
Charles Chanc4a68c32018-01-03 16:26:32 -08001450 }
1451
Saurav Das261c3002017-06-13 15:35:54 -07001452 private final class ConfigChange implements Runnable {
1453 @Override
1454 public void run() {
1455 programmingScheduled.set(false);
Saurav Dase321cff2018-02-09 17:26:45 -08001456 log.info("Reacting to config changes after buffer delay");
Saurav Das261c3002017-06-13 15:35:54 -07001457 for (Device device : deviceService.getDevices()) {
1458 processDeviceAdded(device);
1459 }
1460 defaultRoutingHandler.startPopulationProcess();
1461 }
1462 }
Charles Chan72f556a2015-10-05 17:50:33 -07001463 }
Charles Chanf4586112015-11-09 16:37:23 -08001464
1465 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001466 @Override
1467 public void event(HostEvent event) {
Charles Chanf4586112015-11-09 16:37:23 -08001468 switch (event.type()) {
1469 case HOST_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001470 hostHandler.processHostAddedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001471 break;
1472 case HOST_MOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001473 hostHandler.processHostMovedEvent(event);
Charles Chan910be6a2017-08-23 14:46:43 -07001474 routeHandler.processHostMovedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001475 break;
1476 case HOST_REMOVED:
Charles Chand9265a32017-06-16 15:19:24 -07001477 hostHandler.processHostRemovedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001478 break;
1479 case HOST_UPDATED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001480 hostHandler.processHostUpdatedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001481 break;
1482 default:
1483 log.warn("Unsupported host event type: {}", event.type());
1484 break;
1485 }
1486 }
1487 }
1488
Charles Chanc91c8782016-03-30 17:54:24 -07001489 private class InternalMcastListener implements McastListener {
1490 @Override
1491 public void event(McastEvent event) {
1492 switch (event.type()) {
1493 case SOURCE_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001494 mcastHandler.processSourceAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001495 break;
1496 case SINK_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001497 mcastHandler.processSinkAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001498 break;
1499 case SINK_REMOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001500 mcastHandler.processSinkRemoved(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001501 break;
Charles Chanc91c8782016-03-30 17:54:24 -07001502 case ROUTE_REMOVED:
Pier Luigi6786b922018-02-02 16:19:11 +01001503 mcastHandler.processRouteRemoved(event);
1504 break;
1505 case ROUTE_ADDED:
Charles Chanc91c8782016-03-30 17:54:24 -07001506 default:
1507 break;
1508 }
1509 }
1510 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001511
Charles Chandebfea32016-10-24 14:52:01 -07001512 private class InternalRouteEventListener implements RouteListener {
1513 @Override
1514 public void event(RouteEvent event) {
1515 switch (event.type()) {
1516 case ROUTE_ADDED:
1517 routeHandler.processRouteAdded(event);
1518 break;
1519 case ROUTE_UPDATED:
1520 routeHandler.processRouteUpdated(event);
1521 break;
1522 case ROUTE_REMOVED:
1523 routeHandler.processRouteRemoved(event);
1524 break;
Charles Chan910be6a2017-08-23 14:46:43 -07001525 case ALTERNATIVE_ROUTES_CHANGED:
1526 routeHandler.processAlternativeRoutesChanged(event);
1527 break;
Charles Chandebfea32016-10-24 14:52:01 -07001528 default:
1529 break;
1530 }
1531 }
1532 }
Saurav Das62ae6792017-05-15 15:34:25 -07001533
Jonghwan Hyune5ef7622017-08-25 17:48:36 -07001534 private void updateInterface(InterfaceConfig conf, InterfaceConfig prevConf) {
1535 try {
1536 Set<Interface> intfs = conf.getInterfaces();
1537 Set<Interface> prevIntfs = prevConf.getInterfaces();
1538
1539 // Now we only handle one interface config at each port.
1540 if (intfs.size() != 1 || prevIntfs.size() != 1) {
1541 log.warn("Interface update aborted - one at a time is allowed, " +
1542 "but {} / {}(prev) received.", intfs.size(), prevIntfs.size());
1543 return;
1544 }
1545
1546 Interface intf = intfs.stream().findFirst().get();
1547 Interface prevIntf = prevIntfs.stream().findFirst().get();
1548
1549 DeviceId deviceId = intf.connectPoint().deviceId();
1550 PortNumber portNum = intf.connectPoint().port();
1551
1552 if (!mastershipService.isLocalMaster(deviceId)) {
1553 log.debug("CONFIG_UPDATED event for interfaces should be " +
1554 "handled by master node for device {}", deviceId);
1555 return;
1556 }
1557
1558 removeSubnetConfig(prevIntf.connectPoint(),
1559 Sets.difference(new HashSet<>(prevIntf.ipAddressesList()),
1560 new HashSet<>(intf.ipAddressesList())));
1561
1562 if (prevIntf.vlanNative() != VlanId.NONE && !intf.vlanNative().equals(prevIntf.vlanNative())) {
1563 // RemoveVlanNative
1564 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanNative(), true, false);
1565 }
1566
1567 if (!prevIntf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1568 // RemoveVlanTagged
1569 prevIntf.vlanTagged().stream().filter(i -> !intf.vlanTagged().contains(i)).forEach(
1570 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, false)
1571 );
1572 }
1573
1574 if (prevIntf.vlanUntagged() != VlanId.NONE && !intf.vlanUntagged().equals(prevIntf.vlanUntagged())) {
1575 // RemoveVlanUntagged
1576 updateVlanConfigInternal(deviceId, portNum, prevIntf.vlanUntagged(), true, false);
1577 }
1578
1579 if (intf.vlanNative() != VlanId.NONE && !prevIntf.vlanNative().equals(intf.vlanNative())) {
1580 // AddVlanNative
1581 updateVlanConfigInternal(deviceId, portNum, intf.vlanNative(), true, true);
1582 }
1583
1584 if (!intf.vlanTagged().isEmpty() && !intf.vlanTagged().equals(prevIntf.vlanTagged())) {
1585 // AddVlanTagged
1586 intf.vlanTagged().stream().filter(i -> !prevIntf.vlanTagged().contains(i)).forEach(
1587 vlanId -> updateVlanConfigInternal(deviceId, portNum, vlanId, false, true)
1588 );
1589 }
1590
1591 if (intf.vlanUntagged() != VlanId.NONE && !prevIntf.vlanUntagged().equals(intf.vlanUntagged())) {
1592 // AddVlanUntagged
1593 updateVlanConfigInternal(deviceId, portNum, intf.vlanUntagged(), true, true);
1594 }
1595 addSubnetConfig(prevIntf.connectPoint(),
1596 Sets.difference(new HashSet<>(intf.ipAddressesList()),
1597 new HashSet<>(prevIntf.ipAddressesList())));
1598 } catch (ConfigException e) {
1599 log.error("Error in configuration");
1600 }
1601 }
1602
1603 private void updateVlanConfigInternal(DeviceId deviceId, PortNumber portNum,
1604 VlanId vlanId, boolean pushVlan, boolean install) {
1605 DefaultGroupHandler grpHandler = getGroupHandler(deviceId);
1606 if (grpHandler == null) {
1607 log.warn("Failed to retrieve group handler for device {}", deviceId);
1608 return;
1609 }
1610
1611 // Update filtering objective for a single port
1612 routingRulePopulator.updateSinglePortFilters(deviceId, portNum, pushVlan, vlanId, install);
1613
1614 // Update filtering objective for multicast ingress port
1615 mcastHandler.updateFilterToDevice(deviceId, portNum, vlanId, install);
1616
1617 int nextId = getVlanNextObjectiveId(deviceId, vlanId);
1618
1619 if (nextId != -1 && !install) {
1620 // Update next objective for a single port as an output port
1621 // Remove a single port from L2FG
1622 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1623 // Remove L2 Bridging rule and L3 Unicast rule to the host
1624 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1625 // Remove broadcast forwarding rule and corresponding L2FG for VLAN
1626 // only if there is no port configured on that VLAN ID
1627 if (!getVlanPortMap(deviceId).containsKey(vlanId)) {
1628 // Remove broadcast forwarding rule for the VLAN
1629 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1630 // Remove L2FG for VLAN
1631 grpHandler.removeBcastGroupFromVlan(deviceId, portNum, vlanId, pushVlan);
1632 } else {
1633 // Remove L2IG of the port
1634 grpHandler.removePortNextObjective(deviceId, portNum, vlanId, pushVlan);
1635 }
1636 } else if (install) {
1637 if (nextId != -1) {
1638 // Add a single port to L2FG
1639 grpHandler.updateGroupFromVlanConfiguration(portNum, Collections.singleton(vlanId), nextId, install);
1640 } else {
1641 // Create L2FG for VLAN
1642 grpHandler.createBcastGroupFromVlan(vlanId, Collections.singleton(portNum));
1643 routingRulePopulator.updateSubnetBroadcastRule(deviceId, vlanId, install);
1644 }
1645 hostHandler.processIntfVlanUpdatedEvent(deviceId, portNum, vlanId, pushVlan, install);
1646 } else {
1647 log.warn("Failed to retrieve next objective for vlan {} in device {}:{}", vlanId, deviceId, portNum);
1648 }
1649 }
1650
1651 private void removeSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1652 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1653 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1654
1655 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1656 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1657 .filter(intf -> !intf.connectPoint().equals(cp))
1658 .flatMap(intf -> intf.ipAddressesList().stream())
1659 .collect(Collectors.toSet());
1660 // 1. Partial subnet population
1661 // Remove routing rules for removed subnet from previous configuration,
1662 // which does not also exist in other interfaces in the same device
1663 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1664 .map(InterfaceIpAddress::subnetAddress)
1665 .collect(Collectors.toSet());
1666
1667 defaultRoutingHandler.revokeSubnet(
1668 ipPrefixSet.stream()
1669 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1670 .collect(Collectors.toSet()));
1671
1672 // 2. Interface IP punts
1673 // Remove IP punts for old Intf address
1674 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1675 .map(InterfaceIpAddress::ipAddress)
1676 .collect(Collectors.toSet());
1677 ipAddressSet.stream()
1678 .map(InterfaceIpAddress::ipAddress)
1679 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1680 .forEach(interfaceIpAddress ->
1681 routingRulePopulator.revokeSingleIpPunts(
1682 cp.deviceId(), interfaceIpAddress));
1683
1684 // 3. Host unicast routing rule
1685 // Remove unicast routing rule
1686 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, false);
1687 }
1688
1689 private void addSubnetConfig(ConnectPoint cp, Set<InterfaceIpAddress> ipAddressSet) {
1690 Set<IpPrefix> ipPrefixSet = ipAddressSet.stream().
1691 map(InterfaceIpAddress::subnetAddress).collect(Collectors.toSet());
1692
1693 Set<InterfaceIpAddress> deviceIntfIpAddrs = interfaceService.getInterfaces().stream()
1694 .filter(intf -> intf.connectPoint().deviceId().equals(cp.deviceId()))
1695 .filter(intf -> !intf.connectPoint().equals(cp))
1696 .flatMap(intf -> intf.ipAddressesList().stream())
1697 .collect(Collectors.toSet());
1698 // 1. Partial subnet population
1699 // Add routing rules for newly added subnet, which does not also exist in
1700 // other interfaces in the same device
1701 Set<IpPrefix> deviceIpPrefixSet = deviceIntfIpAddrs.stream()
1702 .map(InterfaceIpAddress::subnetAddress)
1703 .collect(Collectors.toSet());
1704
1705 defaultRoutingHandler.populateSubnet(
1706 Collections.singleton(cp),
1707 ipPrefixSet.stream()
1708 .filter(ipPrefix -> !deviceIpPrefixSet.contains(ipPrefix))
1709 .collect(Collectors.toSet()));
1710
1711 // 2. Interface IP punts
1712 // Add IP punts for new Intf address
1713 Set<IpAddress> deviceIpAddrs = deviceIntfIpAddrs.stream()
1714 .map(InterfaceIpAddress::ipAddress)
1715 .collect(Collectors.toSet());
1716 ipAddressSet.stream()
1717 .map(InterfaceIpAddress::ipAddress)
1718 .filter(interfaceIpAddress -> !deviceIpAddrs.contains(interfaceIpAddress))
1719 .forEach(interfaceIpAddress ->
1720 routingRulePopulator.populateSingleIpPunts(
1721 cp.deviceId(), interfaceIpAddress));
1722
1723 // 3. Host unicast routing rule
1724 // Add unicast routing rule
1725 hostHandler.processIntfIpUpdatedEvent(cp, ipPrefixSet, true);
1726 }
sangho80f11cb2015-04-01 13:05:26 -07001727}