blob: 9c761e719f85b55c0d4722b6f75ae0c0ddaf8264 [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sanghob35a6192015-04-01 13:05:26 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.segmentrouting;
17
Charles Chanc81c45b2016-10-20 17:02:44 -070018import com.google.common.collect.Maps;
sanghob35a6192015-04-01 13:05:26 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho1e575652015-05-14 00:39:53 -070024import org.apache.felix.scr.annotations.Service;
sanghob35a6192015-04-01 13:05:26 -070025import org.onlab.packet.Ethernet;
Pier Ventre735b8c82016-12-02 08:16:05 -080026import org.onlab.packet.ICMP6;
Charles Chanc42e84e2015-10-20 16:24:19 -070027import org.onlab.packet.IPv4;
Pier Ventre10bd8d12016-11-26 21:05:22 -080028import org.onlab.packet.IPv6;
Charles Chanc42e84e2015-10-20 16:24:19 -070029import org.onlab.packet.Ip4Prefix;
Charles Chanc42e84e2015-10-20 16:24:19 -070030import org.onlab.packet.IpPrefix;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070031import org.onlab.packet.VlanId;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070032import org.onlab.util.KryoNamespace;
Saurav Das80980c72016-03-23 11:22:49 -070033import org.onosproject.cfg.ComponentConfigService;
sanghob35a6192015-04-01 13:05:26 -070034import org.onosproject.core.ApplicationId;
35import org.onosproject.core.CoreService;
36import org.onosproject.event.Event;
Pier Luigi721b6622017-02-03 13:34:21 -080037import org.onosproject.incubator.net.config.basics.ConfigException;
Pier Luigi7dad71c2017-02-01 13:50:04 -080038import org.onosproject.incubator.net.config.basics.InterfaceConfig;
Charles Chand55e84d2016-03-30 17:54:24 -070039import org.onosproject.incubator.net.config.basics.McastConfig;
Pier Luigi721b6622017-02-03 13:34:21 -080040import org.onosproject.incubator.net.intf.Interface;
Charles Chan2c15aca2016-11-09 20:51:44 -080041import org.onosproject.incubator.net.intf.InterfaceService;
Charles Chan03a73e02016-10-24 14:52:01 -070042import org.onosproject.incubator.net.routing.RouteEvent;
43import org.onosproject.incubator.net.routing.RouteListener;
44import org.onosproject.incubator.net.routing.RouteService;
Pier Ventre735b8c82016-12-02 08:16:05 -080045import org.onosproject.incubator.net.neighbour.NeighbourResolutionService;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070046import org.onosproject.mastership.MastershipService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080047import org.onosproject.net.ConnectPoint;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070048import org.onosproject.net.Device;
49import org.onosproject.net.DeviceId;
50import org.onosproject.net.Link;
51import org.onosproject.net.Port;
Charles Chan68aa62d2015-11-09 16:37:23 -080052import org.onosproject.net.PortNumber;
Charles Chand6832882015-10-05 17:50:33 -070053import org.onosproject.net.config.ConfigFactory;
54import org.onosproject.net.config.NetworkConfigEvent;
Charles Chand6832882015-10-05 17:50:33 -070055import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070056import org.onosproject.net.config.NetworkConfigRegistry;
Charles Chand6832882015-10-05 17:50:33 -070057import org.onosproject.net.config.basics.SubjectFactories;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070058import org.onosproject.net.device.DeviceEvent;
59import org.onosproject.net.device.DeviceListener;
60import org.onosproject.net.device.DeviceService;
Charles Chan68aa62d2015-11-09 16:37:23 -080061import org.onosproject.net.flow.TrafficSelector;
62import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070063import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chan68aa62d2015-11-09 16:37:23 -080064import org.onosproject.net.host.HostEvent;
65import org.onosproject.net.host.HostListener;
Pier Ventre10bd8d12016-11-26 21:05:22 -080066import org.onosproject.net.host.HostService;
67import org.onosproject.net.link.LinkEvent;
68import org.onosproject.net.link.LinkListener;
69import org.onosproject.net.link.LinkService;
Charles Chand55e84d2016-03-30 17:54:24 -070070import org.onosproject.net.mcast.McastEvent;
71import org.onosproject.net.mcast.McastListener;
72import org.onosproject.net.mcast.MulticastRouteService;
Pier Ventre10bd8d12016-11-26 21:05:22 -080073import org.onosproject.net.packet.InboundPacket;
74import org.onosproject.net.packet.PacketContext;
75import org.onosproject.net.packet.PacketProcessor;
76import org.onosproject.net.packet.PacketService;
Pier Ventre42287df2016-11-09 14:17:26 -080077import org.onosproject.net.topology.PathService;
Charles Chand55e84d2016-03-30 17:54:24 -070078import org.onosproject.net.topology.TopologyService;
Pier Luigi7dad71c2017-02-01 13:50:04 -080079import org.onosproject.routing.config.RouterConfig;
Charles Chand55e84d2016-03-30 17:54:24 -070080import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
81import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventref34966c2016-11-07 16:21:04 -080082import org.onosproject.segmentrouting.config.PwaasConfig;
Pier Ventre10bd8d12016-11-26 21:05:22 -080083import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Pier Ventref34966c2016-11-07 16:21:04 -080084import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Charles Chanfc5c7802016-05-17 13:13:55 -070085import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chand55e84d2016-03-30 17:54:24 -070086import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
87import org.onosproject.segmentrouting.grouphandler.NeighborSet;
Charles Chand2990362016-04-18 13:44:03 -070088import org.onosproject.segmentrouting.storekey.NeighborSetNextObjectiveStoreKey;
89import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chand2990362016-04-18 13:44:03 -070090import org.onosproject.segmentrouting.storekey.SubnetAssignedVidStoreKey;
91import org.onosproject.segmentrouting.storekey.SubnetNextObjectiveStoreKey;
Charles Chanfc5c7802016-05-17 13:13:55 -070092import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Pier Ventref34966c2016-11-07 16:21:04 -080093import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Jonathan Hartc19f7c12016-04-12 15:39:44 -070094import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070095import org.onosproject.store.service.EventuallyConsistentMap;
96import org.onosproject.store.service.EventuallyConsistentMapBuilder;
97import org.onosproject.store.service.StorageService;
98import org.onosproject.store.service.WallClockTimestamp;
Charles Chan35fd1a72016-06-13 18:54:31 -070099import org.opencord.cordconfig.CordConfigEvent;
100import org.opencord.cordconfig.CordConfigListener;
101import org.opencord.cordconfig.CordConfigService;
sanghob35a6192015-04-01 13:05:26 -0700102import org.slf4j.Logger;
103import org.slf4j.LoggerFactory;
104
Saurav Das0e99e2b2015-10-28 12:39:42 -0700105import java.util.Collections;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700106import java.util.HashSet;
sangho1e575652015-05-14 00:39:53 -0700107import java.util.List;
sanghob35a6192015-04-01 13:05:26 -0700108import java.util.Map;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700109import java.util.Set;
sanghob35a6192015-04-01 13:05:26 -0700110import java.util.concurrent.ConcurrentHashMap;
111import java.util.concurrent.ConcurrentLinkedQueue;
112import java.util.concurrent.Executors;
113import java.util.concurrent.ScheduledExecutorService;
114import java.util.concurrent.ScheduledFuture;
115import java.util.concurrent.TimeUnit;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800116import java.util.stream.Collectors;
sanghob35a6192015-04-01 13:05:26 -0700117
Charles Chan3e783d02016-02-26 22:19:52 -0800118import static com.google.common.base.Preconditions.checkState;
Pier Ventree0ae7a32016-11-23 09:57:42 -0800119import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700120import static org.onlab.util.Tools.groupedThreads;
Charles Chan3e783d02016-02-26 22:19:52 -0800121
Charles Chane849c192016-01-11 18:28:54 -0800122/**
123 * Segment routing manager.
124 */
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700125@Service
126@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -0700127public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -0700128
Charles Chan2c15aca2016-11-09 20:51:44 -0800129 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
sanghob35a6192015-04-01 13:05:26 -0700130
131 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700132 private ComponentConfigService compCfgService;
sanghob35a6192015-04-01 13:05:26 -0700133
134 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre10bd8d12016-11-26 21:05:22 -0800135 private NeighbourResolutionService neighbourResolutionService;
136
137 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800138 public PathService pathService;
139
140 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700141 CoreService coreService;
sanghob35a6192015-04-01 13:05:26 -0700142
143 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700144 PacketService packetService;
sanghob35a6192015-04-01 13:05:26 -0700145
146 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700147 HostService hostService;
sanghob35a6192015-04-01 13:05:26 -0700148
149 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700150 DeviceService deviceService;
sanghob35a6192015-04-01 13:05:26 -0700151
152 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800153 public FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700154
155 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan03a73e02016-10-24 14:52:01 -0700156 LinkService linkService;
sangho1e575652015-05-14 00:39:53 -0700157
Charles Chan5270ed02016-01-30 23:22:37 -0800158 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800159 public MastershipService mastershipService;
Charles Chan03a73e02016-10-24 14:52:01 -0700160
161 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventre42287df2016-11-09 14:17:26 -0800162 public StorageService storageService;
Charles Chan03a73e02016-10-24 14:52:01 -0700163
164 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
165 MulticastRouteService multicastRouteService;
166
167 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
168 TopologyService topologyService;
169
170 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
171 CordConfigService cordConfigService;
172
173 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
174 RouteService routeService;
Charles Chan5270ed02016-01-30 23:22:37 -0800175
176 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800177 public NetworkConfigRegistry cfgService;
Charles Chan5270ed02016-01-30 23:22:37 -0800178
Saurav Das80980c72016-03-23 11:22:49 -0700179 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan2c15aca2016-11-09 20:51:44 -0800180 public InterfaceService interfaceService;
181
Charles Chan03a73e02016-10-24 14:52:01 -0700182 ArpHandler arpHandler = null;
183 IcmpHandler icmpHandler = null;
184 IpHandler ipHandler = null;
185 RoutingRulePopulator routingRulePopulator = null;
Charles Chan2c15aca2016-11-09 20:51:44 -0800186 public ApplicationId appId;
Pier Ventre42287df2016-11-09 14:17:26 -0800187 public DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700188
Charles Chan03a73e02016-10-24 14:52:01 -0700189 DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700190 private TunnelHandler tunnelHandler = null;
191 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700192 private InternalPacketProcessor processor = null;
193 private InternalLinkListener linkListener = null;
194 private InternalDeviceListener deviceListener = null;
Charles Chanfc5c7802016-05-17 13:13:55 -0700195 private AppConfigHandler appCfgHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700196 XConnectHandler xConnectHandler = null;
Charles Chand2990362016-04-18 13:44:03 -0700197 private McastHandler mcastHandler = null;
Charles Chan03a73e02016-10-24 14:52:01 -0700198 HostHandler hostHandler = null;
Charles Chan35fd1a72016-06-13 18:54:31 -0700199 private CordConfigHandler cordConfigHandler = null;
Pier Ventre10bd8d12016-11-26 21:05:22 -0800200 private RouteHandler routeHandler = null;
Pier Ventre735b8c82016-12-02 08:16:05 -0800201 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Pier Ventref34966c2016-11-07 16:21:04 -0800202 private L2TunnelHandler l2TunnelHandler = null;
sanghob35a6192015-04-01 13:05:26 -0700203 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan5270ed02016-01-30 23:22:37 -0800204 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chand55e84d2016-03-30 17:54:24 -0700205 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
206 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan35fd1a72016-06-13 18:54:31 -0700207 private final InternalCordConfigListener cordConfigListener = new InternalCordConfigListener();
Charles Chan03a73e02016-10-24 14:52:01 -0700208 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sanghob35a6192015-04-01 13:05:26 -0700209
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700210 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700211 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sanghob35a6192015-04-01 13:05:26 -0700212
Saurav Das4ce45962015-11-24 23:21:05 -0800213 @SuppressWarnings("unused")
sanghob35a6192015-04-01 13:05:26 -0700214 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800215 @SuppressWarnings("rawtypes")
Yuta HIGUCHI1624df12016-07-21 16:54:33 -0700216 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Charles Chan68aa62d2015-11-09 16:37:23 -0800217 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chane849c192016-01-11 18:28:54 -0800218 new ConcurrentHashMap<>();
219 /**
220 * Per device next objective ID store with (device id + neighbor set) as key.
221 */
222 public EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, Integer>
Charles Chan68aa62d2015-11-09 16:37:23 -0800223 nsNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800224 /**
225 * Per device next objective ID store with (device id + subnet) as key.
226 */
227 public EventuallyConsistentMap<SubnetNextObjectiveStoreKey, Integer>
Charles Chan68aa62d2015-11-09 16:37:23 -0800228 subnetNextObjStore = null;
Charles Chane849c192016-01-11 18:28:54 -0800229 /**
230 * Per device next objective ID store with (device id + port) as key.
231 */
232 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das4ce45962015-11-24 23:21:05 -0800233 portNextObjStore = null;
Saurav Das0e99e2b2015-10-28 12:39:42 -0700234 // Per device, per-subnet assigned-vlans store, with (device id + subnet
235 // IPv4 prefix) as key
236 private EventuallyConsistentMap<SubnetAssignedVidStoreKey, VlanId>
Charles Chane849c192016-01-11 18:28:54 -0800237 subnetVidStore = null;
Saurav Das4ce45962015-11-24 23:21:05 -0800238 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
239 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700240
Charles Chand55e84d2016-03-30 17:54:24 -0700241 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700242 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
243 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700244 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chand6832882015-10-05 17:50:33 -0700245 @Override
Charles Chan5270ed02016-01-30 23:22:37 -0800246 public SegmentRoutingDeviceConfig createConfig() {
247 return new SegmentRoutingDeviceConfig();
Charles Chand6832882015-10-05 17:50:33 -0700248 }
249 };
Pier Ventref34966c2016-11-07 16:21:04 -0800250
Charles Chand55e84d2016-03-30 17:54:24 -0700251 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700252 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
253 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700254 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan5270ed02016-01-30 23:22:37 -0800255 @Override
256 public SegmentRoutingAppConfig createConfig() {
257 return new SegmentRoutingAppConfig();
258 }
259 };
Pier Ventref34966c2016-11-07 16:21:04 -0800260
Charles Chanfc5c7802016-05-17 13:13:55 -0700261 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
262 new ConfigFactory<ApplicationId, XConnectConfig>(
263 SubjectFactories.APP_SUBJECT_FACTORY,
264 XConnectConfig.class, "xconnect") {
265 @Override
266 public XConnectConfig createConfig() {
267 return new XConnectConfig();
268 }
269 };
Pier Ventref34966c2016-11-07 16:21:04 -0800270
Charles Chand55e84d2016-03-30 17:54:24 -0700271 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chanfc5c7802016-05-17 13:13:55 -0700272 new ConfigFactory<ApplicationId, McastConfig>(
273 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chand55e84d2016-03-30 17:54:24 -0700274 McastConfig.class, "multicast") {
275 @Override
276 public McastConfig createConfig() {
277 return new McastConfig();
278 }
279 };
280
Pier Ventref34966c2016-11-07 16:21:04 -0800281 private final ConfigFactory<ApplicationId, PwaasConfig> pwaasConfigFactory =
282 new ConfigFactory<ApplicationId, PwaasConfig>(
283 SubjectFactories.APP_SUBJECT_FACTORY,
284 PwaasConfig.class, "pwaas") {
285 @Override
286 public PwaasConfig createConfig() {
287 return new PwaasConfig();
288 }
289 };
290
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700291 private Object threadSchedulerLock = new Object();
292 private static int numOfEventsQueued = 0;
293 private static int numOfEventsExecuted = 0;
sanghob35a6192015-04-01 13:05:26 -0700294 private static int numOfHandlerExecution = 0;
295 private static int numOfHandlerScheduled = 0;
296
Charles Chan116188d2016-02-18 14:22:42 -0800297 /**
298 * Segment Routing App ID.
299 */
Charles Chan2c15aca2016-11-09 20:51:44 -0800300 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chane849c192016-01-11 18:28:54 -0800301 /**
302 * The starting value of per-subnet VLAN ID assignment.
303 */
Saurav Das0e99e2b2015-10-28 12:39:42 -0700304 private static final short ASSIGNED_VLAN_START = 4093;
Charles Chane849c192016-01-11 18:28:54 -0800305 /**
306 * The default VLAN ID assigned to the interfaces without subnet config.
307 */
Saurav Das0e99e2b2015-10-28 12:39:42 -0700308 public static final short ASSIGNED_VLAN_NO_SUBNET = 4094;
309
sanghob35a6192015-04-01 13:05:26 -0700310 @Activate
311 protected void activate() {
Charles Chan2c15aca2016-11-09 20:51:44 -0800312 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700313
314 log.debug("Creating EC map nsnextobjectivestore");
315 EventuallyConsistentMapBuilder<NeighborSetNextObjectiveStoreKey, Integer>
316 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700317 nsNextObjStore = nsNextObjMapBuilder
318 .withName("nsnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700319 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700320 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700321 .build();
322 log.trace("Current size {}", nsNextObjStore.size());
323
Charles Chanc42e84e2015-10-20 16:24:19 -0700324 log.debug("Creating EC map subnetnextobjectivestore");
325 EventuallyConsistentMapBuilder<SubnetNextObjectiveStoreKey, Integer>
326 subnetNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Charles Chanc42e84e2015-10-20 16:24:19 -0700327 subnetNextObjStore = subnetNextObjMapBuilder
328 .withName("subnetnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700329 .withSerializer(createSerializer())
Charles Chanc42e84e2015-10-20 16:24:19 -0700330 .withTimestampProvider((k, v) -> new WallClockTimestamp())
331 .build();
332
Saurav Das4ce45962015-11-24 23:21:05 -0800333 log.debug("Creating EC map subnetnextobjectivestore");
334 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
335 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
336 portNextObjStore = portNextObjMapBuilder
337 .withName("portnextobjectivestore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700338 .withSerializer(createSerializer())
Saurav Das4ce45962015-11-24 23:21:05 -0800339 .withTimestampProvider((k, v) -> new WallClockTimestamp())
340 .build();
341
sangho0b2b6d12015-05-20 22:16:38 -0700342 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
343 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700344 tunnelStore = tunnelMapBuilder
345 .withName("tunnelstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700346 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700347 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700348 .build();
349
350 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
351 storageService.eventuallyConsistentMapBuilder();
sangho0b2b6d12015-05-20 22:16:38 -0700352 policyStore = policyMapBuilder
353 .withName("policystore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700354 .withSerializer(createSerializer())
Madan Jampanibcf1a482015-06-24 19:05:56 -0700355 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700356 .build();
357
Saurav Das0e99e2b2015-10-28 12:39:42 -0700358 EventuallyConsistentMapBuilder<SubnetAssignedVidStoreKey, VlanId>
359 subnetVidStoreMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das0e99e2b2015-10-28 12:39:42 -0700360 subnetVidStore = subnetVidStoreMapBuilder
361 .withName("subnetvidstore")
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700362 .withSerializer(createSerializer())
Saurav Das0e99e2b2015-10-28 12:39:42 -0700363 .withTimestampProvider((k, v) -> new WallClockTimestamp())
364 .build();
365
Saurav Das80980c72016-03-23 11:22:49 -0700366 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800367 "purgeOnDisconnection", "true");
Saurav Das80980c72016-03-23 11:22:49 -0700368 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800369 "purgeOnDisconnection", "true");
Pier Luigi9e5c5ca2017-01-12 18:14:58 -0800370 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
371 "requestInterceptsEnabled", "false");
Pier Luigi7e415132017-01-12 22:46:39 -0800372 compCfgService.preSetProperty("org.onosproject.incubator.net.neighbour.impl.NeighbourResolutionManager",
373 "requestInterceptsEnabled", "false");
374 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelay",
375 "arpEnabled", "false");
Pier Luigi9b1d6262017-02-02 22:31:34 -0800376 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
377 "greedyLearningIpv6", "true");
378
Saurav Das80980c72016-03-23 11:22:49 -0700379
Charles Chanb8e10c82015-10-14 11:24:40 -0700380 processor = new InternalPacketProcessor();
381 linkListener = new InternalLinkListener();
382 deviceListener = new InternalDeviceListener();
Charles Chanfc5c7802016-05-17 13:13:55 -0700383 appCfgHandler = new AppConfigHandler(this);
384 xConnectHandler = new XConnectHandler(this);
Charles Chand2990362016-04-18 13:44:03 -0700385 mcastHandler = new McastHandler(this);
386 hostHandler = new HostHandler(this);
Charles Chan35fd1a72016-06-13 18:54:31 -0700387 cordConfigHandler = new CordConfigHandler(this);
Charles Chan03a73e02016-10-24 14:52:01 -0700388 routeHandler = new RouteHandler(this);
Pier Ventre735b8c82016-12-02 08:16:05 -0800389 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Pier Ventref34966c2016-11-07 16:21:04 -0800390 l2TunnelHandler = new L2TunnelHandler(this);
Charles Chanb8e10c82015-10-14 11:24:40 -0700391
Charles Chan3e783d02016-02-26 22:19:52 -0800392 cfgService.addListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700393 cfgService.registerConfigFactory(deviceConfigFactory);
394 cfgService.registerConfigFactory(appConfigFactory);
Charles Chanfc5c7802016-05-17 13:13:55 -0700395 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700396 cfgService.registerConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800397 cfgService.registerConfigFactory(pwaasConfigFactory);
Charles Chan5270ed02016-01-30 23:22:37 -0800398 hostService.addListener(hostListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700399 packetService.addProcessor(processor, PacketProcessor.director(2));
400 linkService.addListener(linkListener);
401 deviceService.addListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700402 multicastRouteService.addListener(mcastListener);
Charles Chan35fd1a72016-06-13 18:54:31 -0700403 cordConfigService.addListener(cordConfigListener);
Charles Chanb8e10c82015-10-14 11:24:40 -0700404
405 cfgListener.configureNetwork();
406
Charles Chan03a73e02016-10-24 14:52:01 -0700407 routeService.addListener(routeListener);
408
sanghob35a6192015-04-01 13:05:26 -0700409 log.info("Started");
410 }
411
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700412 private KryoNamespace.Builder createSerializer() {
413 return new KryoNamespace.Builder()
414 .register(KryoNamespaces.API)
415 .register(NeighborSetNextObjectiveStoreKey.class,
416 SubnetNextObjectiveStoreKey.class,
417 SubnetAssignedVidStoreKey.class,
418 NeighborSet.class,
419 Tunnel.class,
420 DefaultTunnel.class,
421 Policy.class,
422 TunnelPolicy.class,
423 Policy.Type.class,
424 PortNextObjectiveStoreKey.class,
Charles Chanfc5c7802016-05-17 13:13:55 -0700425 XConnectStoreKey.class
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700426 );
427 }
428
sanghob35a6192015-04-01 13:05:26 -0700429 @Deactivate
430 protected void deactivate() {
Charles Chand6832882015-10-05 17:50:33 -0700431 cfgService.removeListener(cfgListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700432 cfgService.unregisterConfigFactory(deviceConfigFactory);
433 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chan03a73e02016-10-24 14:52:01 -0700434 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chand55e84d2016-03-30 17:54:24 -0700435 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventref34966c2016-11-07 16:21:04 -0800436 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chand6832882015-10-05 17:50:33 -0700437
sanghob35a6192015-04-01 13:05:26 -0700438 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700439 linkService.removeListener(linkListener);
440 deviceService.removeListener(deviceListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700441 multicastRouteService.removeListener(mcastListener);
Charles Chan35fd1a72016-06-13 18:54:31 -0700442 cordConfigService.removeListener(cordConfigListener);
Charles Chan03a73e02016-10-24 14:52:01 -0700443 routeService.removeListener(routeListener);
Charles Chand55e84d2016-03-30 17:54:24 -0700444
sanghob35a6192015-04-01 13:05:26 -0700445 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700446 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700447 deviceListener = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700448 groupHandlerMap.clear();
449
Charles Chand55e84d2016-03-30 17:54:24 -0700450 nsNextObjStore.destroy();
451 subnetNextObjStore.destroy();
452 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700453 tunnelStore.destroy();
454 policyStore.destroy();
455 subnetVidStore.destroy();
sanghob35a6192015-04-01 13:05:26 -0700456 log.info("Stopped");
457 }
458
sangho1e575652015-05-14 00:39:53 -0700459 @Override
460 public List<Tunnel> getTunnels() {
461 return tunnelHandler.getTunnels();
462 }
463
464 @Override
sangho71abe1b2015-06-29 14:58:47 -0700465 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
466 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700467 }
468
469 @Override
sangho71abe1b2015-06-29 14:58:47 -0700470 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700471 for (Policy policy: policyHandler.getPolicies()) {
472 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
473 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
474 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
475 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700476 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700477 }
478 }
479 }
sangho71abe1b2015-06-29 14:58:47 -0700480 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700481 }
482
483 @Override
sangho71abe1b2015-06-29 14:58:47 -0700484 public PolicyHandler.Result removePolicy(Policy policy) {
485 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700486 }
487
488 @Override
sangho71abe1b2015-06-29 14:58:47 -0700489 public PolicyHandler.Result createPolicy(Policy policy) {
490 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700491 }
492
493 @Override
494 public List<Policy> getPolicies() {
495 return policyHandler.getPolicies();
496 }
497
Saurav Das59232cf2016-04-27 18:35:50 -0700498 @Override
499 public void rerouteNetwork() {
500 cfgListener.configureNetwork();
501 for (Device device : deviceService.getDevices()) {
502 defaultRoutingHandler.populatePortAddressingRules(device.id());
503 }
504 defaultRoutingHandler.startPopulationProcess();
505 }
506
Charles Chanc81c45b2016-10-20 17:02:44 -0700507 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800508 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
509 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Charles Chanc81c45b2016-10-20 17:02:44 -0700510 deviceService.getAvailableDevices().forEach(device -> {
511 deviceSubnetMap.put(device.id(), deviceConfiguration.getSubnets(device.id()));
512 });
513 return deviceSubnetMap;
514 }
515
sanghof9d0bf12015-05-19 11:57:42 -0700516 /**
Pier Ventre98161782016-10-31 15:00:01 -0700517 * Returns the MPLS-ECMP configuration.
518 *
519 * @return MPLS-ECMP value
520 */
521 public boolean getMplsEcmp() {
522 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
523 .getConfig(this.appId, SegmentRoutingAppConfig.class);
524 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
525 }
526
527 /**
sanghof9d0bf12015-05-19 11:57:42 -0700528 * Returns the tunnel object with the tunnel ID.
529 *
530 * @param tunnelId Tunnel ID
531 * @return Tunnel reference
532 */
sangho1e575652015-05-14 00:39:53 -0700533 public Tunnel getTunnel(String tunnelId) {
534 return tunnelHandler.getTunnel(tunnelId);
535 }
536
sanghob35a6192015-04-01 13:05:26 -0700537 /**
Saurav Das0e99e2b2015-10-28 12:39:42 -0700538 * Returns the vlan-id assigned to the subnet configured for a device.
539 * If no vlan-id has been assigned, a new one is assigned out of a pool of ids,
540 * if and only if this controller instance is the master for the device.
541 * <p>
542 * USAGE: The assigned vlans are meant to be applied to untagged packets on those
543 * switches/pipelines that need this functionality. These vids are meant
544 * to be used internally within a switch, and thus need to be unique only
545 * on a switch level. Note that packets never go out on the wire with these
546 * vlans. Currently, vlan ids are assigned from value 4093 down.
547 * Vlan id 4094 expected to be used for all ports that are not assigned subnets.
548 * Vlan id 4095 is reserved and unused. Only a single vlan id is assigned
549 * per subnet.
sanghob35a6192015-04-01 13:05:26 -0700550 *
Saurav Das0e99e2b2015-10-28 12:39:42 -0700551 * @param deviceId switch dpid
Pier Ventref4b5fce2016-11-28 16:48:06 -0800552 * @param subnet IP prefix for which assigned vlan is desired
Saurav Das0e99e2b2015-10-28 12:39:42 -0700553 * @return VlanId assigned for the subnet on the device, or
554 * null if no vlan assignment was found and this instance is not
555 * the master for the device.
sanghob35a6192015-04-01 13:05:26 -0700556 */
Charles Chane849c192016-01-11 18:28:54 -0800557 // TODO: We should avoid assigning VLAN IDs that are used by VLAN cross-connection.
Pier Ventre10bd8d12016-11-26 21:05:22 -0800558 public VlanId getSubnetAssignedVlanId(DeviceId deviceId, IpPrefix subnet) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700559 VlanId assignedVid = subnetVidStore.get(new SubnetAssignedVidStoreKey(
560 deviceId, subnet));
561 if (assignedVid != null) {
562 log.debug("Query for subnet:{} on device:{} returned assigned-vlan "
563 + "{}", subnet, deviceId, assignedVid);
564 return assignedVid;
565 }
566 //check mastership for the right to assign a vlan
567 if (!mastershipService.isLocalMaster(deviceId)) {
568 log.warn("This controller instance is not the master for device {}. "
569 + "Cannot assign vlan-id for subnet {}", deviceId, subnet);
570 return null;
571 }
572 // vlan assignment is expensive but done only once
Pier Ventre10bd8d12016-11-26 21:05:22 -0800573 // FIXME for now we will do assignment considering only the ipv4 subnet.
574 Set<Ip4Prefix> configuredSubnets = deviceConfiguration.getSubnets(deviceId)
575 .stream()
576 .filter(IpPrefix::isIp4)
577 .map(IpPrefix::getIp4Prefix)
578 .collect(Collectors.toSet());
Saurav Das0e99e2b2015-10-28 12:39:42 -0700579 Set<Short> assignedVlans = new HashSet<>();
580 Set<Ip4Prefix> unassignedSubnets = new HashSet<>();
581 for (Ip4Prefix sub : configuredSubnets) {
582 VlanId v = subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId,
583 sub));
584 if (v != null) {
585 assignedVlans.add(v.toShort());
586 } else {
587 unassignedSubnets.add(sub);
588 }
589 }
590 short nextAssignedVlan = ASSIGNED_VLAN_START;
591 if (!assignedVlans.isEmpty()) {
592 nextAssignedVlan = (short) (Collections.min(assignedVlans) - 1);
593 }
594 for (Ip4Prefix unsub : unassignedSubnets) {
Pier Ventref4b5fce2016-11-28 16:48:06 -0800595 subnetVidStore.put(new SubnetAssignedVidStoreKey(deviceId, unsub),
596 VlanId.vlanId(nextAssignedVlan--));
597 log.info("Assigned vlan: {} to subnet: {} on device: {}",
598 nextAssignedVlan + 1, unsub, deviceId);
sanghob35a6192015-04-01 13:05:26 -0700599 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700600 return subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId, subnet));
sanghob35a6192015-04-01 13:05:26 -0700601 }
602
sangho1e575652015-05-14 00:39:53 -0700603 /**
Saurav Das0e99e2b2015-10-28 12:39:42 -0700604 * Returns the next objective ID for the given NeighborSet.
Saurav Das8a0732e2015-11-20 15:27:53 -0800605 * If the nextObjective does not exist, a new one is created and
Saurav Das4ce45962015-11-24 23:21:05 -0800606 * its id is returned.
sangho1e575652015-05-14 00:39:53 -0700607 *
sanghof9d0bf12015-05-19 11:57:42 -0700608 * @param deviceId Device ID
609 * @param ns NegighborSet
Saurav Das8a0732e2015-11-20 15:27:53 -0800610 * @param meta metadata passed into the creation of a Next Objective
Pier Ventre917127a2016-10-31 16:49:19 -0700611 * @param isBos indicates if it is BoS or not
Saurav Das8a0732e2015-11-20 15:27:53 -0800612 * @return next objective ID or -1 if an error was encountered during the
613 * creation of the nextObjective
sangho1e575652015-05-14 00:39:53 -0700614 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800615 public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns,
Pier Ventre917127a2016-10-31 16:49:19 -0700616 TrafficSelector meta, boolean isBos) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700617 if (groupHandlerMap.get(deviceId) != null) {
618 log.trace("getNextObjectiveId query in device {}", deviceId);
619 return groupHandlerMap
Pier Ventre917127a2016-10-31 16:49:19 -0700620 .get(deviceId).getNextObjectiveId(ns, meta, isBos);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700621 } else {
Saurav Das4ce45962015-11-24 23:21:05 -0800622 log.warn("getNextObjectiveId query - groupHandler for device {} "
623 + "not found", deviceId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700624 return -1;
625 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700626 }
627
Charles Chanc42e84e2015-10-20 16:24:19 -0700628 /**
Pier Ventre917127a2016-10-31 16:49:19 -0700629 * Returns the next objective ID for the given NeighborSet.
630 * If the nextObjective does not exist, a new one is created and
631 * its id is returned.
632 *
633 * @param deviceId Device ID
634 * @param ns NegighborSet
635 * @param meta metadata passed into the creation of a Next Objective
636 * @return next objective ID or -1 if an error was encountered during the
637 * creation of the nextObjective
638 */
639 public int getNextObjectiveId(DeviceId deviceId,
640 NeighborSet ns,
641 TrafficSelector meta) {
642 return this.getNextObjectiveId(deviceId, ns, meta, true);
643 }
644
645 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800646 * Returns the next objective ID for the given subnet prefix. It is expected
647 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700648 *
649 * @param deviceId Device ID
650 * @param prefix Subnet
Saurav Das4ce45962015-11-24 23:21:05 -0800651 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700652 */
653 public int getSubnetNextObjectiveId(DeviceId deviceId, IpPrefix prefix) {
654 if (groupHandlerMap.get(deviceId) != null) {
655 log.trace("getSubnetNextObjectiveId query in device {}", deviceId);
656 return groupHandlerMap
657 .get(deviceId).getSubnetNextObjectiveId(prefix);
658 } else {
Saurav Das4ce45962015-11-24 23:21:05 -0800659 log.warn("getSubnetNextObjectiveId query - groupHandler for "
660 + "device {} not found", deviceId);
661 return -1;
662 }
663 }
664
665 /**
666 * Returns the next objective ID for the given portNumber, given the treatment.
667 * There could be multiple different treatments to the same outport, which
668 * would result in different objectives. If the next object
669 * does not exist, a new one is created and its id is returned.
670 *
671 * @param deviceId Device ID
672 * @param portNum port number on device for which NextObjective is queried
673 * @param treatment the actions to apply on the packets (should include outport)
674 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das59232cf2016-04-27 18:35:50 -0700675 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800676 */
677 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
678 TrafficTreatment treatment,
679 TrafficSelector meta) {
680 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
681 if (ghdlr != null) {
682 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta);
683 } else {
Charles Chane849c192016-01-11 18:28:54 -0800684 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
685 + " not found", deviceId);
686 return -1;
687 }
688 }
689
sanghob35a6192015-04-01 13:05:26 -0700690 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700691 @Override
692 public void process(PacketContext context) {
693
694 if (context.isHandled()) {
695 return;
696 }
697
698 InboundPacket pkt = context.inPacket();
699 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -0800700
701 if (ethernet == null) {
702 return;
703 }
704
Saurav Das4ce45962015-11-24 23:21:05 -0800705 log.trace("Rcvd pktin: {}", ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800706 if (ethernet.getEtherType() == TYPE_ARP) {
Charles Chan7a068742017-02-16 11:45:52 -0800707 log.warn("Receive unexpected ARP packet on {}", context.inPacket().receivedFrom());
Pier Ventre10bd8d12016-11-26 21:05:22 -0800708 log.debug("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -0800709 return;
sanghob35a6192015-04-01 13:05:26 -0700710 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800711 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
712 //ipHandler.addToPacketBuffer(ipv4Packet);
713 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
714 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700715 } else {
Charles Chan50035632017-01-13 17:20:44 -0800716 // NOTE: We don't support IP learning at this moment so this
717 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -0800718 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700719 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800720 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
721 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -0800722 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -0800723 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -0800724 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
725 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
726 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
727 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
728 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
729 } else {
Pier Luigi7dad71c2017-02-01 13:50:04 -0800730 // XXX Neigbour hacking, to handle the ICMPv6 packet
731 // not under our control
732 if (icmpHandler.handleUPstreamPackets(context)) {
733 log.debug("Rcvd pktin from UpStream: {}", ipv6Packet);
734 } else {
735 log.debug("Received ICMPv6 0x{} - not handled",
736 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
737 }
Pier Ventre735b8c82016-12-02 08:16:05 -0800738 }
739 } else {
740 // NOTE: We don't support IP learning at this moment so this
741 // is not necessary. Also it causes duplication of DHCPv6 packets.
742 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
743 }
sanghob35a6192015-04-01 13:05:26 -0700744 }
745 }
746 }
747
748 private class InternalLinkListener implements LinkListener {
749 @Override
750 public void event(LinkEvent event) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700751 if (event.type() == LinkEvent.Type.LINK_ADDED
752 || event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700753 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700754 scheduleEventHandlerIfNotScheduled(event);
755 }
756 }
757 }
758
759 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -0700760 @Override
761 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -0700762 switch (event.type()) {
763 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -0800764 case PORT_UPDATED:
765 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -0700766 case DEVICE_UPDATED:
767 case DEVICE_AVAILABILITY_CHANGED:
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700768 log.debug("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700769 scheduleEventHandlerIfNotScheduled(event);
770 break;
771 default:
772 }
773 }
774 }
775
Saurav Das4ce45962015-11-24 23:21:05 -0800776 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -0700777 private void scheduleEventHandlerIfNotScheduled(Event event) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700778 synchronized (threadSchedulerLock) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700779 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700780 numOfEventsQueued++;
781
782 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
783 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700784 eventHandlerFuture = executorService
785 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
786 numOfHandlerScheduled++;
787 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700788 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700789 numOfEventsQueued,
790 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -0700791 }
sanghob35a6192015-04-01 13:05:26 -0700792 }
793
794 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700795 @Override
sanghob35a6192015-04-01 13:05:26 -0700796 public void run() {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700797 try {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700798 while (true) {
Saurav Das4ce45962015-11-24 23:21:05 -0800799 @SuppressWarnings("rawtypes")
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700800 Event event = null;
801 synchronized (threadSchedulerLock) {
802 if (!eventQueue.isEmpty()) {
803 event = eventQueue.poll();
804 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700805 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700806 numOfHandlerExecution++;
807 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
808 numOfHandlerExecution, numOfEventsExecuted);
809 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700810 }
sangho20eff1d2015-04-13 15:15:58 -0700811 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700812 if (event.type() == LinkEvent.Type.LINK_ADDED) {
813 processLinkAdded((Link) event.subject());
814 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre2c515312016-09-13 21:33:40 -0700815 Link linkRemoved = (Link) event.subject();
816 if (linkRemoved.src().elementId() instanceof DeviceId &&
817 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
818 continue;
819 }
820 if (linkRemoved.dst().elementId() instanceof DeviceId &&
821 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
822 continue;
823 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700824 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700825 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
826 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
827 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800828 DeviceId deviceId = ((Device) event.subject()).id();
829 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -0700830 log.info("Processing device event {} for available device {}",
831 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700832 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -0700833 } else {
834 log.info("Processing device event {} for unavailable device {}",
835 event.type(), ((Device) event.subject()).id());
836 processDeviceRemoved((Device) event.subject());
837 }
Saurav Das1a129a02016-11-18 15:21:57 -0800838 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800839 // typically these calls come when device is added first time
840 // so port filtering rules are handled at the device_added event.
841 // port added calls represent all ports on the device,
842 // enabled or not.
Saurav Das1a129a02016-11-18 15:21:57 -0800843 log.debug("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800844 ((DeviceEvent) event).subject().id(),
845 ((DeviceEvent) event).port().number(),
846 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -0800847 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800848 // these calls happen for every subsequent event
849 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -0800850 log.info("** PORT UPDATED {}/{} -> {}",
851 event.subject(),
852 ((DeviceEvent) event).port(),
853 event.type());
854 processPortUpdated(((Device) event.subject()),
855 ((DeviceEvent) event).port());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700856 } else {
857 log.warn("Unhandled event type: {}", event.type());
858 }
sanghob35a6192015-04-01 13:05:26 -0700859 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700860 } catch (Exception e) {
861 log.error("SegmentRouting event handler "
862 + "thread thrown an exception: {}", e);
sanghob35a6192015-04-01 13:05:26 -0700863 }
sanghob35a6192015-04-01 13:05:26 -0700864 }
865 }
866
sanghob35a6192015-04-01 13:05:26 -0700867 private void processLinkAdded(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700868 log.info("** LINK ADDED {}", link.toString());
Charles Chan0b4e6182015-11-03 10:42:14 -0800869 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
870 log.warn("Source device of this link is not configured.");
871 return;
872 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700873 //Irrespective whether the local is a MASTER or not for this device,
874 //create group handler instance and push default TTP flow rules.
875 //Because in a multi-instance setup, instances can initiate
876 //groups for any devices. Also the default TTP rules are needed
877 //to be pushed before inserting any IP table entries for any device
878 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
879 .deviceId());
880 if (groupHandler != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800881 groupHandler.linkUp(link, mastershipService.isLocalMaster(
882 link.src().deviceId()));
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700883 } else {
884 Device device = deviceService.getDevice(link.src().deviceId());
885 if (device != null) {
886 log.warn("processLinkAdded: Link Added "
887 + "Notification without Device Added "
888 + "event, still handling it");
889 processDeviceAdded(device);
890 groupHandler = groupHandlerMap.get(link.src()
891 .deviceId());
Saurav Das8a0732e2015-11-20 15:27:53 -0800892 groupHandler.linkUp(link, mastershipService.isLocalMaster(device.id()));
sanghob35a6192015-04-01 13:05:26 -0700893 }
894 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700895
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700896 log.trace("Starting optimized route population process");
897 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null);
898 //log.trace("processLinkAdded: re-starting route population process");
899 //defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -0700900
901 mcastHandler.init();
sanghob35a6192015-04-01 13:05:26 -0700902 }
903
904 private void processLinkRemoved(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700905 log.info("** LINK REMOVED {}", link.toString());
sangho834e4b02015-05-01 09:38:25 -0700906 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
907 if (groupHandler != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800908 groupHandler.portDown(link.src().port(),
909 mastershipService.isLocalMaster(link.src().deviceId()));
sangho834e4b02015-05-01 09:38:25 -0700910 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700911 log.trace("Starting optimized route population process");
912 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link);
913 //log.trace("processLinkRemoved: re-starting route population process");
914 //defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -0700915
916 mcastHandler.processLinkDown(link);
sanghob35a6192015-04-01 13:05:26 -0700917 }
918
919 private void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700920 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan0b4e6182015-11-03 10:42:14 -0800921 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das2857f382015-11-03 14:39:27 -0800922 log.warn("Device configuration uploading. Device {} will be "
923 + "processed after config completes.", device.id());
924 return;
925 }
Charles Chan2199c302016-04-23 17:36:10 -0700926 processDeviceAddedInternal(device.id());
927 }
928
929 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -0700930 // Irrespective of whether the local is a MASTER or not for this device,
931 // we need to create a SR-group-handler instance. This is because in a
932 // multi-instance setup, any instance can initiate forwarding/next-objectives
933 // for any switch (even if this instance is a SLAVE or not even connected
934 // to the switch). To handle this, a default-group-handler instance is necessary
935 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -0700936 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
937 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800938 DefaultGroupHandler groupHandler;
939 try {
940 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -0700941 createGroupHandler(deviceId,
942 appId,
943 deviceConfiguration,
944 linkService,
945 flowObjectiveService,
946 this);
Charles Chan0b4e6182015-11-03 10:42:14 -0800947 } catch (DeviceConfigNotFoundException e) {
948 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
949 return;
950 }
Charles Chan2199c302016-04-23 17:36:10 -0700951 log.debug("updating groupHandlerMap with new config for device: {}",
952 deviceId);
953 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -0800954 }
Saurav Dasb5c236e2016-06-07 10:08:06 -0700955 // Also, in some cases, drivers may need extra
956 // information to process rules (eg. Router IP/MAC); and so, we send
957 // port addressing rules to the driver as well irrespective of whether
958 // this instance is the master or not.
959 defaultRoutingHandler.populatePortAddressingRules(deviceId);
960
Charles Chan2199c302016-04-23 17:36:10 -0700961 if (mastershipService.isLocalMaster(deviceId)) {
Charles Chan03a73e02016-10-24 14:52:01 -0700962 hostHandler.init(deviceId);
Charles Chanfc5c7802016-05-17 13:13:55 -0700963 xConnectHandler.init(deviceId);
Charles Chan35fd1a72016-06-13 18:54:31 -0700964 cordConfigHandler.init(deviceId);
Charles Chan2199c302016-04-23 17:36:10 -0700965 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700966 groupHandler.createGroupsFromSubnetConfig();
Charles Chan2199c302016-04-23 17:36:10 -0700967 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chanc42e84e2015-10-20 16:24:19 -0700968 }
Charles Chan5270ed02016-01-30 23:22:37 -0800969
Charles Chan03a73e02016-10-24 14:52:01 -0700970 appCfgHandler.init(deviceId);
971 routeHandler.init(deviceId);
sanghob35a6192015-04-01 13:05:26 -0700972 }
973
Saurav Das80980c72016-03-23 11:22:49 -0700974 private void processDeviceRemoved(Device device) {
975 nsNextObjStore.entrySet().stream()
976 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
977 .forEach(entry -> {
978 nsNextObjStore.remove(entry.getKey());
979 });
Saurav Das80980c72016-03-23 11:22:49 -0700980 subnetNextObjStore.entrySet().stream()
981 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
982 .forEach(entry -> {
983 subnetNextObjStore.remove(entry.getKey());
984 });
Saurav Das80980c72016-03-23 11:22:49 -0700985 portNextObjStore.entrySet().stream()
986 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
987 .forEach(entry -> {
988 portNextObjStore.remove(entry.getKey());
989 });
Saurav Das80980c72016-03-23 11:22:49 -0700990 subnetVidStore.entrySet().stream()
991 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
992 .forEach(entry -> {
993 subnetVidStore.remove(entry.getKey());
994 });
Saurav Das80980c72016-03-23 11:22:49 -0700995 groupHandlerMap.remove(device.id());
Saurav Das80980c72016-03-23 11:22:49 -0700996 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan2199c302016-04-23 17:36:10 -0700997 mcastHandler.removeDevice(device.id());
Charles Chanfc5c7802016-05-17 13:13:55 -0700998 xConnectHandler.removeDevice(device.id());
Saurav Das80980c72016-03-23 11:22:49 -0700999 }
1000
Saurav Das1a129a02016-11-18 15:21:57 -08001001 private void processPortUpdated(Device device, Port port) {
1002 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1003 log.warn("Device configuration uploading. Not handling port event for"
1004 + "dev: {} port: {}", device.id(), port.number());
1005 return;
1006 }
1007 /* XXX create method for single port filtering rules which are needed
1008 for both switch-to-switch ports and edge ports
1009 if (defaultRoutingHandler != null) {
1010 defaultRoutingHandler.populatePortAddressingRules(
1011 ((Device) event.subject()).id());
1012 }*/
1013
1014 // portUpdated calls are for ports that have gone down or up. For switch
1015 // to switch ports, link-events should take care of any re-routing or
1016 // group editing necessary for port up/down. Here we only process edge ports
1017 // that are already configured.
Pier Ventre10bd8d12016-11-26 21:05:22 -08001018 Ip4Prefix configuredSubnet = deviceConfiguration.getPortIPv4Subnet(device.id(),
Saurav Das1a129a02016-11-18 15:21:57 -08001019 port.number());
1020 if (configuredSubnet == null) {
1021 log.debug("Not handling port updated event for unconfigured port "
1022 + "dev/port: {}/{}", device.id(), port.number());
1023 return;
1024 }
1025 processEdgePort(device, port, configuredSubnet);
1026 }
1027
1028 private void processEdgePort(Device device, Port port, Ip4Prefix subnet) {
1029 boolean portUp = port.isEnabled();
1030 if (portUp) {
1031 log.info("Device:EdgePort {}:{} is enabled in subnet: {}", device.id(),
1032 port.number(), subnet);
1033 } else {
1034 log.info("Device:EdgePort {}:{} is disabled in subnet: {}", device.id(),
1035 port.number(), subnet);
1036 }
1037
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001038 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001039 if (groupHandler != null) {
Saurav Das1a129a02016-11-18 15:21:57 -08001040 groupHandler.processEdgePort(port.number(), subnet, portUp,
Saurav Das423fe2b2015-12-04 10:52:59 -08001041 mastershipService.isLocalMaster(device.id()));
Saurav Das1a129a02016-11-18 15:21:57 -08001042 } else {
1043 log.warn("Group handler not found for dev:{}. Not handling edge port"
1044 + " {} event for port:{}", device.id(),
1045 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001046 }
1047 }
sangho1e575652015-05-14 00:39:53 -07001048
Pier Ventre10bd8d12016-11-26 21:05:22 -08001049 /**
1050 * Registers the given connect point with the NRS, this is necessary
1051 * to receive the NDP and ARP packets from the NRS.
1052 *
1053 * @param portToRegister connect point to register
1054 */
1055 public void registerConnectPoint(ConnectPoint portToRegister) {
Pier Ventre10bd8d12016-11-26 21:05:22 -08001056 this.neighbourResolutionService.registerNeighbourHandler(
1057 portToRegister,
1058 neighbourHandler,
1059 appId
1060 );
1061 }
1062
Charles Chand6832882015-10-05 17:50:33 -07001063 private class InternalConfigListener implements NetworkConfigListener {
Charles Chan2c15aca2016-11-09 20:51:44 -08001064 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001065
Charles Chane849c192016-01-11 18:28:54 -08001066 /**
1067 * Constructs the internal network config listener.
1068 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001069 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001070 */
Charles Chan2c15aca2016-11-09 20:51:44 -08001071 public InternalConfigListener(SegmentRoutingManager srManager) {
1072 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001073 }
1074
Charles Chane849c192016-01-11 18:28:54 -08001075 /**
1076 * Reads network config and initializes related data structure accordingly.
1077 */
Charles Chan4636be02015-10-07 14:21:45 -07001078 public void configureNetwork() {
Pier Ventre10bd8d12016-11-26 21:05:22 -08001079
Charles Chan2c15aca2016-11-09 20:51:44 -08001080 deviceConfiguration = new DeviceConfiguration(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001081
Charles Chan2c15aca2016-11-09 20:51:44 -08001082 arpHandler = new ArpHandler(srManager);
1083 icmpHandler = new IcmpHandler(srManager);
1084 ipHandler = new IpHandler(srManager);
1085 routingRulePopulator = new RoutingRulePopulator(srManager);
1086 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001087
1088 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1089 groupHandlerMap, tunnelStore);
1090 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1091 flowObjectiveService,
1092 tunnelHandler, policyStore);
1093
Charles Chan4636be02015-10-07 14:21:45 -07001094 for (Device device : deviceService.getDevices()) {
Charles Chan2199c302016-04-23 17:36:10 -07001095 processDeviceAddedInternal(device.id());
Charles Chan4636be02015-10-07 14:21:45 -07001096 }
1097
1098 defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -07001099 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001100 }
1101
Charles Chand6832882015-10-05 17:50:33 -07001102 @Override
1103 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001104 // TODO move this part to NetworkConfigEventHandler
1105 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1106 switch (event.type()) {
1107 case CONFIG_ADDED:
1108 log.info("Segment Routing Config added.");
1109 configureNetwork();
1110 break;
1111 case CONFIG_UPDATED:
1112 log.info("Segment Routing Config updated.");
1113 // TODO support dynamic configuration
1114 break;
1115 default:
1116 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001117 }
Charles Chan5270ed02016-01-30 23:22:37 -08001118 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001119 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001120 switch (event.type()) {
1121 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001122 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001123 break;
1124 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001125 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001126 break;
1127 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001128 appCfgHandler.processAppConfigRemoved(event);
1129 break;
1130 default:
1131 break;
1132 }
Charles Chan03a73e02016-10-24 14:52:01 -07001133 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001134 } else if (event.configClass().equals(XConnectConfig.class)) {
1135 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1136 switch (event.type()) {
1137 case CONFIG_ADDED:
1138 xConnectHandler.processXConnectConfigAdded(event);
1139 break;
1140 case CONFIG_UPDATED:
1141 xConnectHandler.processXConnectConfigUpdated(event);
1142 break;
1143 case CONFIG_REMOVED:
1144 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001145 break;
1146 default:
1147 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001148 }
Pier Luigi7dad71c2017-02-01 13:50:04 -08001149 // XXX Neighbour hacking. This method is looking for
1150 // the Internet-Router interface. In order to retrieve
1151 // the upstream port.
1152 } else if (event.configClass().equals(InterfaceConfig.class)) {
1153 switch (event.type()) {
1154 case CONFIG_ADDED:
1155 case CONFIG_UPDATED:
Pier Luigi721b6622017-02-03 13:34:21 -08001156 updateUPstreamCP();
Pier Luigi7dad71c2017-02-01 13:50:04 -08001157 case CONFIG_REGISTERED:
1158 case CONFIG_UNREGISTERED:
1159 case CONFIG_REMOVED:
1160 break;
1161 default:
1162 break;
1163 }
1164 // XXX Neighbour hacking. This method is looking for
1165 // the vrouter port.
1166 } else if (event.configClass().equals(RouterConfig.class)) {
1167 switch (event.type()) {
1168 case CONFIG_ADDED:
1169 case CONFIG_UPDATED:
Pier Luigi721b6622017-02-03 13:34:21 -08001170 updateVRouterCP(event);
Pier Luigi7dad71c2017-02-01 13:50:04 -08001171 case CONFIG_REGISTERED:
1172 case CONFIG_UNREGISTERED:
1173 case CONFIG_REMOVED:
1174 break;
1175 default:
1176 break;
1177 }
Pier Ventref34966c2016-11-07 16:21:04 -08001178 } else if (event.configClass().equals(PwaasConfig.class)) {
1179 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1180 switch (event.type()) {
1181 case CONFIG_ADDED:
1182 l2TunnelHandler.processPwaasConfigAdded(event);
1183 break;
1184 case CONFIG_UPDATED:
1185 l2TunnelHandler.processPwaasConfigUpdated(event);
1186 break;
1187 case CONFIG_REMOVED:
1188 l2TunnelHandler.processPwaasConfigRemoved(event);
1189 break;
1190 default:
1191 break;
1192 }
Charles Chand6832882015-10-05 17:50:33 -07001193 }
1194 }
1195 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001196
Pier Luigi721b6622017-02-03 13:34:21 -08001197 /////////////////////////////////////////////////////////////////
1198 // XXX Neighbour hacking, temporary workaround will be //
1199 // removed as soon as possible, when the bridging will //
1200 // be implemented. For now, it's fine to leave this //
1201 /////////////////////////////////////////////////////////////////
1202
1203 // XXX Neighbour hacking. To store upstream connect
1204 // point and vRouter connect point
1205 ConnectPoint upstreamCP = null;
1206 ConnectPoint vRouterCP = null;
1207
1208 // XXX Neighbour hacking. To update the Upstream CP
1209 public void updateUPstreamCP() {
1210 Set<ConnectPoint> portSubjects = cfgService.getSubjects(ConnectPoint.class, InterfaceConfig.class);
1211 upstreamCP = null;
1212 portSubjects.stream().forEach(subject -> {
1213 InterfaceConfig config = cfgService.getConfig(subject, InterfaceConfig.class);
1214 Set<Interface> networkInterfaces;
1215 try {
1216 networkInterfaces = config.getInterfaces();
1217 } catch (ConfigException e) {
1218 log.error("Error loading port configuration");
1219 return;
1220 }
1221 networkInterfaces.forEach(networkInterface -> {
1222 if (networkInterface.name().equals("internet-router")) {
1223 upstreamCP = subject;
1224 }
1225 });
1226 });
1227
1228 }
1229
1230 // XXX Neighbour hacking. To update the Upstream CP
1231 public void updateVRouterCP(NetworkConfigEvent event) {
1232 RouterConfig config = (RouterConfig) event.config().get();
1233 if (config == null) {
1234 log.warn("Router config not available");
1235 vRouterCP = null;
1236 return;
1237 }
1238 vRouterCP = config.getControlPlaneConnectPoint();
1239 }
1240
Charles Chan68aa62d2015-11-09 16:37:23 -08001241 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001242 @Override
1243 public void event(HostEvent event) {
1244 // Do not proceed without mastership
1245 DeviceId deviceId = event.subject().location().deviceId();
1246 if (!mastershipService.isLocalMaster(deviceId)) {
1247 return;
1248 }
1249
1250 switch (event.type()) {
1251 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001252 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001253 break;
1254 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001255 hostHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001256 break;
1257 case HOST_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001258 hostHandler.processHostRemoveEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001259 break;
1260 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001261 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001262 break;
1263 default:
1264 log.warn("Unsupported host event type: {}", event.type());
1265 break;
1266 }
1267 }
1268 }
1269
Charles Chand55e84d2016-03-30 17:54:24 -07001270 private class InternalMcastListener implements McastListener {
1271 @Override
1272 public void event(McastEvent event) {
1273 switch (event.type()) {
1274 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001275 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001276 break;
1277 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001278 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001279 break;
1280 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001281 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001282 break;
1283 case ROUTE_ADDED:
1284 case ROUTE_REMOVED:
1285 default:
1286 break;
1287 }
1288 }
1289 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001290
1291 private class InternalCordConfigListener implements CordConfigListener {
1292 @Override
1293 public void event(CordConfigEvent event) {
1294 switch (event.type()) {
1295 case ACCESS_AGENT_ADDED:
1296 cordConfigHandler.processAccessAgentAddedEvent(event);
1297 break;
1298 case ACCESS_AGENT_UPDATED:
1299 cordConfigHandler.processAccessAgentUpdatedEvent(event);
1300 break;
1301 case ACCESS_AGENT_REMOVED:
1302 cordConfigHandler.processAccessAgentRemovedEvent(event);
1303 break;
1304 case ACCESS_DEVICE_ADDED:
1305 case ACCESS_DEVICE_UPDATED:
1306 case ACCESS_DEVICE_REMOVED:
1307 default:
1308 break;
1309 }
1310 }
1311 }
Charles Chan03a73e02016-10-24 14:52:01 -07001312
1313 private class InternalRouteEventListener implements RouteListener {
1314 @Override
1315 public void event(RouteEvent event) {
1316 switch (event.type()) {
1317 case ROUTE_ADDED:
1318 routeHandler.processRouteAdded(event);
1319 break;
1320 case ROUTE_UPDATED:
1321 routeHandler.processRouteUpdated(event);
1322 break;
1323 case ROUTE_REMOVED:
1324 routeHandler.processRouteRemoved(event);
1325 break;
1326 default:
1327 break;
1328 }
1329 }
1330 }
sanghob35a6192015-04-01 13:05:26 -07001331}