blob: 07fc120e2b6f5eab1b6ecc4e1772286c555e8787 [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;
Charles Chan77277672015-10-20 16:24:19 -070026import org.onlab.packet.IPv4;
Charles Chan77277672015-10-20 16:24:19 -070027import org.onlab.packet.Ip4Prefix;
Charles Chan77277672015-10-20 16:24:19 -070028import org.onlab.packet.IpPrefix;
Jonathan Hart54541d12016-04-12 15:39:44 -070029import org.onlab.packet.VlanId;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070030import org.onlab.util.KryoNamespace;
Saurav Dasc3604f12016-03-23 11:22:49 -070031import org.onosproject.cfg.ComponentConfigService;
sangho80f11cb2015-04-01 13:05:26 -070032import org.onosproject.core.ApplicationId;
33import org.onosproject.core.CoreService;
34import org.onosproject.event.Event;
Charles Chanc91c8782016-03-30 17:54:24 -070035import org.onosproject.incubator.net.config.basics.McastConfig;
Charles Chan46fdfaf2016-11-09 20:51:44 -080036import org.onosproject.incubator.net.intf.InterfaceService;
Charles Chandebfea32016-10-24 14:52:01 -070037import org.onosproject.incubator.net.routing.RouteEvent;
38import org.onosproject.incubator.net.routing.RouteListener;
39import org.onosproject.incubator.net.routing.RouteService;
Jonathan Hart54541d12016-04-12 15:39:44 -070040import org.onosproject.mastership.MastershipService;
Jonathan Hart54541d12016-04-12 15:39:44 -070041import org.onosproject.net.Device;
42import org.onosproject.net.DeviceId;
43import org.onosproject.net.Link;
44import org.onosproject.net.Port;
Charles Chanf4586112015-11-09 16:37:23 -080045import org.onosproject.net.PortNumber;
Charles Chan72f556a2015-10-05 17:50:33 -070046import org.onosproject.net.config.ConfigFactory;
47import org.onosproject.net.config.NetworkConfigEvent;
Charles Chan72f556a2015-10-05 17:50:33 -070048import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070049import org.onosproject.net.config.NetworkConfigRegistry;
Charles Chan72f556a2015-10-05 17:50:33 -070050import org.onosproject.net.config.basics.SubjectFactories;
Jonathan Hart54541d12016-04-12 15:39:44 -070051import org.onosproject.net.device.DeviceEvent;
52import org.onosproject.net.device.DeviceListener;
53import org.onosproject.net.device.DeviceService;
Charles Chanf4586112015-11-09 16:37:23 -080054import org.onosproject.net.flow.TrafficSelector;
55import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hart54541d12016-04-12 15:39:44 -070056import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chanf4586112015-11-09 16:37:23 -080057import org.onosproject.net.host.HostEvent;
58import org.onosproject.net.host.HostListener;
Charles Chanc91c8782016-03-30 17:54:24 -070059import org.onosproject.net.mcast.McastEvent;
60import org.onosproject.net.mcast.McastListener;
61import org.onosproject.net.mcast.MulticastRouteService;
Charles Chanc91c8782016-03-30 17:54:24 -070062import org.onosproject.net.topology.TopologyService;
63import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
64import org.onosproject.segmentrouting.config.DeviceConfiguration;
65import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
66import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Charles Chan82f19972016-05-17 13:13:55 -070067import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chanc91c8782016-03-30 17:54:24 -070068import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
69import org.onosproject.segmentrouting.grouphandler.NeighborSet;
Charles Chan1eaf4802016-04-18 13:44:03 -070070import org.onosproject.segmentrouting.storekey.NeighborSetNextObjectiveStoreKey;
71import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
sangho80f11cb2015-04-01 13:05:26 -070072import org.onosproject.net.host.HostService;
sangho80f11cb2015-04-01 13:05:26 -070073import org.onosproject.net.link.LinkEvent;
74import org.onosproject.net.link.LinkListener;
75import org.onosproject.net.link.LinkService;
76import org.onosproject.net.packet.InboundPacket;
77import org.onosproject.net.packet.PacketContext;
78import org.onosproject.net.packet.PacketProcessor;
79import org.onosproject.net.packet.PacketService;
Charles Chan1eaf4802016-04-18 13:44:03 -070080import org.onosproject.segmentrouting.storekey.SubnetAssignedVidStoreKey;
81import org.onosproject.segmentrouting.storekey.SubnetNextObjectiveStoreKey;
Charles Chan82f19972016-05-17 13:13:55 -070082import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hart54541d12016-04-12 15:39:44 -070083import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070084import org.onosproject.store.service.EventuallyConsistentMap;
85import org.onosproject.store.service.EventuallyConsistentMapBuilder;
86import org.onosproject.store.service.StorageService;
87import org.onosproject.store.service.WallClockTimestamp;
Charles Chan41f5ec02016-06-13 18:54:31 -070088import org.opencord.cordconfig.CordConfigEvent;
89import org.opencord.cordconfig.CordConfigListener;
90import org.opencord.cordconfig.CordConfigService;
sangho80f11cb2015-04-01 13:05:26 -070091import org.slf4j.Logger;
92import org.slf4j.LoggerFactory;
93
Saurav Das7c305372015-10-28 12:39:42 -070094import java.util.Collections;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070095import java.util.HashSet;
sangho27462c62015-05-14 00:39:53 -070096import java.util.List;
sangho80f11cb2015-04-01 13:05:26 -070097import java.util.Map;
Saurav Das7c305372015-10-28 12:39:42 -070098import java.util.Set;
sangho80f11cb2015-04-01 13:05:26 -070099import java.util.concurrent.ConcurrentHashMap;
100import java.util.concurrent.ConcurrentLinkedQueue;
101import java.util.concurrent.Executors;
102import java.util.concurrent.ScheduledExecutorService;
103import java.util.concurrent.ScheduledFuture;
104import java.util.concurrent.TimeUnit;
105
Charles Chand6d25332016-02-26 22:19:52 -0800106import static com.google.common.base.Preconditions.checkState;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700107import static org.onlab.util.Tools.groupedThreads;
Charles Chand6d25332016-02-26 22:19:52 -0800108
Charles Chanb7f75ac2016-01-11 18:28:54 -0800109/**
110 * Segment routing manager.
111 */
Jonathan Hart54541d12016-04-12 15:39:44 -0700112@Service
113@Component(immediate = true)
sangho27462c62015-05-14 00:39:53 -0700114public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700115
Charles Chan46fdfaf2016-11-09 20:51:44 -0800116 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
sangho80f11cb2015-04-01 13:05:26 -0700117
118 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700119 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700120
121 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700122 CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700123
124 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700125 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700126
127 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700128 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700129
130 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700131 DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700132
133 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700134 FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700135
136 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700137 LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700138
Charles Chan82ab1932016-01-30 23:22:37 -0800139 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700140 MastershipService mastershipService;
141
142 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
143 StorageService storageService;
144
145 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
146 MulticastRouteService multicastRouteService;
147
148 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
149 TopologyService topologyService;
150
151 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
152 CordConfigService cordConfigService;
153
154 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
155 RouteService routeService;
Charles Chan82ab1932016-01-30 23:22:37 -0800156
157 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800158 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800159
Saurav Dasc3604f12016-03-23 11:22:49 -0700160 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800161 public InterfaceService interfaceService;
162
Charles Chandebfea32016-10-24 14:52:01 -0700163 ArpHandler arpHandler = null;
164 IcmpHandler icmpHandler = null;
165 IpHandler ipHandler = null;
166 RoutingRulePopulator routingRulePopulator = null;
Charles Chan46fdfaf2016-11-09 20:51:44 -0800167 public ApplicationId appId;
sangho9b169e32015-04-14 16:27:13 -0700168 protected DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700169
Charles Chandebfea32016-10-24 14:52:01 -0700170 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700171 private TunnelHandler tunnelHandler = null;
172 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700173 private InternalPacketProcessor processor = null;
174 private InternalLinkListener linkListener = null;
175 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700176 private AppConfigHandler appCfgHandler = null;
Charles Chandebfea32016-10-24 14:52:01 -0700177 XConnectHandler xConnectHandler = null;
Charles Chan1eaf4802016-04-18 13:44:03 -0700178 private McastHandler mcastHandler = null;
Charles Chandebfea32016-10-24 14:52:01 -0700179 HostHandler hostHandler = null;
Charles Chan41f5ec02016-06-13 18:54:31 -0700180 private CordConfigHandler cordConfigHandler = null;
Charles Chandebfea32016-10-24 14:52:01 -0700181 RouteHandler routeHandler = null;
sangho80f11cb2015-04-01 13:05:26 -0700182 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan82ab1932016-01-30 23:22:37 -0800183 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700184 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
185 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan41f5ec02016-06-13 18:54:31 -0700186 private final InternalCordConfigListener cordConfigListener = new InternalCordConfigListener();
Charles Chandebfea32016-10-24 14:52:01 -0700187 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sangho80f11cb2015-04-01 13:05:26 -0700188
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700189 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700190 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sangho80f11cb2015-04-01 13:05:26 -0700191
Saurav Das2d94d312015-11-24 23:21:05 -0800192 @SuppressWarnings("unused")
sangho80f11cb2015-04-01 13:05:26 -0700193 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das2d94d312015-11-24 23:21:05 -0800194 @SuppressWarnings("rawtypes")
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700195 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Charles Chanf4586112015-11-09 16:37:23 -0800196 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chanb7f75ac2016-01-11 18:28:54 -0800197 new ConcurrentHashMap<>();
198 /**
199 * Per device next objective ID store with (device id + neighbor set) as key.
200 */
201 public EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, Integer>
Charles Chanf4586112015-11-09 16:37:23 -0800202 nsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800203 /**
204 * Per device next objective ID store with (device id + subnet) as key.
205 */
206 public EventuallyConsistentMap<SubnetNextObjectiveStoreKey, Integer>
Charles Chanf4586112015-11-09 16:37:23 -0800207 subnetNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800208 /**
209 * Per device next objective ID store with (device id + port) as key.
210 */
211 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800212 portNextObjStore = null;
Saurav Das7c305372015-10-28 12:39:42 -0700213 // Per device, per-subnet assigned-vlans store, with (device id + subnet
214 // IPv4 prefix) as key
215 private EventuallyConsistentMap<SubnetAssignedVidStoreKey, VlanId>
Charles Chanb7f75ac2016-01-11 18:28:54 -0800216 subnetVidStore = null;
Saurav Das2d94d312015-11-24 23:21:05 -0800217 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
218 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700219
Charles Chanc91c8782016-03-30 17:54:24 -0700220 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700221 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
222 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700223 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700224 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800225 public SegmentRoutingDeviceConfig createConfig() {
226 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700227 }
228 };
Charles Chanc91c8782016-03-30 17:54:24 -0700229 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700230 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
231 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700232 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800233 @Override
234 public SegmentRoutingAppConfig createConfig() {
235 return new SegmentRoutingAppConfig();
236 }
237 };
Charles Chan82f19972016-05-17 13:13:55 -0700238 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
239 new ConfigFactory<ApplicationId, XConnectConfig>(
240 SubjectFactories.APP_SUBJECT_FACTORY,
241 XConnectConfig.class, "xconnect") {
242 @Override
243 public XConnectConfig createConfig() {
244 return new XConnectConfig();
245 }
246 };
Charles Chanc91c8782016-03-30 17:54:24 -0700247 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700248 new ConfigFactory<ApplicationId, McastConfig>(
249 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700250 McastConfig.class, "multicast") {
251 @Override
252 public McastConfig createConfig() {
253 return new McastConfig();
254 }
255 };
256
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700257 private Object threadSchedulerLock = new Object();
258 private static int numOfEventsQueued = 0;
259 private static int numOfEventsExecuted = 0;
sangho80f11cb2015-04-01 13:05:26 -0700260 private static int numOfHandlerExecution = 0;
261 private static int numOfHandlerScheduled = 0;
262
Charles Chan1963f4f2016-02-18 14:22:42 -0800263 /**
264 * Segment Routing App ID.
265 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800266 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chanb7f75ac2016-01-11 18:28:54 -0800267 /**
268 * The starting value of per-subnet VLAN ID assignment.
269 */
Saurav Das7c305372015-10-28 12:39:42 -0700270 private static final short ASSIGNED_VLAN_START = 4093;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800271 /**
272 * The default VLAN ID assigned to the interfaces without subnet config.
273 */
Saurav Das7c305372015-10-28 12:39:42 -0700274 public static final short ASSIGNED_VLAN_NO_SUBNET = 4094;
275
sangho80f11cb2015-04-01 13:05:26 -0700276 @Activate
277 protected void activate() {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800278 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700279
280 log.debug("Creating EC map nsnextobjectivestore");
281 EventuallyConsistentMapBuilder<NeighborSetNextObjectiveStoreKey, Integer>
282 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700283 nsNextObjStore = nsNextObjMapBuilder
284 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700285 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700286 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700287 .build();
288 log.trace("Current size {}", nsNextObjStore.size());
289
Charles Chan77277672015-10-20 16:24:19 -0700290 log.debug("Creating EC map subnetnextobjectivestore");
291 EventuallyConsistentMapBuilder<SubnetNextObjectiveStoreKey, Integer>
292 subnetNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Charles Chan77277672015-10-20 16:24:19 -0700293 subnetNextObjStore = subnetNextObjMapBuilder
294 .withName("subnetnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700295 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700296 .withTimestampProvider((k, v) -> new WallClockTimestamp())
297 .build();
298
Saurav Das2d94d312015-11-24 23:21:05 -0800299 log.debug("Creating EC map subnetnextobjectivestore");
300 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
301 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
302 portNextObjStore = portNextObjMapBuilder
303 .withName("portnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700304 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800305 .withTimestampProvider((k, v) -> new WallClockTimestamp())
306 .build();
307
sangho4a5c42a2015-05-20 22:16:38 -0700308 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
309 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700310 tunnelStore = tunnelMapBuilder
311 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700312 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700313 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700314 .build();
315
316 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
317 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700318 policyStore = policyMapBuilder
319 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700320 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700321 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700322 .build();
323
Saurav Das7c305372015-10-28 12:39:42 -0700324 EventuallyConsistentMapBuilder<SubnetAssignedVidStoreKey, VlanId>
325 subnetVidStoreMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7c305372015-10-28 12:39:42 -0700326 subnetVidStore = subnetVidStoreMapBuilder
327 .withName("subnetvidstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700328 .withSerializer(createSerializer())
Saurav Das7c305372015-10-28 12:39:42 -0700329 .withTimestampProvider((k, v) -> new WallClockTimestamp())
330 .build();
331
Saurav Dasc3604f12016-03-23 11:22:49 -0700332 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800333 "purgeOnDisconnection", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700334 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800335 "purgeOnDisconnection", "true");
Charles Chandebfea32016-10-24 14:52:01 -0700336 compCfgService.preSetProperty("org.onosproject.vrouter.Vrouter",
Pier Luigib9632ba2017-01-12 18:14:58 -0800337 "fibInstalledEnabled", "false");
338 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
339 "requestInterceptsEnabled", "false");
Pier Luigibc976df2017-01-12 22:46:39 -0800340 compCfgService.preSetProperty("org.onosproject.incubator.net.neighbour.impl.NeighbourResolutionManager",
341 "requestInterceptsEnabled", "false");
342 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelay",
343 "arpEnabled", "false");
Saurav Dasc3604f12016-03-23 11:22:49 -0700344
Charles Chan2b078ae2015-10-14 11:24:40 -0700345 processor = new InternalPacketProcessor();
346 linkListener = new InternalLinkListener();
347 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700348 appCfgHandler = new AppConfigHandler(this);
349 xConnectHandler = new XConnectHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700350 mcastHandler = new McastHandler(this);
351 hostHandler = new HostHandler(this);
Charles Chan41f5ec02016-06-13 18:54:31 -0700352 cordConfigHandler = new CordConfigHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700353 routeHandler = new RouteHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700354
Charles Chand6d25332016-02-26 22:19:52 -0800355 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700356 cfgService.registerConfigFactory(deviceConfigFactory);
357 cfgService.registerConfigFactory(appConfigFactory);
Charles Chan82f19972016-05-17 13:13:55 -0700358 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700359 cfgService.registerConfigFactory(mcastConfigFactory);
Charles Chan82ab1932016-01-30 23:22:37 -0800360 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700361 packetService.addProcessor(processor, PacketProcessor.director(2));
362 linkService.addListener(linkListener);
363 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700364 multicastRouteService.addListener(mcastListener);
Charles Chan41f5ec02016-06-13 18:54:31 -0700365 cordConfigService.addListener(cordConfigListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700366
367 cfgListener.configureNetwork();
368
Charles Chandebfea32016-10-24 14:52:01 -0700369 routeService.addListener(routeListener);
370
sangho80f11cb2015-04-01 13:05:26 -0700371 log.info("Started");
372 }
373
Jonathan Hart54541d12016-04-12 15:39:44 -0700374 private KryoNamespace.Builder createSerializer() {
375 return new KryoNamespace.Builder()
376 .register(KryoNamespaces.API)
377 .register(NeighborSetNextObjectiveStoreKey.class,
378 SubnetNextObjectiveStoreKey.class,
379 SubnetAssignedVidStoreKey.class,
380 NeighborSet.class,
381 Tunnel.class,
382 DefaultTunnel.class,
383 Policy.class,
384 TunnelPolicy.class,
385 Policy.Type.class,
386 PortNextObjectiveStoreKey.class,
Charles Chan82f19972016-05-17 13:13:55 -0700387 XConnectStoreKey.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700388 );
389 }
390
sangho80f11cb2015-04-01 13:05:26 -0700391 @Deactivate
392 protected void deactivate() {
Charles Chan72f556a2015-10-05 17:50:33 -0700393 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700394 cfgService.unregisterConfigFactory(deviceConfigFactory);
395 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chandebfea32016-10-24 14:52:01 -0700396 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700397 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chan72f556a2015-10-05 17:50:33 -0700398
sangho80f11cb2015-04-01 13:05:26 -0700399 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700400 linkService.removeListener(linkListener);
401 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700402 multicastRouteService.removeListener(mcastListener);
Charles Chan41f5ec02016-06-13 18:54:31 -0700403 cordConfigService.removeListener(cordConfigListener);
Charles Chandebfea32016-10-24 14:52:01 -0700404 routeService.removeListener(routeListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700405
sangho80f11cb2015-04-01 13:05:26 -0700406 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700407 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700408 deviceListener = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700409 groupHandlerMap.clear();
410
Charles Chanc91c8782016-03-30 17:54:24 -0700411 nsNextObjStore.destroy();
412 subnetNextObjStore.destroy();
413 portNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700414 tunnelStore.destroy();
415 policyStore.destroy();
416 subnetVidStore.destroy();
sangho80f11cb2015-04-01 13:05:26 -0700417 log.info("Stopped");
418 }
419
sangho27462c62015-05-14 00:39:53 -0700420 @Override
421 public List<Tunnel> getTunnels() {
422 return tunnelHandler.getTunnels();
423 }
424
425 @Override
sanghobd812f82015-06-29 14:58:47 -0700426 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
427 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700428 }
429
430 @Override
sanghobd812f82015-06-29 14:58:47 -0700431 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700432 for (Policy policy: policyHandler.getPolicies()) {
433 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
434 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
435 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
436 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700437 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700438 }
439 }
440 }
sanghobd812f82015-06-29 14:58:47 -0700441 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700442 }
443
444 @Override
sanghobd812f82015-06-29 14:58:47 -0700445 public PolicyHandler.Result removePolicy(Policy policy) {
446 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700447 }
448
449 @Override
sanghobd812f82015-06-29 14:58:47 -0700450 public PolicyHandler.Result createPolicy(Policy policy) {
451 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700452 }
453
454 @Override
455 public List<Policy> getPolicies() {
456 return policyHandler.getPolicies();
457 }
458
Saurav Das07c74602016-04-27 18:35:50 -0700459 @Override
460 public void rerouteNetwork() {
461 cfgListener.configureNetwork();
462 for (Device device : deviceService.getDevices()) {
463 defaultRoutingHandler.populatePortAddressingRules(device.id());
464 }
465 defaultRoutingHandler.startPopulationProcess();
466 }
467
Charles Chand7844e52016-10-20 17:02:44 -0700468 @Override
469 public Map<DeviceId, Set<Ip4Prefix>> getDeviceSubnetMap() {
470 Map<DeviceId, Set<Ip4Prefix>> deviceSubnetMap = Maps.newHashMap();
471 deviceService.getAvailableDevices().forEach(device -> {
472 deviceSubnetMap.put(device.id(), deviceConfiguration.getSubnets(device.id()));
473 });
474 return deviceSubnetMap;
475 }
476
sangho80f1f892015-05-19 11:57:42 -0700477 /**
Pier Ventre7a78de22016-10-31 15:00:01 -0700478 * Returns the MPLS-ECMP configuration.
479 *
480 * @return MPLS-ECMP value
481 */
482 public boolean getMplsEcmp() {
483 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
484 .getConfig(this.appId, SegmentRoutingAppConfig.class);
485 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
486 }
487
488 /**
sangho80f1f892015-05-19 11:57:42 -0700489 * Returns the tunnel object with the tunnel ID.
490 *
491 * @param tunnelId Tunnel ID
492 * @return Tunnel reference
493 */
sangho27462c62015-05-14 00:39:53 -0700494 public Tunnel getTunnel(String tunnelId) {
495 return tunnelHandler.getTunnel(tunnelId);
496 }
497
sangho80f11cb2015-04-01 13:05:26 -0700498 /**
Saurav Das7c305372015-10-28 12:39:42 -0700499 * Returns the vlan-id assigned to the subnet configured for a device.
500 * If no vlan-id has been assigned, a new one is assigned out of a pool of ids,
501 * if and only if this controller instance is the master for the device.
502 * <p>
503 * USAGE: The assigned vlans are meant to be applied to untagged packets on those
504 * switches/pipelines that need this functionality. These vids are meant
505 * to be used internally within a switch, and thus need to be unique only
506 * on a switch level. Note that packets never go out on the wire with these
507 * vlans. Currently, vlan ids are assigned from value 4093 down.
508 * Vlan id 4094 expected to be used for all ports that are not assigned subnets.
509 * Vlan id 4095 is reserved and unused. Only a single vlan id is assigned
510 * per subnet.
sangho80f11cb2015-04-01 13:05:26 -0700511 *
Saurav Das7c305372015-10-28 12:39:42 -0700512 * @param deviceId switch dpid
513 * @param subnet IPv4 prefix for which assigned vlan is desired
514 * @return VlanId assigned for the subnet on the device, or
515 * null if no vlan assignment was found and this instance is not
516 * the master for the device.
sangho80f11cb2015-04-01 13:05:26 -0700517 */
Charles Chanb7f75ac2016-01-11 18:28:54 -0800518 // TODO: We should avoid assigning VLAN IDs that are used by VLAN cross-connection.
Saurav Das7c305372015-10-28 12:39:42 -0700519 public VlanId getSubnetAssignedVlanId(DeviceId deviceId, Ip4Prefix subnet) {
520 VlanId assignedVid = subnetVidStore.get(new SubnetAssignedVidStoreKey(
521 deviceId, subnet));
522 if (assignedVid != null) {
523 log.debug("Query for subnet:{} on device:{} returned assigned-vlan "
524 + "{}", subnet, deviceId, assignedVid);
525 return assignedVid;
526 }
527 //check mastership for the right to assign a vlan
528 if (!mastershipService.isLocalMaster(deviceId)) {
529 log.warn("This controller instance is not the master for device {}. "
530 + "Cannot assign vlan-id for subnet {}", deviceId, subnet);
531 return null;
532 }
533 // vlan assignment is expensive but done only once
Charles Chanc6ad7752015-10-29 14:58:10 -0700534 Set<Ip4Prefix> configuredSubnets = deviceConfiguration.getSubnets(deviceId);
Saurav Das7c305372015-10-28 12:39:42 -0700535 Set<Short> assignedVlans = new HashSet<>();
536 Set<Ip4Prefix> unassignedSubnets = new HashSet<>();
537 for (Ip4Prefix sub : configuredSubnets) {
538 VlanId v = subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId,
539 sub));
540 if (v != null) {
541 assignedVlans.add(v.toShort());
542 } else {
543 unassignedSubnets.add(sub);
544 }
545 }
546 short nextAssignedVlan = ASSIGNED_VLAN_START;
547 if (!assignedVlans.isEmpty()) {
548 nextAssignedVlan = (short) (Collections.min(assignedVlans) - 1);
549 }
550 for (Ip4Prefix unsub : unassignedSubnets) {
Charles Chan82ab1932016-01-30 23:22:37 -0800551 // Special case for default route. Assign default VLAN ID to /32 and /0 subnets
552 if (unsub.prefixLength() == IpPrefix.MAX_INET_MASK_LENGTH ||
553 unsub.prefixLength() == 0) {
554 subnetVidStore.put(new SubnetAssignedVidStoreKey(deviceId, unsub),
555 VlanId.vlanId(ASSIGNED_VLAN_NO_SUBNET));
556 } else {
557 subnetVidStore.put(new SubnetAssignedVidStoreKey(deviceId, unsub),
558 VlanId.vlanId(nextAssignedVlan--));
559 log.info("Assigned vlan: {} to subnet: {} on device: {}",
560 nextAssignedVlan + 1, unsub, deviceId);
561 }
sangho80f11cb2015-04-01 13:05:26 -0700562 }
563
Saurav Das7c305372015-10-28 12:39:42 -0700564 return subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId, subnet));
sangho80f11cb2015-04-01 13:05:26 -0700565 }
566
sangho27462c62015-05-14 00:39:53 -0700567 /**
Saurav Das7c305372015-10-28 12:39:42 -0700568 * Returns the next objective ID for the given NeighborSet.
Saurav Das4c35fc42015-11-20 15:27:53 -0800569 * If the nextObjective does not exist, a new one is created and
Saurav Das2d94d312015-11-24 23:21:05 -0800570 * its id is returned.
sangho27462c62015-05-14 00:39:53 -0700571 *
sangho80f1f892015-05-19 11:57:42 -0700572 * @param deviceId Device ID
573 * @param ns NegighborSet
Saurav Das4c35fc42015-11-20 15:27:53 -0800574 * @param meta metadata passed into the creation of a Next Objective
575 * @return next objective ID or -1 if an error was encountered during the
576 * creation of the nextObjective
sangho27462c62015-05-14 00:39:53 -0700577 */
Saurav Das4c35fc42015-11-20 15:27:53 -0800578 public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns,
579 TrafficSelector meta) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700580 if (groupHandlerMap.get(deviceId) != null) {
581 log.trace("getNextObjectiveId query in device {}", deviceId);
582 return groupHandlerMap
Saurav Das4c35fc42015-11-20 15:27:53 -0800583 .get(deviceId).getNextObjectiveId(ns, meta);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700584 } else {
Saurav Das2d94d312015-11-24 23:21:05 -0800585 log.warn("getNextObjectiveId query - groupHandler for device {} "
586 + "not found", deviceId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700587 return -1;
588 }
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700589 }
590
Charles Chan77277672015-10-20 16:24:19 -0700591 /**
Saurav Das2d94d312015-11-24 23:21:05 -0800592 * Returns the next objective ID for the given subnet prefix. It is expected
593 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -0700594 *
595 * @param deviceId Device ID
596 * @param prefix Subnet
Saurav Das2d94d312015-11-24 23:21:05 -0800597 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -0700598 */
599 public int getSubnetNextObjectiveId(DeviceId deviceId, IpPrefix prefix) {
600 if (groupHandlerMap.get(deviceId) != null) {
601 log.trace("getSubnetNextObjectiveId query in device {}", deviceId);
602 return groupHandlerMap
603 .get(deviceId).getSubnetNextObjectiveId(prefix);
604 } else {
Saurav Das2d94d312015-11-24 23:21:05 -0800605 log.warn("getSubnetNextObjectiveId query - groupHandler for "
606 + "device {} not found", deviceId);
607 return -1;
608 }
609 }
610
611 /**
612 * Returns the next objective ID for the given portNumber, given the treatment.
613 * There could be multiple different treatments to the same outport, which
614 * would result in different objectives. If the next object
615 * does not exist, a new one is created and its id is returned.
616 *
617 * @param deviceId Device ID
618 * @param portNum port number on device for which NextObjective is queried
619 * @param treatment the actions to apply on the packets (should include outport)
620 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das07c74602016-04-27 18:35:50 -0700621 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -0800622 */
623 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
624 TrafficTreatment treatment,
625 TrafficSelector meta) {
626 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
627 if (ghdlr != null) {
628 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta);
629 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -0800630 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
631 + " not found", deviceId);
632 return -1;
633 }
634 }
635
sangho80f11cb2015-04-01 13:05:26 -0700636 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -0700637 @Override
638 public void process(PacketContext context) {
639
640 if (context.isHandled()) {
641 return;
642 }
643
644 InboundPacket pkt = context.inPacket();
645 Ethernet ethernet = pkt.parsed();
Saurav Das2d94d312015-11-24 23:21:05 -0800646 log.trace("Rcvd pktin: {}", ethernet);
sangho80f11cb2015-04-01 13:05:26 -0700647 if (ethernet.getEtherType() == Ethernet.TYPE_ARP) {
648 arpHandler.processPacketIn(pkt);
649 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
650 IPv4 ipPacket = (IPv4) ethernet.getPayload();
651 ipHandler.addToPacketBuffer(ipPacket);
652 if (ipPacket.getProtocol() == IPv4.PROTOCOL_ICMP) {
653 icmpHandler.processPacketIn(pkt);
654 } else {
655 ipHandler.processPacketIn(pkt);
656 }
657 }
658 }
659 }
660
661 private class InternalLinkListener implements LinkListener {
662 @Override
663 public void event(LinkEvent event) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700664 if (event.type() == LinkEvent.Type.LINK_ADDED
665 || event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700666 log.debug("Event {} received from Link Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -0700667 scheduleEventHandlerIfNotScheduled(event);
668 }
669 }
670 }
671
672 private class InternalDeviceListener implements DeviceListener {
sangho80f11cb2015-04-01 13:05:26 -0700673 @Override
674 public void event(DeviceEvent event) {
sangho80f11cb2015-04-01 13:05:26 -0700675 switch (event.type()) {
676 case DEVICE_ADDED:
Saurav Dasf0f592d2016-11-18 15:21:57 -0800677 case PORT_UPDATED:
678 case PORT_ADDED:
sanghofb7c7292015-04-13 15:15:58 -0700679 case DEVICE_UPDATED:
680 case DEVICE_AVAILABILITY_CHANGED:
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700681 log.debug("Event {} received from Device Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -0700682 scheduleEventHandlerIfNotScheduled(event);
683 break;
684 default:
685 }
686 }
687 }
688
Saurav Das2d94d312015-11-24 23:21:05 -0800689 @SuppressWarnings("rawtypes")
sangho80f11cb2015-04-01 13:05:26 -0700690 private void scheduleEventHandlerIfNotScheduled(Event event) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700691 synchronized (threadSchedulerLock) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700692 eventQueue.add(event);
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700693 numOfEventsQueued++;
694
695 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
696 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700697 eventHandlerFuture = executorService
698 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
699 numOfHandlerScheduled++;
700 }
Jonathan Hart54541d12016-04-12 15:39:44 -0700701 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700702 numOfEventsQueued,
703 numOfHandlerScheduled);
sangho80f11cb2015-04-01 13:05:26 -0700704 }
sangho80f11cb2015-04-01 13:05:26 -0700705 }
706
707 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700708 @Override
sangho80f11cb2015-04-01 13:05:26 -0700709 public void run() {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700710 try {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700711 while (true) {
Saurav Das2d94d312015-11-24 23:21:05 -0800712 @SuppressWarnings("rawtypes")
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700713 Event event = null;
714 synchronized (threadSchedulerLock) {
715 if (!eventQueue.isEmpty()) {
716 event = eventQueue.poll();
717 numOfEventsExecuted++;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700718 } else {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700719 numOfHandlerExecution++;
720 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
721 numOfHandlerExecution, numOfEventsExecuted);
722 break;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700723 }
sanghofb7c7292015-04-13 15:15:58 -0700724 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700725 if (event.type() == LinkEvent.Type.LINK_ADDED) {
726 processLinkAdded((Link) event.subject());
727 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre6d593892016-09-13 21:33:40 -0700728 Link linkRemoved = (Link) event.subject();
729 if (linkRemoved.src().elementId() instanceof DeviceId &&
730 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
731 continue;
732 }
733 if (linkRemoved.dst().elementId() instanceof DeviceId &&
734 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
735 continue;
736 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700737 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700738 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
739 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
740 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das62af8802015-12-04 10:52:59 -0800741 DeviceId deviceId = ((Device) event.subject()).id();
742 if (deviceService.isAvailable(deviceId)) {
Saurav Dasc28b3432015-10-30 17:45:38 -0700743 log.info("Processing device event {} for available device {}",
744 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700745 processDeviceAdded((Device) event.subject());
Saurav Dasc3604f12016-03-23 11:22:49 -0700746 } else {
747 log.info("Processing device event {} for unavailable device {}",
748 event.type(), ((Device) event.subject()).id());
749 processDeviceRemoved((Device) event.subject());
750 }
Saurav Dasf0f592d2016-11-18 15:21:57 -0800751 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd1872b02016-12-02 15:43:47 -0800752 // typically these calls come when device is added first time
753 // so port filtering rules are handled at the device_added event.
754 // port added calls represent all ports on the device,
755 // enabled or not.
Saurav Dasf0f592d2016-11-18 15:21:57 -0800756 log.debug("** PORT ADDED {}/{} -> {}",
Saurav Dasd1872b02016-12-02 15:43:47 -0800757 ((DeviceEvent) event).subject().id(),
758 ((DeviceEvent) event).port().number(),
759 event.type());
Saurav Dasf0f592d2016-11-18 15:21:57 -0800760 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd1872b02016-12-02 15:43:47 -0800761 // these calls happen for every subsequent event
762 // ports enabled, disabled, switch goes away, comes back
Saurav Dasf0f592d2016-11-18 15:21:57 -0800763 log.info("** PORT UPDATED {}/{} -> {}",
764 event.subject(),
765 ((DeviceEvent) event).port(),
766 event.type());
767 processPortUpdated(((Device) event.subject()),
768 ((DeviceEvent) event).port());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700769 } else {
770 log.warn("Unhandled event type: {}", event.type());
771 }
sangho80f11cb2015-04-01 13:05:26 -0700772 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700773 } catch (Exception e) {
774 log.error("SegmentRouting event handler "
775 + "thread thrown an exception: {}", e);
sangho80f11cb2015-04-01 13:05:26 -0700776 }
sangho80f11cb2015-04-01 13:05:26 -0700777 }
778 }
779
sangho80f11cb2015-04-01 13:05:26 -0700780 private void processLinkAdded(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -0700781 log.info("** LINK ADDED {}", link.toString());
Charles Chan319d1a22015-11-03 10:42:14 -0800782 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
783 log.warn("Source device of this link is not configured.");
784 return;
785 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700786 //Irrespective whether the local is a MASTER or not for this device,
787 //create group handler instance and push default TTP flow rules.
788 //Because in a multi-instance setup, instances can initiate
789 //groups for any devices. Also the default TTP rules are needed
790 //to be pushed before inserting any IP table entries for any device
791 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
792 .deviceId());
793 if (groupHandler != null) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800794 groupHandler.linkUp(link, mastershipService.isLocalMaster(
795 link.src().deviceId()));
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700796 } else {
797 Device device = deviceService.getDevice(link.src().deviceId());
798 if (device != null) {
799 log.warn("processLinkAdded: Link Added "
800 + "Notification without Device Added "
801 + "event, still handling it");
802 processDeviceAdded(device);
803 groupHandler = groupHandlerMap.get(link.src()
804 .deviceId());
Saurav Das4c35fc42015-11-20 15:27:53 -0800805 groupHandler.linkUp(link, mastershipService.isLocalMaster(device.id()));
sangho80f11cb2015-04-01 13:05:26 -0700806 }
807 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700808
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700809 log.trace("Starting optimized route population process");
810 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null);
811 //log.trace("processLinkAdded: re-starting route population process");
812 //defaultRoutingHandler.startPopulationProcess();
Charles Chan72779502016-04-23 17:36:10 -0700813
814 mcastHandler.init();
sangho80f11cb2015-04-01 13:05:26 -0700815 }
816
817 private void processLinkRemoved(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -0700818 log.info("** LINK REMOVED {}", link.toString());
sangho2165d222015-05-01 09:38:25 -0700819 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
820 if (groupHandler != null) {
Saurav Das62af8802015-12-04 10:52:59 -0800821 groupHandler.portDown(link.src().port(),
822 mastershipService.isLocalMaster(link.src().deviceId()));
sangho2165d222015-05-01 09:38:25 -0700823 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700824 log.trace("Starting optimized route population process");
825 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link);
826 //log.trace("processLinkRemoved: re-starting route population process");
827 //defaultRoutingHandler.startPopulationProcess();
Charles Chan72779502016-04-23 17:36:10 -0700828
829 mcastHandler.processLinkDown(link);
sangho80f11cb2015-04-01 13:05:26 -0700830 }
831
832 private void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -0700833 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan319d1a22015-11-03 10:42:14 -0800834 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das8ec0ec42015-11-03 14:39:27 -0800835 log.warn("Device configuration uploading. Device {} will be "
836 + "processed after config completes.", device.id());
837 return;
838 }
Charles Chan72779502016-04-23 17:36:10 -0700839 processDeviceAddedInternal(device.id());
840 }
841
842 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -0700843 // Irrespective of whether the local is a MASTER or not for this device,
844 // we need to create a SR-group-handler instance. This is because in a
845 // multi-instance setup, any instance can initiate forwarding/next-objectives
846 // for any switch (even if this instance is a SLAVE or not even connected
847 // to the switch). To handle this, a default-group-handler instance is necessary
848 // per switch.
Charles Chan72779502016-04-23 17:36:10 -0700849 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
850 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -0800851 DefaultGroupHandler groupHandler;
852 try {
853 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -0700854 createGroupHandler(deviceId,
855 appId,
856 deviceConfiguration,
857 linkService,
858 flowObjectiveService,
859 this);
Charles Chan319d1a22015-11-03 10:42:14 -0800860 } catch (DeviceConfigNotFoundException e) {
861 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
862 return;
863 }
Charles Chan72779502016-04-23 17:36:10 -0700864 log.debug("updating groupHandlerMap with new config for device: {}",
865 deviceId);
866 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -0800867 }
Saurav Dasb149be12016-06-07 10:08:06 -0700868 // Also, in some cases, drivers may need extra
869 // information to process rules (eg. Router IP/MAC); and so, we send
870 // port addressing rules to the driver as well irrespective of whether
871 // this instance is the master or not.
872 defaultRoutingHandler.populatePortAddressingRules(deviceId);
873
Charles Chan72779502016-04-23 17:36:10 -0700874 if (mastershipService.isLocalMaster(deviceId)) {
Charles Chandebfea32016-10-24 14:52:01 -0700875 hostHandler.init(deviceId);
Charles Chan82f19972016-05-17 13:13:55 -0700876 xConnectHandler.init(deviceId);
Charles Chan41f5ec02016-06-13 18:54:31 -0700877 cordConfigHandler.init(deviceId);
Charles Chan72779502016-04-23 17:36:10 -0700878 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan77277672015-10-20 16:24:19 -0700879 groupHandler.createGroupsFromSubnetConfig();
Charles Chan72779502016-04-23 17:36:10 -0700880 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -0700881 }
Charles Chan82ab1932016-01-30 23:22:37 -0800882
Charles Chandebfea32016-10-24 14:52:01 -0700883 appCfgHandler.init(deviceId);
884 routeHandler.init(deviceId);
sangho80f11cb2015-04-01 13:05:26 -0700885 }
886
Saurav Dasc3604f12016-03-23 11:22:49 -0700887 private void processDeviceRemoved(Device device) {
888 nsNextObjStore.entrySet().stream()
889 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
890 .forEach(entry -> {
891 nsNextObjStore.remove(entry.getKey());
892 });
Saurav Dasc3604f12016-03-23 11:22:49 -0700893 subnetNextObjStore.entrySet().stream()
894 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
895 .forEach(entry -> {
896 subnetNextObjStore.remove(entry.getKey());
897 });
Saurav Dasc3604f12016-03-23 11:22:49 -0700898 portNextObjStore.entrySet().stream()
899 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
900 .forEach(entry -> {
901 portNextObjStore.remove(entry.getKey());
902 });
Saurav Dasc3604f12016-03-23 11:22:49 -0700903 subnetVidStore.entrySet().stream()
904 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
905 .forEach(entry -> {
906 subnetVidStore.remove(entry.getKey());
907 });
Saurav Dasc3604f12016-03-23 11:22:49 -0700908 groupHandlerMap.remove(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -0700909 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan72779502016-04-23 17:36:10 -0700910 mcastHandler.removeDevice(device.id());
Charles Chan82f19972016-05-17 13:13:55 -0700911 xConnectHandler.removeDevice(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -0700912 }
913
Saurav Dasf0f592d2016-11-18 15:21:57 -0800914 private void processPortUpdated(Device device, Port port) {
915 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
916 log.warn("Device configuration uploading. Not handling port event for"
917 + "dev: {} port: {}", device.id(), port.number());
918 return;
919 }
920 /* XXX create method for single port filtering rules which are needed
921 for both switch-to-switch ports and edge ports
922 if (defaultRoutingHandler != null) {
923 defaultRoutingHandler.populatePortAddressingRules(
924 ((Device) event.subject()).id());
925 }*/
926
927 // portUpdated calls are for ports that have gone down or up. For switch
928 // to switch ports, link-events should take care of any re-routing or
929 // group editing necessary for port up/down. Here we only process edge ports
930 // that are already configured.
931 Ip4Prefix configuredSubnet = deviceConfiguration.getPortSubnet(device.id(),
932 port.number());
933 if (configuredSubnet == null) {
934 log.debug("Not handling port updated event for unconfigured port "
935 + "dev/port: {}/{}", device.id(), port.number());
936 return;
937 }
938 processEdgePort(device, port, configuredSubnet);
939 }
940
941 private void processEdgePort(Device device, Port port, Ip4Prefix subnet) {
942 boolean portUp = port.isEnabled();
943 if (portUp) {
944 log.info("Device:EdgePort {}:{} is enabled in subnet: {}", device.id(),
945 port.number(), subnet);
946 } else {
947 log.info("Device:EdgePort {}:{} is disabled in subnet: {}", device.id(),
948 port.number(), subnet);
949 }
950
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700951 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sangho80f11cb2015-04-01 13:05:26 -0700952 if (groupHandler != null) {
Saurav Dasf0f592d2016-11-18 15:21:57 -0800953 groupHandler.processEdgePort(port.number(), subnet, portUp,
Saurav Das62af8802015-12-04 10:52:59 -0800954 mastershipService.isLocalMaster(device.id()));
Saurav Dasf0f592d2016-11-18 15:21:57 -0800955 } else {
956 log.warn("Group handler not found for dev:{}. Not handling edge port"
957 + " {} event for port:{}", device.id(),
958 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -0700959 }
960 }
sangho27462c62015-05-14 00:39:53 -0700961
Charles Chan72f556a2015-10-05 17:50:33 -0700962 private class InternalConfigListener implements NetworkConfigListener {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800963 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -0700964
Charles Chanb7f75ac2016-01-11 18:28:54 -0800965 /**
966 * Constructs the internal network config listener.
967 *
Charles Chan46fdfaf2016-11-09 20:51:44 -0800968 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -0800969 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800970 public InternalConfigListener(SegmentRoutingManager srManager) {
971 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -0700972 }
973
Charles Chanb7f75ac2016-01-11 18:28:54 -0800974 /**
975 * Reads network config and initializes related data structure accordingly.
976 */
Charles Chane7c61022015-10-07 14:21:45 -0700977 public void configureNetwork() {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800978 deviceConfiguration = new DeviceConfiguration(srManager);
Charles Chane7c61022015-10-07 14:21:45 -0700979
Charles Chan46fdfaf2016-11-09 20:51:44 -0800980 arpHandler = new ArpHandler(srManager);
981 icmpHandler = new IcmpHandler(srManager);
982 ipHandler = new IpHandler(srManager);
983 routingRulePopulator = new RoutingRulePopulator(srManager);
984 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chane7c61022015-10-07 14:21:45 -0700985
986 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
987 groupHandlerMap, tunnelStore);
988 policyHandler = new PolicyHandler(appId, deviceConfiguration,
989 flowObjectiveService,
990 tunnelHandler, policyStore);
991
Charles Chane7c61022015-10-07 14:21:45 -0700992 for (Device device : deviceService.getDevices()) {
Charles Chan72779502016-04-23 17:36:10 -0700993 processDeviceAddedInternal(device.id());
Charles Chane7c61022015-10-07 14:21:45 -0700994 }
995
996 defaultRoutingHandler.startPopulationProcess();
Charles Chan72779502016-04-23 17:36:10 -0700997 mcastHandler.init();
Charles Chane7c61022015-10-07 14:21:45 -0700998 }
999
Charles Chan72f556a2015-10-05 17:50:33 -07001000 @Override
1001 public void event(NetworkConfigEvent event) {
Charles Chan82ab1932016-01-30 23:22:37 -08001002 // TODO move this part to NetworkConfigEventHandler
1003 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1004 switch (event.type()) {
1005 case CONFIG_ADDED:
1006 log.info("Segment Routing Config added.");
1007 configureNetwork();
1008 break;
1009 case CONFIG_UPDATED:
1010 log.info("Segment Routing Config updated.");
1011 // TODO support dynamic configuration
1012 break;
1013 default:
1014 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001015 }
Charles Chan82ab1932016-01-30 23:22:37 -08001016 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chan82f19972016-05-17 13:13:55 -07001017 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan82ab1932016-01-30 23:22:37 -08001018 switch (event.type()) {
1019 case CONFIG_ADDED:
Charles Chan82f19972016-05-17 13:13:55 -07001020 appCfgHandler.processAppConfigAdded(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001021 break;
1022 case CONFIG_UPDATED:
Charles Chan82f19972016-05-17 13:13:55 -07001023 appCfgHandler.processAppConfigUpdated(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001024 break;
1025 case CONFIG_REMOVED:
Charles Chan82f19972016-05-17 13:13:55 -07001026 appCfgHandler.processAppConfigRemoved(event);
1027 break;
1028 default:
1029 break;
1030 }
Charles Chandebfea32016-10-24 14:52:01 -07001031 configureNetwork();
Charles Chan82f19972016-05-17 13:13:55 -07001032 } else if (event.configClass().equals(XConnectConfig.class)) {
1033 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1034 switch (event.type()) {
1035 case CONFIG_ADDED:
1036 xConnectHandler.processXConnectConfigAdded(event);
1037 break;
1038 case CONFIG_UPDATED:
1039 xConnectHandler.processXConnectConfigUpdated(event);
1040 break;
1041 case CONFIG_REMOVED:
1042 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001043 break;
1044 default:
1045 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001046 }
Charles Chan72f556a2015-10-05 17:50:33 -07001047 }
1048 }
1049 }
Charles Chanf4586112015-11-09 16:37:23 -08001050
1051 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001052 @Override
1053 public void event(HostEvent event) {
1054 // Do not proceed without mastership
1055 DeviceId deviceId = event.subject().location().deviceId();
1056 if (!mastershipService.isLocalMaster(deviceId)) {
1057 return;
1058 }
1059
1060 switch (event.type()) {
1061 case HOST_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001062 hostHandler.processHostAddedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001063 break;
1064 case HOST_MOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001065 hostHandler.processHostMovedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001066 break;
1067 case HOST_REMOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001068 hostHandler.processHostRemoveEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001069 break;
1070 case HOST_UPDATED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001071 hostHandler.processHostUpdatedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001072 break;
1073 default:
1074 log.warn("Unsupported host event type: {}", event.type());
1075 break;
1076 }
1077 }
1078 }
1079
Charles Chanc91c8782016-03-30 17:54:24 -07001080 private class InternalMcastListener implements McastListener {
1081 @Override
1082 public void event(McastEvent event) {
1083 switch (event.type()) {
1084 case SOURCE_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001085 mcastHandler.processSourceAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001086 break;
1087 case SINK_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001088 mcastHandler.processSinkAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001089 break;
1090 case SINK_REMOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001091 mcastHandler.processSinkRemoved(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001092 break;
1093 case ROUTE_ADDED:
1094 case ROUTE_REMOVED:
1095 default:
1096 break;
1097 }
1098 }
1099 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001100
1101 private class InternalCordConfigListener implements CordConfigListener {
1102 @Override
1103 public void event(CordConfigEvent event) {
1104 switch (event.type()) {
1105 case ACCESS_AGENT_ADDED:
1106 cordConfigHandler.processAccessAgentAddedEvent(event);
1107 break;
1108 case ACCESS_AGENT_UPDATED:
1109 cordConfigHandler.processAccessAgentUpdatedEvent(event);
1110 break;
1111 case ACCESS_AGENT_REMOVED:
1112 cordConfigHandler.processAccessAgentRemovedEvent(event);
1113 break;
1114 case ACCESS_DEVICE_ADDED:
1115 case ACCESS_DEVICE_UPDATED:
1116 case ACCESS_DEVICE_REMOVED:
1117 default:
1118 break;
1119 }
1120 }
1121 }
Charles Chandebfea32016-10-24 14:52:01 -07001122
1123 private class InternalRouteEventListener implements RouteListener {
1124 @Override
1125 public void event(RouteEvent event) {
1126 switch (event.type()) {
1127 case ROUTE_ADDED:
1128 routeHandler.processRouteAdded(event);
1129 break;
1130 case ROUTE_UPDATED:
1131 routeHandler.processRouteUpdated(event);
1132 break;
1133 case ROUTE_REMOVED:
1134 routeHandler.processRouteRemoved(event);
1135 break;
1136 default:
1137 break;
1138 }
1139 }
1140 }
sangho80f11cb2015-04-01 13:05:26 -07001141}