blob: 02623e2756412905a8c12a597e3cdec52284f39a [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sanghob35a6192015-04-01 13:05:26 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.segmentrouting;
17
Charles Chan7ffd81f2017-02-08 15:52:08 -080018import com.google.common.collect.HashMultimap;
Charles Chanc81c45b2016-10-20 17:02:44 -070019import com.google.common.collect.Maps;
Charles Chan7ffd81f2017-02-08 15:52:08 -080020import com.google.common.collect.Multimap;
sanghob35a6192015-04-01 13:05:26 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho1e575652015-05-14 00:39:53 -070026import org.apache.felix.scr.annotations.Service;
sanghob35a6192015-04-01 13:05:26 -070027import org.onlab.packet.Ethernet;
Pier Ventre735b8c82016-12-02 08:16:05 -080028import org.onlab.packet.ICMP6;
Charles Chanc42e84e2015-10-20 16:24:19 -070029import org.onlab.packet.IPv4;
Pier Ventre10bd8d12016-11-26 21:05:22 -080030import org.onlab.packet.IPv6;
Charles Chanc42e84e2015-10-20 16:24:19 -070031import org.onlab.packet.IpPrefix;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070032import org.onlab.packet.VlanId;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070033import org.onlab.util.KryoNamespace;
Saurav Das80980c72016-03-23 11:22:49 -070034import org.onosproject.cfg.ComponentConfigService;
sanghob35a6192015-04-01 13:05:26 -070035import org.onosproject.core.ApplicationId;
36import org.onosproject.core.CoreService;
37import org.onosproject.event.Event;
Charles Chand55e84d2016-03-30 17:54:24 -070038import org.onosproject.incubator.net.config.basics.McastConfig;
Pier Luigi721b6622017-02-03 13:34:21 -080039import org.onosproject.incubator.net.intf.Interface;
Charles Chan2c15aca2016-11-09 20:51:44 -080040import org.onosproject.incubator.net.intf.InterfaceService;
Charles Chan03a73e02016-10-24 14:52:01 -070041import org.onosproject.incubator.net.routing.RouteEvent;
42import org.onosproject.incubator.net.routing.RouteListener;
43import org.onosproject.incubator.net.routing.RouteService;
Pier Ventre735b8c82016-12-02 08:16:05 -080044import org.onosproject.incubator.net.neighbour.NeighbourResolutionService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070045import org.onosproject.mastership.MastershipService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080046import org.onosproject.net.ConnectPoint;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070047import org.onosproject.net.Device;
48import org.onosproject.net.DeviceId;
49import org.onosproject.net.Link;
50import org.onosproject.net.Port;
Charles Chan68aa62d2015-11-09 16:37:23 -080051import org.onosproject.net.PortNumber;
Charles Chand6832882015-10-05 17:50:33 -070052import org.onosproject.net.config.ConfigFactory;
53import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070054import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070055import org.onosproject.net.config.NetworkConfigRegistry;
Charles Chand6832882015-10-05 17:50:33 -070056import org.onosproject.net.config.basics.SubjectFactories;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070057import org.onosproject.net.device.DeviceEvent;
58import org.onosproject.net.device.DeviceListener;
59import org.onosproject.net.device.DeviceService;
Charles Chan68aa62d2015-11-09 16:37:23 -080060import org.onosproject.net.flow.TrafficSelector;
61import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070062import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan68aa62d2015-11-09 16:37:23 -080063import org.onosproject.net.host.HostEvent;
64import org.onosproject.net.host.HostListener;
Pier Ventre10bd8d12016-11-26 21:05:22 -080065import org.onosproject.net.host.HostService;
66import org.onosproject.net.link.LinkEvent;
67import org.onosproject.net.link.LinkListener;
68import org.onosproject.net.link.LinkService;
Charles Chand55e84d2016-03-30 17:54:24 -070069import org.onosproject.net.mcast.McastEvent;
70import org.onosproject.net.mcast.McastListener;
71import org.onosproject.net.mcast.MulticastRouteService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080072import org.onosproject.net.packet.InboundPacket;
73import org.onosproject.net.packet.PacketContext;
74import org.onosproject.net.packet.PacketProcessor;
75import org.onosproject.net.packet.PacketService;
Pier Ventre42287df2016-11-09 14:17:26 -080076import org.onosproject.net.topology.PathService;
Charles Chand55e84d2016-03-30 17:54:24 -070077import org.onosproject.net.topology.TopologyService;
78import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
79import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventref34966c2016-11-07 16:21:04 -080080import org.onosproject.segmentrouting.config.PwaasConfig;
Pier Ventre10bd8d12016-11-26 21:05:22 -080081import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Pier Ventref34966c2016-11-07 16:21:04 -080082import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -070083import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -070084import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
85import org.onosproject.segmentrouting.grouphandler.NeighborSet;
Charles Chand2990362016-04-18 13:44:03 -070086import org.onosproject.segmentrouting.storekey.NeighborSetNextObjectiveStoreKey;
87import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chand2990362016-04-18 13:44:03 -070088import org.onosproject.segmentrouting.storekey.SubnetAssignedVidStoreKey;
Charles Chan59cc16d2017-02-02 16:20:42 -080089import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -070090import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Pier Ventref34966c2016-11-07 16:21:04 -080091import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070092import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070093import org.onosproject.store.service.EventuallyConsistentMap;
94import org.onosproject.store.service.EventuallyConsistentMapBuilder;
95import org.onosproject.store.service.StorageService;
96import org.onosproject.store.service.WallClockTimestamp;
Charles Chan35fd1a72016-06-13 18:54:31 -070097import org.opencord.cordconfig.CordConfigEvent;
98import org.opencord.cordconfig.CordConfigListener;
99import org.opencord.cordconfig.CordConfigService;
sanghob35a6192015-04-01 13:05:26 -0700100import org.slf4j.Logger;
101import org.slf4j.LoggerFactory;
102
sangho1e575652015-05-14 00:39:53 -0700103import java.util.List;
sanghob35a6192015-04-01 13:05:26 -0700104import java.util.Map;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700105import java.util.Set;
sanghob35a6192015-04-01 13:05:26 -0700106import java.util.concurrent.ConcurrentHashMap;
107import java.util.concurrent.ConcurrentLinkedQueue;
108import java.util.concurrent.Executors;
109import java.util.concurrent.ScheduledExecutorService;
110import java.util.concurrent.ScheduledFuture;
111import java.util.concurrent.TimeUnit;
Charles Chan7ffd81f2017-02-08 15:52:08 -0800112import java.util.stream.Collectors;
sanghob35a6192015-04-01 13:05:26 -0700113
Charles Chan3e783d02016-02-26 22:19:52 -0800114import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800115import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700116import static org.onlab.util.Tools.groupedThreads;
Charles Chan3e783d02016-02-26 22:19:52 -0800117
Charles Chane849c192016-01-11 18:28:54 -0800118/**
119 * Segment routing manager.
120 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700121@Service
122@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700123public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700124
Charles Chan2c15aca2016-11-09 20:51:44 -0800125 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
sanghob35a6192015-04-01 13:05:26 -0700126
127 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700128 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700129
130 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre10bd8d12016-11-26 21:05:22 -0800131 private NeighbourResolutionService neighbourResolutionService;
132
133 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800134 public PathService pathService;
135
136 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700137 CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700138
139 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700140 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700141
142 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700143 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700144
145 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700146 DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700147
148 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800149 public FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700150
151 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700152 LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700153
Charles Chan5270ed02016-01-30 23:22:37 -0800154 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800155 public MastershipService mastershipService;
Charles Chan03a73e02016-10-24 14:52:01 -0700156
157 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800158 public StorageService storageService;
Charles Chan03a73e02016-10-24 14:52:01 -0700159
160 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
161 MulticastRouteService multicastRouteService;
162
163 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
164 TopologyService topologyService;
165
166 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
167 CordConfigService cordConfigService;
168
169 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
170 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800171
172 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800173 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800174
Saurav Das80980c72016-03-23 11:22:49 -0700175 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800176 public InterfaceService interfaceService;
177
Charles Chan03a73e02016-10-24 14:52:01 -0700178 ArpHandler arpHandler = null;
179 IcmpHandler icmpHandler = null;
180 IpHandler ipHandler = null;
181 RoutingRulePopulator routingRulePopulator = null;
Charles Chan2c15aca2016-11-09 20:51:44 -0800182 public ApplicationId appId;
Pier Ventre42287df2016-11-09 14:17:26 -0800183 public DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700184
Charles Chan03a73e02016-10-24 14:52:01 -0700185 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700186 private TunnelHandler tunnelHandler = null;
187 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700188 private InternalPacketProcessor processor = null;
189 private InternalLinkListener linkListener = null;
190 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700191 private AppConfigHandler appCfgHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700192 XConnectHandler xConnectHandler = null;
Charles Chand2990362016-04-18 13:44:03 -0700193 private McastHandler mcastHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700194 HostHandler hostHandler = null;
Charles Chan35fd1a72016-06-13 18:54:31 -0700195 private CordConfigHandler cordConfigHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800196 private RouteHandler routeHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800197 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Pier Ventref34966c2016-11-07 16:21:04 -0800198 private L2TunnelHandler l2TunnelHandler = null;
sanghob35a6192015-04-01 13:05:26 -0700199 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan5270ed02016-01-30 23:22:37 -0800200 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700201 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
202 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan35fd1a72016-06-13 18:54:31 -0700203 private final InternalCordConfigListener cordConfigListener = new InternalCordConfigListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700204 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sanghob35a6192015-04-01 13:05:26 -0700205
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700206 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700207 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sanghob35a6192015-04-01 13:05:26 -0700208
Saurav Das4ce45962015-11-24 23:21:05 -0800209 @SuppressWarnings("unused")
sanghob35a6192015-04-01 13:05:26 -0700210 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800211 @SuppressWarnings("rawtypes")
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700212 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Charles Chan68aa62d2015-11-09 16:37:23 -0800213 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chane849c192016-01-11 18:28:54 -0800214 new ConcurrentHashMap<>();
215 /**
216 * Per device next objective ID store with (device id + neighbor set) as key.
217 */
218 public EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, Integer>
Charles Chan68aa62d2015-11-09 16:37:23 -0800219 nsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800220 /**
221 * Per device next objective ID store with (device id + subnet) as key.
222 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800223 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
224 vlanNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800225 /**
226 * Per device next objective ID store with (device id + port) as key.
227 */
228 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800229 portNextObjStore = null;
Charles Chan59cc16d2017-02-02 16:20:42 -0800230
Saurav Das4ce45962015-11-24 23:21:05 -0800231 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
232 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700233
Charles Chand55e84d2016-03-30 17:54:24 -0700234 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700235 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
236 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700237 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700238 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800239 public SegmentRoutingDeviceConfig createConfig() {
240 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700241 }
242 };
Pier Ventref34966c2016-11-07 16:21:04 -0800243
Charles Chand55e84d2016-03-30 17:54:24 -0700244 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700245 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
246 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700247 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800248 @Override
249 public SegmentRoutingAppConfig createConfig() {
250 return new SegmentRoutingAppConfig();
251 }
252 };
Pier Ventref34966c2016-11-07 16:21:04 -0800253
Charles Chanfc5c7802016-05-17 13:13:55 -0700254 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
255 new ConfigFactory<ApplicationId, XConnectConfig>(
256 SubjectFactories.APP_SUBJECT_FACTORY,
257 XConnectConfig.class, "xconnect") {
258 @Override
259 public XConnectConfig createConfig() {
260 return new XConnectConfig();
261 }
262 };
Pier Ventref34966c2016-11-07 16:21:04 -0800263
Charles Chand55e84d2016-03-30 17:54:24 -0700264 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700265 new ConfigFactory<ApplicationId, McastConfig>(
266 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700267 McastConfig.class, "multicast") {
268 @Override
269 public McastConfig createConfig() {
270 return new McastConfig();
271 }
272 };
273
Pier Ventref34966c2016-11-07 16:21:04 -0800274 private final ConfigFactory<ApplicationId, PwaasConfig> pwaasConfigFactory =
275 new ConfigFactory<ApplicationId, PwaasConfig>(
276 SubjectFactories.APP_SUBJECT_FACTORY,
277 PwaasConfig.class, "pwaas") {
278 @Override
279 public PwaasConfig createConfig() {
280 return new PwaasConfig();
281 }
282 };
283
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700284 private Object threadSchedulerLock = new Object();
285 private static int numOfEventsQueued = 0;
286 private static int numOfEventsExecuted = 0;
sanghob35a6192015-04-01 13:05:26 -0700287 private static int numOfHandlerExecution = 0;
288 private static int numOfHandlerScheduled = 0;
289
Charles Chan116188d2016-02-18 14:22:42 -0800290 /**
291 * Segment Routing App ID.
292 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800293 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan59cc16d2017-02-02 16:20:42 -0800294
Charles Chane849c192016-01-11 18:28:54 -0800295 /**
296 * The default VLAN ID assigned to the interfaces without subnet config.
297 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800298 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das0e99e2b2015-10-28 12:39:42 -0700299
sanghob35a6192015-04-01 13:05:26 -0700300 @Activate
301 protected void activate() {
Charles Chan2c15aca2016-11-09 20:51:44 -0800302 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700303
304 log.debug("Creating EC map nsnextobjectivestore");
305 EventuallyConsistentMapBuilder<NeighborSetNextObjectiveStoreKey, Integer>
306 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700307 nsNextObjStore = nsNextObjMapBuilder
308 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700309 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700310 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700311 .build();
312 log.trace("Current size {}", nsNextObjStore.size());
313
Charles Chan59cc16d2017-02-02 16:20:42 -0800314 log.debug("Creating EC map vlannextobjectivestore");
315 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
316 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
317 vlanNextObjStore = vlanNextObjMapBuilder
318 .withName("vlannextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700319 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700320 .withTimestampProvider((k, v) -> new WallClockTimestamp())
321 .build();
322
Saurav Das4ce45962015-11-24 23:21:05 -0800323 log.debug("Creating EC map subnetnextobjectivestore");
324 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
325 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
326 portNextObjStore = portNextObjMapBuilder
327 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700328 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800329 .withTimestampProvider((k, v) -> new WallClockTimestamp())
330 .build();
331
sangho0b2b6d12015-05-20 22:16:38 -0700332 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
333 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700334 tunnelStore = tunnelMapBuilder
335 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700336 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700337 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700338 .build();
339
340 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
341 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700342 policyStore = policyMapBuilder
343 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700344 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700345 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700346 .build();
347
Saurav Das80980c72016-03-23 11:22:49 -0700348 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800349 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700350 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800351 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800352 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
353 "requestInterceptsEnabled", "false");
Pier Luigi7e415132017-01-12 22:46:39 -0800354 compCfgService.preSetProperty("org.onosproject.incubator.net.neighbour.impl.NeighbourResolutionManager",
355 "requestInterceptsEnabled", "false");
356 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelay",
357 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800358 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
359 "greedyLearningIpv6", "true");
Charles Chanc6d227e2017-02-28 15:15:17 -0800360 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
361 "forceUnprovision", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700362
Charles Chanb8e10c82015-10-14 11:24:40 -0700363 processor = new InternalPacketProcessor();
364 linkListener = new InternalLinkListener();
365 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700366 appCfgHandler = new AppConfigHandler(this);
367 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700368 mcastHandler = new McastHandler(this);
369 hostHandler = new HostHandler(this);
Charles Chan35fd1a72016-06-13 18:54:31 -0700370 cordConfigHandler = new CordConfigHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700371 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800372 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Pier Ventref34966c2016-11-07 16:21:04 -0800373 l2TunnelHandler = new L2TunnelHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700374
Charles Chan3e783d02016-02-26 22:19:52 -0800375 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700376 cfgService.registerConfigFactory(deviceConfigFactory);
377 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700378 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700379 cfgService.registerConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800380 cfgService.registerConfigFactory(pwaasConfigFactory);
Charles Chan5270ed02016-01-30 23:22:37 -0800381 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700382 packetService.addProcessor(processor, PacketProcessor.director(2));
383 linkService.addListener(linkListener);
384 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700385 multicastRouteService.addListener(mcastListener);
Charles Chan35fd1a72016-06-13 18:54:31 -0700386 cordConfigService.addListener(cordConfigListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700387
388 cfgListener.configureNetwork();
389
Charles Chan03a73e02016-10-24 14:52:01 -0700390 routeService.addListener(routeListener);
391
sanghob35a6192015-04-01 13:05:26 -0700392 log.info("Started");
393 }
394
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700395 private KryoNamespace.Builder createSerializer() {
396 return new KryoNamespace.Builder()
397 .register(KryoNamespaces.API)
398 .register(NeighborSetNextObjectiveStoreKey.class,
Charles Chan59cc16d2017-02-02 16:20:42 -0800399 VlanNextObjectiveStoreKey.class,
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700400 SubnetAssignedVidStoreKey.class,
401 NeighborSet.class,
402 Tunnel.class,
403 DefaultTunnel.class,
404 Policy.class,
405 TunnelPolicy.class,
406 Policy.Type.class,
407 PortNextObjectiveStoreKey.class,
Charles Chanfc5c7802016-05-17 13:13:55 -0700408 XConnectStoreKey.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700409 );
410 }
411
sanghob35a6192015-04-01 13:05:26 -0700412 @Deactivate
413 protected void deactivate() {
Charles Chand6832882015-10-05 17:50:33 -0700414 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700415 cfgService.unregisterConfigFactory(deviceConfigFactory);
416 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700417 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700418 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800419 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chand6832882015-10-05 17:50:33 -0700420
sanghob35a6192015-04-01 13:05:26 -0700421 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700422 linkService.removeListener(linkListener);
423 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700424 multicastRouteService.removeListener(mcastListener);
Charles Chan35fd1a72016-06-13 18:54:31 -0700425 cordConfigService.removeListener(cordConfigListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700426 routeService.removeListener(routeListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700427
Charles Chan0aa674e2017-02-23 15:44:08 -0800428 neighbourResolutionService.unregisterNeighbourHandlers(appId);
429
sanghob35a6192015-04-01 13:05:26 -0700430 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700431 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700432 deviceListener = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700433 groupHandlerMap.clear();
434
Charles Chand55e84d2016-03-30 17:54:24 -0700435 nsNextObjStore.destroy();
Charles Chan59cc16d2017-02-02 16:20:42 -0800436 vlanNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700437 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700438 tunnelStore.destroy();
439 policyStore.destroy();
sanghob35a6192015-04-01 13:05:26 -0700440 log.info("Stopped");
441 }
442
sangho1e575652015-05-14 00:39:53 -0700443 @Override
444 public List<Tunnel> getTunnels() {
445 return tunnelHandler.getTunnels();
446 }
447
448 @Override
sangho71abe1b2015-06-29 14:58:47 -0700449 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
450 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700451 }
452
453 @Override
sangho71abe1b2015-06-29 14:58:47 -0700454 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700455 for (Policy policy: policyHandler.getPolicies()) {
456 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
457 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
458 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
459 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700460 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700461 }
462 }
463 }
sangho71abe1b2015-06-29 14:58:47 -0700464 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700465 }
466
467 @Override
sangho71abe1b2015-06-29 14:58:47 -0700468 public PolicyHandler.Result removePolicy(Policy policy) {
469 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700470 }
471
472 @Override
sangho71abe1b2015-06-29 14:58:47 -0700473 public PolicyHandler.Result createPolicy(Policy policy) {
474 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700475 }
476
477 @Override
478 public List<Policy> getPolicies() {
479 return policyHandler.getPolicies();
480 }
481
Saurav Das59232cf2016-04-27 18:35:50 -0700482 @Override
483 public void rerouteNetwork() {
484 cfgListener.configureNetwork();
485 for (Device device : deviceService.getDevices()) {
Saurav Das018605f2017-02-18 14:05:44 -0800486 if (mastershipService.isLocalMaster(device.id())) {
487 defaultRoutingHandler.populatePortAddressingRules(device.id());
488 }
Saurav Das59232cf2016-04-27 18:35:50 -0700489 }
490 defaultRoutingHandler.startPopulationProcess();
491 }
492
Charles Chanc81c45b2016-10-20 17:02:44 -0700493 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800494 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
495 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Charles Chanc81c45b2016-10-20 17:02:44 -0700496 deviceService.getAvailableDevices().forEach(device -> {
497 deviceSubnetMap.put(device.id(), deviceConfiguration.getSubnets(device.id()));
498 });
499 return deviceSubnetMap;
500 }
501
sanghof9d0bf12015-05-19 11:57:42 -0700502 /**
Pier Ventre98161782016-10-31 15:00:01 -0700503 * Returns the MPLS-ECMP configuration.
504 *
505 * @return MPLS-ECMP value
506 */
507 public boolean getMplsEcmp() {
508 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
509 .getConfig(this.appId, SegmentRoutingAppConfig.class);
510 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
511 }
512
513 /**
sanghof9d0bf12015-05-19 11:57:42 -0700514 * Returns the tunnel object with the tunnel ID.
515 *
516 * @param tunnelId Tunnel ID
517 * @return Tunnel reference
518 */
sangho1e575652015-05-14 00:39:53 -0700519 public Tunnel getTunnel(String tunnelId) {
520 return tunnelHandler.getTunnel(tunnelId);
521 }
522
Charles Chan7ffd81f2017-02-08 15:52:08 -0800523 // TODO Consider moving these to InterfaceService
sanghob35a6192015-04-01 13:05:26 -0700524 /**
Charles Chan59cc16d2017-02-02 16:20:42 -0800525 * Returns untagged VLAN configured on given connect point.
Charles Chan7ffd81f2017-02-08 15:52:08 -0800526 * <p>
527 * Only returns the first match if there are multiple untagged VLAN configured
528 * on the connect point.
sanghob35a6192015-04-01 13:05:26 -0700529 *
Charles Chan59cc16d2017-02-02 16:20:42 -0800530 * @param connectPoint connect point
531 * @return untagged VLAN or null if not configured
sanghob35a6192015-04-01 13:05:26 -0700532 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800533 public VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
534 return interfaceService.getInterfacesByPort(connectPoint).stream()
535 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
536 .map(Interface::vlanUntagged)
537 .findFirst().orElse(null);
sanghob35a6192015-04-01 13:05:26 -0700538 }
539
sangho1e575652015-05-14 00:39:53 -0700540 /**
Charles Chan7ffd81f2017-02-08 15:52:08 -0800541 * Returns tagged VLAN configured on given connect point.
542 * <p>
543 * Returns all matches if there are multiple tagged VLAN configured
544 * on the connect point.
545 *
546 * @param connectPoint connect point
547 * @return tagged VLAN or empty set if not configured
548 */
549 public Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
550 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
551 return interfaces.stream()
552 .map(Interface::vlanTagged)
553 .flatMap(vlanIds -> vlanIds.stream())
554 .collect(Collectors.toSet());
555 }
556
557 /**
558 * Returns native VLAN configured on given connect point.
559 * <p>
560 * Only returns the first match if there are multiple native VLAN configured
561 * on the connect point.
562 *
563 * @param connectPoint connect point
564 * @return native VLAN or null if not configured
565 */
566 public VlanId getNativeVlanId(ConnectPoint connectPoint) {
567 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
568 return interfaces.stream()
569 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
570 .map(Interface::vlanNative)
571 .findFirst()
572 .orElse(null);
573 }
574
575 /**
576 * Returns vlan port map of given device.
577 *
578 * @param deviceId device id
579 * @return vlan-port multimap
580 */
581 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
582 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
583
584 interfaceService.getInterfaces().stream()
585 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
586 .forEach(intf -> {
587 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
588 intf.vlanTagged().forEach(vlanTagged -> {
589 vlanPortMap.put(vlanTagged, intf.connectPoint().port());
590 });
591 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
592 });
593 vlanPortMap.removeAll(VlanId.NONE);
594
595 return vlanPortMap;
596 }
597
598 /**
Saurav Das0e99e2b2015-10-28 12:39:42 -0700599 * Returns the next objective ID for the given NeighborSet.
Saurav Das8a0732e2015-11-20 15:27:53 -0800600 * If the nextObjective does not exist, a new one is created and
Saurav Das4ce45962015-11-24 23:21:05 -0800601 * its id is returned.
sangho1e575652015-05-14 00:39:53 -0700602 *
sanghof9d0bf12015-05-19 11:57:42 -0700603 * @param deviceId Device ID
604 * @param ns NegighborSet
Saurav Das8a0732e2015-11-20 15:27:53 -0800605 * @param meta metadata passed into the creation of a Next Objective
Pier Ventre917127a2016-10-31 16:49:19 -0700606 * @param isBos indicates if it is BoS or not
Saurav Das8a0732e2015-11-20 15:27:53 -0800607 * @return next objective ID or -1 if an error was encountered during the
608 * creation of the nextObjective
sangho1e575652015-05-14 00:39:53 -0700609 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800610 public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns,
Pier Ventre917127a2016-10-31 16:49:19 -0700611 TrafficSelector meta, boolean isBos) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700612 if (groupHandlerMap.get(deviceId) != null) {
613 log.trace("getNextObjectiveId query in device {}", deviceId);
614 return groupHandlerMap
Pier Ventre917127a2016-10-31 16:49:19 -0700615 .get(deviceId).getNextObjectiveId(ns, meta, isBos);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700616 } else {
Saurav Das4ce45962015-11-24 23:21:05 -0800617 log.warn("getNextObjectiveId query - groupHandler for device {} "
618 + "not found", deviceId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700619 return -1;
620 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700621 }
622
Charles Chanc42e84e2015-10-20 16:24:19 -0700623 /**
Pier Ventre917127a2016-10-31 16:49:19 -0700624 * Returns the next objective ID for the given NeighborSet.
625 * If the nextObjective does not exist, a new one is created and
626 * its id is returned.
627 *
628 * @param deviceId Device ID
629 * @param ns NegighborSet
630 * @param meta metadata passed into the creation of a Next Objective
631 * @return next objective ID or -1 if an error was encountered during the
632 * creation of the nextObjective
633 */
634 public int getNextObjectiveId(DeviceId deviceId,
635 NeighborSet ns,
636 TrafficSelector meta) {
637 return this.getNextObjectiveId(deviceId, ns, meta, true);
638 }
639
640 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800641 * Returns the next objective ID for the given subnet prefix. It is expected
Charles Chan59cc16d2017-02-02 16:20:42 -0800642 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das4ce45962015-11-24 23:21:05 -0800643 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700644 *
645 * @param deviceId Device ID
Charles Chan59cc16d2017-02-02 16:20:42 -0800646 * @param vlanId VLAN ID
Saurav Das4ce45962015-11-24 23:21:05 -0800647 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700648 */
Charles Chan59cc16d2017-02-02 16:20:42 -0800649 public int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chanc42e84e2015-10-20 16:24:19 -0700650 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan59cc16d2017-02-02 16:20:42 -0800651 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
652 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700653 } else {
Charles Chan59cc16d2017-02-02 16:20:42 -0800654 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das4ce45962015-11-24 23:21:05 -0800655 + "device {} not found", deviceId);
656 return -1;
657 }
658 }
659
660 /**
661 * Returns the next objective ID for the given portNumber, given the treatment.
662 * There could be multiple different treatments to the same outport, which
663 * would result in different objectives. If the next object
664 * does not exist, a new one is created and its id is returned.
665 *
666 * @param deviceId Device ID
667 * @param portNum port number on device for which NextObjective is queried
668 * @param treatment the actions to apply on the packets (should include outport)
669 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das59232cf2016-04-27 18:35:50 -0700670 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800671 */
672 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
673 TrafficTreatment treatment,
674 TrafficSelector meta) {
675 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
676 if (ghdlr != null) {
677 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta);
678 } else {
Charles Chane849c192016-01-11 18:28:54 -0800679 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
680 + " not found", deviceId);
681 return -1;
682 }
683 }
684
sanghob35a6192015-04-01 13:05:26 -0700685 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700686 @Override
687 public void process(PacketContext context) {
688
689 if (context.isHandled()) {
690 return;
691 }
692
693 InboundPacket pkt = context.inPacket();
694 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -0800695
696 if (ethernet == null) {
697 return;
698 }
699
Saurav Das4ce45962015-11-24 23:21:05 -0800700 log.trace("Rcvd pktin: {}", ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800701 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das76ae6812017-03-15 15:15:14 -0700702 log.warn("Received unexpected ARP packet on {}", context.inPacket().receivedFrom());
703 log.trace("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -0800704 return;
sanghob35a6192015-04-01 13:05:26 -0700705 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800706 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
707 //ipHandler.addToPacketBuffer(ipv4Packet);
708 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
709 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700710 } else {
Charles Chan50035632017-01-13 17:20:44 -0800711 // NOTE: We don't support IP learning at this moment so this
712 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -0800713 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700714 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800715 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
716 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -0800717 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -0800718 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -0800719 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
720 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
721 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
722 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
723 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
724 } else {
Charles Chan0ed44fb2017-03-13 13:10:30 -0700725 log.debug("Received ICMPv6 0x{} - not handled",
726 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventre735b8c82016-12-02 08:16:05 -0800727 }
728 } else {
729 // NOTE: We don't support IP learning at this moment so this
730 // is not necessary. Also it causes duplication of DHCPv6 packets.
731 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
732 }
sanghob35a6192015-04-01 13:05:26 -0700733 }
734 }
735 }
736
737 private class InternalLinkListener implements LinkListener {
738 @Override
739 public void event(LinkEvent event) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700740 if (event.type() == LinkEvent.Type.LINK_ADDED
741 || event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700742 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700743 scheduleEventHandlerIfNotScheduled(event);
744 }
745 }
746 }
747
748 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -0700749 @Override
750 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -0700751 switch (event.type()) {
752 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -0800753 case PORT_UPDATED:
754 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -0700755 case DEVICE_UPDATED:
756 case DEVICE_AVAILABILITY_CHANGED:
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700757 log.debug("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700758 scheduleEventHandlerIfNotScheduled(event);
759 break;
760 default:
761 }
762 }
763 }
764
Saurav Das4ce45962015-11-24 23:21:05 -0800765 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -0700766 private void scheduleEventHandlerIfNotScheduled(Event event) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700767 synchronized (threadSchedulerLock) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700768 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700769 numOfEventsQueued++;
770
771 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
772 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700773 eventHandlerFuture = executorService
774 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
775 numOfHandlerScheduled++;
776 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700777 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700778 numOfEventsQueued,
779 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -0700780 }
sanghob35a6192015-04-01 13:05:26 -0700781 }
782
783 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700784 @Override
sanghob35a6192015-04-01 13:05:26 -0700785 public void run() {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700786 try {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700787 while (true) {
Saurav Das4ce45962015-11-24 23:21:05 -0800788 @SuppressWarnings("rawtypes")
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700789 Event event = null;
790 synchronized (threadSchedulerLock) {
791 if (!eventQueue.isEmpty()) {
792 event = eventQueue.poll();
793 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700794 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700795 numOfHandlerExecution++;
796 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
797 numOfHandlerExecution, numOfEventsExecuted);
798 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700799 }
sangho20eff1d2015-04-13 15:15:58 -0700800 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700801 if (event.type() == LinkEvent.Type.LINK_ADDED) {
802 processLinkAdded((Link) event.subject());
803 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre2c515312016-09-13 21:33:40 -0700804 Link linkRemoved = (Link) event.subject();
805 if (linkRemoved.src().elementId() instanceof DeviceId &&
806 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
807 continue;
808 }
809 if (linkRemoved.dst().elementId() instanceof DeviceId &&
810 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
811 continue;
812 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700813 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700814 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
815 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
816 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800817 DeviceId deviceId = ((Device) event.subject()).id();
818 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -0700819 log.info("Processing device event {} for available device {}",
820 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700821 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -0700822 } else {
823 log.info("Processing device event {} for unavailable device {}",
824 event.type(), ((Device) event.subject()).id());
825 processDeviceRemoved((Device) event.subject());
826 }
Saurav Das1a129a02016-11-18 15:21:57 -0800827 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800828 // typically these calls come when device is added first time
829 // so port filtering rules are handled at the device_added event.
830 // port added calls represent all ports on the device,
831 // enabled or not.
Saurav Das1a129a02016-11-18 15:21:57 -0800832 log.debug("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800833 ((DeviceEvent) event).subject().id(),
834 ((DeviceEvent) event).port().number(),
835 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -0800836 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800837 // these calls happen for every subsequent event
838 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -0800839 log.info("** PORT UPDATED {}/{} -> {}",
840 event.subject(),
841 ((DeviceEvent) event).port(),
842 event.type());
843 processPortUpdated(((Device) event.subject()),
844 ((DeviceEvent) event).port());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700845 } else {
846 log.warn("Unhandled event type: {}", event.type());
847 }
sanghob35a6192015-04-01 13:05:26 -0700848 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700849 } catch (Exception e) {
850 log.error("SegmentRouting event handler "
851 + "thread thrown an exception: {}", e);
sanghob35a6192015-04-01 13:05:26 -0700852 }
sanghob35a6192015-04-01 13:05:26 -0700853 }
854 }
855
sanghob35a6192015-04-01 13:05:26 -0700856 private void processLinkAdded(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700857 log.info("** LINK ADDED {}", link.toString());
Charles Chan0b4e6182015-11-03 10:42:14 -0800858 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
859 log.warn("Source device of this link is not configured.");
860 return;
861 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700862 //Irrespective whether the local is a MASTER or not for this device,
863 //create group handler instance and push default TTP flow rules.
864 //Because in a multi-instance setup, instances can initiate
865 //groups for any devices. Also the default TTP rules are needed
866 //to be pushed before inserting any IP table entries for any device
867 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
868 .deviceId());
869 if (groupHandler != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800870 groupHandler.linkUp(link, mastershipService.isLocalMaster(
871 link.src().deviceId()));
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700872 } else {
873 Device device = deviceService.getDevice(link.src().deviceId());
874 if (device != null) {
875 log.warn("processLinkAdded: Link Added "
876 + "Notification without Device Added "
877 + "event, still handling it");
878 processDeviceAdded(device);
879 groupHandler = groupHandlerMap.get(link.src()
880 .deviceId());
Saurav Das8a0732e2015-11-20 15:27:53 -0800881 groupHandler.linkUp(link, mastershipService.isLocalMaster(device.id()));
sanghob35a6192015-04-01 13:05:26 -0700882 }
883 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700884
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700885 log.trace("Starting optimized route population process");
886 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null);
887 //log.trace("processLinkAdded: re-starting route population process");
888 //defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -0700889
890 mcastHandler.init();
sanghob35a6192015-04-01 13:05:26 -0700891 }
892
893 private void processLinkRemoved(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700894 log.info("** LINK REMOVED {}", link.toString());
sangho834e4b02015-05-01 09:38:25 -0700895 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
896 if (groupHandler != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800897 groupHandler.portDown(link.src().port(),
898 mastershipService.isLocalMaster(link.src().deviceId()));
sangho834e4b02015-05-01 09:38:25 -0700899 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700900 log.trace("Starting optimized route population process");
901 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link);
902 //log.trace("processLinkRemoved: re-starting route population process");
903 //defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -0700904
905 mcastHandler.processLinkDown(link);
sanghob35a6192015-04-01 13:05:26 -0700906 }
907
908 private void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700909 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan0b4e6182015-11-03 10:42:14 -0800910 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das2857f382015-11-03 14:39:27 -0800911 log.warn("Device configuration uploading. Device {} will be "
912 + "processed after config completes.", device.id());
913 return;
914 }
Charles Chan2199c302016-04-23 17:36:10 -0700915 processDeviceAddedInternal(device.id());
916 }
917
918 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -0700919 // Irrespective of whether the local is a MASTER or not for this device,
920 // we need to create a SR-group-handler instance. This is because in a
921 // multi-instance setup, any instance can initiate forwarding/next-objectives
922 // for any switch (even if this instance is a SLAVE or not even connected
923 // to the switch). To handle this, a default-group-handler instance is necessary
924 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -0700925 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
926 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800927 DefaultGroupHandler groupHandler;
928 try {
929 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -0700930 createGroupHandler(deviceId,
931 appId,
932 deviceConfiguration,
933 linkService,
934 flowObjectiveService,
935 this);
Charles Chan0b4e6182015-11-03 10:42:14 -0800936 } catch (DeviceConfigNotFoundException e) {
937 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
938 return;
939 }
Charles Chan2199c302016-04-23 17:36:10 -0700940 log.debug("updating groupHandlerMap with new config for device: {}",
941 deviceId);
942 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -0800943 }
Saurav Dasb5c236e2016-06-07 10:08:06 -0700944
Charles Chan2199c302016-04-23 17:36:10 -0700945 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -0800946 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chan03a73e02016-10-24 14:52:01 -0700947 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -0700948 xConnectHandler.init(deviceId);
Charles Chan35fd1a72016-06-13 18:54:31 -0700949 cordConfigHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -0700950 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan59cc16d2017-02-02 16:20:42 -0800951 groupHandler.createGroupsFromVlanConfig();
Charles Chan2199c302016-04-23 17:36:10 -0700952 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700953 }
Charles Chan5270ed02016-01-30 23:22:37 -0800954
Charles Chan03a73e02016-10-24 14:52:01 -0700955 appCfgHandler.init(deviceId);
956 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -0700957 }
958
Saurav Das80980c72016-03-23 11:22:49 -0700959 private void processDeviceRemoved(Device device) {
960 nsNextObjStore.entrySet().stream()
961 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
962 .forEach(entry -> {
963 nsNextObjStore.remove(entry.getKey());
964 });
Charles Chan59cc16d2017-02-02 16:20:42 -0800965 vlanNextObjStore.entrySet().stream()
Saurav Das80980c72016-03-23 11:22:49 -0700966 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
967 .forEach(entry -> {
Charles Chan59cc16d2017-02-02 16:20:42 -0800968 vlanNextObjStore.remove(entry.getKey());
Saurav Das80980c72016-03-23 11:22:49 -0700969 });
Saurav Das80980c72016-03-23 11:22:49 -0700970 portNextObjStore.entrySet().stream()
971 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
972 .forEach(entry -> {
973 portNextObjStore.remove(entry.getKey());
974 });
Saurav Das80980c72016-03-23 11:22:49 -0700975 groupHandlerMap.remove(device.id());
Saurav Das80980c72016-03-23 11:22:49 -0700976 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan2199c302016-04-23 17:36:10 -0700977 mcastHandler.removeDevice(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -0700978 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -0700979 }
980
Saurav Das1a129a02016-11-18 15:21:57 -0800981 private void processPortUpdated(Device device, Port port) {
982 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
983 log.warn("Device configuration uploading. Not handling port event for"
984 + "dev: {} port: {}", device.id(), port.number());
985 return;
986 }
Saurav Das018605f2017-02-18 14:05:44 -0800987
988 if (!mastershipService.isLocalMaster(device.id())) {
989 log.debug("Not master for dev:{} .. not handling port updated event"
990 + "for port {}", device.id(), port.number());
991 return;
992 }
993
994 // first we handle filtering rules associated with the port
995 if (port.isEnabled()) {
996 log.info("Switchport {}/{} enabled..programming filters",
997 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -0800998 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Das018605f2017-02-18 14:05:44 -0800999 } else {
1000 log.info("Switchport {}/{} disabled..removing filters",
1001 device.id(), port.number());
Charles Chan7e4f8192017-02-26 22:59:35 -08001002 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Das018605f2017-02-18 14:05:44 -08001003 }
Saurav Das1a129a02016-11-18 15:21:57 -08001004
1005 // portUpdated calls are for ports that have gone down or up. For switch
1006 // to switch ports, link-events should take care of any re-routing or
1007 // group editing necessary for port up/down. Here we only process edge ports
1008 // that are already configured.
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001009 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1010 VlanId untaggedVlan = getUntaggedVlanId(cp);
1011 VlanId nativeVlan = getNativeVlanId(cp);
1012 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan59cc16d2017-02-02 16:20:42 -08001013
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001014 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das1a129a02016-11-18 15:21:57 -08001015 log.debug("Not handling port updated event for unconfigured port "
1016 + "dev/port: {}/{}", device.id(), port.number());
1017 return;
1018 }
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001019 if (untaggedVlan != null) {
1020 processEdgePort(device, port, untaggedVlan, true);
1021 }
1022 if (nativeVlan != null) {
1023 processEdgePort(device, port, nativeVlan, true);
1024 }
1025 if (!taggedVlans.isEmpty()) {
1026 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1027 }
Saurav Das1a129a02016-11-18 15:21:57 -08001028 }
1029
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001030 private void processEdgePort(Device device, Port port, VlanId vlanId,
1031 boolean popVlan) {
Saurav Das1a129a02016-11-18 15:21:57 -08001032 boolean portUp = port.isEnabled();
1033 if (portUp) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001034 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001035 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001036 } else {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001037 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan59cc16d2017-02-02 16:20:42 -08001038 port.number(), vlanId);
Saurav Das1a129a02016-11-18 15:21:57 -08001039 }
1040
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001041 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001042 if (groupHandler != null) {
Saurav Dasb0ae6ee2017-03-04 16:08:47 -08001043 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001044 } else {
1045 log.warn("Group handler not found for dev:{}. Not handling edge port"
1046 + " {} event for port:{}", device.id(),
1047 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001048 }
1049 }
sangho1e575652015-05-14 00:39:53 -07001050
Pier Ventre10bd8d12016-11-26 21:05:22 -08001051 /**
1052 * Registers the given connect point with the NRS, this is necessary
1053 * to receive the NDP and ARP packets from the NRS.
1054 *
1055 * @param portToRegister connect point to register
1056 */
1057 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001058 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001059 portToRegister,
1060 neighbourHandler,
1061 appId
1062 );
1063 }
1064
Charles Chand6832882015-10-05 17:50:33 -07001065 private class InternalConfigListener implements NetworkConfigListener {
Charles Chan2c15aca2016-11-09 20:51:44 -08001066 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001067
Charles Chane849c192016-01-11 18:28:54 -08001068 /**
1069 * Constructs the internal network config listener.
1070 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001071 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001072 */
Charles Chan2c15aca2016-11-09 20:51:44 -08001073 public InternalConfigListener(SegmentRoutingManager srManager) {
1074 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001075 }
1076
Charles Chane849c192016-01-11 18:28:54 -08001077 /**
1078 * Reads network config and initializes related data structure accordingly.
1079 */
Charles Chan4636be02015-10-07 14:21:45 -07001080 public void configureNetwork() {
Pier Ventre10bd8d12016-11-26 21:05:22 -08001081
Charles Chan2c15aca2016-11-09 20:51:44 -08001082 deviceConfiguration = new DeviceConfiguration(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001083
Charles Chan2c15aca2016-11-09 20:51:44 -08001084 arpHandler = new ArpHandler(srManager);
1085 icmpHandler = new IcmpHandler(srManager);
1086 ipHandler = new IpHandler(srManager);
1087 routingRulePopulator = new RoutingRulePopulator(srManager);
1088 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001089
1090 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1091 groupHandlerMap, tunnelStore);
1092 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1093 flowObjectiveService,
1094 tunnelHandler, policyStore);
1095
Charles Chan4636be02015-10-07 14:21:45 -07001096 for (Device device : deviceService.getDevices()) {
Charles Chan2199c302016-04-23 17:36:10 -07001097 processDeviceAddedInternal(device.id());
Charles Chan4636be02015-10-07 14:21:45 -07001098 }
1099
1100 defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -07001101 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001102 }
1103
Charles Chand6832882015-10-05 17:50:33 -07001104 @Override
1105 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001106 // TODO move this part to NetworkConfigEventHandler
1107 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1108 switch (event.type()) {
1109 case CONFIG_ADDED:
1110 log.info("Segment Routing Config added.");
1111 configureNetwork();
1112 break;
1113 case CONFIG_UPDATED:
1114 log.info("Segment Routing Config updated.");
1115 // TODO support dynamic configuration
1116 break;
1117 default:
1118 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001119 }
Charles Chan5270ed02016-01-30 23:22:37 -08001120 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001121 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001122 switch (event.type()) {
1123 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001124 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001125 break;
1126 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001127 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001128 break;
1129 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001130 appCfgHandler.processAppConfigRemoved(event);
1131 break;
1132 default:
1133 break;
1134 }
Charles Chan03a73e02016-10-24 14:52:01 -07001135 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001136 } else if (event.configClass().equals(XConnectConfig.class)) {
1137 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1138 switch (event.type()) {
1139 case CONFIG_ADDED:
1140 xConnectHandler.processXConnectConfigAdded(event);
1141 break;
1142 case CONFIG_UPDATED:
1143 xConnectHandler.processXConnectConfigUpdated(event);
1144 break;
1145 case CONFIG_REMOVED:
1146 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001147 break;
1148 default:
1149 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001150 }
Pier Ventref34966c2016-11-07 16:21:04 -08001151 } else if (event.configClass().equals(PwaasConfig.class)) {
1152 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1153 switch (event.type()) {
1154 case CONFIG_ADDED:
1155 l2TunnelHandler.processPwaasConfigAdded(event);
1156 break;
1157 case CONFIG_UPDATED:
1158 l2TunnelHandler.processPwaasConfigUpdated(event);
1159 break;
1160 case CONFIG_REMOVED:
1161 l2TunnelHandler.processPwaasConfigRemoved(event);
1162 break;
1163 default:
1164 break;
1165 }
Charles Chand6832882015-10-05 17:50:33 -07001166 }
1167 }
1168 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001169
1170 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001171 @Override
1172 public void event(HostEvent event) {
1173 // Do not proceed without mastership
1174 DeviceId deviceId = event.subject().location().deviceId();
1175 if (!mastershipService.isLocalMaster(deviceId)) {
1176 return;
1177 }
1178
1179 switch (event.type()) {
1180 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001181 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001182 break;
1183 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001184 hostHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001185 break;
1186 case HOST_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001187 hostHandler.processHostRemoveEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001188 break;
1189 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001190 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001191 break;
1192 default:
1193 log.warn("Unsupported host event type: {}", event.type());
1194 break;
1195 }
1196 }
1197 }
1198
Charles Chand55e84d2016-03-30 17:54:24 -07001199 private class InternalMcastListener implements McastListener {
1200 @Override
1201 public void event(McastEvent event) {
1202 switch (event.type()) {
1203 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001204 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001205 break;
1206 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001207 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001208 break;
1209 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001210 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001211 break;
1212 case ROUTE_ADDED:
1213 case ROUTE_REMOVED:
1214 default:
1215 break;
1216 }
1217 }
1218 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001219
1220 private class InternalCordConfigListener implements CordConfigListener {
1221 @Override
1222 public void event(CordConfigEvent event) {
1223 switch (event.type()) {
1224 case ACCESS_AGENT_ADDED:
1225 cordConfigHandler.processAccessAgentAddedEvent(event);
1226 break;
1227 case ACCESS_AGENT_UPDATED:
1228 cordConfigHandler.processAccessAgentUpdatedEvent(event);
1229 break;
1230 case ACCESS_AGENT_REMOVED:
1231 cordConfigHandler.processAccessAgentRemovedEvent(event);
1232 break;
1233 case ACCESS_DEVICE_ADDED:
1234 case ACCESS_DEVICE_UPDATED:
1235 case ACCESS_DEVICE_REMOVED:
1236 default:
1237 break;
1238 }
1239 }
1240 }
Charles Chan03a73e02016-10-24 14:52:01 -07001241
1242 private class InternalRouteEventListener implements RouteListener {
1243 @Override
1244 public void event(RouteEvent event) {
1245 switch (event.type()) {
1246 case ROUTE_ADDED:
1247 routeHandler.processRouteAdded(event);
1248 break;
1249 case ROUTE_UPDATED:
1250 routeHandler.processRouteUpdated(event);
1251 break;
1252 case ROUTE_REMOVED:
1253 routeHandler.processRouteRemoved(event);
1254 break;
1255 default:
1256 break;
1257 }
1258 }
1259 }
sanghob35a6192015-04-01 13:05:26 -07001260}