blob: 614709f37e780d27c993e40ea33c348d6dfe6b1f [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor43b53542016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sangho80f11cb2015-04-01 13:05:26 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.segmentrouting;
17
Charles Chand7844e52016-10-20 17:02:44 -070018import com.google.common.collect.Maps;
sangho80f11cb2015-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;
sangho27462c62015-05-14 00:39:53 -070024import org.apache.felix.scr.annotations.Service;
sangho80f11cb2015-04-01 13:05:26 -070025import org.onlab.packet.Ethernet;
Pier Ventreb6b81d52016-12-02 08:16:05 -080026import org.onlab.packet.ICMP6;
Charles Chan77277672015-10-20 16:24:19 -070027import org.onlab.packet.IPv4;
Pier Ventreb6a7f342016-11-26 21:05:22 -080028import org.onlab.packet.IPv6;
Charles Chan77277672015-10-20 16:24:19 -070029import org.onlab.packet.Ip4Prefix;
Charles Chan77277672015-10-20 16:24:19 -070030import org.onlab.packet.IpPrefix;
Jonathan Hart54541d12016-04-12 15:39:44 -070031import org.onlab.packet.VlanId;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070032import org.onlab.util.KryoNamespace;
Saurav Dasc3604f12016-03-23 11:22:49 -070033import org.onosproject.cfg.ComponentConfigService;
sangho80f11cb2015-04-01 13:05:26 -070034import org.onosproject.core.ApplicationId;
35import org.onosproject.core.CoreService;
36import org.onosproject.event.Event;
Pier Luigi37a35432017-02-01 13:50:04 -080037import org.onosproject.incubator.net.config.basics.InterfaceConfig;
Charles Chanc91c8782016-03-30 17:54:24 -070038import org.onosproject.incubator.net.config.basics.McastConfig;
Charles Chan46fdfaf2016-11-09 20:51:44 -080039import org.onosproject.incubator.net.intf.InterfaceService;
Charles Chandebfea32016-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 Ventreb6b81d52016-12-02 08:16:05 -080043import org.onosproject.incubator.net.neighbour.NeighbourResolutionService;
Jonathan Hart54541d12016-04-12 15:39:44 -070044import org.onosproject.mastership.MastershipService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080045import org.onosproject.net.ConnectPoint;
Jonathan Hart54541d12016-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 Chanf4586112015-11-09 16:37:23 -080050import org.onosproject.net.PortNumber;
Charles Chan72f556a2015-10-05 17:50:33 -070051import org.onosproject.net.config.ConfigFactory;
52import org.onosproject.net.config.NetworkConfigEvent;
Charles Chan72f556a2015-10-05 17:50:33 -070053import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070054import org.onosproject.net.config.NetworkConfigRegistry;
Charles Chan72f556a2015-10-05 17:50:33 -070055import org.onosproject.net.config.basics.SubjectFactories;
Jonathan Hart54541d12016-04-12 15:39:44 -070056import org.onosproject.net.device.DeviceEvent;
57import org.onosproject.net.device.DeviceListener;
58import org.onosproject.net.device.DeviceService;
Charles Chanf4586112015-11-09 16:37:23 -080059import org.onosproject.net.flow.TrafficSelector;
60import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hart54541d12016-04-12 15:39:44 -070061import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chanf4586112015-11-09 16:37:23 -080062import org.onosproject.net.host.HostEvent;
63import org.onosproject.net.host.HostListener;
Pier Ventreb6a7f342016-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 Chanc91c8782016-03-30 17:54:24 -070068import org.onosproject.net.mcast.McastEvent;
69import org.onosproject.net.mcast.McastListener;
70import org.onosproject.net.mcast.MulticastRouteService;
Pier Ventreb6a7f342016-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 Chanc91c8782016-03-30 17:54:24 -070075import org.onosproject.net.topology.TopologyService;
Pier Luigi37a35432017-02-01 13:50:04 -080076import org.onosproject.routing.config.RouterConfig;
Charles Chanc91c8782016-03-30 17:54:24 -070077import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
78import org.onosproject.segmentrouting.config.DeviceConfiguration;
Charles Chanc91c8782016-03-30 17:54:24 -070079import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Pier Ventreb6a7f342016-11-26 21:05:22 -080080import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Charles Chan82f19972016-05-17 13:13:55 -070081import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chanc91c8782016-03-30 17:54:24 -070082import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
83import org.onosproject.segmentrouting.grouphandler.NeighborSet;
Charles Chan1eaf4802016-04-18 13:44:03 -070084import org.onosproject.segmentrouting.storekey.NeighborSetNextObjectiveStoreKey;
85import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -070086import org.onosproject.segmentrouting.storekey.SubnetAssignedVidStoreKey;
87import org.onosproject.segmentrouting.storekey.SubnetNextObjectiveStoreKey;
Charles Chan82f19972016-05-17 13:13:55 -070088import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hart54541d12016-04-12 15:39:44 -070089import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-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 Chan41f5ec02016-06-13 18:54:31 -070094import org.opencord.cordconfig.CordConfigEvent;
95import org.opencord.cordconfig.CordConfigListener;
96import org.opencord.cordconfig.CordConfigService;
sangho80f11cb2015-04-01 13:05:26 -070097import org.slf4j.Logger;
98import org.slf4j.LoggerFactory;
99
Saurav Das7c305372015-10-28 12:39:42 -0700100import java.util.Collections;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700101import java.util.HashSet;
sangho27462c62015-05-14 00:39:53 -0700102import java.util.List;
sangho80f11cb2015-04-01 13:05:26 -0700103import java.util.Map;
Saurav Das7c305372015-10-28 12:39:42 -0700104import java.util.Set;
sangho80f11cb2015-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 Ventreb6a7f342016-11-26 21:05:22 -0800111import java.util.stream.Collectors;
sangho80f11cb2015-04-01 13:05:26 -0700112
Charles Chand6d25332016-02-26 22:19:52 -0800113import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800114import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700115import static org.onlab.util.Tools.groupedThreads;
Charles Chand6d25332016-02-26 22:19:52 -0800116
Charles Chanb7f75ac2016-01-11 18:28:54 -0800117/**
118 * Segment routing manager.
119 */
Jonathan Hart54541d12016-04-12 15:39:44 -0700120@Service
121@Component(immediate = true)
sangho27462c62015-05-14 00:39:53 -0700122public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700123
Charles Chan46fdfaf2016-11-09 20:51:44 -0800124 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
sangho80f11cb2015-04-01 13:05:26 -0700125
126 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700127 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700128
129 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventreb6a7f342016-11-26 21:05:22 -0800130 private NeighbourResolutionService neighbourResolutionService;
131
132 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700133 CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700134
135 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700136 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700137
138 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700139 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700140
141 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700142 DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700143
144 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700145 FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700146
147 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700148 LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700149
Charles Chan82ab1932016-01-30 23:22:37 -0800150 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-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 Chan82ab1932016-01-30 23:22:37 -0800167
168 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800169 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800170
Saurav Dasc3604f12016-03-23 11:22:49 -0700171 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800172 public InterfaceService interfaceService;
173
Charles Chandebfea32016-10-24 14:52:01 -0700174 ArpHandler arpHandler = null;
175 IcmpHandler icmpHandler = null;
176 IpHandler ipHandler = null;
177 RoutingRulePopulator routingRulePopulator = null;
Charles Chan46fdfaf2016-11-09 20:51:44 -0800178 public ApplicationId appId;
sangho9b169e32015-04-14 16:27:13 -0700179 protected DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700180
Charles Chandebfea32016-10-24 14:52:01 -0700181 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700182 private TunnelHandler tunnelHandler = null;
183 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700184 private InternalPacketProcessor processor = null;
185 private InternalLinkListener linkListener = null;
186 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700187 private AppConfigHandler appCfgHandler = null;
Charles Chandebfea32016-10-24 14:52:01 -0700188 XConnectHandler xConnectHandler = null;
Charles Chan1eaf4802016-04-18 13:44:03 -0700189 private McastHandler mcastHandler = null;
Charles Chandebfea32016-10-24 14:52:01 -0700190 HostHandler hostHandler = null;
Charles Chan41f5ec02016-06-13 18:54:31 -0700191 private CordConfigHandler cordConfigHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800192 private RouteHandler routeHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800193 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
sangho80f11cb2015-04-01 13:05:26 -0700194 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan82ab1932016-01-30 23:22:37 -0800195 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700196 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
197 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan41f5ec02016-06-13 18:54:31 -0700198 private final InternalCordConfigListener cordConfigListener = new InternalCordConfigListener();
Charles Chandebfea32016-10-24 14:52:01 -0700199 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sangho80f11cb2015-04-01 13:05:26 -0700200
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700201 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700202 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sangho80f11cb2015-04-01 13:05:26 -0700203
Saurav Das2d94d312015-11-24 23:21:05 -0800204 @SuppressWarnings("unused")
sangho80f11cb2015-04-01 13:05:26 -0700205 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das2d94d312015-11-24 23:21:05 -0800206 @SuppressWarnings("rawtypes")
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700207 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Charles Chanf4586112015-11-09 16:37:23 -0800208 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chanb7f75ac2016-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 Chanf4586112015-11-09 16:37:23 -0800214 nsNextObjStore = null;
Charles Chanb7f75ac2016-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 Chanf4586112015-11-09 16:37:23 -0800219 subnetNextObjStore = null;
Charles Chanb7f75ac2016-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 Das2d94d312015-11-24 23:21:05 -0800224 portNextObjStore = null;
Saurav Das7c305372015-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 Chanb7f75ac2016-01-11 18:28:54 -0800228 subnetVidStore = null;
Saurav Das2d94d312015-11-24 23:21:05 -0800229 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
230 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700231
Charles Chanc91c8782016-03-30 17:54:24 -0700232 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700233 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
234 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700235 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700236 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800237 public SegmentRoutingDeviceConfig createConfig() {
238 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700239 }
240 };
Charles Chanc91c8782016-03-30 17:54:24 -0700241 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700242 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
243 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700244 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800245 @Override
246 public SegmentRoutingAppConfig createConfig() {
247 return new SegmentRoutingAppConfig();
248 }
249 };
Charles Chan82f19972016-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 Chanc91c8782016-03-30 17:54:24 -0700259 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700260 new ConfigFactory<ApplicationId, McastConfig>(
261 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700262 McastConfig.class, "multicast") {
263 @Override
264 public McastConfig createConfig() {
265 return new McastConfig();
266 }
267 };
268
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700269 private Object threadSchedulerLock = new Object();
270 private static int numOfEventsQueued = 0;
271 private static int numOfEventsExecuted = 0;
sangho80f11cb2015-04-01 13:05:26 -0700272 private static int numOfHandlerExecution = 0;
273 private static int numOfHandlerScheduled = 0;
274
Charles Chan1963f4f2016-02-18 14:22:42 -0800275 /**
276 * Segment Routing App ID.
277 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800278 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chanb7f75ac2016-01-11 18:28:54 -0800279 /**
280 * The starting value of per-subnet VLAN ID assignment.
281 */
Saurav Das7c305372015-10-28 12:39:42 -0700282 private static final short ASSIGNED_VLAN_START = 4093;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800283 /**
284 * The default VLAN ID assigned to the interfaces without subnet config.
285 */
Saurav Das7c305372015-10-28 12:39:42 -0700286 public static final short ASSIGNED_VLAN_NO_SUBNET = 4094;
287
sangho80f11cb2015-04-01 13:05:26 -0700288 @Activate
289 protected void activate() {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800290 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700291
292 log.debug("Creating EC map nsnextobjectivestore");
293 EventuallyConsistentMapBuilder<NeighborSetNextObjectiveStoreKey, Integer>
294 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700295 nsNextObjStore = nsNextObjMapBuilder
296 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700297 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700298 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700299 .build();
300 log.trace("Current size {}", nsNextObjStore.size());
301
Charles Chan77277672015-10-20 16:24:19 -0700302 log.debug("Creating EC map subnetnextobjectivestore");
303 EventuallyConsistentMapBuilder<SubnetNextObjectiveStoreKey, Integer>
304 subnetNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Charles Chan77277672015-10-20 16:24:19 -0700305 subnetNextObjStore = subnetNextObjMapBuilder
306 .withName("subnetnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700307 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700308 .withTimestampProvider((k, v) -> new WallClockTimestamp())
309 .build();
310
Saurav Das2d94d312015-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 Hart54541d12016-04-12 15:39:44 -0700316 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800317 .withTimestampProvider((k, v) -> new WallClockTimestamp())
318 .build();
319
sangho4a5c42a2015-05-20 22:16:38 -0700320 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
321 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700322 tunnelStore = tunnelMapBuilder
323 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700324 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700325 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700326 .build();
327
328 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
329 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700330 policyStore = policyMapBuilder
331 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700332 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700333 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700334 .build();
335
Saurav Das7c305372015-10-28 12:39:42 -0700336 EventuallyConsistentMapBuilder<SubnetAssignedVidStoreKey, VlanId>
337 subnetVidStoreMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7c305372015-10-28 12:39:42 -0700338 subnetVidStore = subnetVidStoreMapBuilder
339 .withName("subnetvidstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700340 .withSerializer(createSerializer())
Saurav Das7c305372015-10-28 12:39:42 -0700341 .withTimestampProvider((k, v) -> new WallClockTimestamp())
342 .build();
343
Saurav Dasc3604f12016-03-23 11:22:49 -0700344 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800345 "purgeOnDisconnection", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700346 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800347 "purgeOnDisconnection", "true");
Charles Chandebfea32016-10-24 14:52:01 -0700348 compCfgService.preSetProperty("org.onosproject.vrouter.Vrouter",
Charles Chan5c7bf022017-01-18 20:00:13 -0800349 "fibInstallerEnabled", "false");
Pier Luigib9632ba2017-01-12 18:14:58 -0800350 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
351 "requestInterceptsEnabled", "false");
Pier Luigibc976df2017-01-12 22:46:39 -0800352 compCfgService.preSetProperty("org.onosproject.incubator.net.neighbour.impl.NeighbourResolutionManager",
353 "requestInterceptsEnabled", "false");
354 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelay",
355 "arpEnabled", "false");
Saurav Dasc3604f12016-03-23 11:22:49 -0700356
Charles Chan2b078ae2015-10-14 11:24:40 -0700357 processor = new InternalPacketProcessor();
358 linkListener = new InternalLinkListener();
359 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700360 appCfgHandler = new AppConfigHandler(this);
361 xConnectHandler = new XConnectHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700362 mcastHandler = new McastHandler(this);
363 hostHandler = new HostHandler(this);
Charles Chan41f5ec02016-06-13 18:54:31 -0700364 cordConfigHandler = new CordConfigHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700365 routeHandler = new RouteHandler(this);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800366 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700367
Charles Chand6d25332016-02-26 22:19:52 -0800368 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700369 cfgService.registerConfigFactory(deviceConfigFactory);
370 cfgService.registerConfigFactory(appConfigFactory);
Charles Chan82f19972016-05-17 13:13:55 -0700371 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700372 cfgService.registerConfigFactory(mcastConfigFactory);
Charles Chan82ab1932016-01-30 23:22:37 -0800373 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700374 packetService.addProcessor(processor, PacketProcessor.director(2));
375 linkService.addListener(linkListener);
376 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700377 multicastRouteService.addListener(mcastListener);
Charles Chan41f5ec02016-06-13 18:54:31 -0700378 cordConfigService.addListener(cordConfigListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700379
380 cfgListener.configureNetwork();
381
Charles Chandebfea32016-10-24 14:52:01 -0700382 routeService.addListener(routeListener);
383
sangho80f11cb2015-04-01 13:05:26 -0700384 log.info("Started");
385 }
386
Jonathan Hart54541d12016-04-12 15:39:44 -0700387 private KryoNamespace.Builder createSerializer() {
388 return new KryoNamespace.Builder()
389 .register(KryoNamespaces.API)
390 .register(NeighborSetNextObjectiveStoreKey.class,
391 SubnetNextObjectiveStoreKey.class,
392 SubnetAssignedVidStoreKey.class,
393 NeighborSet.class,
394 Tunnel.class,
395 DefaultTunnel.class,
396 Policy.class,
397 TunnelPolicy.class,
398 Policy.Type.class,
399 PortNextObjectiveStoreKey.class,
Charles Chan82f19972016-05-17 13:13:55 -0700400 XConnectStoreKey.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700401 );
402 }
403
sangho80f11cb2015-04-01 13:05:26 -0700404 @Deactivate
405 protected void deactivate() {
Charles Chan72f556a2015-10-05 17:50:33 -0700406 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700407 cfgService.unregisterConfigFactory(deviceConfigFactory);
408 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chandebfea32016-10-24 14:52:01 -0700409 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700410 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan72f556a2015-10-05 17:50:33 -0700411
sangho80f11cb2015-04-01 13:05:26 -0700412 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700413 linkService.removeListener(linkListener);
414 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700415 multicastRouteService.removeListener(mcastListener);
Charles Chan41f5ec02016-06-13 18:54:31 -0700416 cordConfigService.removeListener(cordConfigListener);
Charles Chandebfea32016-10-24 14:52:01 -0700417 routeService.removeListener(routeListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700418
sangho80f11cb2015-04-01 13:05:26 -0700419 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700420 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700421 deviceListener = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700422 groupHandlerMap.clear();
423
Charles Chanc91c8782016-03-30 17:54:24 -0700424 nsNextObjStore.destroy();
425 subnetNextObjStore.destroy();
426 portNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700427 tunnelStore.destroy();
428 policyStore.destroy();
429 subnetVidStore.destroy();
sangho80f11cb2015-04-01 13:05:26 -0700430 log.info("Stopped");
431 }
432
sangho27462c62015-05-14 00:39:53 -0700433 @Override
434 public List<Tunnel> getTunnels() {
435 return tunnelHandler.getTunnels();
436 }
437
438 @Override
sanghobd812f82015-06-29 14:58:47 -0700439 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
440 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700441 }
442
443 @Override
sanghobd812f82015-06-29 14:58:47 -0700444 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700445 for (Policy policy: policyHandler.getPolicies()) {
446 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
447 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
448 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
449 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700450 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700451 }
452 }
453 }
sanghobd812f82015-06-29 14:58:47 -0700454 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700455 }
456
457 @Override
sanghobd812f82015-06-29 14:58:47 -0700458 public PolicyHandler.Result removePolicy(Policy policy) {
459 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700460 }
461
462 @Override
sanghobd812f82015-06-29 14:58:47 -0700463 public PolicyHandler.Result createPolicy(Policy policy) {
464 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700465 }
466
467 @Override
468 public List<Policy> getPolicies() {
469 return policyHandler.getPolicies();
470 }
471
Saurav Das07c74602016-04-27 18:35:50 -0700472 @Override
473 public void rerouteNetwork() {
474 cfgListener.configureNetwork();
475 for (Device device : deviceService.getDevices()) {
476 defaultRoutingHandler.populatePortAddressingRules(device.id());
477 }
478 defaultRoutingHandler.startPopulationProcess();
479 }
480
Charles Chand7844e52016-10-20 17:02:44 -0700481 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800482 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
483 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Charles Chand7844e52016-10-20 17:02:44 -0700484 deviceService.getAvailableDevices().forEach(device -> {
485 deviceSubnetMap.put(device.id(), deviceConfiguration.getSubnets(device.id()));
486 });
487 return deviceSubnetMap;
488 }
489
sangho80f1f892015-05-19 11:57:42 -0700490 /**
Pier Ventre7a78de22016-10-31 15:00:01 -0700491 * Returns the MPLS-ECMP configuration.
492 *
493 * @return MPLS-ECMP value
494 */
495 public boolean getMplsEcmp() {
496 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
497 .getConfig(this.appId, SegmentRoutingAppConfig.class);
498 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
499 }
500
501 /**
sangho80f1f892015-05-19 11:57:42 -0700502 * Returns the tunnel object with the tunnel ID.
503 *
504 * @param tunnelId Tunnel ID
505 * @return Tunnel reference
506 */
sangho27462c62015-05-14 00:39:53 -0700507 public Tunnel getTunnel(String tunnelId) {
508 return tunnelHandler.getTunnel(tunnelId);
509 }
510
sangho80f11cb2015-04-01 13:05:26 -0700511 /**
Saurav Das7c305372015-10-28 12:39:42 -0700512 * Returns the vlan-id assigned to the subnet configured for a device.
513 * If no vlan-id has been assigned, a new one is assigned out of a pool of ids,
514 * if and only if this controller instance is the master for the device.
515 * <p>
516 * USAGE: The assigned vlans are meant to be applied to untagged packets on those
517 * switches/pipelines that need this functionality. These vids are meant
518 * to be used internally within a switch, and thus need to be unique only
519 * on a switch level. Note that packets never go out on the wire with these
520 * vlans. Currently, vlan ids are assigned from value 4093 down.
521 * Vlan id 4094 expected to be used for all ports that are not assigned subnets.
522 * Vlan id 4095 is reserved and unused. Only a single vlan id is assigned
523 * per subnet.
sangho80f11cb2015-04-01 13:05:26 -0700524 *
Saurav Das7c305372015-10-28 12:39:42 -0700525 * @param deviceId switch dpid
Pier Ventre1a655962016-11-28 16:48:06 -0800526 * @param subnet IP prefix for which assigned vlan is desired
Saurav Das7c305372015-10-28 12:39:42 -0700527 * @return VlanId assigned for the subnet on the device, or
528 * null if no vlan assignment was found and this instance is not
529 * the master for the device.
sangho80f11cb2015-04-01 13:05:26 -0700530 */
Charles Chanb7f75ac2016-01-11 18:28:54 -0800531 // TODO: We should avoid assigning VLAN IDs that are used by VLAN cross-connection.
Pier Ventreb6a7f342016-11-26 21:05:22 -0800532 public VlanId getSubnetAssignedVlanId(DeviceId deviceId, IpPrefix subnet) {
Saurav Das7c305372015-10-28 12:39:42 -0700533 VlanId assignedVid = subnetVidStore.get(new SubnetAssignedVidStoreKey(
534 deviceId, subnet));
535 if (assignedVid != null) {
536 log.debug("Query for subnet:{} on device:{} returned assigned-vlan "
537 + "{}", subnet, deviceId, assignedVid);
538 return assignedVid;
539 }
540 //check mastership for the right to assign a vlan
541 if (!mastershipService.isLocalMaster(deviceId)) {
542 log.warn("This controller instance is not the master for device {}. "
543 + "Cannot assign vlan-id for subnet {}", deviceId, subnet);
544 return null;
545 }
546 // vlan assignment is expensive but done only once
Pier Ventreb6a7f342016-11-26 21:05:22 -0800547 // FIXME for now we will do assignment considering only the ipv4 subnet.
548 Set<Ip4Prefix> configuredSubnets = deviceConfiguration.getSubnets(deviceId)
549 .stream()
550 .filter(IpPrefix::isIp4)
551 .map(IpPrefix::getIp4Prefix)
552 .collect(Collectors.toSet());
Saurav Das7c305372015-10-28 12:39:42 -0700553 Set<Short> assignedVlans = new HashSet<>();
554 Set<Ip4Prefix> unassignedSubnets = new HashSet<>();
555 for (Ip4Prefix sub : configuredSubnets) {
556 VlanId v = subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId,
557 sub));
558 if (v != null) {
559 assignedVlans.add(v.toShort());
560 } else {
561 unassignedSubnets.add(sub);
562 }
563 }
564 short nextAssignedVlan = ASSIGNED_VLAN_START;
565 if (!assignedVlans.isEmpty()) {
566 nextAssignedVlan = (short) (Collections.min(assignedVlans) - 1);
567 }
568 for (Ip4Prefix unsub : unassignedSubnets) {
Pier Ventre1a655962016-11-28 16:48:06 -0800569 subnetVidStore.put(new SubnetAssignedVidStoreKey(deviceId, unsub),
570 VlanId.vlanId(nextAssignedVlan--));
571 log.info("Assigned vlan: {} to subnet: {} on device: {}",
572 nextAssignedVlan + 1, unsub, deviceId);
sangho80f11cb2015-04-01 13:05:26 -0700573 }
Saurav Das7c305372015-10-28 12:39:42 -0700574 return subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId, subnet));
sangho80f11cb2015-04-01 13:05:26 -0700575 }
576
sangho27462c62015-05-14 00:39:53 -0700577 /**
Saurav Das7c305372015-10-28 12:39:42 -0700578 * Returns the next objective ID for the given NeighborSet.
Saurav Das4c35fc42015-11-20 15:27:53 -0800579 * If the nextObjective does not exist, a new one is created and
Saurav Das2d94d312015-11-24 23:21:05 -0800580 * its id is returned.
sangho27462c62015-05-14 00:39:53 -0700581 *
sangho80f1f892015-05-19 11:57:42 -0700582 * @param deviceId Device ID
583 * @param ns NegighborSet
Saurav Das4c35fc42015-11-20 15:27:53 -0800584 * @param meta metadata passed into the creation of a Next Objective
585 * @return next objective ID or -1 if an error was encountered during the
586 * creation of the nextObjective
sangho27462c62015-05-14 00:39:53 -0700587 */
Saurav Das4c35fc42015-11-20 15:27:53 -0800588 public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns,
589 TrafficSelector meta) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700590 if (groupHandlerMap.get(deviceId) != null) {
591 log.trace("getNextObjectiveId query in device {}", deviceId);
592 return groupHandlerMap
Saurav Das4c35fc42015-11-20 15:27:53 -0800593 .get(deviceId).getNextObjectiveId(ns, meta);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700594 } else {
Saurav Das2d94d312015-11-24 23:21:05 -0800595 log.warn("getNextObjectiveId query - groupHandler for device {} "
596 + "not found", deviceId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700597 return -1;
598 }
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700599 }
600
Charles Chan77277672015-10-20 16:24:19 -0700601 /**
Saurav Das2d94d312015-11-24 23:21:05 -0800602 * Returns the next objective ID for the given subnet prefix. It is expected
603 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -0700604 *
605 * @param deviceId Device ID
606 * @param prefix Subnet
Saurav Das2d94d312015-11-24 23:21:05 -0800607 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -0700608 */
609 public int getSubnetNextObjectiveId(DeviceId deviceId, IpPrefix prefix) {
610 if (groupHandlerMap.get(deviceId) != null) {
611 log.trace("getSubnetNextObjectiveId query in device {}", deviceId);
612 return groupHandlerMap
613 .get(deviceId).getSubnetNextObjectiveId(prefix);
614 } else {
Saurav Das2d94d312015-11-24 23:21:05 -0800615 log.warn("getSubnetNextObjectiveId query - groupHandler for "
616 + "device {} not found", deviceId);
617 return -1;
618 }
619 }
620
621 /**
622 * Returns the next objective ID for the given portNumber, given the treatment.
623 * There could be multiple different treatments to the same outport, which
624 * would result in different objectives. If the next object
625 * does not exist, a new one is created and its id is returned.
626 *
627 * @param deviceId Device ID
628 * @param portNum port number on device for which NextObjective is queried
629 * @param treatment the actions to apply on the packets (should include outport)
630 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das07c74602016-04-27 18:35:50 -0700631 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -0800632 */
633 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
634 TrafficTreatment treatment,
635 TrafficSelector meta) {
636 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
637 if (ghdlr != null) {
638 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta);
639 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -0800640 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
641 + " not found", deviceId);
642 return -1;
643 }
644 }
645
sangho80f11cb2015-04-01 13:05:26 -0700646 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -0700647 @Override
648 public void process(PacketContext context) {
649
650 if (context.isHandled()) {
651 return;
652 }
653
654 InboundPacket pkt = context.inPacket();
655 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -0800656
657 if (ethernet == null) {
658 return;
659 }
660
Saurav Das2d94d312015-11-24 23:21:05 -0800661 log.trace("Rcvd pktin: {}", ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -0800662 if (ethernet.getEtherType() == TYPE_ARP) {
Pier Ventreb6a7f342016-11-26 21:05:22 -0800663 log.warn("{} - we are still receiving ARP packets from {}",
664 context.inPacket().receivedFrom());
665 log.debug("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800666 return;
sangho80f11cb2015-04-01 13:05:26 -0700667 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -0800668 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
669 //ipHandler.addToPacketBuffer(ipv4Packet);
670 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
671 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -0700672 } else {
Charles Chand041ad82017-01-13 17:20:44 -0800673 // NOTE: We don't support IP learning at this moment so this
674 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800675 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -0700676 }
Pier Ventreb6a7f342016-11-26 21:05:22 -0800677 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
678 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -0800679 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -0800680 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -0800681 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
682 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
683 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
684 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
685 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
686 } else {
Pier Luigi37a35432017-02-01 13:50:04 -0800687 // XXX Neigbour hacking, to handle the ICMPv6 packet
688 // not under our control
689 if (icmpHandler.handleUPstreamPackets(context)) {
690 log.debug("Rcvd pktin from UpStream: {}", ipv6Packet);
691 } else {
692 log.debug("Received ICMPv6 0x{} - not handled",
693 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
694 }
Pier Ventreb6b81d52016-12-02 08:16:05 -0800695 }
696 } else {
697 // NOTE: We don't support IP learning at this moment so this
698 // is not necessary. Also it causes duplication of DHCPv6 packets.
699 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
700 }
sangho80f11cb2015-04-01 13:05:26 -0700701 }
702 }
703 }
704
705 private class InternalLinkListener implements LinkListener {
706 @Override
707 public void event(LinkEvent event) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700708 if (event.type() == LinkEvent.Type.LINK_ADDED
709 || event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700710 log.debug("Event {} received from Link Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -0700711 scheduleEventHandlerIfNotScheduled(event);
712 }
713 }
714 }
715
716 private class InternalDeviceListener implements DeviceListener {
sangho80f11cb2015-04-01 13:05:26 -0700717 @Override
718 public void event(DeviceEvent event) {
sangho80f11cb2015-04-01 13:05:26 -0700719 switch (event.type()) {
720 case DEVICE_ADDED:
Saurav Dasf0f592d2016-11-18 15:21:57 -0800721 case PORT_UPDATED:
722 case PORT_ADDED:
sanghofb7c7292015-04-13 15:15:58 -0700723 case DEVICE_UPDATED:
724 case DEVICE_AVAILABILITY_CHANGED:
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700725 log.debug("Event {} received from Device Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -0700726 scheduleEventHandlerIfNotScheduled(event);
727 break;
728 default:
729 }
730 }
731 }
732
Saurav Das2d94d312015-11-24 23:21:05 -0800733 @SuppressWarnings("rawtypes")
sangho80f11cb2015-04-01 13:05:26 -0700734 private void scheduleEventHandlerIfNotScheduled(Event event) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700735 synchronized (threadSchedulerLock) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700736 eventQueue.add(event);
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700737 numOfEventsQueued++;
738
739 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
740 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700741 eventHandlerFuture = executorService
742 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
743 numOfHandlerScheduled++;
744 }
Jonathan Hart54541d12016-04-12 15:39:44 -0700745 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700746 numOfEventsQueued,
747 numOfHandlerScheduled);
sangho80f11cb2015-04-01 13:05:26 -0700748 }
sangho80f11cb2015-04-01 13:05:26 -0700749 }
750
751 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700752 @Override
sangho80f11cb2015-04-01 13:05:26 -0700753 public void run() {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700754 try {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700755 while (true) {
Saurav Das2d94d312015-11-24 23:21:05 -0800756 @SuppressWarnings("rawtypes")
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700757 Event event = null;
758 synchronized (threadSchedulerLock) {
759 if (!eventQueue.isEmpty()) {
760 event = eventQueue.poll();
761 numOfEventsExecuted++;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700762 } else {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700763 numOfHandlerExecution++;
764 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
765 numOfHandlerExecution, numOfEventsExecuted);
766 break;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700767 }
sanghofb7c7292015-04-13 15:15:58 -0700768 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700769 if (event.type() == LinkEvent.Type.LINK_ADDED) {
770 processLinkAdded((Link) event.subject());
771 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre6d593892016-09-13 21:33:40 -0700772 Link linkRemoved = (Link) event.subject();
773 if (linkRemoved.src().elementId() instanceof DeviceId &&
774 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
775 continue;
776 }
777 if (linkRemoved.dst().elementId() instanceof DeviceId &&
778 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
779 continue;
780 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700781 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700782 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
783 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
784 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das62af8802015-12-04 10:52:59 -0800785 DeviceId deviceId = ((Device) event.subject()).id();
786 if (deviceService.isAvailable(deviceId)) {
Saurav Dasc28b3432015-10-30 17:45:38 -0700787 log.info("Processing device event {} for available device {}",
788 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700789 processDeviceAdded((Device) event.subject());
Saurav Dasc3604f12016-03-23 11:22:49 -0700790 } else {
791 log.info("Processing device event {} for unavailable device {}",
792 event.type(), ((Device) event.subject()).id());
793 processDeviceRemoved((Device) event.subject());
794 }
Saurav Dasf0f592d2016-11-18 15:21:57 -0800795 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd1872b02016-12-02 15:43:47 -0800796 // typically these calls come when device is added first time
797 // so port filtering rules are handled at the device_added event.
798 // port added calls represent all ports on the device,
799 // enabled or not.
Saurav Dasf0f592d2016-11-18 15:21:57 -0800800 log.debug("** PORT ADDED {}/{} -> {}",
Saurav Dasd1872b02016-12-02 15:43:47 -0800801 ((DeviceEvent) event).subject().id(),
802 ((DeviceEvent) event).port().number(),
803 event.type());
Saurav Dasf0f592d2016-11-18 15:21:57 -0800804 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd1872b02016-12-02 15:43:47 -0800805 // these calls happen for every subsequent event
806 // ports enabled, disabled, switch goes away, comes back
Saurav Dasf0f592d2016-11-18 15:21:57 -0800807 log.info("** PORT UPDATED {}/{} -> {}",
808 event.subject(),
809 ((DeviceEvent) event).port(),
810 event.type());
811 processPortUpdated(((Device) event.subject()),
812 ((DeviceEvent) event).port());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700813 } else {
814 log.warn("Unhandled event type: {}", event.type());
815 }
sangho80f11cb2015-04-01 13:05:26 -0700816 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700817 } catch (Exception e) {
818 log.error("SegmentRouting event handler "
819 + "thread thrown an exception: {}", e);
sangho80f11cb2015-04-01 13:05:26 -0700820 }
sangho80f11cb2015-04-01 13:05:26 -0700821 }
822 }
823
sangho80f11cb2015-04-01 13:05:26 -0700824 private void processLinkAdded(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -0700825 log.info("** LINK ADDED {}", link.toString());
Charles Chan319d1a22015-11-03 10:42:14 -0800826 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
827 log.warn("Source device of this link is not configured.");
828 return;
829 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700830 //Irrespective whether the local is a MASTER or not for this device,
831 //create group handler instance and push default TTP flow rules.
832 //Because in a multi-instance setup, instances can initiate
833 //groups for any devices. Also the default TTP rules are needed
834 //to be pushed before inserting any IP table entries for any device
835 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
836 .deviceId());
837 if (groupHandler != null) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800838 groupHandler.linkUp(link, mastershipService.isLocalMaster(
839 link.src().deviceId()));
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700840 } else {
841 Device device = deviceService.getDevice(link.src().deviceId());
842 if (device != null) {
843 log.warn("processLinkAdded: Link Added "
844 + "Notification without Device Added "
845 + "event, still handling it");
846 processDeviceAdded(device);
847 groupHandler = groupHandlerMap.get(link.src()
848 .deviceId());
Saurav Das4c35fc42015-11-20 15:27:53 -0800849 groupHandler.linkUp(link, mastershipService.isLocalMaster(device.id()));
sangho80f11cb2015-04-01 13:05:26 -0700850 }
851 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700852
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700853 log.trace("Starting optimized route population process");
854 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null);
855 //log.trace("processLinkAdded: re-starting route population process");
856 //defaultRoutingHandler.startPopulationProcess();
Charles Chan72779502016-04-23 17:36:10 -0700857
858 mcastHandler.init();
sangho80f11cb2015-04-01 13:05:26 -0700859 }
860
861 private void processLinkRemoved(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -0700862 log.info("** LINK REMOVED {}", link.toString());
sangho2165d222015-05-01 09:38:25 -0700863 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
864 if (groupHandler != null) {
Saurav Das62af8802015-12-04 10:52:59 -0800865 groupHandler.portDown(link.src().port(),
866 mastershipService.isLocalMaster(link.src().deviceId()));
sangho2165d222015-05-01 09:38:25 -0700867 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700868 log.trace("Starting optimized route population process");
869 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link);
870 //log.trace("processLinkRemoved: re-starting route population process");
871 //defaultRoutingHandler.startPopulationProcess();
Charles Chan72779502016-04-23 17:36:10 -0700872
873 mcastHandler.processLinkDown(link);
sangho80f11cb2015-04-01 13:05:26 -0700874 }
875
876 private void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -0700877 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan319d1a22015-11-03 10:42:14 -0800878 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das8ec0ec42015-11-03 14:39:27 -0800879 log.warn("Device configuration uploading. Device {} will be "
880 + "processed after config completes.", device.id());
881 return;
882 }
Charles Chan72779502016-04-23 17:36:10 -0700883 processDeviceAddedInternal(device.id());
884 }
885
886 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -0700887 // Irrespective of whether the local is a MASTER or not for this device,
888 // we need to create a SR-group-handler instance. This is because in a
889 // multi-instance setup, any instance can initiate forwarding/next-objectives
890 // for any switch (even if this instance is a SLAVE or not even connected
891 // to the switch). To handle this, a default-group-handler instance is necessary
892 // per switch.
Charles Chan72779502016-04-23 17:36:10 -0700893 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
894 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -0800895 DefaultGroupHandler groupHandler;
896 try {
897 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -0700898 createGroupHandler(deviceId,
899 appId,
900 deviceConfiguration,
901 linkService,
902 flowObjectiveService,
903 this);
Charles Chan319d1a22015-11-03 10:42:14 -0800904 } catch (DeviceConfigNotFoundException e) {
905 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
906 return;
907 }
Charles Chan72779502016-04-23 17:36:10 -0700908 log.debug("updating groupHandlerMap with new config for device: {}",
909 deviceId);
910 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -0800911 }
Saurav Dasb149be12016-06-07 10:08:06 -0700912 // Also, in some cases, drivers may need extra
913 // information to process rules (eg. Router IP/MAC); and so, we send
914 // port addressing rules to the driver as well irrespective of whether
915 // this instance is the master or not.
916 defaultRoutingHandler.populatePortAddressingRules(deviceId);
917
Charles Chan72779502016-04-23 17:36:10 -0700918 if (mastershipService.isLocalMaster(deviceId)) {
Charles Chandebfea32016-10-24 14:52:01 -0700919 hostHandler.init(deviceId);
Charles Chan82f19972016-05-17 13:13:55 -0700920 xConnectHandler.init(deviceId);
Charles Chan41f5ec02016-06-13 18:54:31 -0700921 cordConfigHandler.init(deviceId);
Charles Chan72779502016-04-23 17:36:10 -0700922 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan77277672015-10-20 16:24:19 -0700923 groupHandler.createGroupsFromSubnetConfig();
Charles Chan72779502016-04-23 17:36:10 -0700924 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -0700925 }
Charles Chan82ab1932016-01-30 23:22:37 -0800926
Charles Chandebfea32016-10-24 14:52:01 -0700927 appCfgHandler.init(deviceId);
928 routeHandler.init(deviceId);
sangho80f11cb2015-04-01 13:05:26 -0700929 }
930
Saurav Dasc3604f12016-03-23 11:22:49 -0700931 private void processDeviceRemoved(Device device) {
932 nsNextObjStore.entrySet().stream()
933 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
934 .forEach(entry -> {
935 nsNextObjStore.remove(entry.getKey());
936 });
Saurav Dasc3604f12016-03-23 11:22:49 -0700937 subnetNextObjStore.entrySet().stream()
938 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
939 .forEach(entry -> {
940 subnetNextObjStore.remove(entry.getKey());
941 });
Saurav Dasc3604f12016-03-23 11:22:49 -0700942 portNextObjStore.entrySet().stream()
943 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
944 .forEach(entry -> {
945 portNextObjStore.remove(entry.getKey());
946 });
Saurav Dasc3604f12016-03-23 11:22:49 -0700947 subnetVidStore.entrySet().stream()
948 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
949 .forEach(entry -> {
950 subnetVidStore.remove(entry.getKey());
951 });
Saurav Dasc3604f12016-03-23 11:22:49 -0700952 groupHandlerMap.remove(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -0700953 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan72779502016-04-23 17:36:10 -0700954 mcastHandler.removeDevice(device.id());
Charles Chan82f19972016-05-17 13:13:55 -0700955 xConnectHandler.removeDevice(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -0700956 }
957
Saurav Dasf0f592d2016-11-18 15:21:57 -0800958 private void processPortUpdated(Device device, Port port) {
959 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
960 log.warn("Device configuration uploading. Not handling port event for"
961 + "dev: {} port: {}", device.id(), port.number());
962 return;
963 }
964 /* XXX create method for single port filtering rules which are needed
965 for both switch-to-switch ports and edge ports
966 if (defaultRoutingHandler != null) {
967 defaultRoutingHandler.populatePortAddressingRules(
968 ((Device) event.subject()).id());
969 }*/
970
971 // portUpdated calls are for ports that have gone down or up. For switch
972 // to switch ports, link-events should take care of any re-routing or
973 // group editing necessary for port up/down. Here we only process edge ports
974 // that are already configured.
Pier Ventreb6a7f342016-11-26 21:05:22 -0800975 Ip4Prefix configuredSubnet = deviceConfiguration.getPortIPv4Subnet(device.id(),
Saurav Dasf0f592d2016-11-18 15:21:57 -0800976 port.number());
977 if (configuredSubnet == null) {
978 log.debug("Not handling port updated event for unconfigured port "
979 + "dev/port: {}/{}", device.id(), port.number());
980 return;
981 }
982 processEdgePort(device, port, configuredSubnet);
983 }
984
985 private void processEdgePort(Device device, Port port, Ip4Prefix subnet) {
986 boolean portUp = port.isEnabled();
987 if (portUp) {
988 log.info("Device:EdgePort {}:{} is enabled in subnet: {}", device.id(),
989 port.number(), subnet);
990 } else {
991 log.info("Device:EdgePort {}:{} is disabled in subnet: {}", device.id(),
992 port.number(), subnet);
993 }
994
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700995 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sangho80f11cb2015-04-01 13:05:26 -0700996 if (groupHandler != null) {
Saurav Dasf0f592d2016-11-18 15:21:57 -0800997 groupHandler.processEdgePort(port.number(), subnet, portUp,
Saurav Das62af8802015-12-04 10:52:59 -0800998 mastershipService.isLocalMaster(device.id()));
Saurav Dasf0f592d2016-11-18 15:21:57 -0800999 } else {
1000 log.warn("Group handler not found for dev:{}. Not handling edge port"
1001 + " {} event for port:{}", device.id(),
1002 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001003 }
1004 }
sangho27462c62015-05-14 00:39:53 -07001005
Pier Ventreb6a7f342016-11-26 21:05:22 -08001006 /**
1007 * Registers the given connect point with the NRS, this is necessary
1008 * to receive the NDP and ARP packets from the NRS.
1009 *
1010 * @param portToRegister connect point to register
1011 */
1012 public void registerConnectPoint(ConnectPoint portToRegister) {
Pier Ventreb6a7f342016-11-26 21:05:22 -08001013 this.neighbourResolutionService.registerNeighbourHandler(
1014 portToRegister,
1015 neighbourHandler,
1016 appId
1017 );
1018 }
1019
Charles Chan72f556a2015-10-05 17:50:33 -07001020 private class InternalConfigListener implements NetworkConfigListener {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001021 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001022
Charles Chanb7f75ac2016-01-11 18:28:54 -08001023 /**
1024 * Constructs the internal network config listener.
1025 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001026 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001027 */
Charles Chan46fdfaf2016-11-09 20:51:44 -08001028 public InternalConfigListener(SegmentRoutingManager srManager) {
1029 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001030 }
1031
Charles Chanb7f75ac2016-01-11 18:28:54 -08001032 /**
1033 * Reads network config and initializes related data structure accordingly.
1034 */
Charles Chane7c61022015-10-07 14:21:45 -07001035 public void configureNetwork() {
Pier Ventreb6a7f342016-11-26 21:05:22 -08001036
Charles Chan46fdfaf2016-11-09 20:51:44 -08001037 deviceConfiguration = new DeviceConfiguration(srManager);
Charles Chane7c61022015-10-07 14:21:45 -07001038
Charles Chan46fdfaf2016-11-09 20:51:44 -08001039 arpHandler = new ArpHandler(srManager);
1040 icmpHandler = new IcmpHandler(srManager);
1041 ipHandler = new IpHandler(srManager);
1042 routingRulePopulator = new RoutingRulePopulator(srManager);
1043 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chane7c61022015-10-07 14:21:45 -07001044
1045 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1046 groupHandlerMap, tunnelStore);
1047 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1048 flowObjectiveService,
1049 tunnelHandler, policyStore);
1050
Charles Chane7c61022015-10-07 14:21:45 -07001051 for (Device device : deviceService.getDevices()) {
Charles Chan72779502016-04-23 17:36:10 -07001052 processDeviceAddedInternal(device.id());
Charles Chane7c61022015-10-07 14:21:45 -07001053 }
1054
1055 defaultRoutingHandler.startPopulationProcess();
Charles Chan72779502016-04-23 17:36:10 -07001056 mcastHandler.init();
Charles Chane7c61022015-10-07 14:21:45 -07001057 }
1058
Charles Chan72f556a2015-10-05 17:50:33 -07001059 @Override
1060 public void event(NetworkConfigEvent event) {
Charles Chan82ab1932016-01-30 23:22:37 -08001061 // TODO move this part to NetworkConfigEventHandler
1062 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1063 switch (event.type()) {
1064 case CONFIG_ADDED:
1065 log.info("Segment Routing Config added.");
1066 configureNetwork();
1067 break;
1068 case CONFIG_UPDATED:
1069 log.info("Segment Routing Config updated.");
1070 // TODO support dynamic configuration
1071 break;
1072 default:
1073 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001074 }
Charles Chan82ab1932016-01-30 23:22:37 -08001075 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chan82f19972016-05-17 13:13:55 -07001076 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan82ab1932016-01-30 23:22:37 -08001077 switch (event.type()) {
1078 case CONFIG_ADDED:
Charles Chan82f19972016-05-17 13:13:55 -07001079 appCfgHandler.processAppConfigAdded(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001080 break;
1081 case CONFIG_UPDATED:
Charles Chan82f19972016-05-17 13:13:55 -07001082 appCfgHandler.processAppConfigUpdated(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001083 break;
1084 case CONFIG_REMOVED:
Charles Chan82f19972016-05-17 13:13:55 -07001085 appCfgHandler.processAppConfigRemoved(event);
1086 break;
1087 default:
1088 break;
1089 }
Charles Chandebfea32016-10-24 14:52:01 -07001090 configureNetwork();
Charles Chan82f19972016-05-17 13:13:55 -07001091 } else if (event.configClass().equals(XConnectConfig.class)) {
1092 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1093 switch (event.type()) {
1094 case CONFIG_ADDED:
1095 xConnectHandler.processXConnectConfigAdded(event);
1096 break;
1097 case CONFIG_UPDATED:
1098 xConnectHandler.processXConnectConfigUpdated(event);
1099 break;
1100 case CONFIG_REMOVED:
1101 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001102 break;
1103 default:
1104 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001105 }
Pier Luigi37a35432017-02-01 13:50:04 -08001106 // XXX Neighbour hacking. This method is looking for
1107 // the Internet-Router interface. In order to retrieve
1108 // the upstream port.
1109 } else if (event.configClass().equals(InterfaceConfig.class)) {
1110 switch (event.type()) {
1111 case CONFIG_ADDED:
1112 case CONFIG_UPDATED:
1113 icmpHandler.updateUPstreamCP(event);
1114 case CONFIG_REGISTERED:
1115 case CONFIG_UNREGISTERED:
1116 case CONFIG_REMOVED:
1117 break;
1118 default:
1119 break;
1120 }
1121 // XXX Neighbour hacking. This method is looking for
1122 // the vrouter port.
1123 } else if (event.configClass().equals(RouterConfig.class)) {
1124 switch (event.type()) {
1125 case CONFIG_ADDED:
1126 case CONFIG_UPDATED:
1127 icmpHandler.updateVRouterCP(event);
1128 case CONFIG_REGISTERED:
1129 case CONFIG_UNREGISTERED:
1130 case CONFIG_REMOVED:
1131 break;
1132 default:
1133 break;
1134 }
Charles Chan72f556a2015-10-05 17:50:33 -07001135 }
1136 }
1137 }
Charles Chanf4586112015-11-09 16:37:23 -08001138
1139 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001140 @Override
1141 public void event(HostEvent event) {
1142 // Do not proceed without mastership
1143 DeviceId deviceId = event.subject().location().deviceId();
1144 if (!mastershipService.isLocalMaster(deviceId)) {
1145 return;
1146 }
1147
1148 switch (event.type()) {
1149 case HOST_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001150 hostHandler.processHostAddedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001151 break;
1152 case HOST_MOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001153 hostHandler.processHostMovedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001154 break;
1155 case HOST_REMOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001156 hostHandler.processHostRemoveEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001157 break;
1158 case HOST_UPDATED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001159 hostHandler.processHostUpdatedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001160 break;
1161 default:
1162 log.warn("Unsupported host event type: {}", event.type());
1163 break;
1164 }
1165 }
1166 }
1167
Charles Chanc91c8782016-03-30 17:54:24 -07001168 private class InternalMcastListener implements McastListener {
1169 @Override
1170 public void event(McastEvent event) {
1171 switch (event.type()) {
1172 case SOURCE_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001173 mcastHandler.processSourceAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001174 break;
1175 case SINK_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001176 mcastHandler.processSinkAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001177 break;
1178 case SINK_REMOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001179 mcastHandler.processSinkRemoved(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001180 break;
1181 case ROUTE_ADDED:
1182 case ROUTE_REMOVED:
1183 default:
1184 break;
1185 }
1186 }
1187 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001188
1189 private class InternalCordConfigListener implements CordConfigListener {
1190 @Override
1191 public void event(CordConfigEvent event) {
1192 switch (event.type()) {
1193 case ACCESS_AGENT_ADDED:
1194 cordConfigHandler.processAccessAgentAddedEvent(event);
1195 break;
1196 case ACCESS_AGENT_UPDATED:
1197 cordConfigHandler.processAccessAgentUpdatedEvent(event);
1198 break;
1199 case ACCESS_AGENT_REMOVED:
1200 cordConfigHandler.processAccessAgentRemovedEvent(event);
1201 break;
1202 case ACCESS_DEVICE_ADDED:
1203 case ACCESS_DEVICE_UPDATED:
1204 case ACCESS_DEVICE_REMOVED:
1205 default:
1206 break;
1207 }
1208 }
1209 }
Charles Chandebfea32016-10-24 14:52:01 -07001210
1211 private class InternalRouteEventListener implements RouteListener {
1212 @Override
1213 public void event(RouteEvent event) {
1214 switch (event.type()) {
1215 case ROUTE_ADDED:
1216 routeHandler.processRouteAdded(event);
1217 break;
1218 case ROUTE_UPDATED:
1219 routeHandler.processRouteUpdated(event);
1220 break;
1221 case ROUTE_REMOVED:
1222 routeHandler.processRouteRemoved(event);
1223 break;
1224 default:
1225 break;
1226 }
1227 }
1228 }
sangho80f11cb2015-04-01 13:05:26 -07001229}