blob: 6694aea240298ab56e20ff07dcc16e86bf8b38c7 [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);
sanghob35a6192015-04-01 13:05:26 -0700659 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800660 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
661 //ipHandler.addToPacketBuffer(ipv4Packet);
662 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
663 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700664 } else {
Charles Chan50035632017-01-13 17:20:44 -0800665 // NOTE: We don't support IP learning at this moment so this
666 // is not necessary. Also it causes duplication of DHCP packets.
667 // ipHandler.processPacketIn(pkt);
sanghob35a6192015-04-01 13:05:26 -0700668 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800669 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
670 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -0800671 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Ventre10bd8d12016-11-26 21:05:22 -0800672 /*
Pier Ventre735b8c82016-12-02 08:16:05 -0800673 * We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre10bd8d12016-11-26 21:05:22 -0800674 */
Pier Ventre735b8c82016-12-02 08:16:05 -0800675 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
676 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
677 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
678 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
679 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
680 } else {
681 log.warn("Received ICMPv6 0x{} - not handled",
682 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
683 }
684 } else {
685 // NOTE: We don't support IP learning at this moment so this
686 // is not necessary. Also it causes duplication of DHCPv6 packets.
687 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
688 }
sanghob35a6192015-04-01 13:05:26 -0700689 }
690 }
691 }
692
693 private class InternalLinkListener implements LinkListener {
694 @Override
695 public void event(LinkEvent event) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700696 if (event.type() == LinkEvent.Type.LINK_ADDED
697 || event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700698 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700699 scheduleEventHandlerIfNotScheduled(event);
700 }
701 }
702 }
703
704 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -0700705 @Override
706 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -0700707 switch (event.type()) {
708 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -0800709 case PORT_UPDATED:
710 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -0700711 case DEVICE_UPDATED:
712 case DEVICE_AVAILABILITY_CHANGED:
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700713 log.debug("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700714 scheduleEventHandlerIfNotScheduled(event);
715 break;
716 default:
717 }
718 }
719 }
720
Saurav Das4ce45962015-11-24 23:21:05 -0800721 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -0700722 private void scheduleEventHandlerIfNotScheduled(Event event) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700723 synchronized (threadSchedulerLock) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700724 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700725 numOfEventsQueued++;
726
727 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
728 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700729 eventHandlerFuture = executorService
730 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
731 numOfHandlerScheduled++;
732 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700733 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700734 numOfEventsQueued,
735 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -0700736 }
sanghob35a6192015-04-01 13:05:26 -0700737 }
738
739 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700740 @Override
sanghob35a6192015-04-01 13:05:26 -0700741 public void run() {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700742 try {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700743 while (true) {
Saurav Das4ce45962015-11-24 23:21:05 -0800744 @SuppressWarnings("rawtypes")
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700745 Event event = null;
746 synchronized (threadSchedulerLock) {
747 if (!eventQueue.isEmpty()) {
748 event = eventQueue.poll();
749 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700750 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700751 numOfHandlerExecution++;
752 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
753 numOfHandlerExecution, numOfEventsExecuted);
754 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700755 }
sangho20eff1d2015-04-13 15:15:58 -0700756 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700757 if (event.type() == LinkEvent.Type.LINK_ADDED) {
758 processLinkAdded((Link) event.subject());
759 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre2c515312016-09-13 21:33:40 -0700760 Link linkRemoved = (Link) event.subject();
761 if (linkRemoved.src().elementId() instanceof DeviceId &&
762 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
763 continue;
764 }
765 if (linkRemoved.dst().elementId() instanceof DeviceId &&
766 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
767 continue;
768 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700769 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700770 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
771 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
772 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800773 DeviceId deviceId = ((Device) event.subject()).id();
774 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -0700775 log.info("Processing device event {} for available device {}",
776 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700777 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -0700778 } else {
779 log.info("Processing device event {} for unavailable device {}",
780 event.type(), ((Device) event.subject()).id());
781 processDeviceRemoved((Device) event.subject());
782 }
Saurav Das1a129a02016-11-18 15:21:57 -0800783 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800784 // typically these calls come when device is added first time
785 // so port filtering rules are handled at the device_added event.
786 // port added calls represent all ports on the device,
787 // enabled or not.
Saurav Das1a129a02016-11-18 15:21:57 -0800788 log.debug("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800789 ((DeviceEvent) event).subject().id(),
790 ((DeviceEvent) event).port().number(),
791 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -0800792 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800793 // these calls happen for every subsequent event
794 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -0800795 log.info("** PORT UPDATED {}/{} -> {}",
796 event.subject(),
797 ((DeviceEvent) event).port(),
798 event.type());
799 processPortUpdated(((Device) event.subject()),
800 ((DeviceEvent) event).port());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700801 } else {
802 log.warn("Unhandled event type: {}", event.type());
803 }
sanghob35a6192015-04-01 13:05:26 -0700804 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700805 } catch (Exception e) {
806 log.error("SegmentRouting event handler "
807 + "thread thrown an exception: {}", e);
sanghob35a6192015-04-01 13:05:26 -0700808 }
sanghob35a6192015-04-01 13:05:26 -0700809 }
810 }
811
sanghob35a6192015-04-01 13:05:26 -0700812 private void processLinkAdded(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700813 log.info("** LINK ADDED {}", link.toString());
Charles Chan0b4e6182015-11-03 10:42:14 -0800814 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
815 log.warn("Source device of this link is not configured.");
816 return;
817 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700818 //Irrespective whether the local is a MASTER or not for this device,
819 //create group handler instance and push default TTP flow rules.
820 //Because in a multi-instance setup, instances can initiate
821 //groups for any devices. Also the default TTP rules are needed
822 //to be pushed before inserting any IP table entries for any device
823 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
824 .deviceId());
825 if (groupHandler != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800826 groupHandler.linkUp(link, mastershipService.isLocalMaster(
827 link.src().deviceId()));
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700828 } else {
829 Device device = deviceService.getDevice(link.src().deviceId());
830 if (device != null) {
831 log.warn("processLinkAdded: Link Added "
832 + "Notification without Device Added "
833 + "event, still handling it");
834 processDeviceAdded(device);
835 groupHandler = groupHandlerMap.get(link.src()
836 .deviceId());
Saurav Das8a0732e2015-11-20 15:27:53 -0800837 groupHandler.linkUp(link, mastershipService.isLocalMaster(device.id()));
sanghob35a6192015-04-01 13:05:26 -0700838 }
839 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700840
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700841 log.trace("Starting optimized route population process");
842 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null);
843 //log.trace("processLinkAdded: re-starting route population process");
844 //defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -0700845
846 mcastHandler.init();
sanghob35a6192015-04-01 13:05:26 -0700847 }
848
849 private void processLinkRemoved(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700850 log.info("** LINK REMOVED {}", link.toString());
sangho834e4b02015-05-01 09:38:25 -0700851 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
852 if (groupHandler != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800853 groupHandler.portDown(link.src().port(),
854 mastershipService.isLocalMaster(link.src().deviceId()));
sangho834e4b02015-05-01 09:38:25 -0700855 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700856 log.trace("Starting optimized route population process");
857 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link);
858 //log.trace("processLinkRemoved: re-starting route population process");
859 //defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -0700860
861 mcastHandler.processLinkDown(link);
sanghob35a6192015-04-01 13:05:26 -0700862 }
863
864 private void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700865 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan0b4e6182015-11-03 10:42:14 -0800866 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das2857f382015-11-03 14:39:27 -0800867 log.warn("Device configuration uploading. Device {} will be "
868 + "processed after config completes.", device.id());
869 return;
870 }
Charles Chan2199c302016-04-23 17:36:10 -0700871 processDeviceAddedInternal(device.id());
872 }
873
874 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -0700875 // Irrespective of whether the local is a MASTER or not for this device,
876 // we need to create a SR-group-handler instance. This is because in a
877 // multi-instance setup, any instance can initiate forwarding/next-objectives
878 // for any switch (even if this instance is a SLAVE or not even connected
879 // to the switch). To handle this, a default-group-handler instance is necessary
880 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -0700881 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
882 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800883 DefaultGroupHandler groupHandler;
884 try {
885 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -0700886 createGroupHandler(deviceId,
887 appId,
888 deviceConfiguration,
889 linkService,
890 flowObjectiveService,
891 this);
Charles Chan0b4e6182015-11-03 10:42:14 -0800892 } catch (DeviceConfigNotFoundException e) {
893 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
894 return;
895 }
Charles Chan2199c302016-04-23 17:36:10 -0700896 log.debug("updating groupHandlerMap with new config for device: {}",
897 deviceId);
898 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -0800899 }
Saurav Dasb5c236e2016-06-07 10:08:06 -0700900 // Also, in some cases, drivers may need extra
901 // information to process rules (eg. Router IP/MAC); and so, we send
902 // port addressing rules to the driver as well irrespective of whether
903 // this instance is the master or not.
904 defaultRoutingHandler.populatePortAddressingRules(deviceId);
905
Charles Chan2199c302016-04-23 17:36:10 -0700906 if (mastershipService.isLocalMaster(deviceId)) {
Charles Chan03a73e02016-10-24 14:52:01 -0700907 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -0700908 xConnectHandler.init(deviceId);
Charles Chan35fd1a72016-06-13 18:54:31 -0700909 cordConfigHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -0700910 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700911 groupHandler.createGroupsFromSubnetConfig();
Charles Chan2199c302016-04-23 17:36:10 -0700912 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700913 }
Charles Chan5270ed02016-01-30 23:22:37 -0800914
Charles Chan03a73e02016-10-24 14:52:01 -0700915 appCfgHandler.init(deviceId);
916 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -0700917 }
918
Saurav Das80980c72016-03-23 11:22:49 -0700919 private void processDeviceRemoved(Device device) {
920 nsNextObjStore.entrySet().stream()
921 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
922 .forEach(entry -> {
923 nsNextObjStore.remove(entry.getKey());
924 });
Saurav Das80980c72016-03-23 11:22:49 -0700925 subnetNextObjStore.entrySet().stream()
926 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
927 .forEach(entry -> {
928 subnetNextObjStore.remove(entry.getKey());
929 });
Saurav Das80980c72016-03-23 11:22:49 -0700930 portNextObjStore.entrySet().stream()
931 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
932 .forEach(entry -> {
933 portNextObjStore.remove(entry.getKey());
934 });
Saurav Das80980c72016-03-23 11:22:49 -0700935 subnetVidStore.entrySet().stream()
936 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
937 .forEach(entry -> {
938 subnetVidStore.remove(entry.getKey());
939 });
Saurav Das80980c72016-03-23 11:22:49 -0700940 groupHandlerMap.remove(device.id());
Saurav Das80980c72016-03-23 11:22:49 -0700941 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan2199c302016-04-23 17:36:10 -0700942 mcastHandler.removeDevice(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -0700943 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -0700944 }
945
Saurav Das1a129a02016-11-18 15:21:57 -0800946 private void processPortUpdated(Device device, Port port) {
947 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
948 log.warn("Device configuration uploading. Not handling port event for"
949 + "dev: {} port: {}", device.id(), port.number());
950 return;
951 }
952 /* XXX create method for single port filtering rules which are needed
953 for both switch-to-switch ports and edge ports
954 if (defaultRoutingHandler != null) {
955 defaultRoutingHandler.populatePortAddressingRules(
956 ((Device) event.subject()).id());
957 }*/
958
959 // portUpdated calls are for ports that have gone down or up. For switch
960 // to switch ports, link-events should take care of any re-routing or
961 // group editing necessary for port up/down. Here we only process edge ports
962 // that are already configured.
Pier Ventre10bd8d12016-11-26 21:05:22 -0800963 Ip4Prefix configuredSubnet = deviceConfiguration.getPortIPv4Subnet(device.id(),
Saurav Das1a129a02016-11-18 15:21:57 -0800964 port.number());
965 if (configuredSubnet == null) {
966 log.debug("Not handling port updated event for unconfigured port "
967 + "dev/port: {}/{}", device.id(), port.number());
968 return;
969 }
970 processEdgePort(device, port, configuredSubnet);
971 }
972
973 private void processEdgePort(Device device, Port port, Ip4Prefix subnet) {
974 boolean portUp = port.isEnabled();
975 if (portUp) {
976 log.info("Device:EdgePort {}:{} is enabled in subnet: {}", device.id(),
977 port.number(), subnet);
978 } else {
979 log.info("Device:EdgePort {}:{} is disabled in subnet: {}", device.id(),
980 port.number(), subnet);
981 }
982
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700983 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -0700984 if (groupHandler != null) {
Saurav Das1a129a02016-11-18 15:21:57 -0800985 groupHandler.processEdgePort(port.number(), subnet, portUp,
Saurav Das423fe2b2015-12-04 10:52:59 -0800986 mastershipService.isLocalMaster(device.id()));
Saurav Das1a129a02016-11-18 15:21:57 -0800987 } else {
988 log.warn("Group handler not found for dev:{}. Not handling edge port"
989 + " {} event for port:{}", device.id(),
990 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -0700991 }
992 }
sangho1e575652015-05-14 00:39:53 -0700993
Pier Ventre10bd8d12016-11-26 21:05:22 -0800994 /**
995 * Registers the given connect point with the NRS, this is necessary
996 * to receive the NDP and ARP packets from the NRS.
997 *
998 * @param portToRegister connect point to register
999 */
1000 public void registerConnectPoint(ConnectPoint portToRegister) {
1001 /*
1002 * First we register the ARP handler.
1003 */
1004 this.neighbourResolutionService.registerNeighbourHandler(
1005 portToRegister,
1006 neighbourHandler,
1007 appId
1008 );
1009 }
1010
Charles Chand6832882015-10-05 17:50:33 -07001011 private class InternalConfigListener implements NetworkConfigListener {
Charles Chan2c15aca2016-11-09 20:51:44 -08001012 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001013
Charles Chane849c192016-01-11 18:28:54 -08001014 /**
1015 * Constructs the internal network config listener.
1016 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001017 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001018 */
Charles Chan2c15aca2016-11-09 20:51:44 -08001019 public InternalConfigListener(SegmentRoutingManager srManager) {
1020 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001021 }
1022
Charles Chane849c192016-01-11 18:28:54 -08001023 /**
1024 * Reads network config and initializes related data structure accordingly.
1025 */
Charles Chan4636be02015-10-07 14:21:45 -07001026 public void configureNetwork() {
Pier Ventre10bd8d12016-11-26 21:05:22 -08001027
Charles Chan2c15aca2016-11-09 20:51:44 -08001028 deviceConfiguration = new DeviceConfiguration(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001029
Charles Chan2c15aca2016-11-09 20:51:44 -08001030 arpHandler = new ArpHandler(srManager);
1031 icmpHandler = new IcmpHandler(srManager);
1032 ipHandler = new IpHandler(srManager);
1033 routingRulePopulator = new RoutingRulePopulator(srManager);
1034 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001035
1036 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1037 groupHandlerMap, tunnelStore);
1038 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1039 flowObjectiveService,
1040 tunnelHandler, policyStore);
1041
Charles Chan4636be02015-10-07 14:21:45 -07001042 for (Device device : deviceService.getDevices()) {
Charles Chan2199c302016-04-23 17:36:10 -07001043 processDeviceAddedInternal(device.id());
Charles Chan4636be02015-10-07 14:21:45 -07001044 }
1045
1046 defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -07001047 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001048 }
1049
Charles Chand6832882015-10-05 17:50:33 -07001050 @Override
1051 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001052 // TODO move this part to NetworkConfigEventHandler
1053 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1054 switch (event.type()) {
1055 case CONFIG_ADDED:
1056 log.info("Segment Routing Config added.");
1057 configureNetwork();
1058 break;
1059 case CONFIG_UPDATED:
1060 log.info("Segment Routing Config updated.");
1061 // TODO support dynamic configuration
1062 break;
1063 default:
1064 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001065 }
Charles Chan5270ed02016-01-30 23:22:37 -08001066 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001067 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001068 switch (event.type()) {
1069 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001070 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001071 break;
1072 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001073 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001074 break;
1075 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001076 appCfgHandler.processAppConfigRemoved(event);
1077 break;
1078 default:
1079 break;
1080 }
Charles Chan03a73e02016-10-24 14:52:01 -07001081 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001082 } else if (event.configClass().equals(XConnectConfig.class)) {
1083 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1084 switch (event.type()) {
1085 case CONFIG_ADDED:
1086 xConnectHandler.processXConnectConfigAdded(event);
1087 break;
1088 case CONFIG_UPDATED:
1089 xConnectHandler.processXConnectConfigUpdated(event);
1090 break;
1091 case CONFIG_REMOVED:
1092 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001093 break;
1094 default:
1095 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001096 }
Charles Chand6832882015-10-05 17:50:33 -07001097 }
1098 }
1099 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001100
1101 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001102 @Override
1103 public void event(HostEvent event) {
1104 // Do not proceed without mastership
1105 DeviceId deviceId = event.subject().location().deviceId();
1106 if (!mastershipService.isLocalMaster(deviceId)) {
1107 return;
1108 }
1109
1110 switch (event.type()) {
1111 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001112 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001113 break;
1114 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001115 hostHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001116 break;
1117 case HOST_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001118 hostHandler.processHostRemoveEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001119 break;
1120 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001121 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001122 break;
1123 default:
1124 log.warn("Unsupported host event type: {}", event.type());
1125 break;
1126 }
1127 }
1128 }
1129
Charles Chand55e84d2016-03-30 17:54:24 -07001130 private class InternalMcastListener implements McastListener {
1131 @Override
1132 public void event(McastEvent event) {
1133 switch (event.type()) {
1134 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001135 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001136 break;
1137 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001138 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001139 break;
1140 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001141 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001142 break;
1143 case ROUTE_ADDED:
1144 case ROUTE_REMOVED:
1145 default:
1146 break;
1147 }
1148 }
1149 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001150
1151 private class InternalCordConfigListener implements CordConfigListener {
1152 @Override
1153 public void event(CordConfigEvent event) {
1154 switch (event.type()) {
1155 case ACCESS_AGENT_ADDED:
1156 cordConfigHandler.processAccessAgentAddedEvent(event);
1157 break;
1158 case ACCESS_AGENT_UPDATED:
1159 cordConfigHandler.processAccessAgentUpdatedEvent(event);
1160 break;
1161 case ACCESS_AGENT_REMOVED:
1162 cordConfigHandler.processAccessAgentRemovedEvent(event);
1163 break;
1164 case ACCESS_DEVICE_ADDED:
1165 case ACCESS_DEVICE_UPDATED:
1166 case ACCESS_DEVICE_REMOVED:
1167 default:
1168 break;
1169 }
1170 }
1171 }
Charles Chan03a73e02016-10-24 14:52:01 -07001172
1173 private class InternalRouteEventListener implements RouteListener {
1174 @Override
1175 public void event(RouteEvent event) {
1176 switch (event.type()) {
1177 case ROUTE_ADDED:
1178 routeHandler.processRouteAdded(event);
1179 break;
1180 case ROUTE_UPDATED:
1181 routeHandler.processRouteUpdated(event);
1182 break;
1183 case ROUTE_REMOVED:
1184 routeHandler.processRouteRemoved(event);
1185 break;
1186 default:
1187 break;
1188 }
1189 }
1190 }
sanghob35a6192015-04-01 13:05:26 -07001191}