blob: 8cd657bb80c2ad27bc2dc88e792e025708199607 [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
Charles Chan0aa674e2017-02-23 15:44:08 -0800445 neighbourResolutionService.unregisterNeighbourHandlers(appId);
446
sanghob35a6192015-04-01 13:05:26 -0700447 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700448 linkListener = null;
Charles Chand55e84d2016-03-30 17:54:24 -0700449 deviceListener = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700450 groupHandlerMap.clear();
451
Charles Chand55e84d2016-03-30 17:54:24 -0700452 nsNextObjStore.destroy();
453 subnetNextObjStore.destroy();
454 portNextObjStore.destroy();
Charles Chand55e84d2016-03-30 17:54:24 -0700455 tunnelStore.destroy();
456 policyStore.destroy();
457 subnetVidStore.destroy();
sanghob35a6192015-04-01 13:05:26 -0700458 log.info("Stopped");
459 }
460
sangho1e575652015-05-14 00:39:53 -0700461 @Override
462 public List<Tunnel> getTunnels() {
463 return tunnelHandler.getTunnels();
464 }
465
466 @Override
sangho71abe1b2015-06-29 14:58:47 -0700467 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
468 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700469 }
470
471 @Override
sangho71abe1b2015-06-29 14:58:47 -0700472 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700473 for (Policy policy: policyHandler.getPolicies()) {
474 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
475 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
476 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
477 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700478 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700479 }
480 }
481 }
sangho71abe1b2015-06-29 14:58:47 -0700482 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700483 }
484
485 @Override
sangho71abe1b2015-06-29 14:58:47 -0700486 public PolicyHandler.Result removePolicy(Policy policy) {
487 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700488 }
489
490 @Override
sangho71abe1b2015-06-29 14:58:47 -0700491 public PolicyHandler.Result createPolicy(Policy policy) {
492 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700493 }
494
495 @Override
496 public List<Policy> getPolicies() {
497 return policyHandler.getPolicies();
498 }
499
Saurav Das59232cf2016-04-27 18:35:50 -0700500 @Override
501 public void rerouteNetwork() {
502 cfgListener.configureNetwork();
503 for (Device device : deviceService.getDevices()) {
Saurav Das018605f2017-02-18 14:05:44 -0800504 if (mastershipService.isLocalMaster(device.id())) {
505 defaultRoutingHandler.populatePortAddressingRules(device.id());
506 }
Saurav Das59232cf2016-04-27 18:35:50 -0700507 }
508 defaultRoutingHandler.startPopulationProcess();
509 }
510
Charles Chanc81c45b2016-10-20 17:02:44 -0700511 @Override
Pier Ventre10bd8d12016-11-26 21:05:22 -0800512 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
513 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Charles Chanc81c45b2016-10-20 17:02:44 -0700514 deviceService.getAvailableDevices().forEach(device -> {
515 deviceSubnetMap.put(device.id(), deviceConfiguration.getSubnets(device.id()));
516 });
517 return deviceSubnetMap;
518 }
519
sanghof9d0bf12015-05-19 11:57:42 -0700520 /**
Pier Ventre98161782016-10-31 15:00:01 -0700521 * Returns the MPLS-ECMP configuration.
522 *
523 * @return MPLS-ECMP value
524 */
525 public boolean getMplsEcmp() {
526 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
527 .getConfig(this.appId, SegmentRoutingAppConfig.class);
528 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
529 }
530
531 /**
sanghof9d0bf12015-05-19 11:57:42 -0700532 * Returns the tunnel object with the tunnel ID.
533 *
534 * @param tunnelId Tunnel ID
535 * @return Tunnel reference
536 */
sangho1e575652015-05-14 00:39:53 -0700537 public Tunnel getTunnel(String tunnelId) {
538 return tunnelHandler.getTunnel(tunnelId);
539 }
540
sanghob35a6192015-04-01 13:05:26 -0700541 /**
Saurav Das0e99e2b2015-10-28 12:39:42 -0700542 * Returns the vlan-id assigned to the subnet configured for a device.
543 * If no vlan-id has been assigned, a new one is assigned out of a pool of ids,
544 * if and only if this controller instance is the master for the device.
545 * <p>
546 * USAGE: The assigned vlans are meant to be applied to untagged packets on those
547 * switches/pipelines that need this functionality. These vids are meant
548 * to be used internally within a switch, and thus need to be unique only
549 * on a switch level. Note that packets never go out on the wire with these
550 * vlans. Currently, vlan ids are assigned from value 4093 down.
551 * Vlan id 4094 expected to be used for all ports that are not assigned subnets.
552 * Vlan id 4095 is reserved and unused. Only a single vlan id is assigned
553 * per subnet.
sanghob35a6192015-04-01 13:05:26 -0700554 *
Saurav Das0e99e2b2015-10-28 12:39:42 -0700555 * @param deviceId switch dpid
Pier Ventref4b5fce2016-11-28 16:48:06 -0800556 * @param subnet IP prefix for which assigned vlan is desired
Saurav Das0e99e2b2015-10-28 12:39:42 -0700557 * @return VlanId assigned for the subnet on the device, or
558 * null if no vlan assignment was found and this instance is not
559 * the master for the device.
sanghob35a6192015-04-01 13:05:26 -0700560 */
Charles Chane849c192016-01-11 18:28:54 -0800561 // TODO: We should avoid assigning VLAN IDs that are used by VLAN cross-connection.
Pier Ventre10bd8d12016-11-26 21:05:22 -0800562 public VlanId getSubnetAssignedVlanId(DeviceId deviceId, IpPrefix subnet) {
Saurav Das0e99e2b2015-10-28 12:39:42 -0700563 VlanId assignedVid = subnetVidStore.get(new SubnetAssignedVidStoreKey(
564 deviceId, subnet));
565 if (assignedVid != null) {
566 log.debug("Query for subnet:{} on device:{} returned assigned-vlan "
567 + "{}", subnet, deviceId, assignedVid);
568 return assignedVid;
569 }
570 //check mastership for the right to assign a vlan
571 if (!mastershipService.isLocalMaster(deviceId)) {
572 log.warn("This controller instance is not the master for device {}. "
573 + "Cannot assign vlan-id for subnet {}", deviceId, subnet);
574 return null;
575 }
576 // vlan assignment is expensive but done only once
Pier Ventre10bd8d12016-11-26 21:05:22 -0800577 // FIXME for now we will do assignment considering only the ipv4 subnet.
578 Set<Ip4Prefix> configuredSubnets = deviceConfiguration.getSubnets(deviceId)
579 .stream()
580 .filter(IpPrefix::isIp4)
581 .map(IpPrefix::getIp4Prefix)
582 .collect(Collectors.toSet());
Saurav Das0e99e2b2015-10-28 12:39:42 -0700583 Set<Short> assignedVlans = new HashSet<>();
584 Set<Ip4Prefix> unassignedSubnets = new HashSet<>();
585 for (Ip4Prefix sub : configuredSubnets) {
586 VlanId v = subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId,
587 sub));
588 if (v != null) {
589 assignedVlans.add(v.toShort());
590 } else {
591 unassignedSubnets.add(sub);
592 }
593 }
594 short nextAssignedVlan = ASSIGNED_VLAN_START;
595 if (!assignedVlans.isEmpty()) {
596 nextAssignedVlan = (short) (Collections.min(assignedVlans) - 1);
597 }
598 for (Ip4Prefix unsub : unassignedSubnets) {
Pier Ventref4b5fce2016-11-28 16:48:06 -0800599 subnetVidStore.put(new SubnetAssignedVidStoreKey(deviceId, unsub),
600 VlanId.vlanId(nextAssignedVlan--));
601 log.info("Assigned vlan: {} to subnet: {} on device: {}",
602 nextAssignedVlan + 1, unsub, deviceId);
sanghob35a6192015-04-01 13:05:26 -0700603 }
Saurav Das0e99e2b2015-10-28 12:39:42 -0700604 return subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId, subnet));
sanghob35a6192015-04-01 13:05:26 -0700605 }
606
sangho1e575652015-05-14 00:39:53 -0700607 /**
Saurav Das0e99e2b2015-10-28 12:39:42 -0700608 * Returns the next objective ID for the given NeighborSet.
Saurav Das8a0732e2015-11-20 15:27:53 -0800609 * If the nextObjective does not exist, a new one is created and
Saurav Das4ce45962015-11-24 23:21:05 -0800610 * its id is returned.
sangho1e575652015-05-14 00:39:53 -0700611 *
sanghof9d0bf12015-05-19 11:57:42 -0700612 * @param deviceId Device ID
613 * @param ns NegighborSet
Saurav Das8a0732e2015-11-20 15:27:53 -0800614 * @param meta metadata passed into the creation of a Next Objective
Pier Ventre917127a2016-10-31 16:49:19 -0700615 * @param isBos indicates if it is BoS or not
Saurav Das8a0732e2015-11-20 15:27:53 -0800616 * @return next objective ID or -1 if an error was encountered during the
617 * creation of the nextObjective
sangho1e575652015-05-14 00:39:53 -0700618 */
Saurav Das8a0732e2015-11-20 15:27:53 -0800619 public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns,
Pier Ventre917127a2016-10-31 16:49:19 -0700620 TrafficSelector meta, boolean isBos) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700621 if (groupHandlerMap.get(deviceId) != null) {
622 log.trace("getNextObjectiveId query in device {}", deviceId);
623 return groupHandlerMap
Pier Ventre917127a2016-10-31 16:49:19 -0700624 .get(deviceId).getNextObjectiveId(ns, meta, isBos);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700625 } else {
Saurav Das4ce45962015-11-24 23:21:05 -0800626 log.warn("getNextObjectiveId query - groupHandler for device {} "
627 + "not found", deviceId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700628 return -1;
629 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700630 }
631
Charles Chanc42e84e2015-10-20 16:24:19 -0700632 /**
Pier Ventre917127a2016-10-31 16:49:19 -0700633 * Returns the next objective ID for the given NeighborSet.
634 * If the nextObjective does not exist, a new one is created and
635 * its id is returned.
636 *
637 * @param deviceId Device ID
638 * @param ns NegighborSet
639 * @param meta metadata passed into the creation of a Next Objective
640 * @return next objective ID or -1 if an error was encountered during the
641 * creation of the nextObjective
642 */
643 public int getNextObjectiveId(DeviceId deviceId,
644 NeighborSet ns,
645 TrafficSelector meta) {
646 return this.getNextObjectiveId(deviceId, ns, meta, true);
647 }
648
649 /**
Saurav Das4ce45962015-11-24 23:21:05 -0800650 * Returns the next objective ID for the given subnet prefix. It is expected
651 * that the next-objective has been pre-created from configuration.
Charles Chanc42e84e2015-10-20 16:24:19 -0700652 *
653 * @param deviceId Device ID
654 * @param prefix Subnet
Saurav Das4ce45962015-11-24 23:21:05 -0800655 * @return next objective ID or -1 if it was not found
Charles Chanc42e84e2015-10-20 16:24:19 -0700656 */
657 public int getSubnetNextObjectiveId(DeviceId deviceId, IpPrefix prefix) {
658 if (groupHandlerMap.get(deviceId) != null) {
659 log.trace("getSubnetNextObjectiveId query in device {}", deviceId);
660 return groupHandlerMap
661 .get(deviceId).getSubnetNextObjectiveId(prefix);
662 } else {
Saurav Das4ce45962015-11-24 23:21:05 -0800663 log.warn("getSubnetNextObjectiveId query - groupHandler for "
664 + "device {} not found", deviceId);
665 return -1;
666 }
667 }
668
669 /**
670 * Returns the next objective ID for the given portNumber, given the treatment.
671 * There could be multiple different treatments to the same outport, which
672 * would result in different objectives. If the next object
673 * does not exist, a new one is created and its id is returned.
674 *
675 * @param deviceId Device ID
676 * @param portNum port number on device for which NextObjective is queried
677 * @param treatment the actions to apply on the packets (should include outport)
678 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das59232cf2016-04-27 18:35:50 -0700679 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das4ce45962015-11-24 23:21:05 -0800680 */
681 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
682 TrafficTreatment treatment,
683 TrafficSelector meta) {
684 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
685 if (ghdlr != null) {
686 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta);
687 } else {
Charles Chane849c192016-01-11 18:28:54 -0800688 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
689 + " not found", deviceId);
690 return -1;
691 }
692 }
693
sanghob35a6192015-04-01 13:05:26 -0700694 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700695 @Override
696 public void process(PacketContext context) {
697
698 if (context.isHandled()) {
699 return;
700 }
701
702 InboundPacket pkt = context.inPacket();
703 Ethernet ethernet = pkt.parsed();
Pier Luigi7dad71c2017-02-01 13:50:04 -0800704
705 if (ethernet == null) {
706 return;
707 }
708
Saurav Das4ce45962015-11-24 23:21:05 -0800709 log.trace("Rcvd pktin: {}", ethernet);
Pier Ventree0ae7a32016-11-23 09:57:42 -0800710 if (ethernet.getEtherType() == TYPE_ARP) {
Charles Chan7a068742017-02-16 11:45:52 -0800711 log.warn("Receive unexpected ARP packet on {}", context.inPacket().receivedFrom());
Pier Ventre10bd8d12016-11-26 21:05:22 -0800712 log.debug("{}", ethernet);
Pier Ventre968da122016-12-09 17:26:04 -0800713 return;
sanghob35a6192015-04-01 13:05:26 -0700714 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventre735b8c82016-12-02 08:16:05 -0800715 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
716 //ipHandler.addToPacketBuffer(ipv4Packet);
717 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
718 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700719 } else {
Charles Chan50035632017-01-13 17:20:44 -0800720 // NOTE: We don't support IP learning at this moment so this
721 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre968da122016-12-09 17:26:04 -0800722 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sanghob35a6192015-04-01 13:05:26 -0700723 }
Pier Ventre10bd8d12016-11-26 21:05:22 -0800724 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
725 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventre735b8c82016-12-02 08:16:05 -0800726 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi7dad71c2017-02-01 13:50:04 -0800727 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventre735b8c82016-12-02 08:16:05 -0800728 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
729 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
730 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
731 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
732 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
733 } else {
Pier Luigi7dad71c2017-02-01 13:50:04 -0800734 // XXX Neigbour hacking, to handle the ICMPv6 packet
735 // not under our control
736 if (icmpHandler.handleUPstreamPackets(context)) {
737 log.debug("Rcvd pktin from UpStream: {}", ipv6Packet);
738 } else {
739 log.debug("Received ICMPv6 0x{} - not handled",
740 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
741 }
Pier Ventre735b8c82016-12-02 08:16:05 -0800742 }
743 } else {
744 // NOTE: We don't support IP learning at this moment so this
745 // is not necessary. Also it causes duplication of DHCPv6 packets.
746 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
747 }
sanghob35a6192015-04-01 13:05:26 -0700748 }
749 }
750 }
751
752 private class InternalLinkListener implements LinkListener {
753 @Override
754 public void event(LinkEvent event) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700755 if (event.type() == LinkEvent.Type.LINK_ADDED
756 || event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700757 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700758 scheduleEventHandlerIfNotScheduled(event);
759 }
760 }
761 }
762
763 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -0700764 @Override
765 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -0700766 switch (event.type()) {
767 case DEVICE_ADDED:
Saurav Das1a129a02016-11-18 15:21:57 -0800768 case PORT_UPDATED:
769 case PORT_ADDED:
sangho20eff1d2015-04-13 15:15:58 -0700770 case DEVICE_UPDATED:
771 case DEVICE_AVAILABILITY_CHANGED:
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700772 log.debug("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700773 scheduleEventHandlerIfNotScheduled(event);
774 break;
775 default:
776 }
777 }
778 }
779
Saurav Das4ce45962015-11-24 23:21:05 -0800780 @SuppressWarnings("rawtypes")
sanghob35a6192015-04-01 13:05:26 -0700781 private void scheduleEventHandlerIfNotScheduled(Event event) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700782 synchronized (threadSchedulerLock) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700783 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700784 numOfEventsQueued++;
785
786 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
787 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700788 eventHandlerFuture = executorService
789 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
790 numOfHandlerScheduled++;
791 }
Jonathan Hartc19f7c12016-04-12 15:39:44 -0700792 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700793 numOfEventsQueued,
794 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -0700795 }
sanghob35a6192015-04-01 13:05:26 -0700796 }
797
798 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700799 @Override
sanghob35a6192015-04-01 13:05:26 -0700800 public void run() {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700801 try {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700802 while (true) {
Saurav Das4ce45962015-11-24 23:21:05 -0800803 @SuppressWarnings("rawtypes")
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700804 Event event = null;
805 synchronized (threadSchedulerLock) {
806 if (!eventQueue.isEmpty()) {
807 event = eventQueue.poll();
808 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700809 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700810 numOfHandlerExecution++;
811 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
812 numOfHandlerExecution, numOfEventsExecuted);
813 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700814 }
sangho20eff1d2015-04-13 15:15:58 -0700815 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700816 if (event.type() == LinkEvent.Type.LINK_ADDED) {
817 processLinkAdded((Link) event.subject());
818 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre2c515312016-09-13 21:33:40 -0700819 Link linkRemoved = (Link) event.subject();
820 if (linkRemoved.src().elementId() instanceof DeviceId &&
821 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
822 continue;
823 }
824 if (linkRemoved.dst().elementId() instanceof DeviceId &&
825 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
826 continue;
827 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700828 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700829 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
830 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
831 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800832 DeviceId deviceId = ((Device) event.subject()).id();
833 if (deviceService.isAvailable(deviceId)) {
Saurav Das837e0bb2015-10-30 17:45:38 -0700834 log.info("Processing device event {} for available device {}",
835 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700836 processDeviceAdded((Device) event.subject());
Saurav Das80980c72016-03-23 11:22:49 -0700837 } else {
838 log.info("Processing device event {} for unavailable device {}",
839 event.type(), ((Device) event.subject()).id());
840 processDeviceRemoved((Device) event.subject());
841 }
Saurav Das1a129a02016-11-18 15:21:57 -0800842 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800843 // typically these calls come when device is added first time
844 // so port filtering rules are handled at the device_added event.
845 // port added calls represent all ports on the device,
846 // enabled or not.
Saurav Das1a129a02016-11-18 15:21:57 -0800847 log.debug("** PORT ADDED {}/{} -> {}",
Saurav Dasd2fded02016-12-02 15:43:47 -0800848 ((DeviceEvent) event).subject().id(),
849 ((DeviceEvent) event).port().number(),
850 event.type());
Saurav Das1a129a02016-11-18 15:21:57 -0800851 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd2fded02016-12-02 15:43:47 -0800852 // these calls happen for every subsequent event
853 // ports enabled, disabled, switch goes away, comes back
Saurav Das1a129a02016-11-18 15:21:57 -0800854 log.info("** PORT UPDATED {}/{} -> {}",
855 event.subject(),
856 ((DeviceEvent) event).port(),
857 event.type());
858 processPortUpdated(((Device) event.subject()),
859 ((DeviceEvent) event).port());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700860 } else {
861 log.warn("Unhandled event type: {}", event.type());
862 }
sanghob35a6192015-04-01 13:05:26 -0700863 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700864 } catch (Exception e) {
865 log.error("SegmentRouting event handler "
866 + "thread thrown an exception: {}", e);
sanghob35a6192015-04-01 13:05:26 -0700867 }
sanghob35a6192015-04-01 13:05:26 -0700868 }
869 }
870
sanghob35a6192015-04-01 13:05:26 -0700871 private void processLinkAdded(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700872 log.info("** LINK ADDED {}", link.toString());
Charles Chan0b4e6182015-11-03 10:42:14 -0800873 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
874 log.warn("Source device of this link is not configured.");
875 return;
876 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700877 //Irrespective whether the local is a MASTER or not for this device,
878 //create group handler instance and push default TTP flow rules.
879 //Because in a multi-instance setup, instances can initiate
880 //groups for any devices. Also the default TTP rules are needed
881 //to be pushed before inserting any IP table entries for any device
882 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
883 .deviceId());
884 if (groupHandler != null) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800885 groupHandler.linkUp(link, mastershipService.isLocalMaster(
886 link.src().deviceId()));
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700887 } else {
888 Device device = deviceService.getDevice(link.src().deviceId());
889 if (device != null) {
890 log.warn("processLinkAdded: Link Added "
891 + "Notification without Device Added "
892 + "event, still handling it");
893 processDeviceAdded(device);
894 groupHandler = groupHandlerMap.get(link.src()
895 .deviceId());
Saurav Das8a0732e2015-11-20 15:27:53 -0800896 groupHandler.linkUp(link, mastershipService.isLocalMaster(device.id()));
sanghob35a6192015-04-01 13:05:26 -0700897 }
898 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700899
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700900 log.trace("Starting optimized route population process");
901 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null);
902 //log.trace("processLinkAdded: re-starting route population process");
903 //defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -0700904
905 mcastHandler.init();
sanghob35a6192015-04-01 13:05:26 -0700906 }
907
908 private void processLinkRemoved(Link link) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700909 log.info("** LINK REMOVED {}", link.toString());
sangho834e4b02015-05-01 09:38:25 -0700910 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
911 if (groupHandler != null) {
Saurav Das423fe2b2015-12-04 10:52:59 -0800912 groupHandler.portDown(link.src().port(),
913 mastershipService.isLocalMaster(link.src().deviceId()));
sangho834e4b02015-05-01 09:38:25 -0700914 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700915 log.trace("Starting optimized route population process");
916 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link);
917 //log.trace("processLinkRemoved: re-starting route population process");
918 //defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -0700919
920 mcastHandler.processLinkDown(link);
sanghob35a6192015-04-01 13:05:26 -0700921 }
922
923 private void processDeviceAdded(Device device) {
Saurav Dasb5c236e2016-06-07 10:08:06 -0700924 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan0b4e6182015-11-03 10:42:14 -0800925 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das2857f382015-11-03 14:39:27 -0800926 log.warn("Device configuration uploading. Device {} will be "
927 + "processed after config completes.", device.id());
928 return;
929 }
Charles Chan2199c302016-04-23 17:36:10 -0700930 processDeviceAddedInternal(device.id());
931 }
932
933 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Das837e0bb2015-10-30 17:45:38 -0700934 // Irrespective of whether the local is a MASTER or not for this device,
935 // we need to create a SR-group-handler instance. This is because in a
936 // multi-instance setup, any instance can initiate forwarding/next-objectives
937 // for any switch (even if this instance is a SLAVE or not even connected
938 // to the switch). To handle this, a default-group-handler instance is necessary
939 // per switch.
Charles Chan2199c302016-04-23 17:36:10 -0700940 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
941 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan0b4e6182015-11-03 10:42:14 -0800942 DefaultGroupHandler groupHandler;
943 try {
944 groupHandler = DefaultGroupHandler.
Charles Chan2199c302016-04-23 17:36:10 -0700945 createGroupHandler(deviceId,
946 appId,
947 deviceConfiguration,
948 linkService,
949 flowObjectiveService,
950 this);
Charles Chan0b4e6182015-11-03 10:42:14 -0800951 } catch (DeviceConfigNotFoundException e) {
952 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
953 return;
954 }
Charles Chan2199c302016-04-23 17:36:10 -0700955 log.debug("updating groupHandlerMap with new config for device: {}",
956 deviceId);
957 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das2857f382015-11-03 14:39:27 -0800958 }
Saurav Dasb5c236e2016-06-07 10:08:06 -0700959
Charles Chan2199c302016-04-23 17:36:10 -0700960 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Das018605f2017-02-18 14:05:44 -0800961 defaultRoutingHandler.populatePortAddressingRules(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 }
Saurav Das018605f2017-02-18 14:05:44 -08001007
1008 if (!mastershipService.isLocalMaster(device.id())) {
1009 log.debug("Not master for dev:{} .. not handling port updated event"
1010 + "for port {}", device.id(), port.number());
1011 return;
1012 }
1013
1014 // first we handle filtering rules associated with the port
1015 if (port.isEnabled()) {
1016 log.info("Switchport {}/{} enabled..programming filters",
1017 device.id(), port.number());
1018 defaultRoutingHandler.populateSinglePortFilteringRules(device.id(),
1019 port.number());
1020 } else {
1021 log.info("Switchport {}/{} disabled..removing filters",
1022 device.id(), port.number());
1023 defaultRoutingHandler.revokeSinglePortFilteringRules(device.id(),
1024 port.number());
1025 }
Saurav Das1a129a02016-11-18 15:21:57 -08001026
1027 // portUpdated calls are for ports that have gone down or up. For switch
1028 // to switch ports, link-events should take care of any re-routing or
1029 // group editing necessary for port up/down. Here we only process edge ports
1030 // that are already configured.
Saurav Das018605f2017-02-18 14:05:44 -08001031 Ip4Prefix configuredSubnet = deviceConfiguration.getPortIPv4Subnet(device.id(),
1032 port.number());
Saurav Das1a129a02016-11-18 15:21:57 -08001033 if (configuredSubnet == null) {
1034 log.debug("Not handling port updated event for unconfigured port "
1035 + "dev/port: {}/{}", device.id(), port.number());
1036 return;
1037 }
1038 processEdgePort(device, port, configuredSubnet);
1039 }
1040
1041 private void processEdgePort(Device device, Port port, Ip4Prefix subnet) {
1042 boolean portUp = port.isEnabled();
1043 if (portUp) {
1044 log.info("Device:EdgePort {}:{} is enabled in subnet: {}", device.id(),
1045 port.number(), subnet);
1046 } else {
1047 log.info("Device:EdgePort {}:{} is disabled in subnet: {}", device.id(),
1048 port.number(), subnet);
1049 }
1050
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -07001051 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -07001052 if (groupHandler != null) {
Saurav Das018605f2017-02-18 14:05:44 -08001053 groupHandler.processEdgePort(port.number(), subnet, portUp);
Saurav Das1a129a02016-11-18 15:21:57 -08001054 } else {
1055 log.warn("Group handler not found for dev:{}. Not handling edge port"
1056 + " {} event for port:{}", device.id(),
1057 (portUp) ? "UP" : "DOWN", port.number());
sanghob35a6192015-04-01 13:05:26 -07001058 }
1059 }
sangho1e575652015-05-14 00:39:53 -07001060
Pier Ventre10bd8d12016-11-26 21:05:22 -08001061 /**
1062 * Registers the given connect point with the NRS, this is necessary
1063 * to receive the NDP and ARP packets from the NRS.
1064 *
1065 * @param portToRegister connect point to register
1066 */
1067 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan0aa674e2017-02-23 15:44:08 -08001068 neighbourResolutionService.registerNeighbourHandler(
Pier Ventre10bd8d12016-11-26 21:05:22 -08001069 portToRegister,
1070 neighbourHandler,
1071 appId
1072 );
1073 }
1074
Charles Chand6832882015-10-05 17:50:33 -07001075 private class InternalConfigListener implements NetworkConfigListener {
Charles Chan2c15aca2016-11-09 20:51:44 -08001076 SegmentRoutingManager srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001077
Charles Chane849c192016-01-11 18:28:54 -08001078 /**
1079 * Constructs the internal network config listener.
1080 *
Charles Chan2c15aca2016-11-09 20:51:44 -08001081 * @param srManager segment routing manager
Charles Chane849c192016-01-11 18:28:54 -08001082 */
Charles Chan2c15aca2016-11-09 20:51:44 -08001083 public InternalConfigListener(SegmentRoutingManager srManager) {
1084 this.srManager = srManager;
Charles Chan4636be02015-10-07 14:21:45 -07001085 }
1086
Charles Chane849c192016-01-11 18:28:54 -08001087 /**
1088 * Reads network config and initializes related data structure accordingly.
1089 */
Charles Chan4636be02015-10-07 14:21:45 -07001090 public void configureNetwork() {
Pier Ventre10bd8d12016-11-26 21:05:22 -08001091
Charles Chan2c15aca2016-11-09 20:51:44 -08001092 deviceConfiguration = new DeviceConfiguration(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001093
Charles Chan2c15aca2016-11-09 20:51:44 -08001094 arpHandler = new ArpHandler(srManager);
1095 icmpHandler = new IcmpHandler(srManager);
1096 ipHandler = new IpHandler(srManager);
1097 routingRulePopulator = new RoutingRulePopulator(srManager);
1098 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chan4636be02015-10-07 14:21:45 -07001099
1100 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1101 groupHandlerMap, tunnelStore);
1102 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1103 flowObjectiveService,
1104 tunnelHandler, policyStore);
1105
Charles Chan4636be02015-10-07 14:21:45 -07001106 for (Device device : deviceService.getDevices()) {
Charles Chan2199c302016-04-23 17:36:10 -07001107 processDeviceAddedInternal(device.id());
Charles Chan4636be02015-10-07 14:21:45 -07001108 }
1109
1110 defaultRoutingHandler.startPopulationProcess();
Charles Chan2199c302016-04-23 17:36:10 -07001111 mcastHandler.init();
Charles Chan4636be02015-10-07 14:21:45 -07001112 }
1113
Charles Chand6832882015-10-05 17:50:33 -07001114 @Override
1115 public void event(NetworkConfigEvent event) {
Charles Chan5270ed02016-01-30 23:22:37 -08001116 // TODO move this part to NetworkConfigEventHandler
1117 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1118 switch (event.type()) {
1119 case CONFIG_ADDED:
1120 log.info("Segment Routing Config added.");
1121 configureNetwork();
1122 break;
1123 case CONFIG_UPDATED:
1124 log.info("Segment Routing Config updated.");
1125 // TODO support dynamic configuration
1126 break;
1127 default:
1128 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001129 }
Charles Chan5270ed02016-01-30 23:22:37 -08001130 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chanfc5c7802016-05-17 13:13:55 -07001131 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan5270ed02016-01-30 23:22:37 -08001132 switch (event.type()) {
1133 case CONFIG_ADDED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001134 appCfgHandler.processAppConfigAdded(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001135 break;
1136 case CONFIG_UPDATED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001137 appCfgHandler.processAppConfigUpdated(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001138 break;
1139 case CONFIG_REMOVED:
Charles Chanfc5c7802016-05-17 13:13:55 -07001140 appCfgHandler.processAppConfigRemoved(event);
1141 break;
1142 default:
1143 break;
1144 }
Charles Chan03a73e02016-10-24 14:52:01 -07001145 configureNetwork();
Charles Chanfc5c7802016-05-17 13:13:55 -07001146 } else if (event.configClass().equals(XConnectConfig.class)) {
1147 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1148 switch (event.type()) {
1149 case CONFIG_ADDED:
1150 xConnectHandler.processXConnectConfigAdded(event);
1151 break;
1152 case CONFIG_UPDATED:
1153 xConnectHandler.processXConnectConfigUpdated(event);
1154 break;
1155 case CONFIG_REMOVED:
1156 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan5270ed02016-01-30 23:22:37 -08001157 break;
1158 default:
1159 break;
Charles Chanb8e10c82015-10-14 11:24:40 -07001160 }
Pier Luigi7dad71c2017-02-01 13:50:04 -08001161 // XXX Neighbour hacking. This method is looking for
1162 // the Internet-Router interface. In order to retrieve
1163 // the upstream port.
1164 } else if (event.configClass().equals(InterfaceConfig.class)) {
1165 switch (event.type()) {
1166 case CONFIG_ADDED:
1167 case CONFIG_UPDATED:
Pier Luigi721b6622017-02-03 13:34:21 -08001168 updateUPstreamCP();
Pier Luigi7dad71c2017-02-01 13:50:04 -08001169 case CONFIG_REGISTERED:
1170 case CONFIG_UNREGISTERED:
1171 case CONFIG_REMOVED:
1172 break;
1173 default:
1174 break;
1175 }
1176 // XXX Neighbour hacking. This method is looking for
1177 // the vrouter port.
1178 } else if (event.configClass().equals(RouterConfig.class)) {
1179 switch (event.type()) {
1180 case CONFIG_ADDED:
1181 case CONFIG_UPDATED:
Pier Luigi721b6622017-02-03 13:34:21 -08001182 updateVRouterCP(event);
Pier Luigi7dad71c2017-02-01 13:50:04 -08001183 case CONFIG_REGISTERED:
1184 case CONFIG_UNREGISTERED:
1185 case CONFIG_REMOVED:
1186 break;
1187 default:
1188 break;
1189 }
Pier Ventref34966c2016-11-07 16:21:04 -08001190 } else if (event.configClass().equals(PwaasConfig.class)) {
1191 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1192 switch (event.type()) {
1193 case CONFIG_ADDED:
1194 l2TunnelHandler.processPwaasConfigAdded(event);
1195 break;
1196 case CONFIG_UPDATED:
1197 l2TunnelHandler.processPwaasConfigUpdated(event);
1198 break;
1199 case CONFIG_REMOVED:
1200 l2TunnelHandler.processPwaasConfigRemoved(event);
1201 break;
1202 default:
1203 break;
1204 }
Charles Chand6832882015-10-05 17:50:33 -07001205 }
1206 }
1207 }
Charles Chan68aa62d2015-11-09 16:37:23 -08001208
Pier Luigi721b6622017-02-03 13:34:21 -08001209 /////////////////////////////////////////////////////////////////
1210 // XXX Neighbour hacking, temporary workaround will be //
Saurav Das018605f2017-02-18 14:05:44 -08001211 // removed as soon as possible, when bridging based //
1212 // control plane redirect is implemented. //
Pier Luigi721b6622017-02-03 13:34:21 -08001213 /////////////////////////////////////////////////////////////////
1214
1215 // XXX Neighbour hacking. To store upstream connect
1216 // point and vRouter connect point
1217 ConnectPoint upstreamCP = null;
1218 ConnectPoint vRouterCP = null;
1219
1220 // XXX Neighbour hacking. To update the Upstream CP
1221 public void updateUPstreamCP() {
1222 Set<ConnectPoint> portSubjects = cfgService.getSubjects(ConnectPoint.class, InterfaceConfig.class);
1223 upstreamCP = null;
1224 portSubjects.stream().forEach(subject -> {
1225 InterfaceConfig config = cfgService.getConfig(subject, InterfaceConfig.class);
1226 Set<Interface> networkInterfaces;
1227 try {
1228 networkInterfaces = config.getInterfaces();
1229 } catch (ConfigException e) {
1230 log.error("Error loading port configuration");
1231 return;
1232 }
1233 networkInterfaces.forEach(networkInterface -> {
1234 if (networkInterface.name().equals("internet-router")) {
1235 upstreamCP = subject;
1236 }
1237 });
1238 });
1239
1240 }
1241
1242 // XXX Neighbour hacking. To update the Upstream CP
1243 public void updateVRouterCP(NetworkConfigEvent event) {
1244 RouterConfig config = (RouterConfig) event.config().get();
1245 if (config == null) {
1246 log.warn("Router config not available");
1247 vRouterCP = null;
1248 return;
1249 }
1250 vRouterCP = config.getControlPlaneConnectPoint();
1251 }
1252
Charles Chan68aa62d2015-11-09 16:37:23 -08001253 private class InternalHostListener implements HostListener {
Charles Chan68aa62d2015-11-09 16:37:23 -08001254 @Override
1255 public void event(HostEvent event) {
1256 // Do not proceed without mastership
1257 DeviceId deviceId = event.subject().location().deviceId();
1258 if (!mastershipService.isLocalMaster(deviceId)) {
1259 return;
1260 }
1261
1262 switch (event.type()) {
1263 case HOST_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001264 hostHandler.processHostAddedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001265 break;
1266 case HOST_MOVED:
Charles Chand2990362016-04-18 13:44:03 -07001267 hostHandler.processHostMovedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001268 break;
1269 case HOST_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001270 hostHandler.processHostRemoveEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001271 break;
1272 case HOST_UPDATED:
Charles Chand2990362016-04-18 13:44:03 -07001273 hostHandler.processHostUpdatedEvent(event);
Charles Chan68aa62d2015-11-09 16:37:23 -08001274 break;
1275 default:
1276 log.warn("Unsupported host event type: {}", event.type());
1277 break;
1278 }
1279 }
1280 }
1281
Charles Chand55e84d2016-03-30 17:54:24 -07001282 private class InternalMcastListener implements McastListener {
1283 @Override
1284 public void event(McastEvent event) {
1285 switch (event.type()) {
1286 case SOURCE_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001287 mcastHandler.processSourceAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001288 break;
1289 case SINK_ADDED:
Charles Chand2990362016-04-18 13:44:03 -07001290 mcastHandler.processSinkAdded(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001291 break;
1292 case SINK_REMOVED:
Charles Chand2990362016-04-18 13:44:03 -07001293 mcastHandler.processSinkRemoved(event);
Charles Chand55e84d2016-03-30 17:54:24 -07001294 break;
1295 case ROUTE_ADDED:
1296 case ROUTE_REMOVED:
1297 default:
1298 break;
1299 }
1300 }
1301 }
Charles Chan35fd1a72016-06-13 18:54:31 -07001302
1303 private class InternalCordConfigListener implements CordConfigListener {
1304 @Override
1305 public void event(CordConfigEvent event) {
1306 switch (event.type()) {
1307 case ACCESS_AGENT_ADDED:
1308 cordConfigHandler.processAccessAgentAddedEvent(event);
1309 break;
1310 case ACCESS_AGENT_UPDATED:
1311 cordConfigHandler.processAccessAgentUpdatedEvent(event);
1312 break;
1313 case ACCESS_AGENT_REMOVED:
1314 cordConfigHandler.processAccessAgentRemovedEvent(event);
1315 break;
1316 case ACCESS_DEVICE_ADDED:
1317 case ACCESS_DEVICE_UPDATED:
1318 case ACCESS_DEVICE_REMOVED:
1319 default:
1320 break;
1321 }
1322 }
1323 }
Charles Chan03a73e02016-10-24 14:52:01 -07001324
1325 private class InternalRouteEventListener implements RouteListener {
1326 @Override
1327 public void event(RouteEvent event) {
1328 switch (event.type()) {
1329 case ROUTE_ADDED:
1330 routeHandler.processRouteAdded(event);
1331 break;
1332 case ROUTE_UPDATED:
1333 routeHandler.processRouteUpdated(event);
1334 break;
1335 case ROUTE_REMOVED:
1336 routeHandler.processRouteRemoved(event);
1337 break;
1338 default:
1339 break;
1340 }
1341 }
1342 }
sanghob35a6192015-04-01 13:05:26 -07001343}