blob: 07fc120e2b6f5eab1b6ecc4e1772286c555e8787 [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sanghob35a6192015-04-01 13:05:26 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.segmentrouting;
17
Charles Chanc81c45b2016-10-20 17:02:44 -070018import com.google.common.collect.Maps;
sanghob35a6192015-04-01 13:05:26 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho1e575652015-05-14 00:39:53 -070024import org.apache.felix.scr.annotations.Service;
sanghob35a6192015-04-01 13:05:26 -070025import org.onlab.packet.Ethernet;
Charles Chanc42e84e2015-10-20 16:24:19 -070026import org.onlab.packet.IPv4;
Charles Chanc42e84e2015-10-20 16:24:19 -070027import org.onlab.packet.Ip4Prefix;
Charles Chanc42e84e2015-10-20 16:24:19 -070028import org.onlab.packet.IpPrefix;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070029import org.onlab.packet.VlanId;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070030import org.onlab.util.KryoNamespace;
Saurav Das80980c72016-03-23 11:22:49 -070031import org.onosproject.cfg.ComponentConfigService;
sanghob35a6192015-04-01 13:05:26 -070032import org.onosproject.core.ApplicationId;
33import org.onosproject.core.CoreService;
34import org.onosproject.event.Event;
Charles Chand55e84d2016-03-30 17:54:24 -070035import org.onosproject.incubator.net.config.basics.McastConfig;
Charles Chan2c15aca2016-11-09 20:51:44 -080036import org.onosproject.incubator.net.intf.InterfaceService;
Charles Chan03a73e02016-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 Hartc19f7c12016-04-12 15:39:44 -070040import org.onosproject.mastership.MastershipService;
Jonathan Hartc19f7c12016-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 Chan68aa62d2015-11-09 16:37:23 -080045import org.onosproject.net.PortNumber;
Charles Chand6832882015-10-05 17:50:33 -070046import org.onosproject.net.config.ConfigFactory;
47import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070048import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070049import org.onosproject.net.config.NetworkConfigRegistry;
Charles Chand6832882015-10-05 17:50:33 -070050import org.onosproject.net.config.basics.SubjectFactories;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070051import org.onosproject.net.device.DeviceEvent;
52import org.onosproject.net.device.DeviceListener;
53import org.onosproject.net.device.DeviceService;
Charles Chan68aa62d2015-11-09 16:37:23 -080054import org.onosproject.net.flow.TrafficSelector;
55import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070056import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan68aa62d2015-11-09 16:37:23 -080057import org.onosproject.net.host.HostEvent;
58import org.onosproject.net.host.HostListener;
Charles Chand55e84d2016-03-30 17:54:24 -070059import org.onosproject.net.mcast.McastEvent;
60import org.onosproject.net.mcast.McastListener;
61import org.onosproject.net.mcast.MulticastRouteService;
Charles Chand55e84d2016-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 Chanfc5c7802016-05-17 13:13:55 -070067import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -070068import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
69import org.onosproject.segmentrouting.grouphandler.NeighborSet;
Charles Chand2990362016-04-18 13:44:03 -070070import org.onosproject.segmentrouting.storekey.NeighborSetNextObjectiveStoreKey;
71import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
sanghob35a6192015-04-01 13:05:26 -070072import org.onosproject.net.host.HostService;
sanghob35a6192015-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 Chand2990362016-04-18 13:44:03 -070080import org.onosproject.segmentrouting.storekey.SubnetAssignedVidStoreKey;
81import org.onosproject.segmentrouting.storekey.SubnetNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -070082import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070083import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-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 Chan35fd1a72016-06-13 18:54:31 -070088import org.opencord.cordconfig.CordConfigEvent;
89import org.opencord.cordconfig.CordConfigListener;
90import org.opencord.cordconfig.CordConfigService;
sanghob35a6192015-04-01 13:05:26 -070091import org.slf4j.Logger;
92import org.slf4j.LoggerFactory;
93
Saurav Das0e99e2b2015-10-28 12:39:42 -070094import java.util.Collections;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070095import java.util.HashSet;
sangho1e575652015-05-14 00:39:53 -070096import java.util.List;
sanghob35a6192015-04-01 13:05:26 -070097import java.util.Map;
Saurav Das0e99e2b2015-10-28 12:39:42 -070098import java.util.Set;
sanghob35a6192015-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 Chan3e783d02016-02-26 22:19:52 -0800106import static com.google.common.base.Preconditions.checkState;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700107import static org.onlab.util.Tools.groupedThreads;
Charles Chan3e783d02016-02-26 22:19:52 -0800108
Charles Chane849c192016-01-11 18:28:54 -0800109/**
110 * Segment routing manager.
111 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700112@Service
113@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700114public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700115
Charles Chan2c15aca2016-11-09 20:51:44 -0800116 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
sanghob35a6192015-04-01 13:05:26 -0700117
118 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700119 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700120
121 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700122 CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700123
124 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700125 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700126
127 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700128 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700129
130 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700131 DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700132
133 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700134 FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700135
136 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700137 LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700138
Charles Chan5270ed02016-01-30 23:22:37 -0800139 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-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 Chan5270ed02016-01-30 23:22:37 -0800156
157 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800158 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800159
Saurav Das80980c72016-03-23 11:22:49 -0700160 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800161 public InterfaceService interfaceService;
162
Charles Chan03a73e02016-10-24 14:52:01 -0700163 ArpHandler arpHandler = null;
164 IcmpHandler icmpHandler = null;
165 IpHandler ipHandler = null;
166 RoutingRulePopulator routingRulePopulator = null;
Charles Chan2c15aca2016-11-09 20:51:44 -0800167 public ApplicationId appId;
sangho666cd6d2015-04-14 16:27:13 -0700168 protected DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700169
Charles Chan03a73e02016-10-24 14:52:01 -0700170 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700171 private TunnelHandler tunnelHandler = null;
172 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700173 private InternalPacketProcessor processor = null;
174 private InternalLinkListener linkListener = null;
175 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700176 private AppConfigHandler appCfgHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700177 XConnectHandler xConnectHandler = null;
Charles Chand2990362016-04-18 13:44:03 -0700178 private McastHandler mcastHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700179 HostHandler hostHandler = null;
Charles Chan35fd1a72016-06-13 18:54:31 -0700180 private CordConfigHandler cordConfigHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700181 RouteHandler routeHandler = null;
sanghob35a6192015-04-01 13:05:26 -0700182 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan5270ed02016-01-30 23:22:37 -0800183 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700184 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
185 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan35fd1a72016-06-13 18:54:31 -0700186 private final InternalCordConfigListener cordConfigListener = new InternalCordConfigListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700187 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sanghob35a6192015-04-01 13:05:26 -0700188
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700189 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700190 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sanghob35a6192015-04-01 13:05:26 -0700191
Saurav Das4ce45962015-11-24 23:21:05 -0800192 @SuppressWarnings("unused")
sanghob35a6192015-04-01 13:05:26 -0700193 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800194 @SuppressWarnings("rawtypes")
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700195 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Charles Chan68aa62d2015-11-09 16:37:23 -0800196 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chane849c192016-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 Chan68aa62d2015-11-09 16:37:23 -0800202 nsNextObjStore = null;
Charles Chane849c192016-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 Chan68aa62d2015-11-09 16:37:23 -0800207 subnetNextObjStore = null;
Charles Chane849c192016-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 Das4ce45962015-11-24 23:21:05 -0800212 portNextObjStore = null;
Saurav Das0e99e2b2015-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 Chane849c192016-01-11 18:28:54 -0800216 subnetVidStore = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800217 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
218 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700219
Charles Chand55e84d2016-03-30 17:54:24 -0700220 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700221 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
222 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700223 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700224 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800225 public SegmentRoutingDeviceConfig createConfig() {
226 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700227 }
228 };
Charles Chand55e84d2016-03-30 17:54:24 -0700229 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700230 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
231 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700232 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800233 @Override
234 public SegmentRoutingAppConfig createConfig() {
235 return new SegmentRoutingAppConfig();
236 }
237 };
Charles Chanfc5c7802016-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 Chand55e84d2016-03-30 17:54:24 -0700247 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700248 new ConfigFactory<ApplicationId, McastConfig>(
249 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700250 McastConfig.class, "multicast") {
251 @Override
252 public McastConfig createConfig() {
253 return new McastConfig();
254 }
255 };
256
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700257 private Object threadSchedulerLock = new Object();
258 private static int numOfEventsQueued = 0;
259 private static int numOfEventsExecuted = 0;
sanghob35a6192015-04-01 13:05:26 -0700260 private static int numOfHandlerExecution = 0;
261 private static int numOfHandlerScheduled = 0;
262
Charles Chan116188d2016-02-18 14:22:42 -0800263 /**
264 * Segment Routing App ID.
265 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800266 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chane849c192016-01-11 18:28:54 -0800267 /**
268 * The starting value of per-subnet VLAN ID assignment.
269 */
Saurav Das0e99e2b2015-10-28 12:39:42 -0700270 private static final short ASSIGNED_VLAN_START = 4093;
Charles Chane849c192016-01-11 18:28:54 -0800271 /**
272 * The default VLAN ID assigned to the interfaces without subnet config.
273 */
Saurav Das0e99e2b2015-10-28 12:39:42 -0700274 public static final short ASSIGNED_VLAN_NO_SUBNET = 4094;
275
sanghob35a6192015-04-01 13:05:26 -0700276 @Activate
277 protected void activate() {
Charles Chan2c15aca2016-11-09 20:51:44 -0800278 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700279
280 log.debug("Creating EC map nsnextobjectivestore");
281 EventuallyConsistentMapBuilder<NeighborSetNextObjectiveStoreKey, Integer>
282 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700283 nsNextObjStore = nsNextObjMapBuilder
284 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700285 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700286 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700287 .build();
288 log.trace("Current size {}", nsNextObjStore.size());
289
Charles Chanc42e84e2015-10-20 16:24:19 -0700290 log.debug("Creating EC map subnetnextobjectivestore");
291 EventuallyConsistentMapBuilder<SubnetNextObjectiveStoreKey, Integer>
292 subnetNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Charles Chanc42e84e2015-10-20 16:24:19 -0700293 subnetNextObjStore = subnetNextObjMapBuilder
294 .withName("subnetnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700295 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700296 .withTimestampProvider((k, v) -> new WallClockTimestamp())
297 .build();
298
Saurav Das4ce45962015-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 Hartc19f7c12016-04-12 15:39:44 -0700304 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800305 .withTimestampProvider((k, v) -> new WallClockTimestamp())
306 .build();
307
sangho0b2b6d12015-05-20 22:16:38 -0700308 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
309 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700310 tunnelStore = tunnelMapBuilder
311 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700312 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700313 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700314 .build();
315
316 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
317 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700318 policyStore = policyMapBuilder
319 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700320 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700321 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700322 .build();
323
Saurav Das0e99e2b2015-10-28 12:39:42 -0700324 EventuallyConsistentMapBuilder<SubnetAssignedVidStoreKey, VlanId>
325 subnetVidStoreMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das0e99e2b2015-10-28 12:39:42 -0700326 subnetVidStore = subnetVidStoreMapBuilder
327 .withName("subnetvidstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700328 .withSerializer(createSerializer())
Saurav Das0e99e2b2015-10-28 12:39:42 -0700329 .withTimestampProvider((k, v) -> new WallClockTimestamp())
330 .build();
331
Saurav Das80980c72016-03-23 11:22:49 -0700332 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800333 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700334 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800335 "purgeOnDisconnection", "true");
Charles Chan03a73e02016-10-24 14:52:01 -0700336 compCfgService.preSetProperty("org.onosproject.vrouter.Vrouter",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800337 "fibInstalledEnabled", "false");
338 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
339 "requestInterceptsEnabled", "false");
Pier Luigi7e415132017-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 Das80980c72016-03-23 11:22:49 -0700344
Charles Chanb8e10c82015-10-14 11:24:40 -0700345 processor = new InternalPacketProcessor();
346 linkListener = new InternalLinkListener();
347 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700348 appCfgHandler = new AppConfigHandler(this);
349 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700350 mcastHandler = new McastHandler(this);
351 hostHandler = new HostHandler(this);
Charles Chan35fd1a72016-06-13 18:54:31 -0700352 cordConfigHandler = new CordConfigHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700353 routeHandler = new RouteHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700354
Charles Chan3e783d02016-02-26 22:19:52 -0800355 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700356 cfgService.registerConfigFactory(deviceConfigFactory);
357 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700358 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700359 cfgService.registerConfigFactory(mcastConfigFactory);
Charles Chan5270ed02016-01-30 23:22:37 -0800360 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700361 packetService.addProcessor(processor, PacketProcessor.director(2));
362 linkService.addListener(linkListener);
363 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700364 multicastRouteService.addListener(mcastListener);
Charles Chan35fd1a72016-06-13 18:54:31 -0700365 cordConfigService.addListener(cordConfigListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700366
367 cfgListener.configureNetwork();
368
Charles Chan03a73e02016-10-24 14:52:01 -0700369 routeService.addListener(routeListener);
370
sanghob35a6192015-04-01 13:05:26 -0700371 log.info("Started");
372 }
373
Jonathan Hartc19f7c12016-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 Chanfc5c7802016-05-17 13:13:55 -0700387 XConnectStoreKey.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700388 );
389 }
390
sanghob35a6192015-04-01 13:05:26 -0700391 @Deactivate
392 protected void deactivate() {
Charles Chand6832882015-10-05 17:50:33 -0700393 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700394 cfgService.unregisterConfigFactory(deviceConfigFactory);
395 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700396 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700397 cfgService.unregisterConfigFactory(mcastConfigFactory);
Charles Chand6832882015-10-05 17:50:33 -0700398
sanghob35a6192015-04-01 13:05:26 -0700399 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700400 linkService.removeListener(linkListener);
401 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700402 multicastRouteService.removeListener(mcastListener);
Charles Chan35fd1a72016-06-13 18:54:31 -0700403 cordConfigService.removeListener(cordConfigListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700404 routeService.removeListener(routeListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700405
sanghob35a6192015-04-01 13:05:26 -0700406 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700407 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700408 deviceListener = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700409 groupHandlerMap.clear();
410
Charles Chand55e84d2016-03-30 17:54:24 -0700411 nsNextObjStore.destroy();
412 subnetNextObjStore.destroy();
413 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700414 tunnelStore.destroy();
415 policyStore.destroy();
416 subnetVidStore.destroy();
sanghob35a6192015-04-01 13:05:26 -0700417 log.info("Stopped");
418 }
419
sangho1e575652015-05-14 00:39:53 -0700420 @Override
421 public List<Tunnel> getTunnels() {
422 return tunnelHandler.getTunnels();
423 }
424
425 @Override
sangho71abe1b2015-06-29 14:58:47 -0700426 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
427 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700428 }
429
430 @Override
sangho71abe1b2015-06-29 14:58:47 -0700431 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-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");
sangho71abe1b2015-06-29 14:58:47 -0700437 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700438 }
439 }
440 }
sangho71abe1b2015-06-29 14:58:47 -0700441 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700442 }
443
444 @Override
sangho71abe1b2015-06-29 14:58:47 -0700445 public PolicyHandler.Result removePolicy(Policy policy) {
446 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700447 }
448
449 @Override
sangho71abe1b2015-06-29 14:58:47 -0700450 public PolicyHandler.Result createPolicy(Policy policy) {
451 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700452 }
453
454 @Override
455 public List<Policy> getPolicies() {
456 return policyHandler.getPolicies();
457 }
458
Saurav Das59232cf2016-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 Chanc81c45b2016-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
sanghof9d0bf12015-05-19 11:57:42 -0700477 /**
Pier Ventre98161782016-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 /**
sanghof9d0bf12015-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 */
sangho1e575652015-05-14 00:39:53 -0700494 public Tunnel getTunnel(String tunnelId) {
495 return tunnelHandler.getTunnel(tunnelId);
496 }
497
sanghob35a6192015-04-01 13:05:26 -0700498 /**
Saurav Das0e99e2b2015-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.
sanghob35a6192015-04-01 13:05:26 -0700511 *
Saurav Das0e99e2b2015-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.
sanghob35a6192015-04-01 13:05:26 -0700517 */
Charles Chane849c192016-01-11 18:28:54 -0800518 // TODO: We should avoid assigning VLAN IDs that are used by VLAN cross-connection.
Saurav Das0e99e2b2015-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 Chan9f676b62015-10-29 14:58:10 -0700534 Set<Ip4Prefix> configuredSubnets = deviceConfiguration.getSubnets(deviceId);
Saurav Das0e99e2b2015-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 Chan5270ed02016-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 }
sanghob35a6192015-04-01 13:05:26 -0700562 }
563
Saurav Das0e99e2b2015-10-28 12:39:42 -0700564 return subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId, subnet));
sanghob35a6192015-04-01 13:05:26 -0700565 }
566
sangho1e575652015-05-14 00:39:53 -0700567 /**
Saurav Das0e99e2b2015-10-28 12:39:42 -0700568 * Returns the next objective ID for the given NeighborSet.
Saurav Das8a0732e2015-11-20 15:27:53 -0800569 * If the nextObjective does not exist, a new one is created and
Saurav Das4ce45962015-11-24 23:21:05 -0800570 * its id is returned.
sangho1e575652015-05-14 00:39:53 -0700571 *
sanghof9d0bf12015-05-19 11:57:42 -0700572 * @param deviceId Device ID
573 * @param ns NegighborSet
Saurav Das8a0732e2015-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
sangho1e575652015-05-14 00:39:53 -0700577 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800578 public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns,
579 TrafficSelector meta) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700580 if (groupHandlerMap.get(deviceId) != null) {
581 log.trace("getNextObjectiveId query in device {}", deviceId);
582 return groupHandlerMap
Saurav Das8a0732e2015-11-20 15:27:53 -0800583 .get(deviceId).getNextObjectiveId(ns, meta);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700584 } else {
Saurav Das4ce45962015-11-24 23:21:05 -0800585 log.warn("getNextObjectiveId query - groupHandler for device {} "
586 + "not found", deviceId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700587 return -1;
588 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700589 }
590
Charles Chanc42e84e2015-10-20 16:24:19 -0700591 /**
Saurav Das4ce45962015-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 Chanc42e84e2015-10-20 16:24:19 -0700594 *
595 * @param deviceId Device ID
596 * @param prefix Subnet
Saurav Das4ce45962015-11-24 23:21:05 -0800597 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-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 Das4ce45962015-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 Das59232cf2016-04-27 18:35:50 -0700621 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-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 Chane849c192016-01-11 18:28:54 -0800630 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
631 + " not found", deviceId);
632 return -1;
633 }
634 }
635
sanghob35a6192015-04-01 13:05:26 -0700636 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-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 Das4ce45962015-11-24 23:21:05 -0800646 log.trace("Rcvd pktin: {}", ethernet);
sanghob35a6192015-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 Vavilapallif5b234a2015-04-21 13:04:13 -0700664 if (event.type() == LinkEvent.Type.LINK_ADDED
665 || event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700666 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700667 scheduleEventHandlerIfNotScheduled(event);
668 }
669 }
670 }
671
672 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -0700673 @Override
674 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -0700675 switch (event.type()) {
676 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -0800677 case PORT_UPDATED:
678 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -0700679 case DEVICE_UPDATED:
680 case DEVICE_AVAILABILITY_CHANGED:
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700681 log.debug("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700682 scheduleEventHandlerIfNotScheduled(event);
683 break;
684 default:
685 }
686 }
687 }
688
Saurav Das4ce45962015-11-24 23:21:05 -0800689 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -0700690 private void scheduleEventHandlerIfNotScheduled(Event event) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700691 synchronized (threadSchedulerLock) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700692 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-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 Vavilapalli23181912015-05-04 09:48:09 -0700697 eventHandlerFuture = executorService
698 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
699 numOfHandlerScheduled++;
700 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700701 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700702 numOfEventsQueued,
703 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -0700704 }
sanghob35a6192015-04-01 13:05:26 -0700705 }
706
707 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700708 @Override
sanghob35a6192015-04-01 13:05:26 -0700709 public void run() {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700710 try {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700711 while (true) {
Saurav Das4ce45962015-11-24 23:21:05 -0800712 @SuppressWarnings("rawtypes")
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700713 Event event = null;
714 synchronized (threadSchedulerLock) {
715 if (!eventQueue.isEmpty()) {
716 event = eventQueue.poll();
717 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700718 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700719 numOfHandlerExecution++;
720 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
721 numOfHandlerExecution, numOfEventsExecuted);
722 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700723 }
sangho20eff1d2015-04-13 15:15:58 -0700724 }
Srikanth Vavilapalli5428b6c2015-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 Ventre2c515312016-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 Vavilapalli5428b6c2015-05-14 20:22:47 -0700737 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-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 Das423fe2b2015-12-04 10:52:59 -0800741 DeviceId deviceId = ((Device) event.subject()).id();
742 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -0700743 log.info("Processing device event {} for available device {}",
744 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700745 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-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 Das1a129a02016-11-18 15:21:57 -0800751 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-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 Das1a129a02016-11-18 15:21:57 -0800756 log.debug("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800757 ((DeviceEvent) event).subject().id(),
758 ((DeviceEvent) event).port().number(),
759 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -0800760 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800761 // these calls happen for every subsequent event
762 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-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 Vavilapalli5428b6c2015-05-14 20:22:47 -0700769 } else {
770 log.warn("Unhandled event type: {}", event.type());
771 }
sanghob35a6192015-04-01 13:05:26 -0700772 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700773 } catch (Exception e) {
774 log.error("SegmentRouting event handler "
775 + "thread thrown an exception: {}", e);
sanghob35a6192015-04-01 13:05:26 -0700776 }
sanghob35a6192015-04-01 13:05:26 -0700777 }
778 }
779
sanghob35a6192015-04-01 13:05:26 -0700780 private void processLinkAdded(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700781 log.info("** LINK ADDED {}", link.toString());
Charles Chan0b4e6182015-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 Vavilapalli23181912015-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 Das8a0732e2015-11-20 15:27:53 -0800794 groupHandler.linkUp(link, mastershipService.isLocalMaster(
795 link.src().deviceId()));
Srikanth Vavilapalli23181912015-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 Das8a0732e2015-11-20 15:27:53 -0800805 groupHandler.linkUp(link, mastershipService.isLocalMaster(device.id()));
sanghob35a6192015-04-01 13:05:26 -0700806 }
807 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700808
Srikanth Vavilapalli5428b6c2015-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 Chan2199c302016-04-23 17:36:10 -0700813
814 mcastHandler.init();
sanghob35a6192015-04-01 13:05:26 -0700815 }
816
817 private void processLinkRemoved(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700818 log.info("** LINK REMOVED {}", link.toString());
sangho834e4b02015-05-01 09:38:25 -0700819 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
820 if (groupHandler != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800821 groupHandler.portDown(link.src().port(),
822 mastershipService.isLocalMaster(link.src().deviceId()));
sangho834e4b02015-05-01 09:38:25 -0700823 }
Srikanth Vavilapalli5428b6c2015-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 Chan2199c302016-04-23 17:36:10 -0700828
829 mcastHandler.processLinkDown(link);
sanghob35a6192015-04-01 13:05:26 -0700830 }
831
832 private void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700833 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan0b4e6182015-11-03 10:42:14 -0800834 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das2857f382015-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 Chan2199c302016-04-23 17:36:10 -0700839 processDeviceAddedInternal(device.id());
840 }
841
842 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-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 Chan2199c302016-04-23 17:36:10 -0700849 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
850 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800851 DefaultGroupHandler groupHandler;
852 try {
853 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -0700854 createGroupHandler(deviceId,
855 appId,
856 deviceConfiguration,
857 linkService,
858 flowObjectiveService,
859 this);
Charles Chan0b4e6182015-11-03 10:42:14 -0800860 } catch (DeviceConfigNotFoundException e) {
861 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
862 return;
863 }
Charles Chan2199c302016-04-23 17:36:10 -0700864 log.debug("updating groupHandlerMap with new config for device: {}",
865 deviceId);
866 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -0800867 }
Saurav Dasb5c236e2016-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 Chan2199c302016-04-23 17:36:10 -0700874 if (mastershipService.isLocalMaster(deviceId)) {
Charles Chan03a73e02016-10-24 14:52:01 -0700875 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -0700876 xConnectHandler.init(deviceId);
Charles Chan35fd1a72016-06-13 18:54:31 -0700877 cordConfigHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -0700878 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700879 groupHandler.createGroupsFromSubnetConfig();
Charles Chan2199c302016-04-23 17:36:10 -0700880 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700881 }
Charles Chan5270ed02016-01-30 23:22:37 -0800882
Charles Chan03a73e02016-10-24 14:52:01 -0700883 appCfgHandler.init(deviceId);
884 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -0700885 }
886
Saurav Das80980c72016-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 Das80980c72016-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 Das80980c72016-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 Das80980c72016-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 Das80980c72016-03-23 11:22:49 -0700908 groupHandlerMap.remove(device.id());
Saurav Das80980c72016-03-23 11:22:49 -0700909 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan2199c302016-04-23 17:36:10 -0700910 mcastHandler.removeDevice(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -0700911 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -0700912 }
913
Saurav Das1a129a02016-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 Vavilapallif5b234a2015-04-21 13:04:13 -0700951 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -0700952 if (groupHandler != null) {
Saurav Das1a129a02016-11-18 15:21:57 -0800953 groupHandler.processEdgePort(port.number(), subnet, portUp,
Saurav Das423fe2b2015-12-04 10:52:59 -0800954 mastershipService.isLocalMaster(device.id()));
Saurav Das1a129a02016-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());
sanghob35a6192015-04-01 13:05:26 -0700959 }
960 }
sangho1e575652015-05-14 00:39:53 -0700961
Charles Chand6832882015-10-05 17:50:33 -0700962 private class InternalConfigListener implements NetworkConfigListener {
Charles Chan2c15aca2016-11-09 20:51:44 -0800963 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -0700964
Charles Chane849c192016-01-11 18:28:54 -0800965 /**
966 * Constructs the internal network config listener.
967 *
Charles Chan2c15aca2016-11-09 20:51:44 -0800968 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -0800969 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800970 public InternalConfigListener(SegmentRoutingManager srManager) {
971 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -0700972 }
973
Charles Chane849c192016-01-11 18:28:54 -0800974 /**
975 * Reads network config and initializes related data structure accordingly.
976 */
Charles Chan4636be02015-10-07 14:21:45 -0700977 public void configureNetwork() {
Charles Chan2c15aca2016-11-09 20:51:44 -0800978 deviceConfiguration = new DeviceConfiguration(srManager);
Charles Chan4636be02015-10-07 14:21:45 -0700979
Charles Chan2c15aca2016-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 Chan4636be02015-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 Chan4636be02015-10-07 14:21:45 -0700992 for (Device device : deviceService.getDevices()) {
Charles Chan2199c302016-04-23 17:36:10 -0700993 processDeviceAddedInternal(device.id());
Charles Chan4636be02015-10-07 14:21:45 -0700994 }
995
996 defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -0700997 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -0700998 }
999
Charles Chand6832882015-10-05 17:50:33 -07001000 @Override
1001 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-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 Chanb8e10c82015-10-14 11:24:40 -07001015 }
Charles Chan5270ed02016-01-30 23:22:37 -08001016 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001017 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001018 switch (event.type()) {
1019 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001020 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001021 break;
1022 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001023 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001024 break;
1025 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001026 appCfgHandler.processAppConfigRemoved(event);
1027 break;
1028 default:
1029 break;
1030 }
Charles Chan03a73e02016-10-24 14:52:01 -07001031 configureNetwork();
Charles Chanfc5c7802016-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 Chan5270ed02016-01-30 23:22:37 -08001043 break;
1044 default:
1045 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001046 }
Charles Chand6832882015-10-05 17:50:33 -07001047 }
1048 }
1049 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001050
1051 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-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 Chand2990362016-04-18 13:44:03 -07001062 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001063 break;
1064 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001065 hostHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001066 break;
1067 case HOST_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001068 hostHandler.processHostRemoveEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001069 break;
1070 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001071 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-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 Chand55e84d2016-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 Chand2990362016-04-18 13:44:03 -07001085 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001086 break;
1087 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001088 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001089 break;
1090 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001091 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001092 break;
1093 case ROUTE_ADDED:
1094 case ROUTE_REMOVED:
1095 default:
1096 break;
1097 }
1098 }
1099 }
Charles Chan35fd1a72016-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 Chan03a73e02016-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 }
sanghob35a6192015-04-01 13:05:26 -07001141}