blob: 5b0518766c1e10691146eceb9841daa4b3354f32 [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 Chan90772a72017-02-08 15:52:08 -080018import com.google.common.collect.HashMultimap;
Charles Chand7844e52016-10-20 17:02:44 -070019import com.google.common.collect.Maps;
Charles Chan90772a72017-02-08 15:52:08 -080020import com.google.common.collect.Multimap;
sangho80f11cb2015-04-01 13:05:26 -070021import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho27462c62015-05-14 00:39:53 -070026import org.apache.felix.scr.annotations.Service;
sangho80f11cb2015-04-01 13:05:26 -070027import org.onlab.packet.Ethernet;
Pier Ventreb6b81d52016-12-02 08:16:05 -080028import org.onlab.packet.ICMP6;
Charles Chan77277672015-10-20 16:24:19 -070029import org.onlab.packet.IPv4;
Pier Ventreb6a7f342016-11-26 21:05:22 -080030import org.onlab.packet.IPv6;
Charles Chan77277672015-10-20 16:24:19 -070031import org.onlab.packet.IpPrefix;
Jonathan Hart54541d12016-04-12 15:39:44 -070032import org.onlab.packet.VlanId;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070033import org.onlab.util.KryoNamespace;
Saurav Dasc3604f12016-03-23 11:22:49 -070034import org.onosproject.cfg.ComponentConfigService;
sangho80f11cb2015-04-01 13:05:26 -070035import org.onosproject.core.ApplicationId;
36import org.onosproject.core.CoreService;
37import org.onosproject.event.Event;
Charles Chanc91c8782016-03-30 17:54:24 -070038import org.onosproject.incubator.net.config.basics.McastConfig;
Pier Luigiad0a67f2017-02-03 13:34:21 -080039import org.onosproject.incubator.net.intf.Interface;
Charles Chan46fdfaf2016-11-09 20:51:44 -080040import org.onosproject.incubator.net.intf.InterfaceService;
Charles Chandebfea32016-10-24 14:52:01 -070041import org.onosproject.incubator.net.routing.RouteEvent;
42import org.onosproject.incubator.net.routing.RouteListener;
43import org.onosproject.incubator.net.routing.RouteService;
Pier Ventreb6b81d52016-12-02 08:16:05 -080044import org.onosproject.incubator.net.neighbour.NeighbourResolutionService;
Jonathan Hart54541d12016-04-12 15:39:44 -070045import org.onosproject.mastership.MastershipService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080046import org.onosproject.net.ConnectPoint;
Jonathan Hart54541d12016-04-12 15:39:44 -070047import org.onosproject.net.Device;
48import org.onosproject.net.DeviceId;
49import org.onosproject.net.Link;
50import org.onosproject.net.Port;
Charles Chanf4586112015-11-09 16:37:23 -080051import org.onosproject.net.PortNumber;
Charles Chan72f556a2015-10-05 17:50:33 -070052import org.onosproject.net.config.ConfigFactory;
53import org.onosproject.net.config.NetworkConfigEvent;
Charles Chan72f556a2015-10-05 17:50:33 -070054import org.onosproject.net.config.NetworkConfigListener;
Jonathan Hart54541d12016-04-12 15:39:44 -070055import org.onosproject.net.config.NetworkConfigRegistry;
Charles Chan72f556a2015-10-05 17:50:33 -070056import org.onosproject.net.config.basics.SubjectFactories;
Jonathan Hart54541d12016-04-12 15:39:44 -070057import org.onosproject.net.device.DeviceEvent;
58import org.onosproject.net.device.DeviceListener;
59import org.onosproject.net.device.DeviceService;
Charles Chanf4586112015-11-09 16:37:23 -080060import org.onosproject.net.flow.TrafficSelector;
61import org.onosproject.net.flow.TrafficTreatment;
Jonathan Hart54541d12016-04-12 15:39:44 -070062import org.onosproject.net.flowobjective.FlowObjectiveService;
Charles Chanf4586112015-11-09 16:37:23 -080063import org.onosproject.net.host.HostEvent;
64import org.onosproject.net.host.HostListener;
Pier Ventreb6a7f342016-11-26 21:05:22 -080065import org.onosproject.net.host.HostService;
66import org.onosproject.net.link.LinkEvent;
67import org.onosproject.net.link.LinkListener;
68import org.onosproject.net.link.LinkService;
Charles Chanc91c8782016-03-30 17:54:24 -070069import org.onosproject.net.mcast.McastEvent;
70import org.onosproject.net.mcast.McastListener;
71import org.onosproject.net.mcast.MulticastRouteService;
Pier Ventreb6a7f342016-11-26 21:05:22 -080072import org.onosproject.net.packet.InboundPacket;
73import org.onosproject.net.packet.PacketContext;
74import org.onosproject.net.packet.PacketProcessor;
75import org.onosproject.net.packet.PacketService;
Pier Ventref3cf5b92016-11-09 14:17:26 -080076import org.onosproject.net.topology.PathService;
Charles Chanc91c8782016-03-30 17:54:24 -070077import org.onosproject.net.topology.TopologyService;
78import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
79import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventre6b19e482016-11-07 16:21:04 -080080import org.onosproject.segmentrouting.config.PwaasConfig;
Pier Ventreb6a7f342016-11-26 21:05:22 -080081import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Pier Ventre6b19e482016-11-07 16:21:04 -080082import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Charles Chan82f19972016-05-17 13:13:55 -070083import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chanc91c8782016-03-30 17:54:24 -070084import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
85import org.onosproject.segmentrouting.grouphandler.NeighborSet;
Charles Chan1eaf4802016-04-18 13:44:03 -070086import org.onosproject.segmentrouting.storekey.NeighborSetNextObjectiveStoreKey;
87import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -070088import org.onosproject.segmentrouting.storekey.SubnetAssignedVidStoreKey;
Charles Chan10b0fb72017-02-02 16:20:42 -080089import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chan82f19972016-05-17 13:13:55 -070090import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Pier Ventre6b19e482016-11-07 16:21:04 -080091import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Jonathan Hart54541d12016-04-12 15:39:44 -070092import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070093import org.onosproject.store.service.EventuallyConsistentMap;
94import org.onosproject.store.service.EventuallyConsistentMapBuilder;
95import org.onosproject.store.service.StorageService;
96import org.onosproject.store.service.WallClockTimestamp;
Charles Chan41f5ec02016-06-13 18:54:31 -070097import org.opencord.cordconfig.CordConfigEvent;
98import org.opencord.cordconfig.CordConfigListener;
99import org.opencord.cordconfig.CordConfigService;
sangho80f11cb2015-04-01 13:05:26 -0700100import org.slf4j.Logger;
101import org.slf4j.LoggerFactory;
102
sangho27462c62015-05-14 00:39:53 -0700103import java.util.List;
sangho80f11cb2015-04-01 13:05:26 -0700104import java.util.Map;
Saurav Das7c305372015-10-28 12:39:42 -0700105import java.util.Set;
sangho80f11cb2015-04-01 13:05:26 -0700106import java.util.concurrent.ConcurrentHashMap;
107import java.util.concurrent.ConcurrentLinkedQueue;
108import java.util.concurrent.Executors;
109import java.util.concurrent.ScheduledExecutorService;
110import java.util.concurrent.ScheduledFuture;
111import java.util.concurrent.TimeUnit;
Charles Chan90772a72017-02-08 15:52:08 -0800112import java.util.stream.Collectors;
sangho80f11cb2015-04-01 13:05:26 -0700113
Charles Chand6d25332016-02-26 22:19:52 -0800114import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800115import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700116import static org.onlab.util.Tools.groupedThreads;
Charles Chand6d25332016-02-26 22:19:52 -0800117
Charles Chanb7f75ac2016-01-11 18:28:54 -0800118/**
119 * Segment routing manager.
120 */
Jonathan Hart54541d12016-04-12 15:39:44 -0700121@Service
122@Component(immediate = true)
sangho27462c62015-05-14 00:39:53 -0700123public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700124
Charles Chan46fdfaf2016-11-09 20:51:44 -0800125 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
sangho80f11cb2015-04-01 13:05:26 -0700126
127 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700128 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700129
130 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventreb6a7f342016-11-26 21:05:22 -0800131 private NeighbourResolutionService neighbourResolutionService;
132
133 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800134 public PathService pathService;
135
136 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700137 CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700138
139 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700140 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700141
142 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700143 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700144
145 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700146 DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700147
148 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800149 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700150
151 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700152 LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700153
Charles Chan82ab1932016-01-30 23:22:37 -0800154 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800155 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700156
157 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800158 public StorageService storageService;
Charles Chandebfea32016-10-24 14:52:01 -0700159
160 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
161 MulticastRouteService multicastRouteService;
162
163 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
164 TopologyService topologyService;
165
166 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
167 CordConfigService cordConfigService;
168
169 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
170 RouteService routeService;
Charles Chan82ab1932016-01-30 23:22:37 -0800171
172 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800173 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800174
Saurav Dasc3604f12016-03-23 11:22:49 -0700175 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800176 public InterfaceService interfaceService;
177
Charles Chandebfea32016-10-24 14:52:01 -0700178 ArpHandler arpHandler = null;
179 IcmpHandler icmpHandler = null;
180 IpHandler ipHandler = null;
181 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700182 ApplicationId appId;
183 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700184
Charles Chandebfea32016-10-24 14:52:01 -0700185 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700186 private TunnelHandler tunnelHandler = null;
187 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700188 private InternalPacketProcessor processor = null;
189 private InternalLinkListener linkListener = null;
190 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700191 private AppConfigHandler appCfgHandler = null;
Charles Chandebfea32016-10-24 14:52:01 -0700192 XConnectHandler xConnectHandler = null;
Charles Chan1eaf4802016-04-18 13:44:03 -0700193 private McastHandler mcastHandler = null;
Charles Chandebfea32016-10-24 14:52:01 -0700194 HostHandler hostHandler = null;
Charles Chan41f5ec02016-06-13 18:54:31 -0700195 private CordConfigHandler cordConfigHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800196 private RouteHandler routeHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800197 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Pier Ventre6b19e482016-11-07 16:21:04 -0800198 private L2TunnelHandler l2TunnelHandler = null;
sangho80f11cb2015-04-01 13:05:26 -0700199 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan82ab1932016-01-30 23:22:37 -0800200 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700201 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
202 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chan41f5ec02016-06-13 18:54:31 -0700203 private final InternalCordConfigListener cordConfigListener = new InternalCordConfigListener();
Charles Chandebfea32016-10-24 14:52:01 -0700204 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sangho80f11cb2015-04-01 13:05:26 -0700205
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700206 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700207 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sangho80f11cb2015-04-01 13:05:26 -0700208
Saurav Das2d94d312015-11-24 23:21:05 -0800209 @SuppressWarnings("unused")
sangho80f11cb2015-04-01 13:05:26 -0700210 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das2d94d312015-11-24 23:21:05 -0800211 @SuppressWarnings("rawtypes")
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700212 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Charles Chanf4586112015-11-09 16:37:23 -0800213 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chanb7f75ac2016-01-11 18:28:54 -0800214 new ConcurrentHashMap<>();
215 /**
216 * Per device next objective ID store with (device id + neighbor set) as key.
217 */
Ray Milkeyb85de082017-04-05 09:42:04 -0700218 EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, Integer>
Charles Chanf4586112015-11-09 16:37:23 -0800219 nsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800220 /**
221 * Per device next objective ID store with (device id + subnet) as key.
222 */
Ray Milkeyb85de082017-04-05 09:42:04 -0700223 EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800224 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800225 /**
226 * Per device next objective ID store with (device id + port) as key.
227 */
Ray Milkeyb85de082017-04-05 09:42:04 -0700228 EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800229 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800230
Saurav Das2d94d312015-11-24 23:21:05 -0800231 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
232 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700233
Charles Chanc91c8782016-03-30 17:54:24 -0700234 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700235 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
236 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700237 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700238 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800239 public SegmentRoutingDeviceConfig createConfig() {
240 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700241 }
242 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800243
Charles Chanc91c8782016-03-30 17:54:24 -0700244 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700245 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
246 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700247 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800248 @Override
249 public SegmentRoutingAppConfig createConfig() {
250 return new SegmentRoutingAppConfig();
251 }
252 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800253
Charles Chan82f19972016-05-17 13:13:55 -0700254 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
255 new ConfigFactory<ApplicationId, XConnectConfig>(
256 SubjectFactories.APP_SUBJECT_FACTORY,
257 XConnectConfig.class, "xconnect") {
258 @Override
259 public XConnectConfig createConfig() {
260 return new XConnectConfig();
261 }
262 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800263
Charles Chanc91c8782016-03-30 17:54:24 -0700264 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700265 new ConfigFactory<ApplicationId, McastConfig>(
266 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700267 McastConfig.class, "multicast") {
268 @Override
269 public McastConfig createConfig() {
270 return new McastConfig();
271 }
272 };
273
Pier Ventre6b19e482016-11-07 16:21:04 -0800274 private final ConfigFactory<ApplicationId, PwaasConfig> pwaasConfigFactory =
275 new ConfigFactory<ApplicationId, PwaasConfig>(
276 SubjectFactories.APP_SUBJECT_FACTORY,
277 PwaasConfig.class, "pwaas") {
278 @Override
279 public PwaasConfig createConfig() {
280 return new PwaasConfig();
281 }
282 };
283
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700284 private Object threadSchedulerLock = new Object();
285 private static int numOfEventsQueued = 0;
286 private static int numOfEventsExecuted = 0;
sangho80f11cb2015-04-01 13:05:26 -0700287 private static int numOfHandlerExecution = 0;
288 private static int numOfHandlerScheduled = 0;
289
Charles Chan1963f4f2016-02-18 14:22:42 -0800290 /**
291 * Segment Routing App ID.
292 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800293 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan10b0fb72017-02-02 16:20:42 -0800294
Charles Chanb7f75ac2016-01-11 18:28:54 -0800295 /**
296 * The default VLAN ID assigned to the interfaces without subnet config.
297 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800298 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das7c305372015-10-28 12:39:42 -0700299
sangho80f11cb2015-04-01 13:05:26 -0700300 @Activate
301 protected void activate() {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800302 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700303
304 log.debug("Creating EC map nsnextobjectivestore");
305 EventuallyConsistentMapBuilder<NeighborSetNextObjectiveStoreKey, Integer>
306 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700307 nsNextObjStore = nsNextObjMapBuilder
308 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700309 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700310 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700311 .build();
312 log.trace("Current size {}", nsNextObjStore.size());
313
Charles Chan10b0fb72017-02-02 16:20:42 -0800314 log.debug("Creating EC map vlannextobjectivestore");
315 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
316 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
317 vlanNextObjStore = vlanNextObjMapBuilder
318 .withName("vlannextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700319 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700320 .withTimestampProvider((k, v) -> new WallClockTimestamp())
321 .build();
322
Saurav Das2d94d312015-11-24 23:21:05 -0800323 log.debug("Creating EC map subnetnextobjectivestore");
324 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
325 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
326 portNextObjStore = portNextObjMapBuilder
327 .withName("portnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700328 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800329 .withTimestampProvider((k, v) -> new WallClockTimestamp())
330 .build();
331
sangho4a5c42a2015-05-20 22:16:38 -0700332 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
333 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700334 tunnelStore = tunnelMapBuilder
335 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700336 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700337 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700338 .build();
339
340 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
341 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700342 policyStore = policyMapBuilder
343 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700344 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700345 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700346 .build();
347
Saurav Dasc3604f12016-03-23 11:22:49 -0700348 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800349 "purgeOnDisconnection", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700350 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800351 "purgeOnDisconnection", "true");
Pier Luigib9632ba2017-01-12 18:14:58 -0800352 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
353 "requestInterceptsEnabled", "false");
Pier Luigibc976df2017-01-12 22:46:39 -0800354 compCfgService.preSetProperty("org.onosproject.incubator.net.neighbour.impl.NeighbourResolutionManager",
355 "requestInterceptsEnabled", "false");
356 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelay",
357 "arpEnabled", "false");
Pier Luigi0ebeb312017-02-02 22:31:34 -0800358 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
359 "greedyLearningIpv6", "true");
Charles Chancf8ea472017-02-28 15:15:17 -0800360 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
361 "forceUnprovision", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700362
Charles Chan2b078ae2015-10-14 11:24:40 -0700363 processor = new InternalPacketProcessor();
364 linkListener = new InternalLinkListener();
365 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700366 appCfgHandler = new AppConfigHandler(this);
367 xConnectHandler = new XConnectHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700368 mcastHandler = new McastHandler(this);
369 hostHandler = new HostHandler(this);
Charles Chan41f5ec02016-06-13 18:54:31 -0700370 cordConfigHandler = new CordConfigHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700371 routeHandler = new RouteHandler(this);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800372 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Pier Ventre6b19e482016-11-07 16:21:04 -0800373 l2TunnelHandler = new L2TunnelHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700374
Charles Chand6d25332016-02-26 22:19:52 -0800375 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700376 cfgService.registerConfigFactory(deviceConfigFactory);
377 cfgService.registerConfigFactory(appConfigFactory);
Charles Chan82f19972016-05-17 13:13:55 -0700378 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700379 cfgService.registerConfigFactory(mcastConfigFactory);
Pier Ventre6b19e482016-11-07 16:21:04 -0800380 cfgService.registerConfigFactory(pwaasConfigFactory);
Charles Chan82ab1932016-01-30 23:22:37 -0800381 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700382 packetService.addProcessor(processor, PacketProcessor.director(2));
383 linkService.addListener(linkListener);
384 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700385 multicastRouteService.addListener(mcastListener);
Charles Chan41f5ec02016-06-13 18:54:31 -0700386 cordConfigService.addListener(cordConfigListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700387
388 cfgListener.configureNetwork();
389
Charles Chandebfea32016-10-24 14:52:01 -0700390 routeService.addListener(routeListener);
391
sangho80f11cb2015-04-01 13:05:26 -0700392 log.info("Started");
393 }
394
Jonathan Hart54541d12016-04-12 15:39:44 -0700395 private KryoNamespace.Builder createSerializer() {
396 return new KryoNamespace.Builder()
397 .register(KryoNamespaces.API)
398 .register(NeighborSetNextObjectiveStoreKey.class,
Charles Chan10b0fb72017-02-02 16:20:42 -0800399 VlanNextObjectiveStoreKey.class,
Jonathan Hart54541d12016-04-12 15:39:44 -0700400 SubnetAssignedVidStoreKey.class,
401 NeighborSet.class,
402 Tunnel.class,
403 DefaultTunnel.class,
404 Policy.class,
405 TunnelPolicy.class,
406 Policy.Type.class,
407 PortNextObjectiveStoreKey.class,
Charles Chan82f19972016-05-17 13:13:55 -0700408 XConnectStoreKey.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700409 );
410 }
411
sangho80f11cb2015-04-01 13:05:26 -0700412 @Deactivate
413 protected void deactivate() {
Charles Chan72f556a2015-10-05 17:50:33 -0700414 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700415 cfgService.unregisterConfigFactory(deviceConfigFactory);
416 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chandebfea32016-10-24 14:52:01 -0700417 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700418 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventre6b19e482016-11-07 16:21:04 -0800419 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chan72f556a2015-10-05 17:50:33 -0700420
sangho80f11cb2015-04-01 13:05:26 -0700421 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700422 linkService.removeListener(linkListener);
423 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700424 multicastRouteService.removeListener(mcastListener);
Charles Chan41f5ec02016-06-13 18:54:31 -0700425 cordConfigService.removeListener(cordConfigListener);
Charles Chandebfea32016-10-24 14:52:01 -0700426 routeService.removeListener(routeListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700427
Charles Chan2e71ef32017-02-23 15:44:08 -0800428 neighbourResolutionService.unregisterNeighbourHandlers(appId);
429
sangho80f11cb2015-04-01 13:05:26 -0700430 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700431 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700432 deviceListener = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700433 groupHandlerMap.clear();
434
Charles Chanc91c8782016-03-30 17:54:24 -0700435 nsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800436 vlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700437 portNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700438 tunnelStore.destroy();
439 policyStore.destroy();
sangho80f11cb2015-04-01 13:05:26 -0700440 log.info("Stopped");
441 }
442
sangho27462c62015-05-14 00:39:53 -0700443 @Override
444 public List<Tunnel> getTunnels() {
445 return tunnelHandler.getTunnels();
446 }
447
448 @Override
sanghobd812f82015-06-29 14:58:47 -0700449 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
450 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700451 }
452
453 @Override
sanghobd812f82015-06-29 14:58:47 -0700454 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700455 for (Policy policy: policyHandler.getPolicies()) {
456 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
457 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
458 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
459 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700460 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700461 }
462 }
463 }
sanghobd812f82015-06-29 14:58:47 -0700464 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700465 }
466
467 @Override
sanghobd812f82015-06-29 14:58:47 -0700468 public PolicyHandler.Result removePolicy(Policy policy) {
469 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700470 }
471
472 @Override
sanghobd812f82015-06-29 14:58:47 -0700473 public PolicyHandler.Result createPolicy(Policy policy) {
474 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700475 }
476
477 @Override
478 public List<Policy> getPolicies() {
479 return policyHandler.getPolicies();
480 }
481
Saurav Das07c74602016-04-27 18:35:50 -0700482 @Override
483 public void rerouteNetwork() {
484 cfgListener.configureNetwork();
485 for (Device device : deviceService.getDevices()) {
Saurav Dasf9332192017-02-18 14:05:44 -0800486 if (mastershipService.isLocalMaster(device.id())) {
487 defaultRoutingHandler.populatePortAddressingRules(device.id());
488 }
Saurav Das07c74602016-04-27 18:35:50 -0700489 }
490 defaultRoutingHandler.startPopulationProcess();
491 }
492
Charles Chand7844e52016-10-20 17:02:44 -0700493 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800494 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
495 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Charles Chand7844e52016-10-20 17:02:44 -0700496 deviceService.getAvailableDevices().forEach(device -> {
497 deviceSubnetMap.put(device.id(), deviceConfiguration.getSubnets(device.id()));
498 });
499 return deviceSubnetMap;
500 }
501
sangho80f1f892015-05-19 11:57:42 -0700502 /**
Ray Milkeyb85de082017-04-05 09:42:04 -0700503 * Extracts the application ID from the manager.
504 *
505 * @return application ID
506 */
507 public ApplicationId appId() {
508 return appId;
509 }
510
511 /**
512 * Returns the device configuration.
513 *
514 * @return device configuration
515 */
516 public DeviceConfiguration deviceConfiguration() {
517 return deviceConfiguration;
518 }
519
520 /**
521 * Per device next objective ID store with (device id + neighbor set) as key.
522 *
523 * @return next objective ID store
524 */
525 public EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, Integer> nsNextObjStore() {
526 return nsNextObjStore;
527 }
528
529 /**
530 * Per device next objective ID store with (device id + subnet) as key.
531 *
532 * @return vlan next object store
533 */
534 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
535 return vlanNextObjStore;
536 }
537
538 /**
539 * Per device next objective ID store with (device id + port) as key.
540 *
541 * @return port next object store.
542 */
543 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
544 return portNextObjStore;
545 }
546
547 /**
Pier Ventre7a78de22016-10-31 15:00:01 -0700548 * Returns the MPLS-ECMP configuration.
549 *
550 * @return MPLS-ECMP value
551 */
552 public boolean getMplsEcmp() {
553 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
554 .getConfig(this.appId, SegmentRoutingAppConfig.class);
555 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
556 }
557
558 /**
sangho80f1f892015-05-19 11:57:42 -0700559 * Returns the tunnel object with the tunnel ID.
560 *
561 * @param tunnelId Tunnel ID
562 * @return Tunnel reference
563 */
sangho27462c62015-05-14 00:39:53 -0700564 public Tunnel getTunnel(String tunnelId) {
565 return tunnelHandler.getTunnel(tunnelId);
566 }
567
Charles Chan90772a72017-02-08 15:52:08 -0800568 // TODO Consider moving these to InterfaceService
sangho80f11cb2015-04-01 13:05:26 -0700569 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800570 * Returns untagged VLAN configured on given connect point.
Charles Chan90772a72017-02-08 15:52:08 -0800571 * <p>
572 * Only returns the first match if there are multiple untagged VLAN configured
573 * on the connect point.
sangho80f11cb2015-04-01 13:05:26 -0700574 *
Charles Chan10b0fb72017-02-02 16:20:42 -0800575 * @param connectPoint connect point
576 * @return untagged VLAN or null if not configured
sangho80f11cb2015-04-01 13:05:26 -0700577 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800578 public VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
579 return interfaceService.getInterfacesByPort(connectPoint).stream()
580 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
581 .map(Interface::vlanUntagged)
582 .findFirst().orElse(null);
sangho80f11cb2015-04-01 13:05:26 -0700583 }
584
sangho27462c62015-05-14 00:39:53 -0700585 /**
Charles Chan90772a72017-02-08 15:52:08 -0800586 * Returns tagged VLAN configured on given connect point.
587 * <p>
588 * Returns all matches if there are multiple tagged VLAN configured
589 * on the connect point.
590 *
591 * @param connectPoint connect point
592 * @return tagged VLAN or empty set if not configured
593 */
594 public Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
595 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
596 return interfaces.stream()
597 .map(Interface::vlanTagged)
598 .flatMap(vlanIds -> vlanIds.stream())
599 .collect(Collectors.toSet());
600 }
601
602 /**
603 * Returns native VLAN configured on given connect point.
604 * <p>
605 * Only returns the first match if there are multiple native VLAN configured
606 * on the connect point.
607 *
608 * @param connectPoint connect point
609 * @return native VLAN or null if not configured
610 */
611 public VlanId getNativeVlanId(ConnectPoint connectPoint) {
612 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
613 return interfaces.stream()
614 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
615 .map(Interface::vlanNative)
616 .findFirst()
617 .orElse(null);
618 }
619
620 /**
621 * Returns vlan port map of given device.
622 *
623 * @param deviceId device id
624 * @return vlan-port multimap
625 */
626 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
627 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
628
629 interfaceService.getInterfaces().stream()
630 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
631 .forEach(intf -> {
632 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
633 intf.vlanTagged().forEach(vlanTagged -> {
634 vlanPortMap.put(vlanTagged, intf.connectPoint().port());
635 });
636 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
637 });
638 vlanPortMap.removeAll(VlanId.NONE);
639
640 return vlanPortMap;
641 }
642
643 /**
Saurav Das7c305372015-10-28 12:39:42 -0700644 * Returns the next objective ID for the given NeighborSet.
Saurav Das4c35fc42015-11-20 15:27:53 -0800645 * If the nextObjective does not exist, a new one is created and
Saurav Das2d94d312015-11-24 23:21:05 -0800646 * its id is returned.
sangho27462c62015-05-14 00:39:53 -0700647 *
sangho80f1f892015-05-19 11:57:42 -0700648 * @param deviceId Device ID
649 * @param ns NegighborSet
Saurav Das4c35fc42015-11-20 15:27:53 -0800650 * @param meta metadata passed into the creation of a Next Objective
Pier Ventre229fd0b2016-10-31 16:49:19 -0700651 * @param isBos indicates if it is BoS or not
Saurav Das4c35fc42015-11-20 15:27:53 -0800652 * @return next objective ID or -1 if an error was encountered during the
653 * creation of the nextObjective
sangho27462c62015-05-14 00:39:53 -0700654 */
Saurav Das4c35fc42015-11-20 15:27:53 -0800655 public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns,
Pier Ventre229fd0b2016-10-31 16:49:19 -0700656 TrafficSelector meta, boolean isBos) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700657 if (groupHandlerMap.get(deviceId) != null) {
658 log.trace("getNextObjectiveId query in device {}", deviceId);
659 return groupHandlerMap
Pier Ventre229fd0b2016-10-31 16:49:19 -0700660 .get(deviceId).getNextObjectiveId(ns, meta, isBos);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700661 } else {
Saurav Das2d94d312015-11-24 23:21:05 -0800662 log.warn("getNextObjectiveId query - groupHandler for device {} "
663 + "not found", deviceId);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700664 return -1;
665 }
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700666 }
667
Charles Chan77277672015-10-20 16:24:19 -0700668 /**
Pier Ventre229fd0b2016-10-31 16:49:19 -0700669 * Returns the next objective ID for the given NeighborSet.
670 * If the nextObjective does not exist, a new one is created and
671 * its id is returned.
672 *
673 * @param deviceId Device ID
674 * @param ns NegighborSet
675 * @param meta metadata passed into the creation of a Next Objective
676 * @return next objective ID or -1 if an error was encountered during the
677 * creation of the nextObjective
678 */
679 public int getNextObjectiveId(DeviceId deviceId,
680 NeighborSet ns,
681 TrafficSelector meta) {
682 return this.getNextObjectiveId(deviceId, ns, meta, true);
683 }
684
685 /**
Saurav Das2d94d312015-11-24 23:21:05 -0800686 * Returns the next objective ID for the given subnet prefix. It is expected
Charles Chan10b0fb72017-02-02 16:20:42 -0800687 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -0800688 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -0700689 *
690 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -0800691 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -0800692 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -0700693 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800694 public int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -0700695 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800696 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
697 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -0700698 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -0800699 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -0800700 + "device {} not found", deviceId);
701 return -1;
702 }
703 }
704
705 /**
706 * Returns the next objective ID for the given portNumber, given the treatment.
707 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -0700708 * would result in different objectives. If the next object does not exist,
709 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -0800710 *
711 * @param deviceId Device ID
712 * @param portNum port number on device for which NextObjective is queried
713 * @param treatment the actions to apply on the packets (should include outport)
714 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -0700715 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -0700716 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -0800717 */
718 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
719 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -0700720 TrafficSelector meta,
721 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -0800722 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
723 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -0700724 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -0800725 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -0800726 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
727 + " not found", deviceId);
728 return -1;
729 }
730 }
731
sangho80f11cb2015-04-01 13:05:26 -0700732 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -0700733 @Override
734 public void process(PacketContext context) {
735
736 if (context.isHandled()) {
737 return;
738 }
739
740 InboundPacket pkt = context.inPacket();
741 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -0800742
743 if (ethernet == null) {
744 return;
745 }
746
Saurav Das2d94d312015-11-24 23:21:05 -0800747 log.trace("Rcvd pktin: {}", ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -0800748 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das368cf212017-03-15 15:15:14 -0700749 log.warn("Received unexpected ARP packet on {}", context.inPacket().receivedFrom());
750 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800751 return;
sangho80f11cb2015-04-01 13:05:26 -0700752 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -0800753 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
754 //ipHandler.addToPacketBuffer(ipv4Packet);
755 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
756 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -0700757 } else {
Charles Chand041ad82017-01-13 17:20:44 -0800758 // NOTE: We don't support IP learning at this moment so this
759 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800760 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -0700761 }
Pier Ventreb6a7f342016-11-26 21:05:22 -0800762 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
763 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -0800764 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -0800765 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -0800766 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
767 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
768 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
769 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
770 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
771 } else {
Charles Chand3727b72017-03-13 13:10:30 -0700772 log.debug("Received ICMPv6 0x{} - not handled",
773 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -0800774 }
775 } else {
776 // NOTE: We don't support IP learning at this moment so this
777 // is not necessary. Also it causes duplication of DHCPv6 packets.
778 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
779 }
sangho80f11cb2015-04-01 13:05:26 -0700780 }
781 }
782 }
783
784 private class InternalLinkListener implements LinkListener {
785 @Override
786 public void event(LinkEvent event) {
Charles Chan0cff8aa2017-03-29 16:39:05 -0700787 if (event.type() == LinkEvent.Type.LINK_ADDED ||
788 event.type() == LinkEvent.Type.LINK_UPDATED ||
789 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700790 log.debug("Event {} received from Link Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -0700791 scheduleEventHandlerIfNotScheduled(event);
792 }
793 }
794 }
795
796 private class InternalDeviceListener implements DeviceListener {
sangho80f11cb2015-04-01 13:05:26 -0700797 @Override
798 public void event(DeviceEvent event) {
sangho80f11cb2015-04-01 13:05:26 -0700799 switch (event.type()) {
800 case DEVICE_ADDED:
Saurav Dasf0f592d2016-11-18 15:21:57 -0800801 case PORT_UPDATED:
802 case PORT_ADDED:
sanghofb7c7292015-04-13 15:15:58 -0700803 case DEVICE_UPDATED:
804 case DEVICE_AVAILABILITY_CHANGED:
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700805 log.debug("Event {} received from Device Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -0700806 scheduleEventHandlerIfNotScheduled(event);
807 break;
808 default:
809 }
810 }
811 }
812
Saurav Das2d94d312015-11-24 23:21:05 -0800813 @SuppressWarnings("rawtypes")
sangho80f11cb2015-04-01 13:05:26 -0700814 private void scheduleEventHandlerIfNotScheduled(Event event) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700815 synchronized (threadSchedulerLock) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700816 eventQueue.add(event);
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700817 numOfEventsQueued++;
818
819 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
820 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700821 eventHandlerFuture = executorService
822 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
823 numOfHandlerScheduled++;
824 }
Jonathan Hart54541d12016-04-12 15:39:44 -0700825 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700826 numOfEventsQueued,
827 numOfHandlerScheduled);
sangho80f11cb2015-04-01 13:05:26 -0700828 }
sangho80f11cb2015-04-01 13:05:26 -0700829 }
830
831 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700832 @Override
sangho80f11cb2015-04-01 13:05:26 -0700833 public void run() {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700834 try {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700835 while (true) {
Saurav Das2d94d312015-11-24 23:21:05 -0800836 @SuppressWarnings("rawtypes")
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700837 Event event = null;
838 synchronized (threadSchedulerLock) {
839 if (!eventQueue.isEmpty()) {
840 event = eventQueue.poll();
841 numOfEventsExecuted++;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700842 } else {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700843 numOfHandlerExecution++;
844 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
845 numOfHandlerExecution, numOfEventsExecuted);
846 break;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700847 }
sanghofb7c7292015-04-13 15:15:58 -0700848 }
Charles Chan0cff8aa2017-03-29 16:39:05 -0700849 if (event.type() == LinkEvent.Type.LINK_ADDED ||
850 event.type() == LinkEvent.Type.LINK_UPDATED) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700851 processLinkAdded((Link) event.subject());
852 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre6d593892016-09-13 21:33:40 -0700853 Link linkRemoved = (Link) event.subject();
854 if (linkRemoved.src().elementId() instanceof DeviceId &&
855 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
856 continue;
857 }
858 if (linkRemoved.dst().elementId() instanceof DeviceId &&
859 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
860 continue;
861 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700862 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700863 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
864 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
865 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das62af8802015-12-04 10:52:59 -0800866 DeviceId deviceId = ((Device) event.subject()).id();
867 if (deviceService.isAvailable(deviceId)) {
Saurav Dasc28b3432015-10-30 17:45:38 -0700868 log.info("Processing device event {} for available device {}",
869 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700870 processDeviceAdded((Device) event.subject());
Saurav Dasc3604f12016-03-23 11:22:49 -0700871 } else {
872 log.info("Processing device event {} for unavailable device {}",
873 event.type(), ((Device) event.subject()).id());
874 processDeviceRemoved((Device) event.subject());
875 }
Saurav Dasf0f592d2016-11-18 15:21:57 -0800876 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd1872b02016-12-02 15:43:47 -0800877 // typically these calls come when device is added first time
878 // so port filtering rules are handled at the device_added event.
879 // port added calls represent all ports on the device,
880 // enabled or not.
Saurav Dasf0f592d2016-11-18 15:21:57 -0800881 log.debug("** PORT ADDED {}/{} -> {}",
Saurav Dasd1872b02016-12-02 15:43:47 -0800882 ((DeviceEvent) event).subject().id(),
883 ((DeviceEvent) event).port().number(),
884 event.type());
Saurav Dasf0f592d2016-11-18 15:21:57 -0800885 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd1872b02016-12-02 15:43:47 -0800886 // these calls happen for every subsequent event
887 // ports enabled, disabled, switch goes away, comes back
Saurav Dasf0f592d2016-11-18 15:21:57 -0800888 log.info("** PORT UPDATED {}/{} -> {}",
889 event.subject(),
890 ((DeviceEvent) event).port(),
891 event.type());
892 processPortUpdated(((Device) event.subject()),
893 ((DeviceEvent) event).port());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700894 } else {
895 log.warn("Unhandled event type: {}", event.type());
896 }
sangho80f11cb2015-04-01 13:05:26 -0700897 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700898 } catch (Exception e) {
899 log.error("SegmentRouting event handler "
900 + "thread thrown an exception: {}", e);
sangho80f11cb2015-04-01 13:05:26 -0700901 }
sangho80f11cb2015-04-01 13:05:26 -0700902 }
903 }
904
sangho80f11cb2015-04-01 13:05:26 -0700905 private void processLinkAdded(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -0700906 log.info("** LINK ADDED {}", link.toString());
Charles Chan319d1a22015-11-03 10:42:14 -0800907 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
908 log.warn("Source device of this link is not configured.");
909 return;
910 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700911 //Irrespective whether the local is a MASTER or not for this device,
912 //create group handler instance and push default TTP flow rules.
913 //Because in a multi-instance setup, instances can initiate
914 //groups for any devices. Also the default TTP rules are needed
915 //to be pushed before inserting any IP table entries for any device
916 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
917 .deviceId());
918 if (groupHandler != null) {
Saurav Das4c35fc42015-11-20 15:27:53 -0800919 groupHandler.linkUp(link, mastershipService.isLocalMaster(
920 link.src().deviceId()));
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700921 } else {
922 Device device = deviceService.getDevice(link.src().deviceId());
923 if (device != null) {
924 log.warn("processLinkAdded: Link Added "
925 + "Notification without Device Added "
926 + "event, still handling it");
927 processDeviceAdded(device);
928 groupHandler = groupHandlerMap.get(link.src()
929 .deviceId());
Saurav Das4c35fc42015-11-20 15:27:53 -0800930 groupHandler.linkUp(link, mastershipService.isLocalMaster(device.id()));
sangho80f11cb2015-04-01 13:05:26 -0700931 }
932 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700933
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700934 log.trace("Starting optimized route population process");
935 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null);
936 //log.trace("processLinkAdded: re-starting route population process");
937 //defaultRoutingHandler.startPopulationProcess();
Charles Chan72779502016-04-23 17:36:10 -0700938
939 mcastHandler.init();
sangho80f11cb2015-04-01 13:05:26 -0700940 }
941
942 private void processLinkRemoved(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -0700943 log.info("** LINK REMOVED {}", link.toString());
sangho2165d222015-05-01 09:38:25 -0700944 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
945 if (groupHandler != null) {
Saurav Das62af8802015-12-04 10:52:59 -0800946 groupHandler.portDown(link.src().port(),
947 mastershipService.isLocalMaster(link.src().deviceId()));
sangho2165d222015-05-01 09:38:25 -0700948 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700949 log.trace("Starting optimized route population process");
950 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link);
951 //log.trace("processLinkRemoved: re-starting route population process");
952 //defaultRoutingHandler.startPopulationProcess();
Charles Chan72779502016-04-23 17:36:10 -0700953
954 mcastHandler.processLinkDown(link);
sangho80f11cb2015-04-01 13:05:26 -0700955 }
956
957 private void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -0700958 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan319d1a22015-11-03 10:42:14 -0800959 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das8ec0ec42015-11-03 14:39:27 -0800960 log.warn("Device configuration uploading. Device {} will be "
961 + "processed after config completes.", device.id());
962 return;
963 }
Charles Chan72779502016-04-23 17:36:10 -0700964 processDeviceAddedInternal(device.id());
965 }
966
967 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -0700968 // Irrespective of whether the local is a MASTER or not for this device,
969 // we need to create a SR-group-handler instance. This is because in a
970 // multi-instance setup, any instance can initiate forwarding/next-objectives
971 // for any switch (even if this instance is a SLAVE or not even connected
972 // to the switch). To handle this, a default-group-handler instance is necessary
973 // per switch.
Charles Chan72779502016-04-23 17:36:10 -0700974 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
975 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -0800976 DefaultGroupHandler groupHandler;
977 try {
978 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -0700979 createGroupHandler(deviceId,
980 appId,
981 deviceConfiguration,
982 linkService,
983 flowObjectiveService,
984 this);
Charles Chan319d1a22015-11-03 10:42:14 -0800985 } catch (DeviceConfigNotFoundException e) {
986 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
987 return;
988 }
Charles Chan72779502016-04-23 17:36:10 -0700989 log.debug("updating groupHandlerMap with new config for device: {}",
990 deviceId);
991 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -0800992 }
Saurav Dasb149be12016-06-07 10:08:06 -0700993
Charles Chan72779502016-04-23 17:36:10 -0700994 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -0800995 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chandebfea32016-10-24 14:52:01 -0700996 hostHandler.init(deviceId);
Charles Chan82f19972016-05-17 13:13:55 -0700997 xConnectHandler.init(deviceId);
Charles Chan41f5ec02016-06-13 18:54:31 -0700998 cordConfigHandler.init(deviceId);
Charles Chan72779502016-04-23 17:36:10 -0700999 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001000 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001001 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001002 }
Charles Chan82ab1932016-01-30 23:22:37 -08001003
Charles Chandebfea32016-10-24 14:52:01 -07001004 appCfgHandler.init(deviceId);
1005 routeHandler.init(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001006 }
1007
Saurav Dasc3604f12016-03-23 11:22:49 -07001008 private void processDeviceRemoved(Device device) {
1009 nsNextObjStore.entrySet().stream()
1010 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1011 .forEach(entry -> {
1012 nsNextObjStore.remove(entry.getKey());
1013 });
Charles Chan10b0fb72017-02-02 16:20:42 -08001014 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001015 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1016 .forEach(entry -> {
Charles Chan10b0fb72017-02-02 16:20:42 -08001017 vlanNextObjStore.remove(entry.getKey());
Saurav Dasc3604f12016-03-23 11:22:49 -07001018 });
Saurav Dasc3604f12016-03-23 11:22:49 -07001019 portNextObjStore.entrySet().stream()
1020 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1021 .forEach(entry -> {
1022 portNextObjStore.remove(entry.getKey());
1023 });
Saurav Dasc3604f12016-03-23 11:22:49 -07001024 groupHandlerMap.remove(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -07001025 defaultRoutingHandler.purgeEcmpGraph(device.id());
Charles Chan72779502016-04-23 17:36:10 -07001026 mcastHandler.removeDevice(device.id());
Charles Chan82f19972016-05-17 13:13:55 -07001027 xConnectHandler.removeDevice(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -07001028 }
1029
Saurav Dasf0f592d2016-11-18 15:21:57 -08001030 private void processPortUpdated(Device device, Port port) {
1031 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1032 log.warn("Device configuration uploading. Not handling port event for"
1033 + "dev: {} port: {}", device.id(), port.number());
1034 return;
1035 }
Saurav Dasf9332192017-02-18 14:05:44 -08001036
1037 if (!mastershipService.isLocalMaster(device.id())) {
1038 log.debug("Not master for dev:{} .. not handling port updated event"
1039 + "for port {}", device.id(), port.number());
1040 return;
1041 }
1042
1043 // first we handle filtering rules associated with the port
1044 if (port.isEnabled()) {
1045 log.info("Switchport {}/{} enabled..programming filters",
1046 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001047 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001048 } else {
1049 log.info("Switchport {}/{} disabled..removing filters",
1050 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001051 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001052 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001053
1054 // portUpdated calls are for ports that have gone down or up. For switch
1055 // to switch ports, link-events should take care of any re-routing or
1056 // group editing necessary for port up/down. Here we only process edge ports
1057 // that are already configured.
Saurav Das3fb28272017-03-04 16:08:47 -08001058 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1059 VlanId untaggedVlan = getUntaggedVlanId(cp);
1060 VlanId nativeVlan = getNativeVlanId(cp);
1061 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001062
Saurav Das3fb28272017-03-04 16:08:47 -08001063 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001064 log.debug("Not handling port updated event for unconfigured port "
1065 + "dev/port: {}/{}", device.id(), port.number());
1066 return;
1067 }
Saurav Das3fb28272017-03-04 16:08:47 -08001068 if (untaggedVlan != null) {
1069 processEdgePort(device, port, untaggedVlan, true);
1070 }
1071 if (nativeVlan != null) {
1072 processEdgePort(device, port, nativeVlan, true);
1073 }
1074 if (!taggedVlans.isEmpty()) {
1075 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1076 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001077 }
1078
Saurav Das3fb28272017-03-04 16:08:47 -08001079 private void processEdgePort(Device device, Port port, VlanId vlanId,
1080 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001081 boolean portUp = port.isEnabled();
1082 if (portUp) {
Saurav Das3fb28272017-03-04 16:08:47 -08001083 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001084 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001085 } else {
Saurav Das3fb28272017-03-04 16:08:47 -08001086 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001087 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001088 }
1089
Srikanth Vavilapalli64505482015-04-21 13:04:13 -07001090 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sangho80f11cb2015-04-01 13:05:26 -07001091 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001092 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001093 } else {
1094 log.warn("Group handler not found for dev:{}. Not handling edge port"
1095 + " {} event for port:{}", device.id(),
1096 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001097 }
1098 }
sangho27462c62015-05-14 00:39:53 -07001099
Pier Ventreb6a7f342016-11-26 21:05:22 -08001100 /**
1101 * Registers the given connect point with the NRS, this is necessary
1102 * to receive the NDP and ARP packets from the NRS.
1103 *
1104 * @param portToRegister connect point to register
1105 */
1106 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001107 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001108 portToRegister,
1109 neighbourHandler,
1110 appId
1111 );
1112 }
1113
Charles Chan72f556a2015-10-05 17:50:33 -07001114 private class InternalConfigListener implements NetworkConfigListener {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001115 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001116
Charles Chanb7f75ac2016-01-11 18:28:54 -08001117 /**
1118 * Constructs the internal network config listener.
1119 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001120 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001121 */
Charles Chan46fdfaf2016-11-09 20:51:44 -08001122 public InternalConfigListener(SegmentRoutingManager srManager) {
1123 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001124 }
1125
Charles Chanb7f75ac2016-01-11 18:28:54 -08001126 /**
1127 * Reads network config and initializes related data structure accordingly.
1128 */
Charles Chane7c61022015-10-07 14:21:45 -07001129 public void configureNetwork() {
Pier Ventreb6a7f342016-11-26 21:05:22 -08001130
Charles Chan46fdfaf2016-11-09 20:51:44 -08001131 deviceConfiguration = new DeviceConfiguration(srManager);
Charles Chane7c61022015-10-07 14:21:45 -07001132
Charles Chan46fdfaf2016-11-09 20:51:44 -08001133 arpHandler = new ArpHandler(srManager);
1134 icmpHandler = new IcmpHandler(srManager);
1135 ipHandler = new IpHandler(srManager);
1136 routingRulePopulator = new RoutingRulePopulator(srManager);
1137 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chane7c61022015-10-07 14:21:45 -07001138
1139 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1140 groupHandlerMap, tunnelStore);
1141 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1142 flowObjectiveService,
1143 tunnelHandler, policyStore);
1144
Charles Chane7c61022015-10-07 14:21:45 -07001145 for (Device device : deviceService.getDevices()) {
Charles Chan72779502016-04-23 17:36:10 -07001146 processDeviceAddedInternal(device.id());
Charles Chane7c61022015-10-07 14:21:45 -07001147 }
1148
1149 defaultRoutingHandler.startPopulationProcess();
Charles Chan72779502016-04-23 17:36:10 -07001150 mcastHandler.init();
Charles Chane7c61022015-10-07 14:21:45 -07001151 }
1152
Charles Chan72f556a2015-10-05 17:50:33 -07001153 @Override
1154 public void event(NetworkConfigEvent event) {
Charles Chan82ab1932016-01-30 23:22:37 -08001155 // TODO move this part to NetworkConfigEventHandler
1156 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1157 switch (event.type()) {
1158 case CONFIG_ADDED:
1159 log.info("Segment Routing Config added.");
1160 configureNetwork();
1161 break;
1162 case CONFIG_UPDATED:
1163 log.info("Segment Routing Config updated.");
1164 // TODO support dynamic configuration
1165 break;
1166 default:
1167 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001168 }
Charles Chan82ab1932016-01-30 23:22:37 -08001169 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chan82f19972016-05-17 13:13:55 -07001170 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan82ab1932016-01-30 23:22:37 -08001171 switch (event.type()) {
1172 case CONFIG_ADDED:
Charles Chan82f19972016-05-17 13:13:55 -07001173 appCfgHandler.processAppConfigAdded(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001174 break;
1175 case CONFIG_UPDATED:
Charles Chan82f19972016-05-17 13:13:55 -07001176 appCfgHandler.processAppConfigUpdated(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001177 break;
1178 case CONFIG_REMOVED:
Charles Chan82f19972016-05-17 13:13:55 -07001179 appCfgHandler.processAppConfigRemoved(event);
1180 break;
1181 default:
1182 break;
1183 }
Charles Chandebfea32016-10-24 14:52:01 -07001184 configureNetwork();
Charles Chan82f19972016-05-17 13:13:55 -07001185 } else if (event.configClass().equals(XConnectConfig.class)) {
1186 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1187 switch (event.type()) {
1188 case CONFIG_ADDED:
1189 xConnectHandler.processXConnectConfigAdded(event);
1190 break;
1191 case CONFIG_UPDATED:
1192 xConnectHandler.processXConnectConfigUpdated(event);
1193 break;
1194 case CONFIG_REMOVED:
1195 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001196 break;
1197 default:
1198 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001199 }
Pier Ventre6b19e482016-11-07 16:21:04 -08001200 } else if (event.configClass().equals(PwaasConfig.class)) {
1201 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1202 switch (event.type()) {
1203 case CONFIG_ADDED:
1204 l2TunnelHandler.processPwaasConfigAdded(event);
1205 break;
1206 case CONFIG_UPDATED:
1207 l2TunnelHandler.processPwaasConfigUpdated(event);
1208 break;
1209 case CONFIG_REMOVED:
1210 l2TunnelHandler.processPwaasConfigRemoved(event);
1211 break;
1212 default:
1213 break;
1214 }
Charles Chan72f556a2015-10-05 17:50:33 -07001215 }
1216 }
1217 }
Charles Chanf4586112015-11-09 16:37:23 -08001218
1219 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001220 @Override
1221 public void event(HostEvent event) {
1222 // Do not proceed without mastership
1223 DeviceId deviceId = event.subject().location().deviceId();
1224 if (!mastershipService.isLocalMaster(deviceId)) {
1225 return;
1226 }
1227
1228 switch (event.type()) {
1229 case HOST_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001230 hostHandler.processHostAddedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001231 break;
1232 case HOST_MOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001233 hostHandler.processHostMovedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001234 break;
1235 case HOST_REMOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001236 hostHandler.processHostRemoveEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001237 break;
1238 case HOST_UPDATED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001239 hostHandler.processHostUpdatedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001240 break;
1241 default:
1242 log.warn("Unsupported host event type: {}", event.type());
1243 break;
1244 }
1245 }
1246 }
1247
Charles Chanc91c8782016-03-30 17:54:24 -07001248 private class InternalMcastListener implements McastListener {
1249 @Override
1250 public void event(McastEvent event) {
1251 switch (event.type()) {
1252 case SOURCE_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001253 mcastHandler.processSourceAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001254 break;
1255 case SINK_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001256 mcastHandler.processSinkAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001257 break;
1258 case SINK_REMOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001259 mcastHandler.processSinkRemoved(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001260 break;
1261 case ROUTE_ADDED:
1262 case ROUTE_REMOVED:
1263 default:
1264 break;
1265 }
1266 }
1267 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001268
1269 private class InternalCordConfigListener implements CordConfigListener {
1270 @Override
1271 public void event(CordConfigEvent event) {
1272 switch (event.type()) {
1273 case ACCESS_AGENT_ADDED:
1274 cordConfigHandler.processAccessAgentAddedEvent(event);
1275 break;
1276 case ACCESS_AGENT_UPDATED:
1277 cordConfigHandler.processAccessAgentUpdatedEvent(event);
1278 break;
1279 case ACCESS_AGENT_REMOVED:
1280 cordConfigHandler.processAccessAgentRemovedEvent(event);
1281 break;
1282 case ACCESS_DEVICE_ADDED:
1283 case ACCESS_DEVICE_UPDATED:
1284 case ACCESS_DEVICE_REMOVED:
1285 default:
1286 break;
1287 }
1288 }
1289 }
Charles Chandebfea32016-10-24 14:52:01 -07001290
1291 private class InternalRouteEventListener implements RouteListener {
1292 @Override
1293 public void event(RouteEvent event) {
1294 switch (event.type()) {
1295 case ROUTE_ADDED:
1296 routeHandler.processRouteAdded(event);
1297 break;
1298 case ROUTE_UPDATED:
1299 routeHandler.processRouteUpdated(event);
1300 break;
1301 case ROUTE_REMOVED:
1302 routeHandler.processRouteRemoved(event);
1303 break;
1304 default:
1305 break;
1306 }
1307 }
1308 }
sangho80f11cb2015-04-01 13:05:26 -07001309}