blob: d7896a1efae4a307d62163020bace3e8a497150d [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()) {
Saurav Dasf9332192017-02-18 14:05:44 -0800502 if (mastershipService.isLocalMaster(device.id())) {
503 defaultRoutingHandler.populatePortAddressingRules(device.id());
504 }
Saurav Das07c74602016-04-27 18:35:50 -0700505 }
506 defaultRoutingHandler.startPopulationProcess();
507 }
508
Charles Chand7844e52016-10-20 17:02:44 -0700509 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800510 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
511 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Charles Chand7844e52016-10-20 17:02:44 -0700512 deviceService.getAvailableDevices().forEach(device -> {
513 deviceSubnetMap.put(device.id(), deviceConfiguration.getSubnets(device.id()));
514 });
515 return deviceSubnetMap;
516 }
517
sangho80f1f892015-05-19 11:57:42 -0700518 /**
Pier Ventre7a78de22016-10-31 15:00:01 -0700519 * Returns the MPLS-ECMP configuration.
520 *
521 * @return MPLS-ECMP value
522 */
523 public boolean getMplsEcmp() {
524 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
525 .getConfig(this.appId, SegmentRoutingAppConfig.class);
526 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
527 }
528
529 /**
sangho80f1f892015-05-19 11:57:42 -0700530 * Returns the tunnel object with the tunnel ID.
531 *
532 * @param tunnelId Tunnel ID
533 * @return Tunnel reference
534 */
sangho27462c62015-05-14 00:39:53 -0700535 public Tunnel getTunnel(String tunnelId) {
536 return tunnelHandler.getTunnel(tunnelId);
537 }
538
sangho80f11cb2015-04-01 13:05:26 -0700539 /**
Saurav Das7c305372015-10-28 12:39:42 -0700540 * Returns the vlan-id assigned to the subnet configured for a device.
541 * If no vlan-id has been assigned, a new one is assigned out of a pool of ids,
542 * if and only if this controller instance is the master for the device.
543 * <p>
544 * USAGE: The assigned vlans are meant to be applied to untagged packets on those
545 * switches/pipelines that need this functionality. These vids are meant
546 * to be used internally within a switch, and thus need to be unique only
547 * on a switch level. Note that packets never go out on the wire with these
548 * vlans. Currently, vlan ids are assigned from value 4093 down.
549 * Vlan id 4094 expected to be used for all ports that are not assigned subnets.
550 * Vlan id 4095 is reserved and unused. Only a single vlan id is assigned
551 * per subnet.
sangho80f11cb2015-04-01 13:05:26 -0700552 *
Saurav Das7c305372015-10-28 12:39:42 -0700553 * @param deviceId switch dpid
Pier Ventre1a655962016-11-28 16:48:06 -0800554 * @param subnet IP prefix for which assigned vlan is desired
Saurav Das7c305372015-10-28 12:39:42 -0700555 * @return VlanId assigned for the subnet on the device, or
556 * null if no vlan assignment was found and this instance is not
557 * the master for the device.
sangho80f11cb2015-04-01 13:05:26 -0700558 */
Charles Chanb7f75ac2016-01-11 18:28:54 -0800559 // TODO: We should avoid assigning VLAN IDs that are used by VLAN cross-connection.
Pier Ventreb6a7f342016-11-26 21:05:22 -0800560 public VlanId getSubnetAssignedVlanId(DeviceId deviceId, IpPrefix subnet) {
Saurav Das7c305372015-10-28 12:39:42 -0700561 VlanId assignedVid = subnetVidStore.get(new SubnetAssignedVidStoreKey(
562 deviceId, subnet));
563 if (assignedVid != null) {
564 log.debug("Query for subnet:{} on device:{} returned assigned-vlan "
565 + "{}", subnet, deviceId, assignedVid);
566 return assignedVid;
567 }
568 //check mastership for the right to assign a vlan
569 if (!mastershipService.isLocalMaster(deviceId)) {
570 log.warn("This controller instance is not the master for device {}. "
571 + "Cannot assign vlan-id for subnet {}", deviceId, subnet);
572 return null;
573 }
574 // vlan assignment is expensive but done only once
Pier Ventreb6a7f342016-11-26 21:05:22 -0800575 // FIXME for now we will do assignment considering only the ipv4 subnet.
576 Set<Ip4Prefix> configuredSubnets = deviceConfiguration.getSubnets(deviceId)
577 .stream()
578 .filter(IpPrefix::isIp4)
579 .map(IpPrefix::getIp4Prefix)
580 .collect(Collectors.toSet());
Saurav Das7c305372015-10-28 12:39:42 -0700581 Set<Short> assignedVlans = new HashSet<>();
582 Set<Ip4Prefix> unassignedSubnets = new HashSet<>();
583 for (Ip4Prefix sub : configuredSubnets) {
584 VlanId v = subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId,
585 sub));
586 if (v != null) {
587 assignedVlans.add(v.toShort());
588 } else {
589 unassignedSubnets.add(sub);
590 }
591 }
592 short nextAssignedVlan = ASSIGNED_VLAN_START;
593 if (!assignedVlans.isEmpty()) {
594 nextAssignedVlan = (short) (Collections.min(assignedVlans) - 1);
595 }
596 for (Ip4Prefix unsub : unassignedSubnets) {
Pier Ventre1a655962016-11-28 16:48:06 -0800597 subnetVidStore.put(new SubnetAssignedVidStoreKey(deviceId, unsub),
598 VlanId.vlanId(nextAssignedVlan--));
599 log.info("Assigned vlan: {} to subnet: {} on device: {}",
600 nextAssignedVlan + 1, unsub, deviceId);
sangho80f11cb2015-04-01 13:05:26 -0700601 }
Saurav Das7c305372015-10-28 12:39:42 -0700602 return subnetVidStore.get(new SubnetAssignedVidStoreKey(deviceId, subnet));
sangho80f11cb2015-04-01 13:05:26 -0700603 }
604
sangho27462c62015-05-14 00:39:53 -0700605 /**
Saurav Das7c305372015-10-28 12:39:42 -0700606 * Returns the next objective ID for the given NeighborSet.
Saurav Das4c35fc42015-11-20 15:27:53 -0800607 * If the nextObjective does not exist, a new one is created and
Saurav Das2d94d312015-11-24 23:21:05 -0800608 * its id is returned.
sangho27462c62015-05-14 00:39:53 -0700609 *
sangho80f1f892015-05-19 11:57:42 -0700610 * @param deviceId Device ID
611 * @param ns NegighborSet
Saurav Das4c35fc42015-11-20 15:27:53 -0800612 * @param meta metadata passed into the creation of a Next Objective
Pier Ventre229fd0b2016-10-31 16:49:19 -0700613 * @param isBos indicates if it is BoS or not
Saurav Das4c35fc42015-11-20 15:27:53 -0800614 * @return next objective ID or -1 if an error was encountered during the
615 * creation of the nextObjective
sangho27462c62015-05-14 00:39:53 -0700616 */
Saurav Das4c35fc42015-11-20 15:27:53 -0800617 public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns,
Pier Ventre229fd0b2016-10-31 16:49:19 -0700618 TrafficSelector meta, boolean isBos) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700619 if (groupHandlerMap.get(deviceId) != null) {
620 log.trace("getNextObjectiveId query in device {}", deviceId);
621 return groupHandlerMap
Pier Ventre229fd0b2016-10-31 16:49:19 -0700622 .get(deviceId).getNextObjectiveId(ns, meta, isBos);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700623 } else {
Saurav Das2d94d312015-11-24 23:21:05 -0800624 log.warn("getNextObjectiveId query - groupHandler for device {} "
625 + "not found", deviceId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700626 return -1;
627 }
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700628 }
629
Charles Chan77277672015-10-20 16:24:19 -0700630 /**
Pier Ventre229fd0b2016-10-31 16:49:19 -0700631 * Returns the next objective ID for the given NeighborSet.
632 * If the nextObjective does not exist, a new one is created and
633 * its id is returned.
634 *
635 * @param deviceId Device ID
636 * @param ns NegighborSet
637 * @param meta metadata passed into the creation of a Next Objective
638 * @return next objective ID or -1 if an error was encountered during the
639 * creation of the nextObjective
640 */
641 public int getNextObjectiveId(DeviceId deviceId,
642 NeighborSet ns,
643 TrafficSelector meta) {
644 return this.getNextObjectiveId(deviceId, ns, meta, true);
645 }
646
647 /**
Saurav Das2d94d312015-11-24 23:21:05 -0800648 * Returns the next objective ID for the given subnet prefix. It is expected
649 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -0700650 *
651 * @param deviceId Device ID
652 * @param prefix Subnet
Saurav Das2d94d312015-11-24 23:21:05 -0800653 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -0700654 */
655 public int getSubnetNextObjectiveId(DeviceId deviceId, IpPrefix prefix) {
656 if (groupHandlerMap.get(deviceId) != null) {
657 log.trace("getSubnetNextObjectiveId query in device {}", deviceId);
658 return groupHandlerMap
659 .get(deviceId).getSubnetNextObjectiveId(prefix);
660 } else {
Saurav Das2d94d312015-11-24 23:21:05 -0800661 log.warn("getSubnetNextObjectiveId query - groupHandler for "
662 + "device {} not found", deviceId);
663 return -1;
664 }
665 }
666
667 /**
668 * Returns the next objective ID for the given portNumber, given the treatment.
669 * There could be multiple different treatments to the same outport, which
670 * would result in different objectives. If the next object
671 * does not exist, a new one is created and its id is returned.
672 *
673 * @param deviceId Device ID
674 * @param portNum port number on device for which NextObjective is queried
675 * @param treatment the actions to apply on the packets (should include outport)
676 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das07c74602016-04-27 18:35:50 -0700677 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -0800678 */
679 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
680 TrafficTreatment treatment,
681 TrafficSelector meta) {
682 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
683 if (ghdlr != null) {
684 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta);
685 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -0800686 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
687 + " not found", deviceId);
688 return -1;
689 }
690 }
691
sangho80f11cb2015-04-01 13:05:26 -0700692 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -0700693 @Override
694 public void process(PacketContext context) {
695
696 if (context.isHandled()) {
697 return;
698 }
699
700 InboundPacket pkt = context.inPacket();
701 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -0800702
703 if (ethernet == null) {
704 return;
705 }
706
Saurav Das2d94d312015-11-24 23:21:05 -0800707 log.trace("Rcvd pktin: {}", ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -0800708 if (ethernet.getEtherType() == TYPE_ARP) {
Charles Chanef1cfef2017-02-16 11:45:52 -0800709 log.warn("Receive unexpected ARP packet on {}", context.inPacket().receivedFrom());
Pier Ventreb6a7f342016-11-26 21:05:22 -0800710 log.debug("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800711 return;
sangho80f11cb2015-04-01 13:05:26 -0700712 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -0800713 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
714 //ipHandler.addToPacketBuffer(ipv4Packet);
715 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
716 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -0700717 } else {
Charles Chand041ad82017-01-13 17:20:44 -0800718 // NOTE: We don't support IP learning at this moment so this
719 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800720 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -0700721 }
Pier Ventreb6a7f342016-11-26 21:05:22 -0800722 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
723 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -0800724 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -0800725 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -0800726 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
727 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
728 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
729 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
730 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
731 } else {
Pier Luigi37a35432017-02-01 13:50:04 -0800732 // XXX Neigbour hacking, to handle the ICMPv6 packet
733 // not under our control
734 if (icmpHandler.handleUPstreamPackets(context)) {
735 log.debug("Rcvd pktin from UpStream: {}", ipv6Packet);
736 } else {
737 log.debug("Received ICMPv6 0x{} - not handled",
738 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
739 }
Pier Ventreb6b81d52016-12-02 08:16:05 -0800740 }
741 } else {
742 // NOTE: We don't support IP learning at this moment so this
743 // is not necessary. Also it causes duplication of DHCPv6 packets.
744 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
745 }
sangho80f11cb2015-04-01 13:05:26 -0700746 }
747 }
748 }
749
750 private class InternalLinkListener implements LinkListener {
751 @Override
752 public void event(LinkEvent event) {
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700753 if (event.type() == LinkEvent.Type.LINK_ADDED
754 || event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700755 log.debug("Event {} received from Link Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -0700756 scheduleEventHandlerIfNotScheduled(event);
757 }
758 }
759 }
760
761 private class InternalDeviceListener implements DeviceListener {
sangho80f11cb2015-04-01 13:05:26 -0700762 @Override
763 public void event(DeviceEvent event) {
sangho80f11cb2015-04-01 13:05:26 -0700764 switch (event.type()) {
765 case DEVICE_ADDED:
Saurav Dasf0f592d2016-11-18 15:21:57 -0800766 case PORT_UPDATED:
767 case PORT_ADDED:
sanghofb7c7292015-04-13 15:15:58 -0700768 case DEVICE_UPDATED:
769 case DEVICE_AVAILABILITY_CHANGED:
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700770 log.debug("Event {} received from Device Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -0700771 scheduleEventHandlerIfNotScheduled(event);
772 break;
773 default:
774 }
775 }
776 }
777
Saurav Das2d94d312015-11-24 23:21:05 -0800778 @SuppressWarnings("rawtypes")
sangho80f11cb2015-04-01 13:05:26 -0700779 private void scheduleEventHandlerIfNotScheduled(Event event) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700780 synchronized (threadSchedulerLock) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700781 eventQueue.add(event);
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700782 numOfEventsQueued++;
783
784 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
785 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700786 eventHandlerFuture = executorService
787 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
788 numOfHandlerScheduled++;
789 }
Jonathan Hart54541d12016-04-12 15:39:44 -0700790 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700791 numOfEventsQueued,
792 numOfHandlerScheduled);
sangho80f11cb2015-04-01 13:05:26 -0700793 }
sangho80f11cb2015-04-01 13:05:26 -0700794 }
795
796 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700797 @Override
sangho80f11cb2015-04-01 13:05:26 -0700798 public void run() {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700799 try {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700800 while (true) {
Saurav Das2d94d312015-11-24 23:21:05 -0800801 @SuppressWarnings("rawtypes")
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700802 Event event = null;
803 synchronized (threadSchedulerLock) {
804 if (!eventQueue.isEmpty()) {
805 event = eventQueue.poll();
806 numOfEventsExecuted++;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700807 } else {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700808 numOfHandlerExecution++;
809 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
810 numOfHandlerExecution, numOfEventsExecuted);
811 break;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700812 }
sanghofb7c7292015-04-13 15:15:58 -0700813 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700814 if (event.type() == LinkEvent.Type.LINK_ADDED) {
815 processLinkAdded((Link) event.subject());
816 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre6d593892016-09-13 21:33:40 -0700817 Link linkRemoved = (Link) event.subject();
818 if (linkRemoved.src().elementId() instanceof DeviceId &&
819 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
820 continue;
821 }
822 if (linkRemoved.dst().elementId() instanceof DeviceId &&
823 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
824 continue;
825 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700826 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700827 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
828 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
829 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das62af8802015-12-04 10:52:59 -0800830 DeviceId deviceId = ((Device) event.subject()).id();
831 if (deviceService.isAvailable(deviceId)) {
Saurav Dasc28b3432015-10-30 17:45:38 -0700832 log.info("Processing device event {} for available device {}",
833 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700834 processDeviceAdded((Device) event.subject());
Saurav Dasc3604f12016-03-23 11:22:49 -0700835 } else {
836 log.info("Processing device event {} for unavailable device {}",
837 event.type(), ((Device) event.subject()).id());
838 processDeviceRemoved((Device) event.subject());
839 }
Saurav Dasf0f592d2016-11-18 15:21:57 -0800840 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd1872b02016-12-02 15:43:47 -0800841 // typically these calls come when device is added first time
842 // so port filtering rules are handled at the device_added event.
843 // port added calls represent all ports on the device,
844 // enabled or not.
Saurav Dasf0f592d2016-11-18 15:21:57 -0800845 log.debug("** PORT ADDED {}/{} -> {}",
Saurav Dasd1872b02016-12-02 15:43:47 -0800846 ((DeviceEvent) event).subject().id(),
847 ((DeviceEvent) event).port().number(),
848 event.type());
Saurav Dasf0f592d2016-11-18 15:21:57 -0800849 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd1872b02016-12-02 15:43:47 -0800850 // these calls happen for every subsequent event
851 // ports enabled, disabled, switch goes away, comes back
Saurav Dasf0f592d2016-11-18 15:21:57 -0800852 log.info("** PORT UPDATED {}/{} -> {}",
853 event.subject(),
854 ((DeviceEvent) event).port(),
855 event.type());
856 processPortUpdated(((Device) event.subject()),
857 ((DeviceEvent) event).port());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700858 } else {
859 log.warn("Unhandled event type: {}", event.type());
860 }
sangho80f11cb2015-04-01 13:05:26 -0700861 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700862 } catch (Exception e) {
863 log.error("SegmentRouting event handler "
864 + "thread thrown an exception: {}", e);
sangho80f11cb2015-04-01 13:05:26 -0700865 }
sangho80f11cb2015-04-01 13:05:26 -0700866 }
867 }
868
sangho80f11cb2015-04-01 13:05:26 -0700869 private void processLinkAdded(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -0700870 log.info("** LINK ADDED {}", link.toString());
Charles Chan319d1a22015-11-03 10:42:14 -0800871 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
872 log.warn("Source device of this link is not configured.");
873 return;
874 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700875 //Irrespective whether the local is a MASTER or not for this device,
876 //create group handler instance and push default TTP flow rules.
877 //Because in a multi-instance setup, instances can initiate
878 //groups for any devices. Also the default TTP rules are needed
879 //to be pushed before inserting any IP table entries for any device
880 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
881 .deviceId());
882 if (groupHandler != null) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800883 groupHandler.linkUp(link, mastershipService.isLocalMaster(
884 link.src().deviceId()));
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700885 } else {
886 Device device = deviceService.getDevice(link.src().deviceId());
887 if (device != null) {
888 log.warn("processLinkAdded: Link Added "
889 + "Notification without Device Added "
890 + "event, still handling it");
891 processDeviceAdded(device);
892 groupHandler = groupHandlerMap.get(link.src()
893 .deviceId());
Saurav Das4c35fc42015-11-20 15:27:53 -0800894 groupHandler.linkUp(link, mastershipService.isLocalMaster(device.id()));
sangho80f11cb2015-04-01 13:05:26 -0700895 }
896 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700897
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700898 log.trace("Starting optimized route population process");
899 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null);
900 //log.trace("processLinkAdded: re-starting route population process");
901 //defaultRoutingHandler.startPopulationProcess();
Charles Chan72779502016-04-23 17:36:10 -0700902
903 mcastHandler.init();
sangho80f11cb2015-04-01 13:05:26 -0700904 }
905
906 private void processLinkRemoved(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -0700907 log.info("** LINK REMOVED {}", link.toString());
sangho2165d222015-05-01 09:38:25 -0700908 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
909 if (groupHandler != null) {
Saurav Das62af8802015-12-04 10:52:59 -0800910 groupHandler.portDown(link.src().port(),
911 mastershipService.isLocalMaster(link.src().deviceId()));
sangho2165d222015-05-01 09:38:25 -0700912 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700913 log.trace("Starting optimized route population process");
914 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link);
915 //log.trace("processLinkRemoved: re-starting route population process");
916 //defaultRoutingHandler.startPopulationProcess();
Charles Chan72779502016-04-23 17:36:10 -0700917
918 mcastHandler.processLinkDown(link);
sangho80f11cb2015-04-01 13:05:26 -0700919 }
920
921 private void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -0700922 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan319d1a22015-11-03 10:42:14 -0800923 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das8ec0ec42015-11-03 14:39:27 -0800924 log.warn("Device configuration uploading. Device {} will be "
925 + "processed after config completes.", device.id());
926 return;
927 }
Charles Chan72779502016-04-23 17:36:10 -0700928 processDeviceAddedInternal(device.id());
929 }
930
931 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -0700932 // Irrespective of whether the local is a MASTER or not for this device,
933 // we need to create a SR-group-handler instance. This is because in a
934 // multi-instance setup, any instance can initiate forwarding/next-objectives
935 // for any switch (even if this instance is a SLAVE or not even connected
936 // to the switch). To handle this, a default-group-handler instance is necessary
937 // per switch.
Charles Chan72779502016-04-23 17:36:10 -0700938 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
939 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -0800940 DefaultGroupHandler groupHandler;
941 try {
942 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -0700943 createGroupHandler(deviceId,
944 appId,
945 deviceConfiguration,
946 linkService,
947 flowObjectiveService,
948 this);
Charles Chan319d1a22015-11-03 10:42:14 -0800949 } catch (DeviceConfigNotFoundException e) {
950 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
951 return;
952 }
Charles Chan72779502016-04-23 17:36:10 -0700953 log.debug("updating groupHandlerMap with new config for device: {}",
954 deviceId);
955 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -0800956 }
Saurav Dasb149be12016-06-07 10:08:06 -0700957
Charles Chan72779502016-04-23 17:36:10 -0700958 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -0800959 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chandebfea32016-10-24 14:52:01 -0700960 hostHandler.init(deviceId);
Charles Chan82f19972016-05-17 13:13:55 -0700961 xConnectHandler.init(deviceId);
Charles Chan41f5ec02016-06-13 18:54:31 -0700962 cordConfigHandler.init(deviceId);
Charles Chan72779502016-04-23 17:36:10 -0700963 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan77277672015-10-20 16:24:19 -0700964 groupHandler.createGroupsFromSubnetConfig();
Charles Chan72779502016-04-23 17:36:10 -0700965 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -0700966 }
Charles Chan82ab1932016-01-30 23:22:37 -0800967
Charles Chandebfea32016-10-24 14:52:01 -0700968 appCfgHandler.init(deviceId);
969 routeHandler.init(deviceId);
sangho80f11cb2015-04-01 13:05:26 -0700970 }
971
Saurav Dasc3604f12016-03-23 11:22:49 -0700972 private void processDeviceRemoved(Device device) {
973 nsNextObjStore.entrySet().stream()
974 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
975 .forEach(entry -> {
976 nsNextObjStore.remove(entry.getKey());
977 });
Saurav Dasc3604f12016-03-23 11:22:49 -0700978 subnetNextObjStore.entrySet().stream()
979 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
980 .forEach(entry -> {
981 subnetNextObjStore.remove(entry.getKey());
982 });
Saurav Dasc3604f12016-03-23 11:22:49 -0700983 portNextObjStore.entrySet().stream()
984 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
985 .forEach(entry -> {
986 portNextObjStore.remove(entry.getKey());
987 });
Saurav Dasc3604f12016-03-23 11:22:49 -0700988 subnetVidStore.entrySet().stream()
989 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
990 .forEach(entry -> {
991 subnetVidStore.remove(entry.getKey());
992 });
Saurav Dasc3604f12016-03-23 11:22:49 -0700993 groupHandlerMap.remove(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -0700994 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan72779502016-04-23 17:36:10 -0700995 mcastHandler.removeDevice(device.id());
Charles Chan82f19972016-05-17 13:13:55 -0700996 xConnectHandler.removeDevice(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -0700997 }
998
Saurav Dasf0f592d2016-11-18 15:21:57 -0800999 private void processPortUpdated(Device device, Port port) {
1000 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1001 log.warn("Device configuration uploading. Not handling port event for"
1002 + "dev: {} port: {}", device.id(), port.number());
1003 return;
1004 }
Saurav Dasf9332192017-02-18 14:05:44 -08001005
1006 if (!mastershipService.isLocalMaster(device.id())) {
1007 log.debug("Not master for dev:{} .. not handling port updated event"
1008 + "for port {}", device.id(), port.number());
1009 return;
1010 }
1011
1012 // first we handle filtering rules associated with the port
1013 if (port.isEnabled()) {
1014 log.info("Switchport {}/{} enabled..programming filters",
1015 device.id(), port.number());
1016 defaultRoutingHandler.populateSinglePortFilteringRules(device.id(),
1017 port.number());
1018 } else {
1019 log.info("Switchport {}/{} disabled..removing filters",
1020 device.id(), port.number());
1021 defaultRoutingHandler.revokeSinglePortFilteringRules(device.id(),
1022 port.number());
1023 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001024
1025 // portUpdated calls are for ports that have gone down or up. For switch
1026 // to switch ports, link-events should take care of any re-routing or
1027 // group editing necessary for port up/down. Here we only process edge ports
1028 // that are already configured.
Saurav Dasf9332192017-02-18 14:05:44 -08001029 Ip4Prefix configuredSubnet = deviceConfiguration.getPortIPv4Subnet(device.id(),
1030 port.number());
Saurav Dasf0f592d2016-11-18 15:21:57 -08001031 if (configuredSubnet == null) {
1032 log.debug("Not handling port updated event for unconfigured port "
1033 + "dev/port: {}/{}", device.id(), port.number());
1034 return;
1035 }
1036 processEdgePort(device, port, configuredSubnet);
1037 }
1038
1039 private void processEdgePort(Device device, Port port, Ip4Prefix subnet) {
1040 boolean portUp = port.isEnabled();
1041 if (portUp) {
1042 log.info("Device:EdgePort {}:{} is enabled in subnet: {}", device.id(),
1043 port.number(), subnet);
1044 } else {
1045 log.info("Device:EdgePort {}:{} is disabled in subnet: {}", device.id(),
1046 port.number(), subnet);
1047 }
1048
Srikanth Vavilapalli64505482015-04-21 13:04:13 -07001049 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sangho80f11cb2015-04-01 13:05:26 -07001050 if (groupHandler != null) {
Saurav Dasf9332192017-02-18 14:05:44 -08001051 groupHandler.processEdgePort(port.number(), subnet, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001052 } else {
1053 log.warn("Group handler not found for dev:{}. Not handling edge port"
1054 + " {} event for port:{}", device.id(),
1055 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001056 }
1057 }
sangho27462c62015-05-14 00:39:53 -07001058
Pier Ventreb6a7f342016-11-26 21:05:22 -08001059 /**
1060 * Registers the given connect point with the NRS, this is necessary
1061 * to receive the NDP and ARP packets from the NRS.
1062 *
1063 * @param portToRegister connect point to register
1064 */
1065 public void registerConnectPoint(ConnectPoint portToRegister) {
Pier Ventreb6a7f342016-11-26 21:05:22 -08001066 this.neighbourResolutionService.registerNeighbourHandler(
1067 portToRegister,
1068 neighbourHandler,
1069 appId
1070 );
1071 }
1072
Charles Chan72f556a2015-10-05 17:50:33 -07001073 private class InternalConfigListener implements NetworkConfigListener {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001074 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001075
Charles Chanb7f75ac2016-01-11 18:28:54 -08001076 /**
1077 * Constructs the internal network config listener.
1078 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001079 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001080 */
Charles Chan46fdfaf2016-11-09 20:51:44 -08001081 public InternalConfigListener(SegmentRoutingManager srManager) {
1082 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001083 }
1084
Charles Chanb7f75ac2016-01-11 18:28:54 -08001085 /**
1086 * Reads network config and initializes related data structure accordingly.
1087 */
Charles Chane7c61022015-10-07 14:21:45 -07001088 public void configureNetwork() {
Pier Ventreb6a7f342016-11-26 21:05:22 -08001089
Charles Chan46fdfaf2016-11-09 20:51:44 -08001090 deviceConfiguration = new DeviceConfiguration(srManager);
Charles Chane7c61022015-10-07 14:21:45 -07001091
Charles Chan46fdfaf2016-11-09 20:51:44 -08001092 arpHandler = new ArpHandler(srManager);
1093 icmpHandler = new IcmpHandler(srManager);
1094 ipHandler = new IpHandler(srManager);
1095 routingRulePopulator = new RoutingRulePopulator(srManager);
1096 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chane7c61022015-10-07 14:21:45 -07001097
1098 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1099 groupHandlerMap, tunnelStore);
1100 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1101 flowObjectiveService,
1102 tunnelHandler, policyStore);
1103
Charles Chane7c61022015-10-07 14:21:45 -07001104 for (Device device : deviceService.getDevices()) {
Charles Chan72779502016-04-23 17:36:10 -07001105 processDeviceAddedInternal(device.id());
Charles Chane7c61022015-10-07 14:21:45 -07001106 }
1107
1108 defaultRoutingHandler.startPopulationProcess();
Charles Chan72779502016-04-23 17:36:10 -07001109 mcastHandler.init();
Charles Chane7c61022015-10-07 14:21:45 -07001110 }
1111
Charles Chan72f556a2015-10-05 17:50:33 -07001112 @Override
1113 public void event(NetworkConfigEvent event) {
Charles Chan82ab1932016-01-30 23:22:37 -08001114 // TODO move this part to NetworkConfigEventHandler
1115 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1116 switch (event.type()) {
1117 case CONFIG_ADDED:
1118 log.info("Segment Routing Config added.");
1119 configureNetwork();
1120 break;
1121 case CONFIG_UPDATED:
1122 log.info("Segment Routing Config updated.");
1123 // TODO support dynamic configuration
1124 break;
1125 default:
1126 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001127 }
Charles Chan82ab1932016-01-30 23:22:37 -08001128 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chan82f19972016-05-17 13:13:55 -07001129 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan82ab1932016-01-30 23:22:37 -08001130 switch (event.type()) {
1131 case CONFIG_ADDED:
Charles Chan82f19972016-05-17 13:13:55 -07001132 appCfgHandler.processAppConfigAdded(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001133 break;
1134 case CONFIG_UPDATED:
Charles Chan82f19972016-05-17 13:13:55 -07001135 appCfgHandler.processAppConfigUpdated(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001136 break;
1137 case CONFIG_REMOVED:
Charles Chan82f19972016-05-17 13:13:55 -07001138 appCfgHandler.processAppConfigRemoved(event);
1139 break;
1140 default:
1141 break;
1142 }
Charles Chandebfea32016-10-24 14:52:01 -07001143 configureNetwork();
Charles Chan82f19972016-05-17 13:13:55 -07001144 } else if (event.configClass().equals(XConnectConfig.class)) {
1145 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1146 switch (event.type()) {
1147 case CONFIG_ADDED:
1148 xConnectHandler.processXConnectConfigAdded(event);
1149 break;
1150 case CONFIG_UPDATED:
1151 xConnectHandler.processXConnectConfigUpdated(event);
1152 break;
1153 case CONFIG_REMOVED:
1154 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001155 break;
1156 default:
1157 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001158 }
Pier Luigi37a35432017-02-01 13:50:04 -08001159 // XXX Neighbour hacking. This method is looking for
1160 // the Internet-Router interface. In order to retrieve
1161 // the upstream port.
1162 } else if (event.configClass().equals(InterfaceConfig.class)) {
1163 switch (event.type()) {
1164 case CONFIG_ADDED:
1165 case CONFIG_UPDATED:
Pier Luigiad0a67f2017-02-03 13:34:21 -08001166 updateUPstreamCP();
Pier Luigi37a35432017-02-01 13:50:04 -08001167 case CONFIG_REGISTERED:
1168 case CONFIG_UNREGISTERED:
1169 case CONFIG_REMOVED:
1170 break;
1171 default:
1172 break;
1173 }
1174 // XXX Neighbour hacking. This method is looking for
1175 // the vrouter port.
1176 } else if (event.configClass().equals(RouterConfig.class)) {
1177 switch (event.type()) {
1178 case CONFIG_ADDED:
1179 case CONFIG_UPDATED:
Pier Luigiad0a67f2017-02-03 13:34:21 -08001180 updateVRouterCP(event);
Pier Luigi37a35432017-02-01 13:50:04 -08001181 case CONFIG_REGISTERED:
1182 case CONFIG_UNREGISTERED:
1183 case CONFIG_REMOVED:
1184 break;
1185 default:
1186 break;
1187 }
Pier Ventre6b19e482016-11-07 16:21:04 -08001188 } else if (event.configClass().equals(PwaasConfig.class)) {
1189 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1190 switch (event.type()) {
1191 case CONFIG_ADDED:
1192 l2TunnelHandler.processPwaasConfigAdded(event);
1193 break;
1194 case CONFIG_UPDATED:
1195 l2TunnelHandler.processPwaasConfigUpdated(event);
1196 break;
1197 case CONFIG_REMOVED:
1198 l2TunnelHandler.processPwaasConfigRemoved(event);
1199 break;
1200 default:
1201 break;
1202 }
Charles Chan72f556a2015-10-05 17:50:33 -07001203 }
1204 }
1205 }
Charles Chanf4586112015-11-09 16:37:23 -08001206
Pier Luigiad0a67f2017-02-03 13:34:21 -08001207 /////////////////////////////////////////////////////////////////
1208 // XXX Neighbour hacking, temporary workaround will be //
Saurav Dasf9332192017-02-18 14:05:44 -08001209 // removed as soon as possible, when bridging based //
1210 // control plane redirect is implemented. //
Pier Luigiad0a67f2017-02-03 13:34:21 -08001211 /////////////////////////////////////////////////////////////////
1212
1213 // XXX Neighbour hacking. To store upstream connect
1214 // point and vRouter connect point
1215 ConnectPoint upstreamCP = null;
1216 ConnectPoint vRouterCP = null;
1217
1218 // XXX Neighbour hacking. To update the Upstream CP
1219 public void updateUPstreamCP() {
1220 Set<ConnectPoint> portSubjects = cfgService.getSubjects(ConnectPoint.class, InterfaceConfig.class);
1221 upstreamCP = null;
1222 portSubjects.stream().forEach(subject -> {
1223 InterfaceConfig config = cfgService.getConfig(subject, InterfaceConfig.class);
1224 Set<Interface> networkInterfaces;
1225 try {
1226 networkInterfaces = config.getInterfaces();
1227 } catch (ConfigException e) {
1228 log.error("Error loading port configuration");
1229 return;
1230 }
1231 networkInterfaces.forEach(networkInterface -> {
1232 if (networkInterface.name().equals("internet-router")) {
1233 upstreamCP = subject;
1234 }
1235 });
1236 });
1237
1238 }
1239
1240 // XXX Neighbour hacking. To update the Upstream CP
1241 public void updateVRouterCP(NetworkConfigEvent event) {
1242 RouterConfig config = (RouterConfig) event.config().get();
1243 if (config == null) {
1244 log.warn("Router config not available");
1245 vRouterCP = null;
1246 return;
1247 }
1248 vRouterCP = config.getControlPlaneConnectPoint();
1249 }
1250
Charles Chanf4586112015-11-09 16:37:23 -08001251 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001252 @Override
1253 public void event(HostEvent event) {
1254 // Do not proceed without mastership
1255 DeviceId deviceId = event.subject().location().deviceId();
1256 if (!mastershipService.isLocalMaster(deviceId)) {
1257 return;
1258 }
1259
1260 switch (event.type()) {
1261 case HOST_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001262 hostHandler.processHostAddedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001263 break;
1264 case HOST_MOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001265 hostHandler.processHostMovedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001266 break;
1267 case HOST_REMOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001268 hostHandler.processHostRemoveEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001269 break;
1270 case HOST_UPDATED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001271 hostHandler.processHostUpdatedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001272 break;
1273 default:
1274 log.warn("Unsupported host event type: {}", event.type());
1275 break;
1276 }
1277 }
1278 }
1279
Charles Chanc91c8782016-03-30 17:54:24 -07001280 private class InternalMcastListener implements McastListener {
1281 @Override
1282 public void event(McastEvent event) {
1283 switch (event.type()) {
1284 case SOURCE_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001285 mcastHandler.processSourceAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001286 break;
1287 case SINK_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001288 mcastHandler.processSinkAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001289 break;
1290 case SINK_REMOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001291 mcastHandler.processSinkRemoved(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001292 break;
1293 case ROUTE_ADDED:
1294 case ROUTE_REMOVED:
1295 default:
1296 break;
1297 }
1298 }
1299 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001300
1301 private class InternalCordConfigListener implements CordConfigListener {
1302 @Override
1303 public void event(CordConfigEvent event) {
1304 switch (event.type()) {
1305 case ACCESS_AGENT_ADDED:
1306 cordConfigHandler.processAccessAgentAddedEvent(event);
1307 break;
1308 case ACCESS_AGENT_UPDATED:
1309 cordConfigHandler.processAccessAgentUpdatedEvent(event);
1310 break;
1311 case ACCESS_AGENT_REMOVED:
1312 cordConfigHandler.processAccessAgentRemovedEvent(event);
1313 break;
1314 case ACCESS_DEVICE_ADDED:
1315 case ACCESS_DEVICE_UPDATED:
1316 case ACCESS_DEVICE_REMOVED:
1317 default:
1318 break;
1319 }
1320 }
1321 }
Charles Chandebfea32016-10-24 14:52:01 -07001322
1323 private class InternalRouteEventListener implements RouteListener {
1324 @Override
1325 public void event(RouteEvent event) {
1326 switch (event.type()) {
1327 case ROUTE_ADDED:
1328 routeHandler.processRouteAdded(event);
1329 break;
1330 case ROUTE_UPDATED:
1331 routeHandler.processRouteUpdated(event);
1332 break;
1333 case ROUTE_REMOVED:
1334 routeHandler.processRouteRemoved(event);
1335 break;
1336 default:
1337 break;
1338 }
1339 }
1340 }
sangho80f11cb2015-04-01 13:05:26 -07001341}