blob: c356f5eda174bdf4ded13a0245783917d5c42e4f [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;
Pier Luigi7dad71c2017-02-01 13:50:04 -080037import org.onosproject.incubator.net.config.basics.InterfaceConfig;
Charles Chand55e84d2016-03-30 17:54:24 -070038import org.onosproject.incubator.net.config.basics.McastConfig;
Charles Chan2c15aca2016-11-09 20:51:44 -080039import org.onosproject.incubator.net.intf.InterfaceService;
Charles Chan03a73e02016-10-24 14:52:01 -070040import org.onosproject.incubator.net.routing.RouteEvent;
41import org.onosproject.incubator.net.routing.RouteListener;
42import org.onosproject.incubator.net.routing.RouteService;
Pier Ventre735b8c82016-12-02 08:16:05 -080043import org.onosproject.incubator.net.neighbour.NeighbourResolutionService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070044import org.onosproject.mastership.MastershipService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080045import org.onosproject.net.ConnectPoint;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070046import org.onosproject.net.Device;
47import org.onosproject.net.DeviceId;
48import org.onosproject.net.Link;
49import org.onosproject.net.Port;
Charles Chan68aa62d2015-11-09 16:37:23 -080050import org.onosproject.net.PortNumber;
Charles Chand6832882015-10-05 17:50:33 -070051import org.onosproject.net.config.ConfigFactory;
52import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070053import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070054import org.onosproject.net.config.NetworkConfigRegistry;
Charles Chand6832882015-10-05 17:50:33 -070055import org.onosproject.net.config.basics.SubjectFactories;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070056import org.onosproject.net.device.DeviceEvent;
57import org.onosproject.net.device.DeviceListener;
58import org.onosproject.net.device.DeviceService;
Charles Chan68aa62d2015-11-09 16:37:23 -080059import org.onosproject.net.flow.TrafficSelector;
60import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070061import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan68aa62d2015-11-09 16:37:23 -080062import org.onosproject.net.host.HostEvent;
63import org.onosproject.net.host.HostListener;
Pier Ventre10bd8d12016-11-26 21:05:22 -080064import org.onosproject.net.host.HostService;
65import org.onosproject.net.link.LinkEvent;
66import org.onosproject.net.link.LinkListener;
67import org.onosproject.net.link.LinkService;
Charles Chand55e84d2016-03-30 17:54:24 -070068import org.onosproject.net.mcast.McastEvent;
69import org.onosproject.net.mcast.McastListener;
70import org.onosproject.net.mcast.MulticastRouteService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080071import org.onosproject.net.packet.InboundPacket;
72import org.onosproject.net.packet.PacketContext;
73import org.onosproject.net.packet.PacketProcessor;
74import org.onosproject.net.packet.PacketService;
Charles Chand55e84d2016-03-30 17:54:24 -070075import org.onosproject.net.topology.TopologyService;
Pier Luigi7dad71c2017-02-01 13:50:04 -080076import org.onosproject.routing.config.RouterConfig;
Charles Chand55e84d2016-03-30 17:54:24 -070077import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
78import org.onosproject.segmentrouting.config.DeviceConfiguration;
Charles Chand55e84d2016-03-30 17:54:24 -070079import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Pier Ventre10bd8d12016-11-26 21:05:22 -080080import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -070081import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -070082import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
83import org.onosproject.segmentrouting.grouphandler.NeighborSet;
Charles Chand2990362016-04-18 13:44:03 -070084import org.onosproject.segmentrouting.storekey.NeighborSetNextObjectiveStoreKey;
85import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chand2990362016-04-18 13:44:03 -070086import org.onosproject.segmentrouting.storekey.SubnetAssignedVidStoreKey;
87import org.onosproject.segmentrouting.storekey.SubnetNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -070088import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070089import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070090import org.onosproject.store.service.EventuallyConsistentMap;
91import org.onosproject.store.service.EventuallyConsistentMapBuilder;
92import org.onosproject.store.service.StorageService;
93import org.onosproject.store.service.WallClockTimestamp;
Charles Chan35fd1a72016-06-13 18:54:31 -070094import org.opencord.cordconfig.CordConfigEvent;
95import org.opencord.cordconfig.CordConfigListener;
96import org.opencord.cordconfig.CordConfigService;
sanghob35a6192015-04-01 13:05:26 -070097import org.slf4j.Logger;
98import org.slf4j.LoggerFactory;
99
Saurav Das0e99e2b2015-10-28 12:39:42 -0700100import java.util.Collections;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700101import java.util.HashSet;
sangho1e575652015-05-14 00:39:53 -0700102import java.util.List;
sanghob35a6192015-04-01 13:05:26 -0700103import java.util.Map;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700104import java.util.Set;
sanghob35a6192015-04-01 13:05:26 -0700105import java.util.concurrent.ConcurrentHashMap;
106import java.util.concurrent.ConcurrentLinkedQueue;
107import java.util.concurrent.Executors;
108import java.util.concurrent.ScheduledExecutorService;
109import java.util.concurrent.ScheduledFuture;
110import java.util.concurrent.TimeUnit;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800111import java.util.stream.Collectors;
sanghob35a6192015-04-01 13:05:26 -0700112
Charles Chan3e783d02016-02-26 22:19:52 -0800113import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800114import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700115import static org.onlab.util.Tools.groupedThreads;
Charles Chan3e783d02016-02-26 22:19:52 -0800116
Charles Chane849c192016-01-11 18:28:54 -0800117/**
118 * Segment routing manager.
119 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700120@Service
121@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700122public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700123
Charles Chan2c15aca2016-11-09 20:51:44 -0800124 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
sanghob35a6192015-04-01 13:05:26 -0700125
126 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700127 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700128
129 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre10bd8d12016-11-26 21:05:22 -0800130 private NeighbourResolutionService neighbourResolutionService;
131
132 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700133 CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700134
135 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700136 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700137
138 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700139 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700140
141 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700142 DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700143
144 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700145 FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700146
147 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700148 LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700149
Charles Chan5270ed02016-01-30 23:22:37 -0800150 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700151 MastershipService mastershipService;
152
153 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
154 StorageService storageService;
155
156 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
157 MulticastRouteService multicastRouteService;
158
159 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
160 TopologyService topologyService;
161
162 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
163 CordConfigService cordConfigService;
164
165 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
166 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800167
168 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800169 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800170
Saurav Das80980c72016-03-23 11:22:49 -0700171 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800172 public InterfaceService interfaceService;
173
Charles Chan03a73e02016-10-24 14:52:01 -0700174 ArpHandler arpHandler = null;
175 IcmpHandler icmpHandler = null;
176 IpHandler ipHandler = null;
177 RoutingRulePopulator routingRulePopulator = null;
Charles Chan2c15aca2016-11-09 20:51:44 -0800178 public ApplicationId appId;
sangho666cd6d2015-04-14 16:27:13 -0700179 protected DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700180
Charles Chan03a73e02016-10-24 14:52:01 -0700181 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700182 private TunnelHandler tunnelHandler = null;
183 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700184 private InternalPacketProcessor processor = null;
185 private InternalLinkListener linkListener = null;
186 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700187 private AppConfigHandler appCfgHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700188 XConnectHandler xConnectHandler = null;
Charles Chand2990362016-04-18 13:44:03 -0700189 private McastHandler mcastHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700190 HostHandler hostHandler = null;
Charles Chan35fd1a72016-06-13 18:54:31 -0700191 private CordConfigHandler cordConfigHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800192 private RouteHandler routeHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800193 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
sanghob35a6192015-04-01 13:05:26 -0700194 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan5270ed02016-01-30 23:22:37 -0800195 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700196 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
197 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan35fd1a72016-06-13 18:54:31 -0700198 private final InternalCordConfigListener cordConfigListener = new InternalCordConfigListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700199 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sanghob35a6192015-04-01 13:05:26 -0700200
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700201 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700202 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sanghob35a6192015-04-01 13:05:26 -0700203
Saurav Das4ce45962015-11-24 23:21:05 -0800204 @SuppressWarnings("unused")
sanghob35a6192015-04-01 13:05:26 -0700205 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800206 @SuppressWarnings("rawtypes")
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700207 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Charles Chan68aa62d2015-11-09 16:37:23 -0800208 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chane849c192016-01-11 18:28:54 -0800209 new ConcurrentHashMap<>();
210 /**
211 * Per device next objective ID store with (device id + neighbor set) as key.
212 */
213 public EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, Integer>
Charles Chan68aa62d2015-11-09 16:37:23 -0800214 nsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800215 /**
216 * Per device next objective ID store with (device id + subnet) as key.
217 */
218 public EventuallyConsistentMap<SubnetNextObjectiveStoreKey, Integer>
Charles Chan68aa62d2015-11-09 16:37:23 -0800219 subnetNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800220 /**
221 * Per device next objective ID store with (device id + port) as key.
222 */
223 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800224 portNextObjStore = null;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700225 // Per device, per-subnet assigned-vlans store, with (device id + subnet
226 // IPv4 prefix) as key
227 private EventuallyConsistentMap<SubnetAssignedVidStoreKey, VlanId>
Charles Chane849c192016-01-11 18:28:54 -0800228 subnetVidStore = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800229 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
230 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700231
Charles Chand55e84d2016-03-30 17:54:24 -0700232 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700233 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
234 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700235 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700236 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800237 public SegmentRoutingDeviceConfig createConfig() {
238 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700239 }
240 };
Charles Chand55e84d2016-03-30 17:54:24 -0700241 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700242 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
243 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700244 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800245 @Override
246 public SegmentRoutingAppConfig createConfig() {
247 return new SegmentRoutingAppConfig();
248 }
249 };
Charles Chanfc5c7802016-05-17 13:13:55 -0700250 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
251 new ConfigFactory<ApplicationId, XConnectConfig>(
252 SubjectFactories.APP_SUBJECT_FACTORY,
253 XConnectConfig.class, "xconnect") {
254 @Override
255 public XConnectConfig createConfig() {
256 return new XConnectConfig();
257 }
258 };
Charles Chand55e84d2016-03-30 17:54:24 -0700259 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700260 new ConfigFactory<ApplicationId, McastConfig>(
261 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700262 McastConfig.class, "multicast") {
263 @Override
264 public McastConfig createConfig() {
265 return new McastConfig();
266 }
267 };
268
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700269 private Object threadSchedulerLock = new Object();
270 private static int numOfEventsQueued = 0;
271 private static int numOfEventsExecuted = 0;
sanghob35a6192015-04-01 13:05:26 -0700272 private static int numOfHandlerExecution = 0;
273 private static int numOfHandlerScheduled = 0;
274
Charles Chan116188d2016-02-18 14:22:42 -0800275 /**
276 * Segment Routing App ID.
277 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800278 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chane849c192016-01-11 18:28:54 -0800279 /**
280 * The starting value of per-subnet VLAN ID assignment.
281 */
Saurav Das0e99e2b2015-10-28 12:39:42 -0700282 private static final short ASSIGNED_VLAN_START = 4093;
Charles Chane849c192016-01-11 18:28:54 -0800283 /**
284 * The default VLAN ID assigned to the interfaces without subnet config.
285 */
Saurav Das0e99e2b2015-10-28 12:39:42 -0700286 public static final short ASSIGNED_VLAN_NO_SUBNET = 4094;
287
sanghob35a6192015-04-01 13:05:26 -0700288 @Activate
289 protected void activate() {
Charles Chan2c15aca2016-11-09 20:51:44 -0800290 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700291
292 log.debug("Creating EC map nsnextobjectivestore");
293 EventuallyConsistentMapBuilder<NeighborSetNextObjectiveStoreKey, Integer>
294 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700295 nsNextObjStore = nsNextObjMapBuilder
296 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700297 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700298 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700299 .build();
300 log.trace("Current size {}", nsNextObjStore.size());
301
Charles Chanc42e84e2015-10-20 16:24:19 -0700302 log.debug("Creating EC map subnetnextobjectivestore");
303 EventuallyConsistentMapBuilder<SubnetNextObjectiveStoreKey, Integer>
304 subnetNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Charles Chanc42e84e2015-10-20 16:24:19 -0700305 subnetNextObjStore = subnetNextObjMapBuilder
306 .withName("subnetnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700307 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700308 .withTimestampProvider((k, v) -> new WallClockTimestamp())
309 .build();
310
Saurav Das4ce45962015-11-24 23:21:05 -0800311 log.debug("Creating EC map subnetnextobjectivestore");
312 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
313 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
314 portNextObjStore = portNextObjMapBuilder
315 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700316 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800317 .withTimestampProvider((k, v) -> new WallClockTimestamp())
318 .build();
319
sangho0b2b6d12015-05-20 22:16:38 -0700320 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
321 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700322 tunnelStore = tunnelMapBuilder
323 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700324 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700325 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700326 .build();
327
328 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
329 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700330 policyStore = policyMapBuilder
331 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700332 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700333 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700334 .build();
335
Saurav Das0e99e2b2015-10-28 12:39:42 -0700336 EventuallyConsistentMapBuilder<SubnetAssignedVidStoreKey, VlanId>
337 subnetVidStoreMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das0e99e2b2015-10-28 12:39:42 -0700338 subnetVidStore = subnetVidStoreMapBuilder
339 .withName("subnetvidstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700340 .withSerializer(createSerializer())
Saurav Das0e99e2b2015-10-28 12:39:42 -0700341 .withTimestampProvider((k, v) -> new WallClockTimestamp())
342 .build();
343
Saurav Das80980c72016-03-23 11:22:49 -0700344 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800345 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700346 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800347 "purgeOnDisconnection", "true");
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();
Pier Luigi7dad71c2017-02-01 13:50:04 -0800654
655 if (ethernet == null) {
656 return;
657 }
658
Saurav Das4ce45962015-11-24 23:21:05 -0800659 log.trace("Rcvd pktin: {}", ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800660 if (ethernet.getEtherType() == TYPE_ARP) {
Pier Ventre10bd8d12016-11-26 21:05:22 -0800661 log.warn("{} - we are still receiving ARP packets from {}",
662 context.inPacket().receivedFrom());
663 log.debug("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -0800664 return;
sanghob35a6192015-04-01 13:05:26 -0700665 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800666 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
667 //ipHandler.addToPacketBuffer(ipv4Packet);
668 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
669 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700670 } else {
Charles Chan50035632017-01-13 17:20:44 -0800671 // NOTE: We don't support IP learning at this moment so this
672 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -0800673 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700674 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800675 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
676 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -0800677 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -0800678 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -0800679 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
680 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
681 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
682 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
683 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
684 } else {
Pier Luigi7dad71c2017-02-01 13:50:04 -0800685 // XXX Neigbour hacking, to handle the ICMPv6 packet
686 // not under our control
687 if (icmpHandler.handleUPstreamPackets(context)) {
688 log.debug("Rcvd pktin from UpStream: {}", ipv6Packet);
689 } else {
690 log.debug("Received ICMPv6 0x{} - not handled",
691 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
692 }
Pier Ventre735b8c82016-12-02 08:16:05 -0800693 }
694 } else {
695 // NOTE: We don't support IP learning at this moment so this
696 // is not necessary. Also it causes duplication of DHCPv6 packets.
697 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
698 }
sanghob35a6192015-04-01 13:05:26 -0700699 }
700 }
701 }
702
703 private class InternalLinkListener implements LinkListener {
704 @Override
705 public void event(LinkEvent event) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700706 if (event.type() == LinkEvent.Type.LINK_ADDED
707 || event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700708 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700709 scheduleEventHandlerIfNotScheduled(event);
710 }
711 }
712 }
713
714 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -0700715 @Override
716 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -0700717 switch (event.type()) {
718 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -0800719 case PORT_UPDATED:
720 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -0700721 case DEVICE_UPDATED:
722 case DEVICE_AVAILABILITY_CHANGED:
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700723 log.debug("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700724 scheduleEventHandlerIfNotScheduled(event);
725 break;
726 default:
727 }
728 }
729 }
730
Saurav Das4ce45962015-11-24 23:21:05 -0800731 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -0700732 private void scheduleEventHandlerIfNotScheduled(Event event) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700733 synchronized (threadSchedulerLock) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700734 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700735 numOfEventsQueued++;
736
737 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
738 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700739 eventHandlerFuture = executorService
740 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
741 numOfHandlerScheduled++;
742 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700743 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700744 numOfEventsQueued,
745 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -0700746 }
sanghob35a6192015-04-01 13:05:26 -0700747 }
748
749 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700750 @Override
sanghob35a6192015-04-01 13:05:26 -0700751 public void run() {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700752 try {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700753 while (true) {
Saurav Das4ce45962015-11-24 23:21:05 -0800754 @SuppressWarnings("rawtypes")
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700755 Event event = null;
756 synchronized (threadSchedulerLock) {
757 if (!eventQueue.isEmpty()) {
758 event = eventQueue.poll();
759 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700760 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700761 numOfHandlerExecution++;
762 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
763 numOfHandlerExecution, numOfEventsExecuted);
764 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700765 }
sangho20eff1d2015-04-13 15:15:58 -0700766 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700767 if (event.type() == LinkEvent.Type.LINK_ADDED) {
768 processLinkAdded((Link) event.subject());
769 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre2c515312016-09-13 21:33:40 -0700770 Link linkRemoved = (Link) event.subject();
771 if (linkRemoved.src().elementId() instanceof DeviceId &&
772 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
773 continue;
774 }
775 if (linkRemoved.dst().elementId() instanceof DeviceId &&
776 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
777 continue;
778 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700779 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700780 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
781 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
782 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800783 DeviceId deviceId = ((Device) event.subject()).id();
784 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -0700785 log.info("Processing device event {} for available device {}",
786 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700787 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -0700788 } else {
789 log.info("Processing device event {} for unavailable device {}",
790 event.type(), ((Device) event.subject()).id());
791 processDeviceRemoved((Device) event.subject());
792 }
Saurav Das1a129a02016-11-18 15:21:57 -0800793 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800794 // typically these calls come when device is added first time
795 // so port filtering rules are handled at the device_added event.
796 // port added calls represent all ports on the device,
797 // enabled or not.
Saurav Das1a129a02016-11-18 15:21:57 -0800798 log.debug("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800799 ((DeviceEvent) event).subject().id(),
800 ((DeviceEvent) event).port().number(),
801 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -0800802 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800803 // these calls happen for every subsequent event
804 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -0800805 log.info("** PORT UPDATED {}/{} -> {}",
806 event.subject(),
807 ((DeviceEvent) event).port(),
808 event.type());
809 processPortUpdated(((Device) event.subject()),
810 ((DeviceEvent) event).port());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700811 } else {
812 log.warn("Unhandled event type: {}", event.type());
813 }
sanghob35a6192015-04-01 13:05:26 -0700814 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700815 } catch (Exception e) {
816 log.error("SegmentRouting event handler "
817 + "thread thrown an exception: {}", e);
sanghob35a6192015-04-01 13:05:26 -0700818 }
sanghob35a6192015-04-01 13:05:26 -0700819 }
820 }
821
sanghob35a6192015-04-01 13:05:26 -0700822 private void processLinkAdded(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700823 log.info("** LINK ADDED {}", link.toString());
Charles Chan0b4e6182015-11-03 10:42:14 -0800824 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
825 log.warn("Source device of this link is not configured.");
826 return;
827 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700828 //Irrespective whether the local is a MASTER or not for this device,
829 //create group handler instance and push default TTP flow rules.
830 //Because in a multi-instance setup, instances can initiate
831 //groups for any devices. Also the default TTP rules are needed
832 //to be pushed before inserting any IP table entries for any device
833 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
834 .deviceId());
835 if (groupHandler != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800836 groupHandler.linkUp(link, mastershipService.isLocalMaster(
837 link.src().deviceId()));
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700838 } else {
839 Device device = deviceService.getDevice(link.src().deviceId());
840 if (device != null) {
841 log.warn("processLinkAdded: Link Added "
842 + "Notification without Device Added "
843 + "event, still handling it");
844 processDeviceAdded(device);
845 groupHandler = groupHandlerMap.get(link.src()
846 .deviceId());
Saurav Das8a0732e2015-11-20 15:27:53 -0800847 groupHandler.linkUp(link, mastershipService.isLocalMaster(device.id()));
sanghob35a6192015-04-01 13:05:26 -0700848 }
849 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700850
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700851 log.trace("Starting optimized route population process");
852 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null);
853 //log.trace("processLinkAdded: re-starting route population process");
854 //defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -0700855
856 mcastHandler.init();
sanghob35a6192015-04-01 13:05:26 -0700857 }
858
859 private void processLinkRemoved(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700860 log.info("** LINK REMOVED {}", link.toString());
sangho834e4b02015-05-01 09:38:25 -0700861 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
862 if (groupHandler != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800863 groupHandler.portDown(link.src().port(),
864 mastershipService.isLocalMaster(link.src().deviceId()));
sangho834e4b02015-05-01 09:38:25 -0700865 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700866 log.trace("Starting optimized route population process");
867 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link);
868 //log.trace("processLinkRemoved: re-starting route population process");
869 //defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -0700870
871 mcastHandler.processLinkDown(link);
sanghob35a6192015-04-01 13:05:26 -0700872 }
873
874 private void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700875 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan0b4e6182015-11-03 10:42:14 -0800876 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das2857f382015-11-03 14:39:27 -0800877 log.warn("Device configuration uploading. Device {} will be "
878 + "processed after config completes.", device.id());
879 return;
880 }
Charles Chan2199c302016-04-23 17:36:10 -0700881 processDeviceAddedInternal(device.id());
882 }
883
884 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -0700885 // Irrespective of whether the local is a MASTER or not for this device,
886 // we need to create a SR-group-handler instance. This is because in a
887 // multi-instance setup, any instance can initiate forwarding/next-objectives
888 // for any switch (even if this instance is a SLAVE or not even connected
889 // to the switch). To handle this, a default-group-handler instance is necessary
890 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -0700891 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
892 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800893 DefaultGroupHandler groupHandler;
894 try {
895 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -0700896 createGroupHandler(deviceId,
897 appId,
898 deviceConfiguration,
899 linkService,
900 flowObjectiveService,
901 this);
Charles Chan0b4e6182015-11-03 10:42:14 -0800902 } catch (DeviceConfigNotFoundException e) {
903 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
904 return;
905 }
Charles Chan2199c302016-04-23 17:36:10 -0700906 log.debug("updating groupHandlerMap with new config for device: {}",
907 deviceId);
908 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -0800909 }
Saurav Dasb5c236e2016-06-07 10:08:06 -0700910 // Also, in some cases, drivers may need extra
911 // information to process rules (eg. Router IP/MAC); and so, we send
912 // port addressing rules to the driver as well irrespective of whether
913 // this instance is the master or not.
914 defaultRoutingHandler.populatePortAddressingRules(deviceId);
915
Charles Chan2199c302016-04-23 17:36:10 -0700916 if (mastershipService.isLocalMaster(deviceId)) {
Charles Chan03a73e02016-10-24 14:52:01 -0700917 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -0700918 xConnectHandler.init(deviceId);
Charles Chan35fd1a72016-06-13 18:54:31 -0700919 cordConfigHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -0700920 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700921 groupHandler.createGroupsFromSubnetConfig();
Charles Chan2199c302016-04-23 17:36:10 -0700922 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700923 }
Charles Chan5270ed02016-01-30 23:22:37 -0800924
Charles Chan03a73e02016-10-24 14:52:01 -0700925 appCfgHandler.init(deviceId);
926 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -0700927 }
928
Saurav Das80980c72016-03-23 11:22:49 -0700929 private void processDeviceRemoved(Device device) {
930 nsNextObjStore.entrySet().stream()
931 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
932 .forEach(entry -> {
933 nsNextObjStore.remove(entry.getKey());
934 });
Saurav Das80980c72016-03-23 11:22:49 -0700935 subnetNextObjStore.entrySet().stream()
936 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
937 .forEach(entry -> {
938 subnetNextObjStore.remove(entry.getKey());
939 });
Saurav Das80980c72016-03-23 11:22:49 -0700940 portNextObjStore.entrySet().stream()
941 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
942 .forEach(entry -> {
943 portNextObjStore.remove(entry.getKey());
944 });
Saurav Das80980c72016-03-23 11:22:49 -0700945 subnetVidStore.entrySet().stream()
946 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
947 .forEach(entry -> {
948 subnetVidStore.remove(entry.getKey());
949 });
Saurav Das80980c72016-03-23 11:22:49 -0700950 groupHandlerMap.remove(device.id());
Saurav Das80980c72016-03-23 11:22:49 -0700951 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan2199c302016-04-23 17:36:10 -0700952 mcastHandler.removeDevice(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -0700953 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -0700954 }
955
Saurav Das1a129a02016-11-18 15:21:57 -0800956 private void processPortUpdated(Device device, Port port) {
957 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
958 log.warn("Device configuration uploading. Not handling port event for"
959 + "dev: {} port: {}", device.id(), port.number());
960 return;
961 }
962 /* XXX create method for single port filtering rules which are needed
963 for both switch-to-switch ports and edge ports
964 if (defaultRoutingHandler != null) {
965 defaultRoutingHandler.populatePortAddressingRules(
966 ((Device) event.subject()).id());
967 }*/
968
969 // portUpdated calls are for ports that have gone down or up. For switch
970 // to switch ports, link-events should take care of any re-routing or
971 // group editing necessary for port up/down. Here we only process edge ports
972 // that are already configured.
Pier Ventre10bd8d12016-11-26 21:05:22 -0800973 Ip4Prefix configuredSubnet = deviceConfiguration.getPortIPv4Subnet(device.id(),
Saurav Das1a129a02016-11-18 15:21:57 -0800974 port.number());
975 if (configuredSubnet == null) {
976 log.debug("Not handling port updated event for unconfigured port "
977 + "dev/port: {}/{}", device.id(), port.number());
978 return;
979 }
980 processEdgePort(device, port, configuredSubnet);
981 }
982
983 private void processEdgePort(Device device, Port port, Ip4Prefix subnet) {
984 boolean portUp = port.isEnabled();
985 if (portUp) {
986 log.info("Device:EdgePort {}:{} is enabled in subnet: {}", device.id(),
987 port.number(), subnet);
988 } else {
989 log.info("Device:EdgePort {}:{} is disabled in subnet: {}", device.id(),
990 port.number(), subnet);
991 }
992
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700993 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -0700994 if (groupHandler != null) {
Saurav Das1a129a02016-11-18 15:21:57 -0800995 groupHandler.processEdgePort(port.number(), subnet, portUp,
Saurav Das423fe2b2015-12-04 10:52:59 -0800996 mastershipService.isLocalMaster(device.id()));
Saurav Das1a129a02016-11-18 15:21:57 -0800997 } else {
998 log.warn("Group handler not found for dev:{}. Not handling edge port"
999 + " {} event for port:{}", device.id(),
1000 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001001 }
1002 }
sangho1e575652015-05-14 00:39:53 -07001003
Pier Ventre10bd8d12016-11-26 21:05:22 -08001004 /**
1005 * Registers the given connect point with the NRS, this is necessary
1006 * to receive the NDP and ARP packets from the NRS.
1007 *
1008 * @param portToRegister connect point to register
1009 */
1010 public void registerConnectPoint(ConnectPoint portToRegister) {
Pier Ventre10bd8d12016-11-26 21:05:22 -08001011 this.neighbourResolutionService.registerNeighbourHandler(
1012 portToRegister,
1013 neighbourHandler,
1014 appId
1015 );
1016 }
1017
Charles Chand6832882015-10-05 17:50:33 -07001018 private class InternalConfigListener implements NetworkConfigListener {
Charles Chan2c15aca2016-11-09 20:51:44 -08001019 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001020
Charles Chane849c192016-01-11 18:28:54 -08001021 /**
1022 * Constructs the internal network config listener.
1023 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001024 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001025 */
Charles Chan2c15aca2016-11-09 20:51:44 -08001026 public InternalConfigListener(SegmentRoutingManager srManager) {
1027 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001028 }
1029
Charles Chane849c192016-01-11 18:28:54 -08001030 /**
1031 * Reads network config and initializes related data structure accordingly.
1032 */
Charles Chan4636be02015-10-07 14:21:45 -07001033 public void configureNetwork() {
Pier Ventre10bd8d12016-11-26 21:05:22 -08001034
Charles Chan2c15aca2016-11-09 20:51:44 -08001035 deviceConfiguration = new DeviceConfiguration(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001036
Charles Chan2c15aca2016-11-09 20:51:44 -08001037 arpHandler = new ArpHandler(srManager);
1038 icmpHandler = new IcmpHandler(srManager);
1039 ipHandler = new IpHandler(srManager);
1040 routingRulePopulator = new RoutingRulePopulator(srManager);
1041 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001042
1043 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1044 groupHandlerMap, tunnelStore);
1045 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1046 flowObjectiveService,
1047 tunnelHandler, policyStore);
1048
Charles Chan4636be02015-10-07 14:21:45 -07001049 for (Device device : deviceService.getDevices()) {
Charles Chan2199c302016-04-23 17:36:10 -07001050 processDeviceAddedInternal(device.id());
Charles Chan4636be02015-10-07 14:21:45 -07001051 }
1052
1053 defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -07001054 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001055 }
1056
Charles Chand6832882015-10-05 17:50:33 -07001057 @Override
1058 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001059 // TODO move this part to NetworkConfigEventHandler
1060 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1061 switch (event.type()) {
1062 case CONFIG_ADDED:
1063 log.info("Segment Routing Config added.");
1064 configureNetwork();
1065 break;
1066 case CONFIG_UPDATED:
1067 log.info("Segment Routing Config updated.");
1068 // TODO support dynamic configuration
1069 break;
1070 default:
1071 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001072 }
Charles Chan5270ed02016-01-30 23:22:37 -08001073 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001074 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001075 switch (event.type()) {
1076 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001077 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001078 break;
1079 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001080 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001081 break;
1082 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001083 appCfgHandler.processAppConfigRemoved(event);
1084 break;
1085 default:
1086 break;
1087 }
Charles Chan03a73e02016-10-24 14:52:01 -07001088 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001089 } else if (event.configClass().equals(XConnectConfig.class)) {
1090 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1091 switch (event.type()) {
1092 case CONFIG_ADDED:
1093 xConnectHandler.processXConnectConfigAdded(event);
1094 break;
1095 case CONFIG_UPDATED:
1096 xConnectHandler.processXConnectConfigUpdated(event);
1097 break;
1098 case CONFIG_REMOVED:
1099 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001100 break;
1101 default:
1102 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001103 }
Pier Luigi7dad71c2017-02-01 13:50:04 -08001104 // XXX Neighbour hacking. This method is looking for
1105 // the Internet-Router interface. In order to retrieve
1106 // the upstream port.
1107 } else if (event.configClass().equals(InterfaceConfig.class)) {
1108 switch (event.type()) {
1109 case CONFIG_ADDED:
1110 case CONFIG_UPDATED:
1111 icmpHandler.updateUPstreamCP(event);
1112 case CONFIG_REGISTERED:
1113 case CONFIG_UNREGISTERED:
1114 case CONFIG_REMOVED:
1115 break;
1116 default:
1117 break;
1118 }
1119 // XXX Neighbour hacking. This method is looking for
1120 // the vrouter port.
1121 } else if (event.configClass().equals(RouterConfig.class)) {
1122 switch (event.type()) {
1123 case CONFIG_ADDED:
1124 case CONFIG_UPDATED:
1125 icmpHandler.updateVRouterCP(event);
1126 case CONFIG_REGISTERED:
1127 case CONFIG_UNREGISTERED:
1128 case CONFIG_REMOVED:
1129 break;
1130 default:
1131 break;
1132 }
Charles Chand6832882015-10-05 17:50:33 -07001133 }
1134 }
1135 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001136
1137 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001138 @Override
1139 public void event(HostEvent event) {
1140 // Do not proceed without mastership
1141 DeviceId deviceId = event.subject().location().deviceId();
1142 if (!mastershipService.isLocalMaster(deviceId)) {
1143 return;
1144 }
1145
1146 switch (event.type()) {
1147 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001148 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001149 break;
1150 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001151 hostHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001152 break;
1153 case HOST_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001154 hostHandler.processHostRemoveEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001155 break;
1156 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001157 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001158 break;
1159 default:
1160 log.warn("Unsupported host event type: {}", event.type());
1161 break;
1162 }
1163 }
1164 }
1165
Charles Chand55e84d2016-03-30 17:54:24 -07001166 private class InternalMcastListener implements McastListener {
1167 @Override
1168 public void event(McastEvent event) {
1169 switch (event.type()) {
1170 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001171 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001172 break;
1173 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001174 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001175 break;
1176 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001177 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001178 break;
1179 case ROUTE_ADDED:
1180 case ROUTE_REMOVED:
1181 default:
1182 break;
1183 }
1184 }
1185 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001186
1187 private class InternalCordConfigListener implements CordConfigListener {
1188 @Override
1189 public void event(CordConfigEvent event) {
1190 switch (event.type()) {
1191 case ACCESS_AGENT_ADDED:
1192 cordConfigHandler.processAccessAgentAddedEvent(event);
1193 break;
1194 case ACCESS_AGENT_UPDATED:
1195 cordConfigHandler.processAccessAgentUpdatedEvent(event);
1196 break;
1197 case ACCESS_AGENT_REMOVED:
1198 cordConfigHandler.processAccessAgentRemovedEvent(event);
1199 break;
1200 case ACCESS_DEVICE_ADDED:
1201 case ACCESS_DEVICE_UPDATED:
1202 case ACCESS_DEVICE_REMOVED:
1203 default:
1204 break;
1205 }
1206 }
1207 }
Charles Chan03a73e02016-10-24 14:52:01 -07001208
1209 private class InternalRouteEventListener implements RouteListener {
1210 @Override
1211 public void event(RouteEvent event) {
1212 switch (event.type()) {
1213 case ROUTE_ADDED:
1214 routeHandler.processRouteAdded(event);
1215 break;
1216 case ROUTE_UPDATED:
1217 routeHandler.processRouteUpdated(event);
1218 break;
1219 case ROUTE_REMOVED:
1220 routeHandler.processRouteRemoved(event);
1221 break;
1222 default:
1223 break;
1224 }
1225 }
1226 }
sanghob35a6192015-04-01 13:05:26 -07001227}