blob: 4f912f396812cdbf38fbb823a3fa9263c624c924 [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 Chanc81c45b2016-10-20 17:02:44 -070018import com.google.common.collect.Maps;
sanghob35a6192015-04-01 13:05:26 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho1e575652015-05-14 00:39:53 -070024import org.apache.felix.scr.annotations.Service;
sanghob35a6192015-04-01 13:05:26 -070025import org.onlab.packet.Ethernet;
Pier Ventre735b8c82016-12-02 08:16:05 -080026import org.onlab.packet.ICMP6;
Charles Chanc42e84e2015-10-20 16:24:19 -070027import org.onlab.packet.IPv4;
Pier Ventre10bd8d12016-11-26 21:05:22 -080028import org.onlab.packet.IPv6;
Charles Chanc42e84e2015-10-20 16:24:19 -070029import org.onlab.packet.Ip4Prefix;
Charles Chanc42e84e2015-10-20 16:24:19 -070030import org.onlab.packet.IpPrefix;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070031import org.onlab.packet.VlanId;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070032import org.onlab.util.KryoNamespace;
Saurav Das80980c72016-03-23 11:22:49 -070033import org.onosproject.cfg.ComponentConfigService;
sanghob35a6192015-04-01 13:05:26 -070034import org.onosproject.core.ApplicationId;
35import org.onosproject.core.CoreService;
36import org.onosproject.event.Event;
Charles Chand55e84d2016-03-30 17:54:24 -070037import org.onosproject.incubator.net.config.basics.McastConfig;
Charles Chan2c15aca2016-11-09 20:51:44 -080038import org.onosproject.incubator.net.intf.InterfaceService;
Charles Chan03a73e02016-10-24 14:52:01 -070039import org.onosproject.incubator.net.routing.RouteEvent;
40import org.onosproject.incubator.net.routing.RouteListener;
41import org.onosproject.incubator.net.routing.RouteService;
Pier Ventre735b8c82016-12-02 08:16:05 -080042import org.onosproject.incubator.net.neighbour.NeighbourResolutionService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070043import org.onosproject.mastership.MastershipService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080044import org.onosproject.net.ConnectPoint;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070045import org.onosproject.net.Device;
46import org.onosproject.net.DeviceId;
47import org.onosproject.net.Link;
48import org.onosproject.net.Port;
Charles Chan68aa62d2015-11-09 16:37:23 -080049import org.onosproject.net.PortNumber;
Charles Chand6832882015-10-05 17:50:33 -070050import org.onosproject.net.config.ConfigFactory;
51import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070052import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070053import org.onosproject.net.config.NetworkConfigRegistry;
Charles Chand6832882015-10-05 17:50:33 -070054import org.onosproject.net.config.basics.SubjectFactories;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070055import org.onosproject.net.device.DeviceEvent;
56import org.onosproject.net.device.DeviceListener;
57import org.onosproject.net.device.DeviceService;
Charles Chan68aa62d2015-11-09 16:37:23 -080058import org.onosproject.net.flow.TrafficSelector;
59import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070060import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan68aa62d2015-11-09 16:37:23 -080061import org.onosproject.net.host.HostEvent;
62import org.onosproject.net.host.HostListener;
Pier Ventre10bd8d12016-11-26 21:05:22 -080063import org.onosproject.net.host.HostService;
64import org.onosproject.net.link.LinkEvent;
65import org.onosproject.net.link.LinkListener;
66import org.onosproject.net.link.LinkService;
Charles Chand55e84d2016-03-30 17:54:24 -070067import org.onosproject.net.mcast.McastEvent;
68import org.onosproject.net.mcast.McastListener;
69import org.onosproject.net.mcast.MulticastRouteService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080070import org.onosproject.net.packet.InboundPacket;
71import org.onosproject.net.packet.PacketContext;
72import org.onosproject.net.packet.PacketProcessor;
73import org.onosproject.net.packet.PacketService;
Charles Chand55e84d2016-03-30 17:54:24 -070074import org.onosproject.net.topology.TopologyService;
75import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
76import org.onosproject.segmentrouting.config.DeviceConfiguration;
Charles Chand55e84d2016-03-30 17:54:24 -070077import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Pier Ventre10bd8d12016-11-26 21:05:22 -080078import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -070079import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -070080import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
81import org.onosproject.segmentrouting.grouphandler.NeighborSet;
Charles Chand2990362016-04-18 13:44:03 -070082import org.onosproject.segmentrouting.storekey.NeighborSetNextObjectiveStoreKey;
83import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chand2990362016-04-18 13:44:03 -070084import org.onosproject.segmentrouting.storekey.SubnetAssignedVidStoreKey;
85import org.onosproject.segmentrouting.storekey.SubnetNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -070086import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070087import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070088import org.onosproject.store.service.EventuallyConsistentMap;
89import org.onosproject.store.service.EventuallyConsistentMapBuilder;
90import org.onosproject.store.service.StorageService;
91import org.onosproject.store.service.WallClockTimestamp;
Charles Chan35fd1a72016-06-13 18:54:31 -070092import org.opencord.cordconfig.CordConfigEvent;
93import org.opencord.cordconfig.CordConfigListener;
94import org.opencord.cordconfig.CordConfigService;
sanghob35a6192015-04-01 13:05:26 -070095import org.slf4j.Logger;
96import org.slf4j.LoggerFactory;
97
Saurav Das0e99e2b2015-10-28 12:39:42 -070098import java.util.Collections;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070099import java.util.HashSet;
sangho1e575652015-05-14 00:39:53 -0700100import java.util.List;
sanghob35a6192015-04-01 13:05:26 -0700101import java.util.Map;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700102import java.util.Set;
sanghob35a6192015-04-01 13:05:26 -0700103import java.util.concurrent.ConcurrentHashMap;
104import java.util.concurrent.ConcurrentLinkedQueue;
105import java.util.concurrent.Executors;
106import java.util.concurrent.ScheduledExecutorService;
107import java.util.concurrent.ScheduledFuture;
108import java.util.concurrent.TimeUnit;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800109import java.util.stream.Collectors;
sanghob35a6192015-04-01 13:05:26 -0700110
Charles Chan3e783d02016-02-26 22:19:52 -0800111import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800112import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700113import static org.onlab.util.Tools.groupedThreads;
Charles Chan3e783d02016-02-26 22:19:52 -0800114
Charles Chane849c192016-01-11 18:28:54 -0800115/**
116 * Segment routing manager.
117 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700118@Service
119@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700120public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700121
Charles Chan2c15aca2016-11-09 20:51:44 -0800122 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
sanghob35a6192015-04-01 13:05:26 -0700123
124 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700125 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700126
127 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre10bd8d12016-11-26 21:05:22 -0800128 private NeighbourResolutionService neighbourResolutionService;
129
130 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700131 CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700132
133 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700134 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700135
136 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700137 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700138
139 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700140 DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700141
142 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700143 FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700144
145 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700146 LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700147
Charles Chan5270ed02016-01-30 23:22:37 -0800148 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700149 MastershipService mastershipService;
150
151 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
152 StorageService storageService;
153
154 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
155 MulticastRouteService multicastRouteService;
156
157 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
158 TopologyService topologyService;
159
160 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
161 CordConfigService cordConfigService;
162
163 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
164 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800165
166 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800167 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800168
Saurav Das80980c72016-03-23 11:22:49 -0700169 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800170 public InterfaceService interfaceService;
171
Charles Chan03a73e02016-10-24 14:52:01 -0700172 ArpHandler arpHandler = null;
173 IcmpHandler icmpHandler = null;
174 IpHandler ipHandler = null;
175 RoutingRulePopulator routingRulePopulator = null;
Charles Chan2c15aca2016-11-09 20:51:44 -0800176 public ApplicationId appId;
sangho666cd6d2015-04-14 16:27:13 -0700177 protected DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700178
Charles Chan03a73e02016-10-24 14:52:01 -0700179 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700180 private TunnelHandler tunnelHandler = null;
181 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700182 private InternalPacketProcessor processor = null;
183 private InternalLinkListener linkListener = null;
184 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700185 private AppConfigHandler appCfgHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700186 XConnectHandler xConnectHandler = null;
Charles Chand2990362016-04-18 13:44:03 -0700187 private McastHandler mcastHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700188 HostHandler hostHandler = null;
Charles Chan35fd1a72016-06-13 18:54:31 -0700189 private CordConfigHandler cordConfigHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800190 private RouteHandler routeHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800191 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
sanghob35a6192015-04-01 13:05:26 -0700192 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan5270ed02016-01-30 23:22:37 -0800193 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700194 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
195 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan35fd1a72016-06-13 18:54:31 -0700196 private final InternalCordConfigListener cordConfigListener = new InternalCordConfigListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700197 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sanghob35a6192015-04-01 13:05:26 -0700198
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700199 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700200 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sanghob35a6192015-04-01 13:05:26 -0700201
Saurav Das4ce45962015-11-24 23:21:05 -0800202 @SuppressWarnings("unused")
sanghob35a6192015-04-01 13:05:26 -0700203 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800204 @SuppressWarnings("rawtypes")
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700205 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Charles Chan68aa62d2015-11-09 16:37:23 -0800206 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chane849c192016-01-11 18:28:54 -0800207 new ConcurrentHashMap<>();
208 /**
209 * Per device next objective ID store with (device id + neighbor set) as key.
210 */
211 public EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, Integer>
Charles Chan68aa62d2015-11-09 16:37:23 -0800212 nsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800213 /**
214 * Per device next objective ID store with (device id + subnet) as key.
215 */
216 public EventuallyConsistentMap<SubnetNextObjectiveStoreKey, Integer>
Charles Chan68aa62d2015-11-09 16:37:23 -0800217 subnetNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800218 /**
219 * Per device next objective ID store with (device id + port) as key.
220 */
221 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800222 portNextObjStore = null;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700223 // Per device, per-subnet assigned-vlans store, with (device id + subnet
224 // IPv4 prefix) as key
225 private EventuallyConsistentMap<SubnetAssignedVidStoreKey, VlanId>
Charles Chane849c192016-01-11 18:28:54 -0800226 subnetVidStore = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800227 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
228 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700229
Charles Chand55e84d2016-03-30 17:54:24 -0700230 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700231 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
232 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700233 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700234 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800235 public SegmentRoutingDeviceConfig createConfig() {
236 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700237 }
238 };
Charles Chand55e84d2016-03-30 17:54:24 -0700239 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700240 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
241 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700242 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800243 @Override
244 public SegmentRoutingAppConfig createConfig() {
245 return new SegmentRoutingAppConfig();
246 }
247 };
Charles Chanfc5c7802016-05-17 13:13:55 -0700248 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
249 new ConfigFactory<ApplicationId, XConnectConfig>(
250 SubjectFactories.APP_SUBJECT_FACTORY,
251 XConnectConfig.class, "xconnect") {
252 @Override
253 public XConnectConfig createConfig() {
254 return new XConnectConfig();
255 }
256 };
Charles Chand55e84d2016-03-30 17:54:24 -0700257 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700258 new ConfigFactory<ApplicationId, McastConfig>(
259 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700260 McastConfig.class, "multicast") {
261 @Override
262 public McastConfig createConfig() {
263 return new McastConfig();
264 }
265 };
266
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700267 private Object threadSchedulerLock = new Object();
268 private static int numOfEventsQueued = 0;
269 private static int numOfEventsExecuted = 0;
sanghob35a6192015-04-01 13:05:26 -0700270 private static int numOfHandlerExecution = 0;
271 private static int numOfHandlerScheduled = 0;
272
Charles Chan116188d2016-02-18 14:22:42 -0800273 /**
274 * Segment Routing App ID.
275 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800276 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chane849c192016-01-11 18:28:54 -0800277 /**
278 * The starting value of per-subnet VLAN ID assignment.
279 */
Saurav Das0e99e2b2015-10-28 12:39:42 -0700280 private static final short ASSIGNED_VLAN_START = 4093;
Charles Chane849c192016-01-11 18:28:54 -0800281 /**
282 * The default VLAN ID assigned to the interfaces without subnet config.
283 */
Saurav Das0e99e2b2015-10-28 12:39:42 -0700284 public static final short ASSIGNED_VLAN_NO_SUBNET = 4094;
285
sanghob35a6192015-04-01 13:05:26 -0700286 @Activate
287 protected void activate() {
Charles Chan2c15aca2016-11-09 20:51:44 -0800288 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700289
290 log.debug("Creating EC map nsnextobjectivestore");
291 EventuallyConsistentMapBuilder<NeighborSetNextObjectiveStoreKey, Integer>
292 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700293 nsNextObjStore = nsNextObjMapBuilder
294 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700295 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700296 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700297 .build();
298 log.trace("Current size {}", nsNextObjStore.size());
299
Charles Chanc42e84e2015-10-20 16:24:19 -0700300 log.debug("Creating EC map subnetnextobjectivestore");
301 EventuallyConsistentMapBuilder<SubnetNextObjectiveStoreKey, Integer>
302 subnetNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Charles Chanc42e84e2015-10-20 16:24:19 -0700303 subnetNextObjStore = subnetNextObjMapBuilder
304 .withName("subnetnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700305 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700306 .withTimestampProvider((k, v) -> new WallClockTimestamp())
307 .build();
308
Saurav Das4ce45962015-11-24 23:21:05 -0800309 log.debug("Creating EC map subnetnextobjectivestore");
310 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
311 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
312 portNextObjStore = portNextObjMapBuilder
313 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700314 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800315 .withTimestampProvider((k, v) -> new WallClockTimestamp())
316 .build();
317
sangho0b2b6d12015-05-20 22:16:38 -0700318 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
319 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700320 tunnelStore = tunnelMapBuilder
321 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700322 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700323 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700324 .build();
325
326 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
327 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700328 policyStore = policyMapBuilder
329 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700330 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700331 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700332 .build();
333
Saurav Das0e99e2b2015-10-28 12:39:42 -0700334 EventuallyConsistentMapBuilder<SubnetAssignedVidStoreKey, VlanId>
335 subnetVidStoreMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das0e99e2b2015-10-28 12:39:42 -0700336 subnetVidStore = subnetVidStoreMapBuilder
337 .withName("subnetvidstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700338 .withSerializer(createSerializer())
Saurav Das0e99e2b2015-10-28 12:39:42 -0700339 .withTimestampProvider((k, v) -> new WallClockTimestamp())
340 .build();
341
Saurav Das80980c72016-03-23 11:22:49 -0700342 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800343 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700344 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800345 "purgeOnDisconnection", "true");
Charles Chan03a73e02016-10-24 14:52:01 -0700346 compCfgService.preSetProperty("org.onosproject.vrouter.Vrouter",
Charles Chan77899532017-01-18 20:00:13 -0800347 "fibInstallerEnabled", "false");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800348 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
349 "requestInterceptsEnabled", "false");
Pier Luigi7e415132017-01-12 22:46:39 -0800350 compCfgService.preSetProperty("org.onosproject.incubator.net.neighbour.impl.NeighbourResolutionManager",
351 "requestInterceptsEnabled", "false");
352 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelay",
353 "arpEnabled", "false");
Saurav Das80980c72016-03-23 11:22:49 -0700354
Charles Chanb8e10c82015-10-14 11:24:40 -0700355 processor = new InternalPacketProcessor();
356 linkListener = new InternalLinkListener();
357 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700358 appCfgHandler = new AppConfigHandler(this);
359 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700360 mcastHandler = new McastHandler(this);
361 hostHandler = new HostHandler(this);
Charles Chan35fd1a72016-06-13 18:54:31 -0700362 cordConfigHandler = new CordConfigHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700363 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800364 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700365
Charles Chan3e783d02016-02-26 22:19:52 -0800366 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700367 cfgService.registerConfigFactory(deviceConfigFactory);
368 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700369 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700370 cfgService.registerConfigFactory(mcastConfigFactory);
Charles Chan5270ed02016-01-30 23:22:37 -0800371 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700372 packetService.addProcessor(processor, PacketProcessor.director(2));
373 linkService.addListener(linkListener);
374 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700375 multicastRouteService.addListener(mcastListener);
Charles Chan35fd1a72016-06-13 18:54:31 -0700376 cordConfigService.addListener(cordConfigListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700377
378 cfgListener.configureNetwork();
379
Charles Chan03a73e02016-10-24 14:52:01 -0700380 routeService.addListener(routeListener);
381
sanghob35a6192015-04-01 13:05:26 -0700382 log.info("Started");
383 }
384
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700385 private KryoNamespace.Builder createSerializer() {
386 return new KryoNamespace.Builder()
387 .register(KryoNamespaces.API)
388 .register(NeighborSetNextObjectiveStoreKey.class,
389 SubnetNextObjectiveStoreKey.class,
390 SubnetAssignedVidStoreKey.class,
391 NeighborSet.class,
392 Tunnel.class,
393 DefaultTunnel.class,
394 Policy.class,
395 TunnelPolicy.class,
396 Policy.Type.class,
397 PortNextObjectiveStoreKey.class,
Charles Chanfc5c7802016-05-17 13:13:55 -0700398 XConnectStoreKey.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700399 );
400 }
401
sanghob35a6192015-04-01 13:05:26 -0700402 @Deactivate
403 protected void deactivate() {
Charles Chand6832882015-10-05 17:50:33 -0700404 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700405 cfgService.unregisterConfigFactory(deviceConfigFactory);
406 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700407 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700408 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chand6832882015-10-05 17:50:33 -0700409
sanghob35a6192015-04-01 13:05:26 -0700410 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700411 linkService.removeListener(linkListener);
412 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700413 multicastRouteService.removeListener(mcastListener);
Charles Chan35fd1a72016-06-13 18:54:31 -0700414 cordConfigService.removeListener(cordConfigListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700415 routeService.removeListener(routeListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700416
sanghob35a6192015-04-01 13:05:26 -0700417 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700418 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700419 deviceListener = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700420 groupHandlerMap.clear();
421
Charles Chand55e84d2016-03-30 17:54:24 -0700422 nsNextObjStore.destroy();
423 subnetNextObjStore.destroy();
424 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700425 tunnelStore.destroy();
426 policyStore.destroy();
427 subnetVidStore.destroy();
sanghob35a6192015-04-01 13:05:26 -0700428 log.info("Stopped");
429 }
430
sangho1e575652015-05-14 00:39:53 -0700431 @Override
432 public List<Tunnel> getTunnels() {
433 return tunnelHandler.getTunnels();
434 }
435
436 @Override
sangho71abe1b2015-06-29 14:58:47 -0700437 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
438 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700439 }
440
441 @Override
sangho71abe1b2015-06-29 14:58:47 -0700442 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700443 for (Policy policy: policyHandler.getPolicies()) {
444 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
445 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
446 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
447 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700448 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700449 }
450 }
451 }
sangho71abe1b2015-06-29 14:58:47 -0700452 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700453 }
454
455 @Override
sangho71abe1b2015-06-29 14:58:47 -0700456 public PolicyHandler.Result removePolicy(Policy policy) {
457 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700458 }
459
460 @Override
sangho71abe1b2015-06-29 14:58:47 -0700461 public PolicyHandler.Result createPolicy(Policy policy) {
462 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700463 }
464
465 @Override
466 public List<Policy> getPolicies() {
467 return policyHandler.getPolicies();
468 }
469
Saurav Das59232cf2016-04-27 18:35:50 -0700470 @Override
471 public void rerouteNetwork() {
472 cfgListener.configureNetwork();
473 for (Device device : deviceService.getDevices()) {
474 defaultRoutingHandler.populatePortAddressingRules(device.id());
475 }
476 defaultRoutingHandler.startPopulationProcess();
477 }
478
Charles Chanc81c45b2016-10-20 17:02:44 -0700479 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800480 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
481 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Charles Chanc81c45b2016-10-20 17:02:44 -0700482 deviceService.getAvailableDevices().forEach(device -> {
483 deviceSubnetMap.put(device.id(), deviceConfiguration.getSubnets(device.id()));
484 });
485 return deviceSubnetMap;
486 }
487
sanghof9d0bf12015-05-19 11:57:42 -0700488 /**
Pier Ventre98161782016-10-31 15:00:01 -0700489 * Returns the MPLS-ECMP configuration.
490 *
491 * @return MPLS-ECMP value
492 */
493 public boolean getMplsEcmp() {
494 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
495 .getConfig(this.appId, SegmentRoutingAppConfig.class);
496 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
497 }
498
499 /**
sanghof9d0bf12015-05-19 11:57:42 -0700500 * Returns the tunnel object with the tunnel ID.
501 *
502 * @param tunnelId Tunnel ID
503 * @return Tunnel reference
504 */
sangho1e575652015-05-14 00:39:53 -0700505 public Tunnel getTunnel(String tunnelId) {
506 return tunnelHandler.getTunnel(tunnelId);
507 }
508
sanghob35a6192015-04-01 13:05:26 -0700509 /**
Saurav Das0e99e2b2015-10-28 12:39:42 -0700510 * Returns the vlan-id assigned to the subnet configured for a device.
511 * If no vlan-id has been assigned, a new one is assigned out of a pool of ids,
512 * if and only if this controller instance is the master for the device.
513 * <p>
514 * USAGE: The assigned vlans are meant to be applied to untagged packets on those
515 * switches/pipelines that need this functionality. These vids are meant
516 * to be used internally within a switch, and thus need to be unique only
517 * on a switch level. Note that packets never go out on the wire with these
518 * vlans. Currently, vlan ids are assigned from value 4093 down.
519 * Vlan id 4094 expected to be used for all ports that are not assigned subnets.
520 * Vlan id 4095 is reserved and unused. Only a single vlan id is assigned
521 * per subnet.
sanghob35a6192015-04-01 13:05:26 -0700522 *
Saurav Das0e99e2b2015-10-28 12:39:42 -0700523 * @param deviceId switch dpid
Pier Ventref4b5fce2016-11-28 16:48:06 -0800524 * @param subnet IP prefix for which assigned vlan is desired
Saurav Das0e99e2b2015-10-28 12:39:42 -0700525 * @return VlanId assigned for the subnet on the device, or
526 * null if no vlan assignment was found and this instance is not
527 * the master for the device.
sanghob35a6192015-04-01 13:05:26 -0700528 */
Charles Chane849c192016-01-11 18:28:54 -0800529 // TODO: We should avoid assigning VLAN IDs that are used by VLAN cross-connection.
Pier Ventre10bd8d12016-11-26 21:05:22 -0800530 public VlanId getSubnetAssignedVlanId(DeviceId deviceId, IpPrefix subnet) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700531 VlanId assignedVid = subnetVidStore.get(new SubnetAssignedVidStoreKey(
532 deviceId, subnet));
533 if (assignedVid != null) {
534 log.debug("Query for subnet:{} on device:{} returned assigned-vlan "
535 + "{}", subnet, deviceId, assignedVid);
536 return assignedVid;
537 }
538 //check mastership for the right to assign a vlan
539 if (!mastershipService.isLocalMaster(deviceId)) {
540 log.warn("This controller instance is not the master for device {}. "
541 + "Cannot assign vlan-id for subnet {}", deviceId, subnet);
542 return null;
543 }
544 // vlan assignment is expensive but done only once
Pier Ventre10bd8d12016-11-26 21:05:22 -0800545 // FIXME for now we will do assignment considering only the ipv4 subnet.
546 Set<Ip4Prefix> configuredSubnets = deviceConfiguration.getSubnets(deviceId)
547 .stream()
548 .filter(IpPrefix::isIp4)
549 .map(IpPrefix::getIp4Prefix)
550 .collect(Collectors.toSet());
Saurav Das0e99e2b2015-10-28 12:39:42 -0700551 Set<Short> assignedVlans = new HashSet<>();
552 Set<Ip4Prefix> unassignedSubnets = new HashSet<>();
553 for (Ip4Prefix sub : configuredSubnets) {
554 VlanId v = subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId,
555 sub));
556 if (v != null) {
557 assignedVlans.add(v.toShort());
558 } else {
559 unassignedSubnets.add(sub);
560 }
561 }
562 short nextAssignedVlan = ASSIGNED_VLAN_START;
563 if (!assignedVlans.isEmpty()) {
564 nextAssignedVlan = (short) (Collections.min(assignedVlans) - 1);
565 }
566 for (Ip4Prefix unsub : unassignedSubnets) {
Pier Ventref4b5fce2016-11-28 16:48:06 -0800567 subnetVidStore.put(new SubnetAssignedVidStoreKey(deviceId, unsub),
568 VlanId.vlanId(nextAssignedVlan--));
569 log.info("Assigned vlan: {} to subnet: {} on device: {}",
570 nextAssignedVlan + 1, unsub, deviceId);
sanghob35a6192015-04-01 13:05:26 -0700571 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700572 return subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId, subnet));
sanghob35a6192015-04-01 13:05:26 -0700573 }
574
sangho1e575652015-05-14 00:39:53 -0700575 /**
Saurav Das0e99e2b2015-10-28 12:39:42 -0700576 * Returns the next objective ID for the given NeighborSet.
Saurav Das8a0732e2015-11-20 15:27:53 -0800577 * If the nextObjective does not exist, a new one is created and
Saurav Das4ce45962015-11-24 23:21:05 -0800578 * its id is returned.
sangho1e575652015-05-14 00:39:53 -0700579 *
sanghof9d0bf12015-05-19 11:57:42 -0700580 * @param deviceId Device ID
581 * @param ns NegighborSet
Saurav Das8a0732e2015-11-20 15:27:53 -0800582 * @param meta metadata passed into the creation of a Next Objective
583 * @return next objective ID or -1 if an error was encountered during the
584 * creation of the nextObjective
sangho1e575652015-05-14 00:39:53 -0700585 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800586 public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns,
587 TrafficSelector meta) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700588 if (groupHandlerMap.get(deviceId) != null) {
589 log.trace("getNextObjectiveId query in device {}", deviceId);
590 return groupHandlerMap
Saurav Das8a0732e2015-11-20 15:27:53 -0800591 .get(deviceId).getNextObjectiveId(ns, meta);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700592 } else {
Saurav Das4ce45962015-11-24 23:21:05 -0800593 log.warn("getNextObjectiveId query - groupHandler for device {} "
594 + "not found", deviceId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700595 return -1;
596 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700597 }
598
Charles Chanc42e84e2015-10-20 16:24:19 -0700599 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800600 * Returns the next objective ID for the given subnet prefix. It is expected
601 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700602 *
603 * @param deviceId Device ID
604 * @param prefix Subnet
Saurav Das4ce45962015-11-24 23:21:05 -0800605 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700606 */
607 public int getSubnetNextObjectiveId(DeviceId deviceId, IpPrefix prefix) {
608 if (groupHandlerMap.get(deviceId) != null) {
609 log.trace("getSubnetNextObjectiveId query in device {}", deviceId);
610 return groupHandlerMap
611 .get(deviceId).getSubnetNextObjectiveId(prefix);
612 } else {
Saurav Das4ce45962015-11-24 23:21:05 -0800613 log.warn("getSubnetNextObjectiveId query - groupHandler for "
614 + "device {} not found", deviceId);
615 return -1;
616 }
617 }
618
619 /**
620 * Returns the next objective ID for the given portNumber, given the treatment.
621 * There could be multiple different treatments to the same outport, which
622 * would result in different objectives. If the next object
623 * does not exist, a new one is created and its id is returned.
624 *
625 * @param deviceId Device ID
626 * @param portNum port number on device for which NextObjective is queried
627 * @param treatment the actions to apply on the packets (should include outport)
628 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das59232cf2016-04-27 18:35:50 -0700629 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800630 */
631 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
632 TrafficTreatment treatment,
633 TrafficSelector meta) {
634 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
635 if (ghdlr != null) {
636 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta);
637 } else {
Charles Chane849c192016-01-11 18:28:54 -0800638 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
639 + " not found", deviceId);
640 return -1;
641 }
642 }
643
sanghob35a6192015-04-01 13:05:26 -0700644 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700645 @Override
646 public void process(PacketContext context) {
647
648 if (context.isHandled()) {
649 return;
650 }
651
652 InboundPacket pkt = context.inPacket();
653 Ethernet ethernet = pkt.parsed();
Saurav Das4ce45962015-11-24 23:21:05 -0800654 log.trace("Rcvd pktin: {}", ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800655 if (ethernet.getEtherType() == TYPE_ARP) {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800656 log.warn("{} - we are still receiving ARP packets from {}",
657 context.inPacket().receivedFrom());
658 log.debug("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -0800659 return;
sanghob35a6192015-04-01 13:05:26 -0700660 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800661 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
662 //ipHandler.addToPacketBuffer(ipv4Packet);
663 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
664 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700665 } else {
Charles Chan50035632017-01-13 17:20:44 -0800666 // NOTE: We don't support IP learning at this moment so this
667 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -0800668 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700669 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800670 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
671 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -0800672 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Ventre10bd8d12016-11-26 21:05:22 -0800673 /*
Pier Ventre735b8c82016-12-02 08:16:05 -0800674 * We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre10bd8d12016-11-26 21:05:22 -0800675 */
Pier Ventre735b8c82016-12-02 08:16:05 -0800676 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
677 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
678 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
679 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
680 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
681 } else {
682 log.warn("Received ICMPv6 0x{} - not handled",
683 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
684 }
685 } else {
686 // NOTE: We don't support IP learning at this moment so this
687 // is not necessary. Also it causes duplication of DHCPv6 packets.
688 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
689 }
sanghob35a6192015-04-01 13:05:26 -0700690 }
691 }
692 }
693
694 private class InternalLinkListener implements LinkListener {
695 @Override
696 public void event(LinkEvent event) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700697 if (event.type() == LinkEvent.Type.LINK_ADDED
698 || event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700699 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700700 scheduleEventHandlerIfNotScheduled(event);
701 }
702 }
703 }
704
705 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -0700706 @Override
707 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -0700708 switch (event.type()) {
709 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -0800710 case PORT_UPDATED:
711 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -0700712 case DEVICE_UPDATED:
713 case DEVICE_AVAILABILITY_CHANGED:
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700714 log.debug("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700715 scheduleEventHandlerIfNotScheduled(event);
716 break;
717 default:
718 }
719 }
720 }
721
Saurav Das4ce45962015-11-24 23:21:05 -0800722 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -0700723 private void scheduleEventHandlerIfNotScheduled(Event event) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700724 synchronized (threadSchedulerLock) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700725 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700726 numOfEventsQueued++;
727
728 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
729 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700730 eventHandlerFuture = executorService
731 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
732 numOfHandlerScheduled++;
733 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700734 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700735 numOfEventsQueued,
736 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -0700737 }
sanghob35a6192015-04-01 13:05:26 -0700738 }
739
740 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700741 @Override
sanghob35a6192015-04-01 13:05:26 -0700742 public void run() {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700743 try {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700744 while (true) {
Saurav Das4ce45962015-11-24 23:21:05 -0800745 @SuppressWarnings("rawtypes")
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700746 Event event = null;
747 synchronized (threadSchedulerLock) {
748 if (!eventQueue.isEmpty()) {
749 event = eventQueue.poll();
750 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700751 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700752 numOfHandlerExecution++;
753 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
754 numOfHandlerExecution, numOfEventsExecuted);
755 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700756 }
sangho20eff1d2015-04-13 15:15:58 -0700757 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700758 if (event.type() == LinkEvent.Type.LINK_ADDED) {
759 processLinkAdded((Link) event.subject());
760 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre2c515312016-09-13 21:33:40 -0700761 Link linkRemoved = (Link) event.subject();
762 if (linkRemoved.src().elementId() instanceof DeviceId &&
763 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
764 continue;
765 }
766 if (linkRemoved.dst().elementId() instanceof DeviceId &&
767 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
768 continue;
769 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700770 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700771 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
772 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
773 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800774 DeviceId deviceId = ((Device) event.subject()).id();
775 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -0700776 log.info("Processing device event {} for available device {}",
777 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700778 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -0700779 } else {
780 log.info("Processing device event {} for unavailable device {}",
781 event.type(), ((Device) event.subject()).id());
782 processDeviceRemoved((Device) event.subject());
783 }
Saurav Das1a129a02016-11-18 15:21:57 -0800784 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800785 // typically these calls come when device is added first time
786 // so port filtering rules are handled at the device_added event.
787 // port added calls represent all ports on the device,
788 // enabled or not.
Saurav Das1a129a02016-11-18 15:21:57 -0800789 log.debug("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800790 ((DeviceEvent) event).subject().id(),
791 ((DeviceEvent) event).port().number(),
792 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -0800793 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800794 // these calls happen for every subsequent event
795 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -0800796 log.info("** PORT UPDATED {}/{} -> {}",
797 event.subject(),
798 ((DeviceEvent) event).port(),
799 event.type());
800 processPortUpdated(((Device) event.subject()),
801 ((DeviceEvent) event).port());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700802 } else {
803 log.warn("Unhandled event type: {}", event.type());
804 }
sanghob35a6192015-04-01 13:05:26 -0700805 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700806 } catch (Exception e) {
807 log.error("SegmentRouting event handler "
808 + "thread thrown an exception: {}", e);
sanghob35a6192015-04-01 13:05:26 -0700809 }
sanghob35a6192015-04-01 13:05:26 -0700810 }
811 }
812
sanghob35a6192015-04-01 13:05:26 -0700813 private void processLinkAdded(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700814 log.info("** LINK ADDED {}", link.toString());
Charles Chan0b4e6182015-11-03 10:42:14 -0800815 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
816 log.warn("Source device of this link is not configured.");
817 return;
818 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700819 //Irrespective whether the local is a MASTER or not for this device,
820 //create group handler instance and push default TTP flow rules.
821 //Because in a multi-instance setup, instances can initiate
822 //groups for any devices. Also the default TTP rules are needed
823 //to be pushed before inserting any IP table entries for any device
824 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
825 .deviceId());
826 if (groupHandler != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800827 groupHandler.linkUp(link, mastershipService.isLocalMaster(
828 link.src().deviceId()));
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700829 } else {
830 Device device = deviceService.getDevice(link.src().deviceId());
831 if (device != null) {
832 log.warn("processLinkAdded: Link Added "
833 + "Notification without Device Added "
834 + "event, still handling it");
835 processDeviceAdded(device);
836 groupHandler = groupHandlerMap.get(link.src()
837 .deviceId());
Saurav Das8a0732e2015-11-20 15:27:53 -0800838 groupHandler.linkUp(link, mastershipService.isLocalMaster(device.id()));
sanghob35a6192015-04-01 13:05:26 -0700839 }
840 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700841
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700842 log.trace("Starting optimized route population process");
843 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null);
844 //log.trace("processLinkAdded: re-starting route population process");
845 //defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -0700846
847 mcastHandler.init();
sanghob35a6192015-04-01 13:05:26 -0700848 }
849
850 private void processLinkRemoved(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700851 log.info("** LINK REMOVED {}", link.toString());
sangho834e4b02015-05-01 09:38:25 -0700852 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
853 if (groupHandler != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800854 groupHandler.portDown(link.src().port(),
855 mastershipService.isLocalMaster(link.src().deviceId()));
sangho834e4b02015-05-01 09:38:25 -0700856 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700857 log.trace("Starting optimized route population process");
858 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link);
859 //log.trace("processLinkRemoved: re-starting route population process");
860 //defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -0700861
862 mcastHandler.processLinkDown(link);
sanghob35a6192015-04-01 13:05:26 -0700863 }
864
865 private void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700866 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan0b4e6182015-11-03 10:42:14 -0800867 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das2857f382015-11-03 14:39:27 -0800868 log.warn("Device configuration uploading. Device {} will be "
869 + "processed after config completes.", device.id());
870 return;
871 }
Charles Chan2199c302016-04-23 17:36:10 -0700872 processDeviceAddedInternal(device.id());
873 }
874
875 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -0700876 // Irrespective of whether the local is a MASTER or not for this device,
877 // we need to create a SR-group-handler instance. This is because in a
878 // multi-instance setup, any instance can initiate forwarding/next-objectives
879 // for any switch (even if this instance is a SLAVE or not even connected
880 // to the switch). To handle this, a default-group-handler instance is necessary
881 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -0700882 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
883 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800884 DefaultGroupHandler groupHandler;
885 try {
886 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -0700887 createGroupHandler(deviceId,
888 appId,
889 deviceConfiguration,
890 linkService,
891 flowObjectiveService,
892 this);
Charles Chan0b4e6182015-11-03 10:42:14 -0800893 } catch (DeviceConfigNotFoundException e) {
894 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
895 return;
896 }
Charles Chan2199c302016-04-23 17:36:10 -0700897 log.debug("updating groupHandlerMap with new config for device: {}",
898 deviceId);
899 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -0800900 }
Saurav Dasb5c236e2016-06-07 10:08:06 -0700901 // Also, in some cases, drivers may need extra
902 // information to process rules (eg. Router IP/MAC); and so, we send
903 // port addressing rules to the driver as well irrespective of whether
904 // this instance is the master or not.
905 defaultRoutingHandler.populatePortAddressingRules(deviceId);
906
Charles Chan2199c302016-04-23 17:36:10 -0700907 if (mastershipService.isLocalMaster(deviceId)) {
Charles Chan03a73e02016-10-24 14:52:01 -0700908 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -0700909 xConnectHandler.init(deviceId);
Charles Chan35fd1a72016-06-13 18:54:31 -0700910 cordConfigHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -0700911 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700912 groupHandler.createGroupsFromSubnetConfig();
Charles Chan2199c302016-04-23 17:36:10 -0700913 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700914 }
Charles Chan5270ed02016-01-30 23:22:37 -0800915
Charles Chan03a73e02016-10-24 14:52:01 -0700916 appCfgHandler.init(deviceId);
917 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -0700918 }
919
Saurav Das80980c72016-03-23 11:22:49 -0700920 private void processDeviceRemoved(Device device) {
921 nsNextObjStore.entrySet().stream()
922 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
923 .forEach(entry -> {
924 nsNextObjStore.remove(entry.getKey());
925 });
Saurav Das80980c72016-03-23 11:22:49 -0700926 subnetNextObjStore.entrySet().stream()
927 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
928 .forEach(entry -> {
929 subnetNextObjStore.remove(entry.getKey());
930 });
Saurav Das80980c72016-03-23 11:22:49 -0700931 portNextObjStore.entrySet().stream()
932 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
933 .forEach(entry -> {
934 portNextObjStore.remove(entry.getKey());
935 });
Saurav Das80980c72016-03-23 11:22:49 -0700936 subnetVidStore.entrySet().stream()
937 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
938 .forEach(entry -> {
939 subnetVidStore.remove(entry.getKey());
940 });
Saurav Das80980c72016-03-23 11:22:49 -0700941 groupHandlerMap.remove(device.id());
Saurav Das80980c72016-03-23 11:22:49 -0700942 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan2199c302016-04-23 17:36:10 -0700943 mcastHandler.removeDevice(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -0700944 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -0700945 }
946
Saurav Das1a129a02016-11-18 15:21:57 -0800947 private void processPortUpdated(Device device, Port port) {
948 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
949 log.warn("Device configuration uploading. Not handling port event for"
950 + "dev: {} port: {}", device.id(), port.number());
951 return;
952 }
953 /* XXX create method for single port filtering rules which are needed
954 for both switch-to-switch ports and edge ports
955 if (defaultRoutingHandler != null) {
956 defaultRoutingHandler.populatePortAddressingRules(
957 ((Device) event.subject()).id());
958 }*/
959
960 // portUpdated calls are for ports that have gone down or up. For switch
961 // to switch ports, link-events should take care of any re-routing or
962 // group editing necessary for port up/down. Here we only process edge ports
963 // that are already configured.
Pier Ventre10bd8d12016-11-26 21:05:22 -0800964 Ip4Prefix configuredSubnet = deviceConfiguration.getPortIPv4Subnet(device.id(),
Saurav Das1a129a02016-11-18 15:21:57 -0800965 port.number());
966 if (configuredSubnet == null) {
967 log.debug("Not handling port updated event for unconfigured port "
968 + "dev/port: {}/{}", device.id(), port.number());
969 return;
970 }
971 processEdgePort(device, port, configuredSubnet);
972 }
973
974 private void processEdgePort(Device device, Port port, Ip4Prefix subnet) {
975 boolean portUp = port.isEnabled();
976 if (portUp) {
977 log.info("Device:EdgePort {}:{} is enabled in subnet: {}", device.id(),
978 port.number(), subnet);
979 } else {
980 log.info("Device:EdgePort {}:{} is disabled in subnet: {}", device.id(),
981 port.number(), subnet);
982 }
983
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700984 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -0700985 if (groupHandler != null) {
Saurav Das1a129a02016-11-18 15:21:57 -0800986 groupHandler.processEdgePort(port.number(), subnet, portUp,
Saurav Das423fe2b2015-12-04 10:52:59 -0800987 mastershipService.isLocalMaster(device.id()));
Saurav Das1a129a02016-11-18 15:21:57 -0800988 } else {
989 log.warn("Group handler not found for dev:{}. Not handling edge port"
990 + " {} event for port:{}", device.id(),
991 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -0700992 }
993 }
sangho1e575652015-05-14 00:39:53 -0700994
Pier Ventre10bd8d12016-11-26 21:05:22 -0800995 /**
996 * Registers the given connect point with the NRS, this is necessary
997 * to receive the NDP and ARP packets from the NRS.
998 *
999 * @param portToRegister connect point to register
1000 */
1001 public void registerConnectPoint(ConnectPoint portToRegister) {
Pier Ventre10bd8d12016-11-26 21:05:22 -08001002 this.neighbourResolutionService.registerNeighbourHandler(
1003 portToRegister,
1004 neighbourHandler,
1005 appId
1006 );
1007 }
1008
Charles Chand6832882015-10-05 17:50:33 -07001009 private class InternalConfigListener implements NetworkConfigListener {
Charles Chan2c15aca2016-11-09 20:51:44 -08001010 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001011
Charles Chane849c192016-01-11 18:28:54 -08001012 /**
1013 * Constructs the internal network config listener.
1014 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001015 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001016 */
Charles Chan2c15aca2016-11-09 20:51:44 -08001017 public InternalConfigListener(SegmentRoutingManager srManager) {
1018 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001019 }
1020
Charles Chane849c192016-01-11 18:28:54 -08001021 /**
1022 * Reads network config and initializes related data structure accordingly.
1023 */
Charles Chan4636be02015-10-07 14:21:45 -07001024 public void configureNetwork() {
Pier Ventre10bd8d12016-11-26 21:05:22 -08001025
Charles Chan2c15aca2016-11-09 20:51:44 -08001026 deviceConfiguration = new DeviceConfiguration(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001027
Charles Chan2c15aca2016-11-09 20:51:44 -08001028 arpHandler = new ArpHandler(srManager);
1029 icmpHandler = new IcmpHandler(srManager);
1030 ipHandler = new IpHandler(srManager);
1031 routingRulePopulator = new RoutingRulePopulator(srManager);
1032 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001033
1034 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1035 groupHandlerMap, tunnelStore);
1036 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1037 flowObjectiveService,
1038 tunnelHandler, policyStore);
1039
Charles Chan4636be02015-10-07 14:21:45 -07001040 for (Device device : deviceService.getDevices()) {
Charles Chan2199c302016-04-23 17:36:10 -07001041 processDeviceAddedInternal(device.id());
Charles Chan4636be02015-10-07 14:21:45 -07001042 }
1043
1044 defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -07001045 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001046 }
1047
Charles Chand6832882015-10-05 17:50:33 -07001048 @Override
1049 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001050 // TODO move this part to NetworkConfigEventHandler
1051 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1052 switch (event.type()) {
1053 case CONFIG_ADDED:
1054 log.info("Segment Routing Config added.");
1055 configureNetwork();
1056 break;
1057 case CONFIG_UPDATED:
1058 log.info("Segment Routing Config updated.");
1059 // TODO support dynamic configuration
1060 break;
1061 default:
1062 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001063 }
Charles Chan5270ed02016-01-30 23:22:37 -08001064 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001065 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001066 switch (event.type()) {
1067 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001068 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001069 break;
1070 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001071 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001072 break;
1073 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001074 appCfgHandler.processAppConfigRemoved(event);
1075 break;
1076 default:
1077 break;
1078 }
Charles Chan03a73e02016-10-24 14:52:01 -07001079 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001080 } else if (event.configClass().equals(XConnectConfig.class)) {
1081 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1082 switch (event.type()) {
1083 case CONFIG_ADDED:
1084 xConnectHandler.processXConnectConfigAdded(event);
1085 break;
1086 case CONFIG_UPDATED:
1087 xConnectHandler.processXConnectConfigUpdated(event);
1088 break;
1089 case CONFIG_REMOVED:
1090 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001091 break;
1092 default:
1093 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001094 }
Charles Chand6832882015-10-05 17:50:33 -07001095 }
1096 }
1097 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001098
1099 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001100 @Override
1101 public void event(HostEvent event) {
1102 // Do not proceed without mastership
1103 DeviceId deviceId = event.subject().location().deviceId();
1104 if (!mastershipService.isLocalMaster(deviceId)) {
1105 return;
1106 }
1107
1108 switch (event.type()) {
1109 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001110 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001111 break;
1112 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001113 hostHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001114 break;
1115 case HOST_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001116 hostHandler.processHostRemoveEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001117 break;
1118 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001119 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001120 break;
1121 default:
1122 log.warn("Unsupported host event type: {}", event.type());
1123 break;
1124 }
1125 }
1126 }
1127
Charles Chand55e84d2016-03-30 17:54:24 -07001128 private class InternalMcastListener implements McastListener {
1129 @Override
1130 public void event(McastEvent event) {
1131 switch (event.type()) {
1132 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001133 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001134 break;
1135 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001136 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001137 break;
1138 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001139 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001140 break;
1141 case ROUTE_ADDED:
1142 case ROUTE_REMOVED:
1143 default:
1144 break;
1145 }
1146 }
1147 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001148
1149 private class InternalCordConfigListener implements CordConfigListener {
1150 @Override
1151 public void event(CordConfigEvent event) {
1152 switch (event.type()) {
1153 case ACCESS_AGENT_ADDED:
1154 cordConfigHandler.processAccessAgentAddedEvent(event);
1155 break;
1156 case ACCESS_AGENT_UPDATED:
1157 cordConfigHandler.processAccessAgentUpdatedEvent(event);
1158 break;
1159 case ACCESS_AGENT_REMOVED:
1160 cordConfigHandler.processAccessAgentRemovedEvent(event);
1161 break;
1162 case ACCESS_DEVICE_ADDED:
1163 case ACCESS_DEVICE_UPDATED:
1164 case ACCESS_DEVICE_REMOVED:
1165 default:
1166 break;
1167 }
1168 }
1169 }
Charles Chan03a73e02016-10-24 14:52:01 -07001170
1171 private class InternalRouteEventListener implements RouteListener {
1172 @Override
1173 public void event(RouteEvent event) {
1174 switch (event.type()) {
1175 case ROUTE_ADDED:
1176 routeHandler.processRouteAdded(event);
1177 break;
1178 case ROUTE_UPDATED:
1179 routeHandler.processRouteUpdated(event);
1180 break;
1181 case ROUTE_REMOVED:
1182 routeHandler.processRouteRemoved(event);
1183 break;
1184 default:
1185 break;
1186 }
1187 }
1188 }
sanghob35a6192015-04-01 13:05:26 -07001189}