blob: 1423e06946f602542d4d6a602bd10bcef602c149 [file] [log] [blame]
sangho80f11cb2015-04-01 13:05:26 -07001/*
Brian O'Connor43b53542016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
sangho80f11cb2015-04-01 13:05:26 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.segmentrouting;
17
Charles Chand7844e52016-10-20 17:02:44 -070018import com.google.common.collect.Maps;
sangho80f11cb2015-04-01 13:05:26 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho27462c62015-05-14 00:39:53 -070024import org.apache.felix.scr.annotations.Service;
sangho80f11cb2015-04-01 13:05:26 -070025import org.onlab.packet.Ethernet;
Pier Ventreb6b81d52016-12-02 08:16:05 -080026import org.onlab.packet.ICMP6;
Charles Chan77277672015-10-20 16:24:19 -070027import org.onlab.packet.IPv4;
Pier Ventreb6a7f342016-11-26 21:05:22 -080028import org.onlab.packet.IPv6;
Charles Chan77277672015-10-20 16:24:19 -070029import org.onlab.packet.Ip4Prefix;
Charles Chan77277672015-10-20 16:24:19 -070030import org.onlab.packet.IpPrefix;
Jonathan Hart54541d12016-04-12 15:39:44 -070031import org.onlab.packet.VlanId;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070032import org.onlab.util.KryoNamespace;
Saurav Dasc3604f12016-03-23 11:22:49 -070033import org.onosproject.cfg.ComponentConfigService;
sangho80f11cb2015-04-01 13:05:26 -070034import org.onosproject.core.ApplicationId;
35import org.onosproject.core.CoreService;
36import org.onosproject.event.Event;
Pier Luigiad0a67f2017-02-03 13:34:21 -080037import org.onosproject.incubator.net.config.basics.ConfigException;
Pier Luigi37a35432017-02-01 13:50:04 -080038import org.onosproject.incubator.net.config.basics.InterfaceConfig;
Charles Chanc91c8782016-03-30 17:54:24 -070039import org.onosproject.incubator.net.config.basics.McastConfig;
Pier Luigiad0a67f2017-02-03 13:34:21 -080040import org.onosproject.incubator.net.intf.Interface;
Charles Chan46fdfaf2016-11-09 20:51:44 -080041import org.onosproject.incubator.net.intf.InterfaceService;
Charles Chandebfea32016-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 Ventreb6b81d52016-12-02 08:16:05 -080045import org.onosproject.incubator.net.neighbour.NeighbourResolutionService;
Jonathan Hart54541d12016-04-12 15:39:44 -070046import org.onosproject.mastership.MastershipService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080047import org.onosproject.net.ConnectPoint;
Jonathan Hart54541d12016-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 Chanf4586112015-11-09 16:37:23 -080052import org.onosproject.net.PortNumber;
Charles Chan72f556a2015-10-05 17:50:33 -070053import org.onosproject.net.config.ConfigFactory;
54import org.onosproject.net.config.NetworkConfigEvent;
Charles Chan72f556a2015-10-05 17:50:33 -070055import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070056import org.onosproject.net.config.NetworkConfigRegistry;
Charles Chan72f556a2015-10-05 17:50:33 -070057import org.onosproject.net.config.basics.SubjectFactories;
Jonathan Hart54541d12016-04-12 15:39:44 -070058import org.onosproject.net.device.DeviceEvent;
59import org.onosproject.net.device.DeviceListener;
60import org.onosproject.net.device.DeviceService;
Charles Chanf4586112015-11-09 16:37:23 -080061import org.onosproject.net.flow.TrafficSelector;
62import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hart54541d12016-04-12 15:39:44 -070063import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chanf4586112015-11-09 16:37:23 -080064import org.onosproject.net.host.HostEvent;
65import org.onosproject.net.host.HostListener;
Pier Ventreb6a7f342016-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 Chanc91c8782016-03-30 17:54:24 -070070import org.onosproject.net.mcast.McastEvent;
71import org.onosproject.net.mcast.McastListener;
72import org.onosproject.net.mcast.MulticastRouteService;
Pier Ventreb6a7f342016-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 Ventref3cf5b92016-11-09 14:17:26 -080077import org.onosproject.net.topology.PathService;
Charles Chanc91c8782016-03-30 17:54:24 -070078import org.onosproject.net.topology.TopologyService;
Pier Luigi37a35432017-02-01 13:50:04 -080079import org.onosproject.routing.config.RouterConfig;
Charles Chanc91c8782016-03-30 17:54:24 -070080import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
81import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventre6b19e482016-11-07 16:21:04 -080082import org.onosproject.segmentrouting.config.PwaasConfig;
Pier Ventreb6a7f342016-11-26 21:05:22 -080083import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Pier Ventre6b19e482016-11-07 16:21:04 -080084import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Charles Chan82f19972016-05-17 13:13:55 -070085import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chanc91c8782016-03-30 17:54:24 -070086import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
87import org.onosproject.segmentrouting.grouphandler.NeighborSet;
Charles Chan1eaf4802016-04-18 13:44:03 -070088import org.onosproject.segmentrouting.storekey.NeighborSetNextObjectiveStoreKey;
89import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -070090import org.onosproject.segmentrouting.storekey.SubnetAssignedVidStoreKey;
91import org.onosproject.segmentrouting.storekey.SubnetNextObjectiveStoreKey;
Charles Chan82f19972016-05-17 13:13:55 -070092import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Pier Ventre6b19e482016-11-07 16:21:04 -080093import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Jonathan Hart54541d12016-04-12 15:39:44 -070094import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-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 Chan41f5ec02016-06-13 18:54:31 -070099import org.opencord.cordconfig.CordConfigEvent;
100import org.opencord.cordconfig.CordConfigListener;
101import org.opencord.cordconfig.CordConfigService;
sangho80f11cb2015-04-01 13:05:26 -0700102import org.slf4j.Logger;
103import org.slf4j.LoggerFactory;
104
Saurav Das7c305372015-10-28 12:39:42 -0700105import java.util.Collections;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700106import java.util.HashSet;
sangho27462c62015-05-14 00:39:53 -0700107import java.util.List;
sangho80f11cb2015-04-01 13:05:26 -0700108import java.util.Map;
Saurav Das7c305372015-10-28 12:39:42 -0700109import java.util.Set;
sangho80f11cb2015-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 Ventreb6a7f342016-11-26 21:05:22 -0800116import java.util.stream.Collectors;
sangho80f11cb2015-04-01 13:05:26 -0700117
Charles Chand6d25332016-02-26 22:19:52 -0800118import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800119import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700120import static org.onlab.util.Tools.groupedThreads;
Charles Chand6d25332016-02-26 22:19:52 -0800121
Charles Chanb7f75ac2016-01-11 18:28:54 -0800122/**
123 * Segment routing manager.
124 */
Jonathan Hart54541d12016-04-12 15:39:44 -0700125@Service
126@Component(immediate = true)
sangho27462c62015-05-14 00:39:53 -0700127public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700128
Charles Chan46fdfaf2016-11-09 20:51:44 -0800129 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
sangho80f11cb2015-04-01 13:05:26 -0700130
131 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700132 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700133
134 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventreb6a7f342016-11-26 21:05:22 -0800135 private NeighbourResolutionService neighbourResolutionService;
136
137 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800138 public PathService pathService;
139
140 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700141 CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700142
143 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700144 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700145
146 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700147 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700148
149 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700150 DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700151
152 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800153 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700154
155 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700156 LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700157
Charles Chan82ab1932016-01-30 23:22:37 -0800158 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800159 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700160
161 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800162 public StorageService storageService;
Charles Chandebfea32016-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 Chan82ab1932016-01-30 23:22:37 -0800175
176 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800177 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800178
Saurav Dasc3604f12016-03-23 11:22:49 -0700179 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800180 public InterfaceService interfaceService;
181
Charles Chandebfea32016-10-24 14:52:01 -0700182 ArpHandler arpHandler = null;
183 IcmpHandler icmpHandler = null;
184 IpHandler ipHandler = null;
185 RoutingRulePopulator routingRulePopulator = null;
Charles Chan46fdfaf2016-11-09 20:51:44 -0800186 public ApplicationId appId;
Pier Ventref3cf5b92016-11-09 14:17:26 -0800187 public DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700188
Charles Chandebfea32016-10-24 14:52:01 -0700189 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700190 private TunnelHandler tunnelHandler = null;
191 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700192 private InternalPacketProcessor processor = null;
193 private InternalLinkListener linkListener = null;
194 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700195 private AppConfigHandler appCfgHandler = null;
Charles Chandebfea32016-10-24 14:52:01 -0700196 XConnectHandler xConnectHandler = null;
Charles Chan1eaf4802016-04-18 13:44:03 -0700197 private McastHandler mcastHandler = null;
Charles Chandebfea32016-10-24 14:52:01 -0700198 HostHandler hostHandler = null;
Charles Chan41f5ec02016-06-13 18:54:31 -0700199 private CordConfigHandler cordConfigHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800200 private RouteHandler routeHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800201 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Pier Ventre6b19e482016-11-07 16:21:04 -0800202 private L2TunnelHandler l2TunnelHandler = null;
sangho80f11cb2015-04-01 13:05:26 -0700203 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan82ab1932016-01-30 23:22:37 -0800204 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700205 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
206 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan41f5ec02016-06-13 18:54:31 -0700207 private final InternalCordConfigListener cordConfigListener = new InternalCordConfigListener();
Charles Chandebfea32016-10-24 14:52:01 -0700208 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sangho80f11cb2015-04-01 13:05:26 -0700209
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700210 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700211 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sangho80f11cb2015-04-01 13:05:26 -0700212
Saurav Das2d94d312015-11-24 23:21:05 -0800213 @SuppressWarnings("unused")
sangho80f11cb2015-04-01 13:05:26 -0700214 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das2d94d312015-11-24 23:21:05 -0800215 @SuppressWarnings("rawtypes")
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700216 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Charles Chanf4586112015-11-09 16:37:23 -0800217 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chanb7f75ac2016-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 Chanf4586112015-11-09 16:37:23 -0800223 nsNextObjStore = null;
Charles Chanb7f75ac2016-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 Chanf4586112015-11-09 16:37:23 -0800228 subnetNextObjStore = null;
Charles Chanb7f75ac2016-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 Das2d94d312015-11-24 23:21:05 -0800233 portNextObjStore = null;
Saurav Das7c305372015-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 Chanb7f75ac2016-01-11 18:28:54 -0800237 subnetVidStore = null;
Saurav Das2d94d312015-11-24 23:21:05 -0800238 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
239 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700240
Charles Chanc91c8782016-03-30 17:54:24 -0700241 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700242 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
243 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700244 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700245 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800246 public SegmentRoutingDeviceConfig createConfig() {
247 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700248 }
249 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800250
Charles Chanc91c8782016-03-30 17:54:24 -0700251 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700252 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
253 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700254 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800255 @Override
256 public SegmentRoutingAppConfig createConfig() {
257 return new SegmentRoutingAppConfig();
258 }
259 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800260
Charles Chan82f19972016-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 Ventre6b19e482016-11-07 16:21:04 -0800270
Charles Chanc91c8782016-03-30 17:54:24 -0700271 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700272 new ConfigFactory<ApplicationId, McastConfig>(
273 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700274 McastConfig.class, "multicast") {
275 @Override
276 public McastConfig createConfig() {
277 return new McastConfig();
278 }
279 };
280
Pier Ventre6b19e482016-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 Vavilapalli64d96c12015-05-14 20:22:47 -0700291 private Object threadSchedulerLock = new Object();
292 private static int numOfEventsQueued = 0;
293 private static int numOfEventsExecuted = 0;
sangho80f11cb2015-04-01 13:05:26 -0700294 private static int numOfHandlerExecution = 0;
295 private static int numOfHandlerScheduled = 0;
296
Charles Chan1963f4f2016-02-18 14:22:42 -0800297 /**
298 * Segment Routing App ID.
299 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800300 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chanb7f75ac2016-01-11 18:28:54 -0800301 /**
302 * The starting value of per-subnet VLAN ID assignment.
303 */
Saurav Das7c305372015-10-28 12:39:42 -0700304 private static final short ASSIGNED_VLAN_START = 4093;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800305 /**
306 * The default VLAN ID assigned to the interfaces without subnet config.
307 */
Saurav Das7c305372015-10-28 12:39:42 -0700308 public static final short ASSIGNED_VLAN_NO_SUBNET = 4094;
309
sangho80f11cb2015-04-01 13:05:26 -0700310 @Activate
311 protected void activate() {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800312 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700313
314 log.debug("Creating EC map nsnextobjectivestore");
315 EventuallyConsistentMapBuilder<NeighborSetNextObjectiveStoreKey, Integer>
316 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700317 nsNextObjStore = nsNextObjMapBuilder
318 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700319 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700320 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700321 .build();
322 log.trace("Current size {}", nsNextObjStore.size());
323
Charles Chan77277672015-10-20 16:24:19 -0700324 log.debug("Creating EC map subnetnextobjectivestore");
325 EventuallyConsistentMapBuilder<SubnetNextObjectiveStoreKey, Integer>
326 subnetNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Charles Chan77277672015-10-20 16:24:19 -0700327 subnetNextObjStore = subnetNextObjMapBuilder
328 .withName("subnetnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700329 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700330 .withTimestampProvider((k, v) -> new WallClockTimestamp())
331 .build();
332
Saurav Das2d94d312015-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 Hart54541d12016-04-12 15:39:44 -0700338 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800339 .withTimestampProvider((k, v) -> new WallClockTimestamp())
340 .build();
341
sangho4a5c42a2015-05-20 22:16:38 -0700342 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
343 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700344 tunnelStore = tunnelMapBuilder
345 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700346 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700347 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700348 .build();
349
350 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
351 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700352 policyStore = policyMapBuilder
353 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700354 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700355 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700356 .build();
357
Saurav Das7c305372015-10-28 12:39:42 -0700358 EventuallyConsistentMapBuilder<SubnetAssignedVidStoreKey, VlanId>
359 subnetVidStoreMapBuilder = storageService.eventuallyConsistentMapBuilder();
Saurav Das7c305372015-10-28 12:39:42 -0700360 subnetVidStore = subnetVidStoreMapBuilder
361 .withName("subnetvidstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700362 .withSerializer(createSerializer())
Saurav Das7c305372015-10-28 12:39:42 -0700363 .withTimestampProvider((k, v) -> new WallClockTimestamp())
364 .build();
365
Saurav Dasc3604f12016-03-23 11:22:49 -0700366 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800367 "purgeOnDisconnection", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700368 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800369 "purgeOnDisconnection", "true");
Pier Luigib9632ba2017-01-12 18:14:58 -0800370 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
371 "requestInterceptsEnabled", "false");
Pier Luigibc976df2017-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 Luigi0ebeb312017-02-02 22:31:34 -0800376 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
377 "greedyLearningIpv6", "true");
378
Saurav Dasc3604f12016-03-23 11:22:49 -0700379
Charles Chan2b078ae2015-10-14 11:24:40 -0700380 processor = new InternalPacketProcessor();
381 linkListener = new InternalLinkListener();
382 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700383 appCfgHandler = new AppConfigHandler(this);
384 xConnectHandler = new XConnectHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700385 mcastHandler = new McastHandler(this);
386 hostHandler = new HostHandler(this);
Charles Chan41f5ec02016-06-13 18:54:31 -0700387 cordConfigHandler = new CordConfigHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700388 routeHandler = new RouteHandler(this);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800389 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Pier Ventre6b19e482016-11-07 16:21:04 -0800390 l2TunnelHandler = new L2TunnelHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700391
Charles Chand6d25332016-02-26 22:19:52 -0800392 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700393 cfgService.registerConfigFactory(deviceConfigFactory);
394 cfgService.registerConfigFactory(appConfigFactory);
Charles Chan82f19972016-05-17 13:13:55 -0700395 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700396 cfgService.registerConfigFactory(mcastConfigFactory);
Pier Ventre6b19e482016-11-07 16:21:04 -0800397 cfgService.registerConfigFactory(pwaasConfigFactory);
Charles Chan82ab1932016-01-30 23:22:37 -0800398 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700399 packetService.addProcessor(processor, PacketProcessor.director(2));
400 linkService.addListener(linkListener);
401 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700402 multicastRouteService.addListener(mcastListener);
Charles Chan41f5ec02016-06-13 18:54:31 -0700403 cordConfigService.addListener(cordConfigListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700404
405 cfgListener.configureNetwork();
406
Charles Chandebfea32016-10-24 14:52:01 -0700407 routeService.addListener(routeListener);
408
sangho80f11cb2015-04-01 13:05:26 -0700409 log.info("Started");
410 }
411
Jonathan Hart54541d12016-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 Chan82f19972016-05-17 13:13:55 -0700425 XConnectStoreKey.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700426 );
427 }
428
sangho80f11cb2015-04-01 13:05:26 -0700429 @Deactivate
430 protected void deactivate() {
Charles Chan72f556a2015-10-05 17:50:33 -0700431 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700432 cfgService.unregisterConfigFactory(deviceConfigFactory);
433 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chandebfea32016-10-24 14:52:01 -0700434 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700435 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventre6b19e482016-11-07 16:21:04 -0800436 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chan72f556a2015-10-05 17:50:33 -0700437
sangho80f11cb2015-04-01 13:05:26 -0700438 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700439 linkService.removeListener(linkListener);
440 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700441 multicastRouteService.removeListener(mcastListener);
Charles Chan41f5ec02016-06-13 18:54:31 -0700442 cordConfigService.removeListener(cordConfigListener);
Charles Chandebfea32016-10-24 14:52:01 -0700443 routeService.removeListener(routeListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700444
sangho80f11cb2015-04-01 13:05:26 -0700445 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700446 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700447 deviceListener = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700448 groupHandlerMap.clear();
449
Charles Chanc91c8782016-03-30 17:54:24 -0700450 nsNextObjStore.destroy();
451 subnetNextObjStore.destroy();
452 portNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700453 tunnelStore.destroy();
454 policyStore.destroy();
455 subnetVidStore.destroy();
sangho80f11cb2015-04-01 13:05:26 -0700456 log.info("Stopped");
457 }
458
sangho27462c62015-05-14 00:39:53 -0700459 @Override
460 public List<Tunnel> getTunnels() {
461 return tunnelHandler.getTunnels();
462 }
463
464 @Override
sanghobd812f82015-06-29 14:58:47 -0700465 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
466 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700467 }
468
469 @Override
sanghobd812f82015-06-29 14:58:47 -0700470 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-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");
sanghobd812f82015-06-29 14:58:47 -0700476 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700477 }
478 }
479 }
sanghobd812f82015-06-29 14:58:47 -0700480 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700481 }
482
483 @Override
sanghobd812f82015-06-29 14:58:47 -0700484 public PolicyHandler.Result removePolicy(Policy policy) {
485 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700486 }
487
488 @Override
sanghobd812f82015-06-29 14:58:47 -0700489 public PolicyHandler.Result createPolicy(Policy policy) {
490 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700491 }
492
493 @Override
494 public List<Policy> getPolicies() {
495 return policyHandler.getPolicies();
496 }
497
Saurav Das07c74602016-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 Chand7844e52016-10-20 17:02:44 -0700507 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800508 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
509 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Charles Chand7844e52016-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
sangho80f1f892015-05-19 11:57:42 -0700516 /**
Pier Ventre7a78de22016-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 /**
sangho80f1f892015-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 */
sangho27462c62015-05-14 00:39:53 -0700533 public Tunnel getTunnel(String tunnelId) {
534 return tunnelHandler.getTunnel(tunnelId);
535 }
536
sangho80f11cb2015-04-01 13:05:26 -0700537 /**
Saurav Das7c305372015-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.
sangho80f11cb2015-04-01 13:05:26 -0700550 *
Saurav Das7c305372015-10-28 12:39:42 -0700551 * @param deviceId switch dpid
Pier Ventre1a655962016-11-28 16:48:06 -0800552 * @param subnet IP prefix for which assigned vlan is desired
Saurav Das7c305372015-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.
sangho80f11cb2015-04-01 13:05:26 -0700556 */
Charles Chanb7f75ac2016-01-11 18:28:54 -0800557 // TODO: We should avoid assigning VLAN IDs that are used by VLAN cross-connection.
Pier Ventreb6a7f342016-11-26 21:05:22 -0800558 public VlanId getSubnetAssignedVlanId(DeviceId deviceId, IpPrefix subnet) {
Saurav Das7c305372015-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 Ventreb6a7f342016-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 Das7c305372015-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 Ventre1a655962016-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);
sangho80f11cb2015-04-01 13:05:26 -0700599 }
Saurav Das7c305372015-10-28 12:39:42 -0700600 return subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId, subnet));
sangho80f11cb2015-04-01 13:05:26 -0700601 }
602
sangho27462c62015-05-14 00:39:53 -0700603 /**
Saurav Das7c305372015-10-28 12:39:42 -0700604 * Returns the next objective ID for the given NeighborSet.
Saurav Das4c35fc42015-11-20 15:27:53 -0800605 * If the nextObjective does not exist, a new one is created and
Saurav Das2d94d312015-11-24 23:21:05 -0800606 * its id is returned.
sangho27462c62015-05-14 00:39:53 -0700607 *
sangho80f1f892015-05-19 11:57:42 -0700608 * @param deviceId Device ID
609 * @param ns NegighborSet
Saurav Das4c35fc42015-11-20 15:27:53 -0800610 * @param meta metadata passed into the creation of a Next Objective
Pier Ventre229fd0b2016-10-31 16:49:19 -0700611 * @param isBos indicates if it is BoS or not
Saurav Das4c35fc42015-11-20 15:27:53 -0800612 * @return next objective ID or -1 if an error was encountered during the
613 * creation of the nextObjective
sangho27462c62015-05-14 00:39:53 -0700614 */
Saurav Das4c35fc42015-11-20 15:27:53 -0800615 public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns,
Pier Ventre229fd0b2016-10-31 16:49:19 -0700616 TrafficSelector meta, boolean isBos) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700617 if (groupHandlerMap.get(deviceId) != null) {
618 log.trace("getNextObjectiveId query in device {}", deviceId);
619 return groupHandlerMap
Pier Ventre229fd0b2016-10-31 16:49:19 -0700620 .get(deviceId).getNextObjectiveId(ns, meta, isBos);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700621 } else {
Saurav Das2d94d312015-11-24 23:21:05 -0800622 log.warn("getNextObjectiveId query - groupHandler for device {} "
623 + "not found", deviceId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700624 return -1;
625 }
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700626 }
627
Charles Chan77277672015-10-20 16:24:19 -0700628 /**
Pier Ventre229fd0b2016-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 Das2d94d312015-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 Chan77277672015-10-20 16:24:19 -0700648 *
649 * @param deviceId Device ID
650 * @param prefix Subnet
Saurav Das2d94d312015-11-24 23:21:05 -0800651 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-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 Das2d94d312015-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 Das07c74602016-04-27 18:35:50 -0700675 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-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 Chanb7f75ac2016-01-11 18:28:54 -0800684 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
685 + " not found", deviceId);
686 return -1;
687 }
688 }
689
sangho80f11cb2015-04-01 13:05:26 -0700690 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-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 Luigi37a35432017-02-01 13:50:04 -0800700
701 if (ethernet == null) {
702 return;
703 }
704
Saurav Das2d94d312015-11-24 23:21:05 -0800705 log.trace("Rcvd pktin: {}", ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -0800706 if (ethernet.getEtherType() == TYPE_ARP) {
Pier Ventreb6a7f342016-11-26 21:05:22 -0800707 log.warn("{} - we are still receiving ARP packets from {}",
708 context.inPacket().receivedFrom());
709 log.debug("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800710 return;
sangho80f11cb2015-04-01 13:05:26 -0700711 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -0800712 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
713 //ipHandler.addToPacketBuffer(ipv4Packet);
714 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
715 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -0700716 } else {
Charles Chand041ad82017-01-13 17:20:44 -0800717 // NOTE: We don't support IP learning at this moment so this
718 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800719 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -0700720 }
Pier Ventreb6a7f342016-11-26 21:05:22 -0800721 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
722 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -0800723 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -0800724 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -0800725 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
726 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
727 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
728 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
729 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
730 } else {
Pier Luigi37a35432017-02-01 13:50:04 -0800731 // XXX Neigbour hacking, to handle the ICMPv6 packet
732 // not under our control
733 if (icmpHandler.handleUPstreamPackets(context)) {
734 log.debug("Rcvd pktin from UpStream: {}", ipv6Packet);
735 } else {
736 log.debug("Received ICMPv6 0x{} - not handled",
737 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
738 }
Pier Ventreb6b81d52016-12-02 08:16:05 -0800739 }
740 } else {
741 // NOTE: We don't support IP learning at this moment so this
742 // is not necessary. Also it causes duplication of DHCPv6 packets.
743 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
744 }
sangho80f11cb2015-04-01 13:05:26 -0700745 }
746 }
747 }
748
749 private class InternalLinkListener implements LinkListener {
750 @Override
751 public void event(LinkEvent event) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700752 if (event.type() == LinkEvent.Type.LINK_ADDED
753 || event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700754 log.debug("Event {} received from Link Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -0700755 scheduleEventHandlerIfNotScheduled(event);
756 }
757 }
758 }
759
760 private class InternalDeviceListener implements DeviceListener {
sangho80f11cb2015-04-01 13:05:26 -0700761 @Override
762 public void event(DeviceEvent event) {
sangho80f11cb2015-04-01 13:05:26 -0700763 switch (event.type()) {
764 case DEVICE_ADDED:
Saurav Dasf0f592d2016-11-18 15:21:57 -0800765 case PORT_UPDATED:
766 case PORT_ADDED:
sanghofb7c7292015-04-13 15:15:58 -0700767 case DEVICE_UPDATED:
768 case DEVICE_AVAILABILITY_CHANGED:
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700769 log.debug("Event {} received from Device Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -0700770 scheduleEventHandlerIfNotScheduled(event);
771 break;
772 default:
773 }
774 }
775 }
776
Saurav Das2d94d312015-11-24 23:21:05 -0800777 @SuppressWarnings("rawtypes")
sangho80f11cb2015-04-01 13:05:26 -0700778 private void scheduleEventHandlerIfNotScheduled(Event event) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700779 synchronized (threadSchedulerLock) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700780 eventQueue.add(event);
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700781 numOfEventsQueued++;
782
783 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
784 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700785 eventHandlerFuture = executorService
786 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
787 numOfHandlerScheduled++;
788 }
Jonathan Hart54541d12016-04-12 15:39:44 -0700789 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700790 numOfEventsQueued,
791 numOfHandlerScheduled);
sangho80f11cb2015-04-01 13:05:26 -0700792 }
sangho80f11cb2015-04-01 13:05:26 -0700793 }
794
795 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700796 @Override
sangho80f11cb2015-04-01 13:05:26 -0700797 public void run() {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700798 try {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700799 while (true) {
Saurav Das2d94d312015-11-24 23:21:05 -0800800 @SuppressWarnings("rawtypes")
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700801 Event event = null;
802 synchronized (threadSchedulerLock) {
803 if (!eventQueue.isEmpty()) {
804 event = eventQueue.poll();
805 numOfEventsExecuted++;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700806 } else {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700807 numOfHandlerExecution++;
808 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
809 numOfHandlerExecution, numOfEventsExecuted);
810 break;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700811 }
sanghofb7c7292015-04-13 15:15:58 -0700812 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700813 if (event.type() == LinkEvent.Type.LINK_ADDED) {
814 processLinkAdded((Link) event.subject());
815 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre6d593892016-09-13 21:33:40 -0700816 Link linkRemoved = (Link) event.subject();
817 if (linkRemoved.src().elementId() instanceof DeviceId &&
818 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
819 continue;
820 }
821 if (linkRemoved.dst().elementId() instanceof DeviceId &&
822 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
823 continue;
824 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700825 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700826 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
827 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
828 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das62af8802015-12-04 10:52:59 -0800829 DeviceId deviceId = ((Device) event.subject()).id();
830 if (deviceService.isAvailable(deviceId)) {
Saurav Dasc28b3432015-10-30 17:45:38 -0700831 log.info("Processing device event {} for available device {}",
832 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700833 processDeviceAdded((Device) event.subject());
Saurav Dasc3604f12016-03-23 11:22:49 -0700834 } else {
835 log.info("Processing device event {} for unavailable device {}",
836 event.type(), ((Device) event.subject()).id());
837 processDeviceRemoved((Device) event.subject());
838 }
Saurav Dasf0f592d2016-11-18 15:21:57 -0800839 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd1872b02016-12-02 15:43:47 -0800840 // typically these calls come when device is added first time
841 // so port filtering rules are handled at the device_added event.
842 // port added calls represent all ports on the device,
843 // enabled or not.
Saurav Dasf0f592d2016-11-18 15:21:57 -0800844 log.debug("** PORT ADDED {}/{} -> {}",
Saurav Dasd1872b02016-12-02 15:43:47 -0800845 ((DeviceEvent) event).subject().id(),
846 ((DeviceEvent) event).port().number(),
847 event.type());
Saurav Dasf0f592d2016-11-18 15:21:57 -0800848 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd1872b02016-12-02 15:43:47 -0800849 // these calls happen for every subsequent event
850 // ports enabled, disabled, switch goes away, comes back
Saurav Dasf0f592d2016-11-18 15:21:57 -0800851 log.info("** PORT UPDATED {}/{} -> {}",
852 event.subject(),
853 ((DeviceEvent) event).port(),
854 event.type());
855 processPortUpdated(((Device) event.subject()),
856 ((DeviceEvent) event).port());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700857 } else {
858 log.warn("Unhandled event type: {}", event.type());
859 }
sangho80f11cb2015-04-01 13:05:26 -0700860 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700861 } catch (Exception e) {
862 log.error("SegmentRouting event handler "
863 + "thread thrown an exception: {}", e);
sangho80f11cb2015-04-01 13:05:26 -0700864 }
sangho80f11cb2015-04-01 13:05:26 -0700865 }
866 }
867
sangho80f11cb2015-04-01 13:05:26 -0700868 private void processLinkAdded(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -0700869 log.info("** LINK ADDED {}", link.toString());
Charles Chan319d1a22015-11-03 10:42:14 -0800870 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
871 log.warn("Source device of this link is not configured.");
872 return;
873 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700874 //Irrespective whether the local is a MASTER or not for this device,
875 //create group handler instance and push default TTP flow rules.
876 //Because in a multi-instance setup, instances can initiate
877 //groups for any devices. Also the default TTP rules are needed
878 //to be pushed before inserting any IP table entries for any device
879 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
880 .deviceId());
881 if (groupHandler != null) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800882 groupHandler.linkUp(link, mastershipService.isLocalMaster(
883 link.src().deviceId()));
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700884 } else {
885 Device device = deviceService.getDevice(link.src().deviceId());
886 if (device != null) {
887 log.warn("processLinkAdded: Link Added "
888 + "Notification without Device Added "
889 + "event, still handling it");
890 processDeviceAdded(device);
891 groupHandler = groupHandlerMap.get(link.src()
892 .deviceId());
Saurav Das4c35fc42015-11-20 15:27:53 -0800893 groupHandler.linkUp(link, mastershipService.isLocalMaster(device.id()));
sangho80f11cb2015-04-01 13:05:26 -0700894 }
895 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700896
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700897 log.trace("Starting optimized route population process");
898 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null);
899 //log.trace("processLinkAdded: re-starting route population process");
900 //defaultRoutingHandler.startPopulationProcess();
Charles Chan72779502016-04-23 17:36:10 -0700901
902 mcastHandler.init();
sangho80f11cb2015-04-01 13:05:26 -0700903 }
904
905 private void processLinkRemoved(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -0700906 log.info("** LINK REMOVED {}", link.toString());
sangho2165d222015-05-01 09:38:25 -0700907 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
908 if (groupHandler != null) {
Saurav Das62af8802015-12-04 10:52:59 -0800909 groupHandler.portDown(link.src().port(),
910 mastershipService.isLocalMaster(link.src().deviceId()));
sangho2165d222015-05-01 09:38:25 -0700911 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700912 log.trace("Starting optimized route population process");
913 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link);
914 //log.trace("processLinkRemoved: re-starting route population process");
915 //defaultRoutingHandler.startPopulationProcess();
Charles Chan72779502016-04-23 17:36:10 -0700916
917 mcastHandler.processLinkDown(link);
sangho80f11cb2015-04-01 13:05:26 -0700918 }
919
920 private void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -0700921 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan319d1a22015-11-03 10:42:14 -0800922 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das8ec0ec42015-11-03 14:39:27 -0800923 log.warn("Device configuration uploading. Device {} will be "
924 + "processed after config completes.", device.id());
925 return;
926 }
Charles Chan72779502016-04-23 17:36:10 -0700927 processDeviceAddedInternal(device.id());
928 }
929
930 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -0700931 // Irrespective of whether the local is a MASTER or not for this device,
932 // we need to create a SR-group-handler instance. This is because in a
933 // multi-instance setup, any instance can initiate forwarding/next-objectives
934 // for any switch (even if this instance is a SLAVE or not even connected
935 // to the switch). To handle this, a default-group-handler instance is necessary
936 // per switch.
Charles Chan72779502016-04-23 17:36:10 -0700937 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
938 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -0800939 DefaultGroupHandler groupHandler;
940 try {
941 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -0700942 createGroupHandler(deviceId,
943 appId,
944 deviceConfiguration,
945 linkService,
946 flowObjectiveService,
947 this);
Charles Chan319d1a22015-11-03 10:42:14 -0800948 } catch (DeviceConfigNotFoundException e) {
949 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
950 return;
951 }
Charles Chan72779502016-04-23 17:36:10 -0700952 log.debug("updating groupHandlerMap with new config for device: {}",
953 deviceId);
954 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -0800955 }
Saurav Dasb149be12016-06-07 10:08:06 -0700956 // Also, in some cases, drivers may need extra
957 // information to process rules (eg. Router IP/MAC); and so, we send
958 // port addressing rules to the driver as well irrespective of whether
959 // this instance is the master or not.
960 defaultRoutingHandler.populatePortAddressingRules(deviceId);
961
Charles Chan72779502016-04-23 17:36:10 -0700962 if (mastershipService.isLocalMaster(deviceId)) {
Charles Chandebfea32016-10-24 14:52:01 -0700963 hostHandler.init(deviceId);
Charles Chan82f19972016-05-17 13:13:55 -0700964 xConnectHandler.init(deviceId);
Charles Chan41f5ec02016-06-13 18:54:31 -0700965 cordConfigHandler.init(deviceId);
Charles Chan72779502016-04-23 17:36:10 -0700966 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan77277672015-10-20 16:24:19 -0700967 groupHandler.createGroupsFromSubnetConfig();
Charles Chan72779502016-04-23 17:36:10 -0700968 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -0700969 }
Charles Chan82ab1932016-01-30 23:22:37 -0800970
Charles Chandebfea32016-10-24 14:52:01 -0700971 appCfgHandler.init(deviceId);
972 routeHandler.init(deviceId);
sangho80f11cb2015-04-01 13:05:26 -0700973 }
974
Saurav Dasc3604f12016-03-23 11:22:49 -0700975 private void processDeviceRemoved(Device device) {
976 nsNextObjStore.entrySet().stream()
977 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
978 .forEach(entry -> {
979 nsNextObjStore.remove(entry.getKey());
980 });
Saurav Dasc3604f12016-03-23 11:22:49 -0700981 subnetNextObjStore.entrySet().stream()
982 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
983 .forEach(entry -> {
984 subnetNextObjStore.remove(entry.getKey());
985 });
Saurav Dasc3604f12016-03-23 11:22:49 -0700986 portNextObjStore.entrySet().stream()
987 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
988 .forEach(entry -> {
989 portNextObjStore.remove(entry.getKey());
990 });
Saurav Dasc3604f12016-03-23 11:22:49 -0700991 subnetVidStore.entrySet().stream()
992 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
993 .forEach(entry -> {
994 subnetVidStore.remove(entry.getKey());
995 });
Saurav Dasc3604f12016-03-23 11:22:49 -0700996 groupHandlerMap.remove(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -0700997 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan72779502016-04-23 17:36:10 -0700998 mcastHandler.removeDevice(device.id());
Charles Chan82f19972016-05-17 13:13:55 -0700999 xConnectHandler.removeDevice(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -07001000 }
1001
Saurav Dasf0f592d2016-11-18 15:21:57 -08001002 private void processPortUpdated(Device device, Port port) {
1003 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1004 log.warn("Device configuration uploading. Not handling port event for"
1005 + "dev: {} port: {}", device.id(), port.number());
1006 return;
1007 }
1008 /* XXX create method for single port filtering rules which are needed
1009 for both switch-to-switch ports and edge ports
1010 if (defaultRoutingHandler != null) {
1011 defaultRoutingHandler.populatePortAddressingRules(
1012 ((Device) event.subject()).id());
1013 }*/
1014
1015 // portUpdated calls are for ports that have gone down or up. For switch
1016 // to switch ports, link-events should take care of any re-routing or
1017 // group editing necessary for port up/down. Here we only process edge ports
1018 // that are already configured.
Pier Ventreb6a7f342016-11-26 21:05:22 -08001019 Ip4Prefix configuredSubnet = deviceConfiguration.getPortIPv4Subnet(device.id(),
Saurav Dasf0f592d2016-11-18 15:21:57 -08001020 port.number());
1021 if (configuredSubnet == null) {
1022 log.debug("Not handling port updated event for unconfigured port "
1023 + "dev/port: {}/{}", device.id(), port.number());
1024 return;
1025 }
1026 processEdgePort(device, port, configuredSubnet);
1027 }
1028
1029 private void processEdgePort(Device device, Port port, Ip4Prefix subnet) {
1030 boolean portUp = port.isEnabled();
1031 if (portUp) {
1032 log.info("Device:EdgePort {}:{} is enabled in subnet: {}", device.id(),
1033 port.number(), subnet);
1034 } else {
1035 log.info("Device:EdgePort {}:{} is disabled in subnet: {}", device.id(),
1036 port.number(), subnet);
1037 }
1038
Srikanth Vavilapalli64505482015-04-21 13:04:13 -07001039 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sangho80f11cb2015-04-01 13:05:26 -07001040 if (groupHandler != null) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001041 groupHandler.processEdgePort(port.number(), subnet, portUp,
Saurav Das62af8802015-12-04 10:52:59 -08001042 mastershipService.isLocalMaster(device.id()));
Saurav Dasf0f592d2016-11-18 15:21:57 -08001043 } else {
1044 log.warn("Group handler not found for dev:{}. Not handling edge port"
1045 + " {} event for port:{}", device.id(),
1046 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001047 }
1048 }
sangho27462c62015-05-14 00:39:53 -07001049
Pier Ventreb6a7f342016-11-26 21:05:22 -08001050 /**
1051 * Registers the given connect point with the NRS, this is necessary
1052 * to receive the NDP and ARP packets from the NRS.
1053 *
1054 * @param portToRegister connect point to register
1055 */
1056 public void registerConnectPoint(ConnectPoint portToRegister) {
Pier Ventreb6a7f342016-11-26 21:05:22 -08001057 this.neighbourResolutionService.registerNeighbourHandler(
1058 portToRegister,
1059 neighbourHandler,
1060 appId
1061 );
1062 }
1063
Charles Chan72f556a2015-10-05 17:50:33 -07001064 private class InternalConfigListener implements NetworkConfigListener {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001065 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001066
Charles Chanb7f75ac2016-01-11 18:28:54 -08001067 /**
1068 * Constructs the internal network config listener.
1069 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001070 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001071 */
Charles Chan46fdfaf2016-11-09 20:51:44 -08001072 public InternalConfigListener(SegmentRoutingManager srManager) {
1073 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001074 }
1075
Charles Chanb7f75ac2016-01-11 18:28:54 -08001076 /**
1077 * Reads network config and initializes related data structure accordingly.
1078 */
Charles Chane7c61022015-10-07 14:21:45 -07001079 public void configureNetwork() {
Pier Ventreb6a7f342016-11-26 21:05:22 -08001080
Charles Chan46fdfaf2016-11-09 20:51:44 -08001081 deviceConfiguration = new DeviceConfiguration(srManager);
Charles Chane7c61022015-10-07 14:21:45 -07001082
Charles Chan46fdfaf2016-11-09 20:51:44 -08001083 arpHandler = new ArpHandler(srManager);
1084 icmpHandler = new IcmpHandler(srManager);
1085 ipHandler = new IpHandler(srManager);
1086 routingRulePopulator = new RoutingRulePopulator(srManager);
1087 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chane7c61022015-10-07 14:21:45 -07001088
1089 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1090 groupHandlerMap, tunnelStore);
1091 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1092 flowObjectiveService,
1093 tunnelHandler, policyStore);
1094
Charles Chane7c61022015-10-07 14:21:45 -07001095 for (Device device : deviceService.getDevices()) {
Charles Chan72779502016-04-23 17:36:10 -07001096 processDeviceAddedInternal(device.id());
Charles Chane7c61022015-10-07 14:21:45 -07001097 }
1098
1099 defaultRoutingHandler.startPopulationProcess();
Charles Chan72779502016-04-23 17:36:10 -07001100 mcastHandler.init();
Charles Chane7c61022015-10-07 14:21:45 -07001101 }
1102
Charles Chan72f556a2015-10-05 17:50:33 -07001103 @Override
1104 public void event(NetworkConfigEvent event) {
Charles Chan82ab1932016-01-30 23:22:37 -08001105 // TODO move this part to NetworkConfigEventHandler
1106 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1107 switch (event.type()) {
1108 case CONFIG_ADDED:
1109 log.info("Segment Routing Config added.");
1110 configureNetwork();
1111 break;
1112 case CONFIG_UPDATED:
1113 log.info("Segment Routing Config updated.");
1114 // TODO support dynamic configuration
1115 break;
1116 default:
1117 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001118 }
Charles Chan82ab1932016-01-30 23:22:37 -08001119 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chan82f19972016-05-17 13:13:55 -07001120 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan82ab1932016-01-30 23:22:37 -08001121 switch (event.type()) {
1122 case CONFIG_ADDED:
Charles Chan82f19972016-05-17 13:13:55 -07001123 appCfgHandler.processAppConfigAdded(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001124 break;
1125 case CONFIG_UPDATED:
Charles Chan82f19972016-05-17 13:13:55 -07001126 appCfgHandler.processAppConfigUpdated(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001127 break;
1128 case CONFIG_REMOVED:
Charles Chan82f19972016-05-17 13:13:55 -07001129 appCfgHandler.processAppConfigRemoved(event);
1130 break;
1131 default:
1132 break;
1133 }
Charles Chandebfea32016-10-24 14:52:01 -07001134 configureNetwork();
Charles Chan82f19972016-05-17 13:13:55 -07001135 } else if (event.configClass().equals(XConnectConfig.class)) {
1136 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1137 switch (event.type()) {
1138 case CONFIG_ADDED:
1139 xConnectHandler.processXConnectConfigAdded(event);
1140 break;
1141 case CONFIG_UPDATED:
1142 xConnectHandler.processXConnectConfigUpdated(event);
1143 break;
1144 case CONFIG_REMOVED:
1145 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001146 break;
1147 default:
1148 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001149 }
Pier Luigi37a35432017-02-01 13:50:04 -08001150 // XXX Neighbour hacking. This method is looking for
1151 // the Internet-Router interface. In order to retrieve
1152 // the upstream port.
1153 } else if (event.configClass().equals(InterfaceConfig.class)) {
1154 switch (event.type()) {
1155 case CONFIG_ADDED:
1156 case CONFIG_UPDATED:
Pier Luigiad0a67f2017-02-03 13:34:21 -08001157 updateUPstreamCP();
Pier Luigi37a35432017-02-01 13:50:04 -08001158 case CONFIG_REGISTERED:
1159 case CONFIG_UNREGISTERED:
1160 case CONFIG_REMOVED:
1161 break;
1162 default:
1163 break;
1164 }
1165 // XXX Neighbour hacking. This method is looking for
1166 // the vrouter port.
1167 } else if (event.configClass().equals(RouterConfig.class)) {
1168 switch (event.type()) {
1169 case CONFIG_ADDED:
1170 case CONFIG_UPDATED:
Pier Luigiad0a67f2017-02-03 13:34:21 -08001171 updateVRouterCP(event);
Pier Luigi37a35432017-02-01 13:50:04 -08001172 case CONFIG_REGISTERED:
1173 case CONFIG_UNREGISTERED:
1174 case CONFIG_REMOVED:
1175 break;
1176 default:
1177 break;
1178 }
Pier Ventre6b19e482016-11-07 16:21:04 -08001179 } else if (event.configClass().equals(PwaasConfig.class)) {
1180 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1181 switch (event.type()) {
1182 case CONFIG_ADDED:
1183 l2TunnelHandler.processPwaasConfigAdded(event);
1184 break;
1185 case CONFIG_UPDATED:
1186 l2TunnelHandler.processPwaasConfigUpdated(event);
1187 break;
1188 case CONFIG_REMOVED:
1189 l2TunnelHandler.processPwaasConfigRemoved(event);
1190 break;
1191 default:
1192 break;
1193 }
Charles Chan72f556a2015-10-05 17:50:33 -07001194 }
1195 }
1196 }
Charles Chanf4586112015-11-09 16:37:23 -08001197
Pier Luigiad0a67f2017-02-03 13:34:21 -08001198 /////////////////////////////////////////////////////////////////
1199 // XXX Neighbour hacking, temporary workaround will be //
1200 // removed as soon as possible, when the bridging will //
1201 // be implemented. For now, it's fine to leave this //
1202 /////////////////////////////////////////////////////////////////
1203
1204 // XXX Neighbour hacking. To store upstream connect
1205 // point and vRouter connect point
1206 ConnectPoint upstreamCP = null;
1207 ConnectPoint vRouterCP = null;
1208
1209 // XXX Neighbour hacking. To update the Upstream CP
1210 public void updateUPstreamCP() {
1211 Set<ConnectPoint> portSubjects = cfgService.getSubjects(ConnectPoint.class, InterfaceConfig.class);
1212 upstreamCP = null;
1213 portSubjects.stream().forEach(subject -> {
1214 InterfaceConfig config = cfgService.getConfig(subject, InterfaceConfig.class);
1215 Set<Interface> networkInterfaces;
1216 try {
1217 networkInterfaces = config.getInterfaces();
1218 } catch (ConfigException e) {
1219 log.error("Error loading port configuration");
1220 return;
1221 }
1222 networkInterfaces.forEach(networkInterface -> {
1223 if (networkInterface.name().equals("internet-router")) {
1224 upstreamCP = subject;
1225 }
1226 });
1227 });
1228
1229 }
1230
1231 // XXX Neighbour hacking. To update the Upstream CP
1232 public void updateVRouterCP(NetworkConfigEvent event) {
1233 RouterConfig config = (RouterConfig) event.config().get();
1234 if (config == null) {
1235 log.warn("Router config not available");
1236 vRouterCP = null;
1237 return;
1238 }
1239 vRouterCP = config.getControlPlaneConnectPoint();
1240 }
1241
Charles Chanf4586112015-11-09 16:37:23 -08001242 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001243 @Override
1244 public void event(HostEvent event) {
1245 // Do not proceed without mastership
1246 DeviceId deviceId = event.subject().location().deviceId();
1247 if (!mastershipService.isLocalMaster(deviceId)) {
1248 return;
1249 }
1250
1251 switch (event.type()) {
1252 case HOST_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001253 hostHandler.processHostAddedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001254 break;
1255 case HOST_MOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001256 hostHandler.processHostMovedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001257 break;
1258 case HOST_REMOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001259 hostHandler.processHostRemoveEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001260 break;
1261 case HOST_UPDATED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001262 hostHandler.processHostUpdatedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001263 break;
1264 default:
1265 log.warn("Unsupported host event type: {}", event.type());
1266 break;
1267 }
1268 }
1269 }
1270
Charles Chanc91c8782016-03-30 17:54:24 -07001271 private class InternalMcastListener implements McastListener {
1272 @Override
1273 public void event(McastEvent event) {
1274 switch (event.type()) {
1275 case SOURCE_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001276 mcastHandler.processSourceAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001277 break;
1278 case SINK_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001279 mcastHandler.processSinkAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001280 break;
1281 case SINK_REMOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001282 mcastHandler.processSinkRemoved(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001283 break;
1284 case ROUTE_ADDED:
1285 case ROUTE_REMOVED:
1286 default:
1287 break;
1288 }
1289 }
1290 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001291
1292 private class InternalCordConfigListener implements CordConfigListener {
1293 @Override
1294 public void event(CordConfigEvent event) {
1295 switch (event.type()) {
1296 case ACCESS_AGENT_ADDED:
1297 cordConfigHandler.processAccessAgentAddedEvent(event);
1298 break;
1299 case ACCESS_AGENT_UPDATED:
1300 cordConfigHandler.processAccessAgentUpdatedEvent(event);
1301 break;
1302 case ACCESS_AGENT_REMOVED:
1303 cordConfigHandler.processAccessAgentRemovedEvent(event);
1304 break;
1305 case ACCESS_DEVICE_ADDED:
1306 case ACCESS_DEVICE_UPDATED:
1307 case ACCESS_DEVICE_REMOVED:
1308 default:
1309 break;
1310 }
1311 }
1312 }
Charles Chandebfea32016-10-24 14:52:01 -07001313
1314 private class InternalRouteEventListener implements RouteListener {
1315 @Override
1316 public void event(RouteEvent event) {
1317 switch (event.type()) {
1318 case ROUTE_ADDED:
1319 routeHandler.processRouteAdded(event);
1320 break;
1321 case ROUTE_UPDATED:
1322 routeHandler.processRouteUpdated(event);
1323 break;
1324 case ROUTE_REMOVED:
1325 routeHandler.processRouteRemoved(event);
1326 break;
1327 default:
1328 break;
1329 }
1330 }
1331 }
sangho80f11cb2015-04-01 13:05:26 -07001332}