blob: c61d5c9395c736fe6280cbc88dff4857950c4feb [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;
Saurav Das62ae6792017-05-15 15:34:25 -070019import com.google.common.collect.ImmutableMap;
Charles Chand7844e52016-10-20 17:02:44 -070020import com.google.common.collect.Maps;
Charles Chan90772a72017-02-08 15:52:08 -080021import com.google.common.collect.Multimap;
sangho80f11cb2015-04-01 13:05:26 -070022import org.apache.felix.scr.annotations.Activate;
23import org.apache.felix.scr.annotations.Component;
24import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho27462c62015-05-14 00:39:53 -070027import org.apache.felix.scr.annotations.Service;
sangho80f11cb2015-04-01 13:05:26 -070028import org.onlab.packet.Ethernet;
Pier Ventreb6b81d52016-12-02 08:16:05 -080029import org.onlab.packet.ICMP6;
Charles Chan77277672015-10-20 16:24:19 -070030import org.onlab.packet.IPv4;
Pier Ventreb6a7f342016-11-26 21:05:22 -080031import org.onlab.packet.IPv6;
Charles Chan77277672015-10-20 16:24:19 -070032import org.onlab.packet.IpPrefix;
Jonathan Hart54541d12016-04-12 15:39:44 -070033import org.onlab.packet.VlanId;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070034import org.onlab.util.KryoNamespace;
Saurav Dasc3604f12016-03-23 11:22:49 -070035import org.onosproject.cfg.ComponentConfigService;
sangho80f11cb2015-04-01 13:05:26 -070036import org.onosproject.core.ApplicationId;
37import org.onosproject.core.CoreService;
38import org.onosproject.event.Event;
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;
79import org.onosproject.segmentrouting.config.DeviceConfigNotFoundException;
80import org.onosproject.segmentrouting.config.DeviceConfiguration;
Pier Ventre6b19e482016-11-07 16:21:04 -080081import org.onosproject.segmentrouting.config.PwaasConfig;
Pier Ventreb6a7f342016-11-26 21:05:22 -080082import org.onosproject.segmentrouting.config.SegmentRoutingDeviceConfig;
Pier Ventre6b19e482016-11-07 16:21:04 -080083import org.onosproject.segmentrouting.config.SegmentRoutingAppConfig;
Charles Chan82f19972016-05-17 13:13:55 -070084import org.onosproject.segmentrouting.config.XConnectConfig;
Charles Chanc91c8782016-03-30 17:54:24 -070085import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
86import org.onosproject.segmentrouting.grouphandler.NeighborSet;
Charles Chan1eaf4802016-04-18 13:44:03 -070087import org.onosproject.segmentrouting.storekey.NeighborSetNextObjectiveStoreKey;
88import org.onosproject.segmentrouting.storekey.PortNextObjectiveStoreKey;
Charles Chan1eaf4802016-04-18 13:44:03 -070089import org.onosproject.segmentrouting.storekey.SubnetAssignedVidStoreKey;
Charles Chan10b0fb72017-02-02 16:20:42 -080090import org.onosproject.segmentrouting.storekey.VlanNextObjectiveStoreKey;
Charles Chan82f19972016-05-17 13:13:55 -070091import org.onosproject.segmentrouting.storekey.XConnectStoreKey;
Pier Ventre6b19e482016-11-07 16:21:04 -080092import org.onosproject.segmentrouting.pwaas.L2TunnelHandler;
Jonathan Hart54541d12016-04-12 15:39:44 -070093import org.onosproject.store.serializers.KryoNamespaces;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -070094import org.onosproject.store.service.EventuallyConsistentMap;
95import org.onosproject.store.service.EventuallyConsistentMapBuilder;
96import org.onosproject.store.service.StorageService;
97import org.onosproject.store.service.WallClockTimestamp;
sangho80f11cb2015-04-01 13:05:26 -070098import org.slf4j.Logger;
99import org.slf4j.LoggerFactory;
100
sangho27462c62015-05-14 00:39:53 -0700101import java.util.List;
sangho80f11cb2015-04-01 13:05:26 -0700102import java.util.Map;
Saurav Das62ae6792017-05-15 15:34:25 -0700103import java.util.Map.Entry;
Saurav Das7c305372015-10-28 12:39:42 -0700104import java.util.Set;
sangho80f11cb2015-04-01 13:05:26 -0700105import java.util.concurrent.ConcurrentHashMap;
106import java.util.concurrent.ConcurrentLinkedQueue;
107import java.util.concurrent.Executors;
108import java.util.concurrent.ScheduledExecutorService;
109import java.util.concurrent.ScheduledFuture;
110import java.util.concurrent.TimeUnit;
Charles Chan90772a72017-02-08 15:52:08 -0800111import java.util.stream.Collectors;
sangho80f11cb2015-04-01 13:05:26 -0700112
Charles Chand6d25332016-02-26 22:19:52 -0800113import static com.google.common.base.Preconditions.checkState;
Pier Ventreadb4ae62016-11-23 09:57:42 -0800114import static org.onlab.packet.Ethernet.TYPE_ARP;
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700115import static org.onlab.util.Tools.groupedThreads;
Charles Chand6d25332016-02-26 22:19:52 -0800116
Charles Chanb7f75ac2016-01-11 18:28:54 -0800117/**
118 * Segment routing manager.
119 */
Jonathan Hart54541d12016-04-12 15:39:44 -0700120@Service
121@Component(immediate = true)
sangho27462c62015-05-14 00:39:53 -0700122public class SegmentRoutingManager implements SegmentRoutingService {
sangho80f11cb2015-04-01 13:05:26 -0700123
Charles Chan46fdfaf2016-11-09 20:51:44 -0800124 private static Logger log = LoggerFactory.getLogger(SegmentRoutingManager.class);
sangho80f11cb2015-04-01 13:05:26 -0700125
126 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700127 private ComponentConfigService compCfgService;
sangho80f11cb2015-04-01 13:05:26 -0700128
129 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventreb6a7f342016-11-26 21:05:22 -0800130 private NeighbourResolutionService neighbourResolutionService;
131
132 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800133 public PathService pathService;
134
135 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700136 CoreService coreService;
sangho80f11cb2015-04-01 13:05:26 -0700137
138 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700139 PacketService packetService;
sangho80f11cb2015-04-01 13:05:26 -0700140
141 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700142 HostService hostService;
sangho80f11cb2015-04-01 13:05:26 -0700143
144 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700145 DeviceService deviceService;
sangho80f11cb2015-04-01 13:05:26 -0700146
147 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800148 public FlowObjectiveService flowObjectiveService;
sangho80f11cb2015-04-01 13:05:26 -0700149
150 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700151 LinkService linkService;
sangho27462c62015-05-14 00:39:53 -0700152
Charles Chan82ab1932016-01-30 23:22:37 -0800153 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800154 public MastershipService mastershipService;
Charles Chandebfea32016-10-24 14:52:01 -0700155
156 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Pier Ventref3cf5b92016-11-09 14:17:26 -0800157 public StorageService storageService;
Charles Chandebfea32016-10-24 14:52:01 -0700158
159 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
160 MulticastRouteService multicastRouteService;
161
162 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
163 TopologyService topologyService;
164
165 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chandebfea32016-10-24 14:52:01 -0700166 RouteService routeService;
Charles Chan82ab1932016-01-30 23:22:37 -0800167
168 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800169 public NetworkConfigRegistry cfgService;
Charles Chan82ab1932016-01-30 23:22:37 -0800170
Saurav Dasc3604f12016-03-23 11:22:49 -0700171 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Charles Chan46fdfaf2016-11-09 20:51:44 -0800172 public InterfaceService interfaceService;
173
Charles Chandebfea32016-10-24 14:52:01 -0700174 ArpHandler arpHandler = null;
175 IcmpHandler icmpHandler = null;
176 IpHandler ipHandler = null;
177 RoutingRulePopulator routingRulePopulator = null;
Ray Milkeyb85de082017-04-05 09:42:04 -0700178 ApplicationId appId;
179 DeviceConfiguration deviceConfiguration = null;
sangho80f11cb2015-04-01 13:05:26 -0700180
Charles Chandebfea32016-10-24 14:52:01 -0700181 DefaultRoutingHandler defaultRoutingHandler = null;
sangho27462c62015-05-14 00:39:53 -0700182 private TunnelHandler tunnelHandler = null;
183 private PolicyHandler policyHandler = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700184 private InternalPacketProcessor processor = null;
185 private InternalLinkListener linkListener = null;
186 private InternalDeviceListener deviceListener = null;
Charles Chan82f19972016-05-17 13:13:55 -0700187 private AppConfigHandler appCfgHandler = null;
Charles Chandebfea32016-10-24 14:52:01 -0700188 XConnectHandler xConnectHandler = null;
Charles Chan1eaf4802016-04-18 13:44:03 -0700189 private McastHandler mcastHandler = null;
Charles Chandebfea32016-10-24 14:52:01 -0700190 HostHandler hostHandler = null;
Pier Ventreb6a7f342016-11-26 21:05:22 -0800191 private RouteHandler routeHandler = null;
Pier Ventreb6b81d52016-12-02 08:16:05 -0800192 private SegmentRoutingNeighbourDispatcher neighbourHandler = null;
Pier Ventre6b19e482016-11-07 16:21:04 -0800193 private L2TunnelHandler l2TunnelHandler = null;
sangho80f11cb2015-04-01 13:05:26 -0700194 private InternalEventHandler eventHandler = new InternalEventHandler();
Charles Chan82ab1932016-01-30 23:22:37 -0800195 private final InternalHostListener hostListener = new InternalHostListener();
Charles Chanc91c8782016-03-30 17:54:24 -0700196 private final InternalConfigListener cfgListener = new InternalConfigListener(this);
197 private final InternalMcastListener mcastListener = new InternalMcastListener();
Charles Chandebfea32016-10-24 14:52:01 -0700198 private final InternalRouteEventListener routeListener = new InternalRouteEventListener();
sangho80f11cb2015-04-01 13:05:26 -0700199
Srikanth Vavilapalli64505482015-04-21 13:04:13 -0700200 private ScheduledExecutorService executorService = Executors
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700201 .newScheduledThreadPool(1, groupedThreads("SegmentRoutingManager", "event-%d", log));
sangho80f11cb2015-04-01 13:05:26 -0700202
Saurav Das2d94d312015-11-24 23:21:05 -0800203 @SuppressWarnings("unused")
sangho80f11cb2015-04-01 13:05:26 -0700204 private static ScheduledFuture<?> eventHandlerFuture = null;
Saurav Das2d94d312015-11-24 23:21:05 -0800205 @SuppressWarnings("rawtypes")
Yuta HIGUCHIebee2f12016-07-21 16:54:33 -0700206 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<>();
Charles Chanf4586112015-11-09 16:37:23 -0800207 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap =
Charles Chanb7f75ac2016-01-11 18:28:54 -0800208 new ConcurrentHashMap<>();
209 /**
210 * Per device next objective ID store with (device id + neighbor set) as key.
211 */
Ray Milkeyb85de082017-04-05 09:42:04 -0700212 EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, Integer>
Charles Chanf4586112015-11-09 16:37:23 -0800213 nsNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800214 /**
215 * Per device next objective ID store with (device id + subnet) as key.
216 */
Ray Milkeyb85de082017-04-05 09:42:04 -0700217 EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer>
Charles Chan10b0fb72017-02-02 16:20:42 -0800218 vlanNextObjStore = null;
Charles Chanb7f75ac2016-01-11 18:28:54 -0800219 /**
220 * Per device next objective ID store with (device id + port) as key.
221 */
Ray Milkeyb85de082017-04-05 09:42:04 -0700222 EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer>
Saurav Das2d94d312015-11-24 23:21:05 -0800223 portNextObjStore = null;
Charles Chan10b0fb72017-02-02 16:20:42 -0800224
Saurav Das62ae6792017-05-15 15:34:25 -0700225 // Local store for all links seen and their present status, used for
226 // optimized routing. The existence of the link in the keys is enough to know
227 // if the link has been "seen-before" by this instance of the controller.
228 // The boolean value indicates if the link is currently up or not.
229 // XXX Currently the optimized routing logic depends on "forgetting" a link
230 // when a switch goes down, but "remembering" it when only the link goes down.
231 // Consider changing this logic so we can use the Link Service instead of
232 // a local cache.
233 private Map<Link, Boolean> seenLinks = new ConcurrentHashMap<>();
234
Saurav Das2d94d312015-11-24 23:21:05 -0800235 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
236 private EventuallyConsistentMap<String, Policy> policyStore = null;
sangho4a5c42a2015-05-20 22:16:38 -0700237
Charles Chanc91c8782016-03-30 17:54:24 -0700238 private final ConfigFactory<DeviceId, SegmentRoutingDeviceConfig> deviceConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700239 new ConfigFactory<DeviceId, SegmentRoutingDeviceConfig>(
240 SubjectFactories.DEVICE_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700241 SegmentRoutingDeviceConfig.class, "segmentrouting") {
Charles Chan72f556a2015-10-05 17:50:33 -0700242 @Override
Charles Chan82ab1932016-01-30 23:22:37 -0800243 public SegmentRoutingDeviceConfig createConfig() {
244 return new SegmentRoutingDeviceConfig();
Charles Chan72f556a2015-10-05 17:50:33 -0700245 }
246 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800247
Charles Chanc91c8782016-03-30 17:54:24 -0700248 private final ConfigFactory<ApplicationId, SegmentRoutingAppConfig> appConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700249 new ConfigFactory<ApplicationId, SegmentRoutingAppConfig>(
250 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700251 SegmentRoutingAppConfig.class, "segmentrouting") {
Charles Chan82ab1932016-01-30 23:22:37 -0800252 @Override
253 public SegmentRoutingAppConfig createConfig() {
254 return new SegmentRoutingAppConfig();
255 }
256 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800257
Charles Chan82f19972016-05-17 13:13:55 -0700258 private final ConfigFactory<ApplicationId, XConnectConfig> xConnectConfigFactory =
259 new ConfigFactory<ApplicationId, XConnectConfig>(
260 SubjectFactories.APP_SUBJECT_FACTORY,
261 XConnectConfig.class, "xconnect") {
262 @Override
263 public XConnectConfig createConfig() {
264 return new XConnectConfig();
265 }
266 };
Pier Ventre6b19e482016-11-07 16:21:04 -0800267
Charles Chanc91c8782016-03-30 17:54:24 -0700268 private ConfigFactory<ApplicationId, McastConfig> mcastConfigFactory =
Charles Chan82f19972016-05-17 13:13:55 -0700269 new ConfigFactory<ApplicationId, McastConfig>(
270 SubjectFactories.APP_SUBJECT_FACTORY,
Charles Chanc91c8782016-03-30 17:54:24 -0700271 McastConfig.class, "multicast") {
272 @Override
273 public McastConfig createConfig() {
274 return new McastConfig();
275 }
276 };
277
Pier Ventre6b19e482016-11-07 16:21:04 -0800278 private final ConfigFactory<ApplicationId, PwaasConfig> pwaasConfigFactory =
279 new ConfigFactory<ApplicationId, PwaasConfig>(
280 SubjectFactories.APP_SUBJECT_FACTORY,
281 PwaasConfig.class, "pwaas") {
282 @Override
283 public PwaasConfig createConfig() {
284 return new PwaasConfig();
285 }
286 };
287
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700288 private Object threadSchedulerLock = new Object();
289 private static int numOfEventsQueued = 0;
290 private static int numOfEventsExecuted = 0;
sangho80f11cb2015-04-01 13:05:26 -0700291 private static int numOfHandlerExecution = 0;
292 private static int numOfHandlerScheduled = 0;
293
Charles Chan1963f4f2016-02-18 14:22:42 -0800294 /**
295 * Segment Routing App ID.
296 */
Charles Chan46fdfaf2016-11-09 20:51:44 -0800297 public static final String APP_NAME = "org.onosproject.segmentrouting";
Charles Chan10b0fb72017-02-02 16:20:42 -0800298
Charles Chanb7f75ac2016-01-11 18:28:54 -0800299 /**
300 * The default VLAN ID assigned to the interfaces without subnet config.
301 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800302 public static final VlanId INTERNAL_VLAN = VlanId.vlanId((short) 4094);
Saurav Das7c305372015-10-28 12:39:42 -0700303
sangho80f11cb2015-04-01 13:05:26 -0700304 @Activate
305 protected void activate() {
Charles Chan46fdfaf2016-11-09 20:51:44 -0800306 appId = coreService.registerApplication(APP_NAME);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700307
308 log.debug("Creating EC map nsnextobjectivestore");
309 EventuallyConsistentMapBuilder<NeighborSetNextObjectiveStoreKey, Integer>
310 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700311 nsNextObjStore = nsNextObjMapBuilder
312 .withName("nsnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700313 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700314 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700315 .build();
316 log.trace("Current size {}", nsNextObjStore.size());
317
Charles Chan10b0fb72017-02-02 16:20:42 -0800318 log.debug("Creating EC map vlannextobjectivestore");
319 EventuallyConsistentMapBuilder<VlanNextObjectiveStoreKey, Integer>
320 vlanNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
321 vlanNextObjStore = vlanNextObjMapBuilder
322 .withName("vlannextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700323 .withSerializer(createSerializer())
Charles Chan77277672015-10-20 16:24:19 -0700324 .withTimestampProvider((k, v) -> new WallClockTimestamp())
325 .build();
326
Saurav Das2d94d312015-11-24 23:21:05 -0800327 log.debug("Creating EC map subnetnextobjectivestore");
328 EventuallyConsistentMapBuilder<PortNextObjectiveStoreKey, Integer>
329 portNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
330 portNextObjStore = portNextObjMapBuilder
331 .withName("portnextobjectivestore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700332 .withSerializer(createSerializer())
Saurav Das2d94d312015-11-24 23:21:05 -0800333 .withTimestampProvider((k, v) -> new WallClockTimestamp())
334 .build();
335
sangho4a5c42a2015-05-20 22:16:38 -0700336 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
337 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700338 tunnelStore = tunnelMapBuilder
339 .withName("tunnelstore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700340 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700341 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700342 .build();
343
344 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
345 storageService.eventuallyConsistentMapBuilder();
sangho4a5c42a2015-05-20 22:16:38 -0700346 policyStore = policyMapBuilder
347 .withName("policystore")
Jonathan Hart54541d12016-04-12 15:39:44 -0700348 .withSerializer(createSerializer())
Madan Jampani675ae202015-06-24 19:05:56 -0700349 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho4a5c42a2015-05-20 22:16:38 -0700350 .build();
351
Saurav Dasc3604f12016-03-23 11:22:49 -0700352 compCfgService.preSetProperty("org.onosproject.net.group.impl.GroupManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800353 "purgeOnDisconnection", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700354 compCfgService.preSetProperty("org.onosproject.net.flow.impl.FlowRuleManager",
Pier Luigib9632ba2017-01-12 18:14:58 -0800355 "purgeOnDisconnection", "true");
Pier Luigib9632ba2017-01-12 18:14:58 -0800356 compCfgService.preSetProperty("org.onosproject.provider.host.impl.HostLocationProvider",
357 "requestInterceptsEnabled", "false");
Pier Luigibc976df2017-01-12 22:46:39 -0800358 compCfgService.preSetProperty("org.onosproject.incubator.net.neighbour.impl.NeighbourResolutionManager",
359 "requestInterceptsEnabled", "false");
360 compCfgService.preSetProperty("org.onosproject.dhcprelay.DhcpRelay",
361 "arpEnabled", "false");
Pier Luigi0ebeb312017-02-02 22:31:34 -0800362 compCfgService.preSetProperty("org.onosproject.net.host.impl.HostManager",
363 "greedyLearningIpv6", "true");
Charles Chancf8ea472017-02-28 15:15:17 -0800364 compCfgService.preSetProperty("org.onosproject.routing.cpr.ControlPlaneRedirectManager",
365 "forceUnprovision", "true");
Saurav Dasc3604f12016-03-23 11:22:49 -0700366
Charles Chan2b078ae2015-10-14 11:24:40 -0700367 processor = new InternalPacketProcessor();
368 linkListener = new InternalLinkListener();
369 deviceListener = new InternalDeviceListener();
Charles Chan82f19972016-05-17 13:13:55 -0700370 appCfgHandler = new AppConfigHandler(this);
371 xConnectHandler = new XConnectHandler(this);
Charles Chan1eaf4802016-04-18 13:44:03 -0700372 mcastHandler = new McastHandler(this);
373 hostHandler = new HostHandler(this);
Charles Chandebfea32016-10-24 14:52:01 -0700374 routeHandler = new RouteHandler(this);
Pier Ventreb6b81d52016-12-02 08:16:05 -0800375 neighbourHandler = new SegmentRoutingNeighbourDispatcher(this);
Pier Ventre6b19e482016-11-07 16:21:04 -0800376 l2TunnelHandler = new L2TunnelHandler(this);
Charles Chan2b078ae2015-10-14 11:24:40 -0700377
Charles Chand6d25332016-02-26 22:19:52 -0800378 cfgService.addListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700379 cfgService.registerConfigFactory(deviceConfigFactory);
380 cfgService.registerConfigFactory(appConfigFactory);
Charles Chan82f19972016-05-17 13:13:55 -0700381 cfgService.registerConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700382 cfgService.registerConfigFactory(mcastConfigFactory);
Pier Ventre6b19e482016-11-07 16:21:04 -0800383 cfgService.registerConfigFactory(pwaasConfigFactory);
Charles Chan82ab1932016-01-30 23:22:37 -0800384 hostService.addListener(hostListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700385 packetService.addProcessor(processor, PacketProcessor.director(2));
386 linkService.addListener(linkListener);
387 deviceService.addListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700388 multicastRouteService.addListener(mcastListener);
Charles Chanfd48c222017-06-19 00:43:31 -0700389 routeService.addListener(routeListener);
Charles Chan2b078ae2015-10-14 11:24:40 -0700390
391 cfgListener.configureNetwork();
392
sangho80f11cb2015-04-01 13:05:26 -0700393 log.info("Started");
394 }
395
Jonathan Hart54541d12016-04-12 15:39:44 -0700396 private KryoNamespace.Builder createSerializer() {
397 return new KryoNamespace.Builder()
398 .register(KryoNamespaces.API)
399 .register(NeighborSetNextObjectiveStoreKey.class,
Charles Chan10b0fb72017-02-02 16:20:42 -0800400 VlanNextObjectiveStoreKey.class,
Jonathan Hart54541d12016-04-12 15:39:44 -0700401 SubnetAssignedVidStoreKey.class,
402 NeighborSet.class,
403 Tunnel.class,
404 DefaultTunnel.class,
405 Policy.class,
406 TunnelPolicy.class,
407 Policy.Type.class,
408 PortNextObjectiveStoreKey.class,
Charles Chan82f19972016-05-17 13:13:55 -0700409 XConnectStoreKey.class
Jonathan Hart54541d12016-04-12 15:39:44 -0700410 );
411 }
412
sangho80f11cb2015-04-01 13:05:26 -0700413 @Deactivate
414 protected void deactivate() {
Charles Chan72f556a2015-10-05 17:50:33 -0700415 cfgService.removeListener(cfgListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700416 cfgService.unregisterConfigFactory(deviceConfigFactory);
417 cfgService.unregisterConfigFactory(appConfigFactory);
Charles Chandebfea32016-10-24 14:52:01 -0700418 cfgService.unregisterConfigFactory(xConnectConfigFactory);
Charles Chanc91c8782016-03-30 17:54:24 -0700419 cfgService.unregisterConfigFactory(mcastConfigFactory);
Pier Ventre6b19e482016-11-07 16:21:04 -0800420 cfgService.unregisterConfigFactory(pwaasConfigFactory);
Charles Chan72f556a2015-10-05 17:50:33 -0700421
Charles Chanfd48c222017-06-19 00:43:31 -0700422 hostService.removeListener(hostListener);
sangho80f11cb2015-04-01 13:05:26 -0700423 packetService.removeProcessor(processor);
Charles Chan2b078ae2015-10-14 11:24:40 -0700424 linkService.removeListener(linkListener);
425 deviceService.removeListener(deviceListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700426 multicastRouteService.removeListener(mcastListener);
Charles Chandebfea32016-10-24 14:52:01 -0700427 routeService.removeListener(routeListener);
Charles Chanc91c8782016-03-30 17:54:24 -0700428
Charles Chan2e71ef32017-02-23 15:44:08 -0800429 neighbourResolutionService.unregisterNeighbourHandlers(appId);
430
sangho80f11cb2015-04-01 13:05:26 -0700431 processor = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700432 linkListener = null;
Charles Chanc91c8782016-03-30 17:54:24 -0700433 deviceListener = null;
Charles Chan2b078ae2015-10-14 11:24:40 -0700434 groupHandlerMap.clear();
435
Charles Chanc91c8782016-03-30 17:54:24 -0700436 nsNextObjStore.destroy();
Charles Chan10b0fb72017-02-02 16:20:42 -0800437 vlanNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700438 portNextObjStore.destroy();
Charles Chanc91c8782016-03-30 17:54:24 -0700439 tunnelStore.destroy();
440 policyStore.destroy();
sangho80f11cb2015-04-01 13:05:26 -0700441 log.info("Stopped");
442 }
443
sangho27462c62015-05-14 00:39:53 -0700444 @Override
445 public List<Tunnel> getTunnels() {
446 return tunnelHandler.getTunnels();
447 }
448
449 @Override
sanghobd812f82015-06-29 14:58:47 -0700450 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
451 return tunnelHandler.createTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700452 }
453
454 @Override
sanghobd812f82015-06-29 14:58:47 -0700455 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho27462c62015-05-14 00:39:53 -0700456 for (Policy policy: policyHandler.getPolicies()) {
457 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
458 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
459 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
460 log.warn("Cannot remove the tunnel used by a policy");
sanghobd812f82015-06-29 14:58:47 -0700461 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho27462c62015-05-14 00:39:53 -0700462 }
463 }
464 }
sanghobd812f82015-06-29 14:58:47 -0700465 return tunnelHandler.removeTunnel(tunnel);
sangho27462c62015-05-14 00:39:53 -0700466 }
467
468 @Override
sanghobd812f82015-06-29 14:58:47 -0700469 public PolicyHandler.Result removePolicy(Policy policy) {
470 return policyHandler.removePolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700471 }
472
473 @Override
sanghobd812f82015-06-29 14:58:47 -0700474 public PolicyHandler.Result createPolicy(Policy policy) {
475 return policyHandler.createPolicy(policy);
sangho27462c62015-05-14 00:39:53 -0700476 }
477
478 @Override
479 public List<Policy> getPolicies() {
480 return policyHandler.getPolicies();
481 }
482
Saurav Das07c74602016-04-27 18:35:50 -0700483 @Override
484 public void rerouteNetwork() {
485 cfgListener.configureNetwork();
486 for (Device device : deviceService.getDevices()) {
Saurav Dasf9332192017-02-18 14:05:44 -0800487 if (mastershipService.isLocalMaster(device.id())) {
488 defaultRoutingHandler.populatePortAddressingRules(device.id());
489 }
Saurav Das07c74602016-04-27 18:35:50 -0700490 }
491 defaultRoutingHandler.startPopulationProcess();
492 }
493
Charles Chand7844e52016-10-20 17:02:44 -0700494 @Override
Pier Ventreb6a7f342016-11-26 21:05:22 -0800495 public Map<DeviceId, Set<IpPrefix>> getDeviceSubnetMap() {
496 Map<DeviceId, Set<IpPrefix>> deviceSubnetMap = Maps.newHashMap();
Charles Chand7844e52016-10-20 17:02:44 -0700497 deviceService.getAvailableDevices().forEach(device -> {
498 deviceSubnetMap.put(device.id(), deviceConfiguration.getSubnets(device.id()));
499 });
500 return deviceSubnetMap;
501 }
502
Saurav Das62ae6792017-05-15 15:34:25 -0700503
504 @Override
505 public ImmutableMap<DeviceId, EcmpShortestPathGraph> getCurrentEcmpSpg() {
506 if (defaultRoutingHandler != null) {
507 return defaultRoutingHandler.getCurrentEmcpSpgMap();
508 } else {
509 return null;
510 }
511 }
512
513 @Override
514 public ImmutableMap<NeighborSetNextObjectiveStoreKey, Integer> getNeighborSet() {
515 if (nsNextObjStore != null) {
516 return ImmutableMap.copyOf(nsNextObjStore.entrySet());
517 } else {
518 return ImmutableMap.of();
519 }
520 }
521
sangho80f1f892015-05-19 11:57:42 -0700522 /**
Ray Milkeyb85de082017-04-05 09:42:04 -0700523 * Extracts the application ID from the manager.
524 *
525 * @return application ID
526 */
527 public ApplicationId appId() {
528 return appId;
529 }
530
531 /**
532 * Returns the device configuration.
533 *
534 * @return device configuration
535 */
536 public DeviceConfiguration deviceConfiguration() {
537 return deviceConfiguration;
538 }
539
540 /**
541 * Per device next objective ID store with (device id + neighbor set) as key.
542 *
543 * @return next objective ID store
544 */
545 public EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey, Integer> nsNextObjStore() {
546 return nsNextObjStore;
547 }
548
549 /**
550 * Per device next objective ID store with (device id + subnet) as key.
551 *
552 * @return vlan next object store
553 */
554 public EventuallyConsistentMap<VlanNextObjectiveStoreKey, Integer> vlanNextObjStore() {
555 return vlanNextObjStore;
556 }
557
558 /**
559 * Per device next objective ID store with (device id + port) as key.
560 *
561 * @return port next object store.
562 */
563 public EventuallyConsistentMap<PortNextObjectiveStoreKey, Integer> portNextObjStore() {
564 return portNextObjStore;
565 }
566
567 /**
Pier Ventre7a78de22016-10-31 15:00:01 -0700568 * Returns the MPLS-ECMP configuration.
569 *
570 * @return MPLS-ECMP value
571 */
572 public boolean getMplsEcmp() {
573 SegmentRoutingAppConfig segmentRoutingAppConfig = cfgService
574 .getConfig(this.appId, SegmentRoutingAppConfig.class);
575 return segmentRoutingAppConfig != null && segmentRoutingAppConfig.mplsEcmp();
576 }
577
578 /**
sangho80f1f892015-05-19 11:57:42 -0700579 * Returns the tunnel object with the tunnel ID.
580 *
581 * @param tunnelId Tunnel ID
582 * @return Tunnel reference
583 */
sangho27462c62015-05-14 00:39:53 -0700584 public Tunnel getTunnel(String tunnelId) {
585 return tunnelHandler.getTunnel(tunnelId);
586 }
587
Charles Chan90772a72017-02-08 15:52:08 -0800588 // TODO Consider moving these to InterfaceService
sangho80f11cb2015-04-01 13:05:26 -0700589 /**
Charles Chan10b0fb72017-02-02 16:20:42 -0800590 * Returns untagged VLAN configured on given connect point.
Charles Chan90772a72017-02-08 15:52:08 -0800591 * <p>
592 * Only returns the first match if there are multiple untagged VLAN configured
593 * on the connect point.
sangho80f11cb2015-04-01 13:05:26 -0700594 *
Charles Chan10b0fb72017-02-02 16:20:42 -0800595 * @param connectPoint connect point
596 * @return untagged VLAN or null if not configured
sangho80f11cb2015-04-01 13:05:26 -0700597 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800598 public VlanId getUntaggedVlanId(ConnectPoint connectPoint) {
599 return interfaceService.getInterfacesByPort(connectPoint).stream()
600 .filter(intf -> !intf.vlanUntagged().equals(VlanId.NONE))
601 .map(Interface::vlanUntagged)
602 .findFirst().orElse(null);
sangho80f11cb2015-04-01 13:05:26 -0700603 }
604
sangho27462c62015-05-14 00:39:53 -0700605 /**
Charles Chan90772a72017-02-08 15:52:08 -0800606 * Returns tagged VLAN configured on given connect point.
607 * <p>
608 * Returns all matches if there are multiple tagged VLAN configured
609 * on the connect point.
610 *
611 * @param connectPoint connect point
612 * @return tagged VLAN or empty set if not configured
613 */
614 public Set<VlanId> getTaggedVlanId(ConnectPoint connectPoint) {
615 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
616 return interfaces.stream()
617 .map(Interface::vlanTagged)
618 .flatMap(vlanIds -> vlanIds.stream())
619 .collect(Collectors.toSet());
620 }
621
622 /**
623 * Returns native VLAN configured on given connect point.
624 * <p>
625 * Only returns the first match if there are multiple native VLAN configured
626 * on the connect point.
627 *
628 * @param connectPoint connect point
629 * @return native VLAN or null if not configured
630 */
631 public VlanId getNativeVlanId(ConnectPoint connectPoint) {
632 Set<Interface> interfaces = interfaceService.getInterfacesByPort(connectPoint);
633 return interfaces.stream()
634 .filter(intf -> !intf.vlanNative().equals(VlanId.NONE))
635 .map(Interface::vlanNative)
636 .findFirst()
637 .orElse(null);
638 }
639
640 /**
641 * Returns vlan port map of given device.
642 *
643 * @param deviceId device id
644 * @return vlan-port multimap
645 */
646 public Multimap<VlanId, PortNumber> getVlanPortMap(DeviceId deviceId) {
647 HashMultimap<VlanId, PortNumber> vlanPortMap = HashMultimap.create();
648
649 interfaceService.getInterfaces().stream()
650 .filter(intf -> intf.connectPoint().deviceId().equals(deviceId))
651 .forEach(intf -> {
652 vlanPortMap.put(intf.vlanUntagged(), intf.connectPoint().port());
653 intf.vlanTagged().forEach(vlanTagged -> {
654 vlanPortMap.put(vlanTagged, intf.connectPoint().port());
655 });
656 vlanPortMap.put(intf.vlanNative(), intf.connectPoint().port());
657 });
658 vlanPortMap.removeAll(VlanId.NONE);
659
660 return vlanPortMap;
661 }
662
663 /**
Saurav Das2d94d312015-11-24 23:21:05 -0800664 * Returns the next objective ID for the given subnet prefix. It is expected
Charles Chan10b0fb72017-02-02 16:20:42 -0800665 * Returns the next objective ID for the given vlan id. It is expected
Saurav Das2d94d312015-11-24 23:21:05 -0800666 * that the next-objective has been pre-created from configuration.
Charles Chan77277672015-10-20 16:24:19 -0700667 *
668 * @param deviceId Device ID
Charles Chan10b0fb72017-02-02 16:20:42 -0800669 * @param vlanId VLAN ID
Saurav Das2d94d312015-11-24 23:21:05 -0800670 * @return next objective ID or -1 if it was not found
Charles Chan77277672015-10-20 16:24:19 -0700671 */
Charles Chan10b0fb72017-02-02 16:20:42 -0800672 public int getVlanNextObjectiveId(DeviceId deviceId, VlanId vlanId) {
Charles Chan77277672015-10-20 16:24:19 -0700673 if (groupHandlerMap.get(deviceId) != null) {
Charles Chan10b0fb72017-02-02 16:20:42 -0800674 log.trace("getVlanNextObjectiveId query in device {}", deviceId);
675 return groupHandlerMap.get(deviceId).getVlanNextObjectiveId(vlanId);
Charles Chan77277672015-10-20 16:24:19 -0700676 } else {
Charles Chan10b0fb72017-02-02 16:20:42 -0800677 log.warn("getVlanNextObjectiveId query - groupHandler for "
Saurav Das2d94d312015-11-24 23:21:05 -0800678 + "device {} not found", deviceId);
679 return -1;
680 }
681 }
682
683 /**
684 * Returns the next objective ID for the given portNumber, given the treatment.
685 * There could be multiple different treatments to the same outport, which
Saurav Das2cb38292017-03-29 19:09:17 -0700686 * would result in different objectives. If the next object does not exist,
687 * and should be created, a new one is created and its id is returned.
Saurav Das2d94d312015-11-24 23:21:05 -0800688 *
689 * @param deviceId Device ID
690 * @param portNum port number on device for which NextObjective is queried
691 * @param treatment the actions to apply on the packets (should include outport)
692 * @param meta metadata passed into the creation of a Next Objective if necessary
Saurav Das2cb38292017-03-29 19:09:17 -0700693 * @param createIfMissing true if a next object should be created if not found
Saurav Das07c74602016-04-27 18:35:50 -0700694 * @return next objective ID or -1 if an error occurred during retrieval or creation
Saurav Das2d94d312015-11-24 23:21:05 -0800695 */
696 public int getPortNextObjectiveId(DeviceId deviceId, PortNumber portNum,
697 TrafficTreatment treatment,
Saurav Das2cb38292017-03-29 19:09:17 -0700698 TrafficSelector meta,
699 boolean createIfMissing) {
Saurav Das2d94d312015-11-24 23:21:05 -0800700 DefaultGroupHandler ghdlr = groupHandlerMap.get(deviceId);
701 if (ghdlr != null) {
Saurav Das2cb38292017-03-29 19:09:17 -0700702 return ghdlr.getPortNextObjectiveId(portNum, treatment, meta, createIfMissing);
Saurav Das2d94d312015-11-24 23:21:05 -0800703 } else {
Charles Chanb7f75ac2016-01-11 18:28:54 -0800704 log.warn("getPortNextObjectiveId query - groupHandler for device {}"
705 + " not found", deviceId);
706 return -1;
707 }
708 }
709
Saurav Das62ae6792017-05-15 15:34:25 -0700710 /**
711 * Returns the group handler object for the specified device id.
712 *
713 * @param devId the device identifier
714 * @return the groupHandler object for the device id, or null if not found
715 */
716 public DefaultGroupHandler getGroupHandler(DeviceId devId) {
717 return groupHandlerMap.get(devId);
718 }
719
720 /**
721 * Returns true if this controller instance has seen this link before. The
722 * link may not be currently up, but as long as the link had been seen before
723 * this method will return true. The one exception is when the link was
724 * indeed seen before, but this controller instance was forced to forget it
725 * by a call to purgeSeenLink method.
726 *
727 * @param link the infrastructure link being queried
728 * @return true if this controller instance has seen this link before
729 */
730 public boolean isSeenLink(Link link) {
731 return seenLinks.containsKey(link);
732 }
733
734 /**
735 * Updates the seen link store. Updates can be for links that are currently
736 * available or not.
737 *
738 * @param link the link to update in the seen-link local store
739 * @param up the status of the link, true if up, false if down
740 */
741 public void updateSeenLink(Link link, boolean up) {
742 seenLinks.put(link, up);
743 }
744
745 /**
746 * Returns the status of a seen-link (up or down). If the link has not
747 * been seen-before, a null object is returned.
748 *
749 * @param link the infrastructure link being queried
750 * @return null if the link was not seen-before;
751 * true if the seen-link is up;
752 * false if the seen-link is down
753 */
754 public Boolean isSeenLinkUp(Link link) {
755 return seenLinks.get(link);
756 }
757
758 /**
759 * Makes this controller instance forget a previously seen before link.
760 *
761 * @param link the infrastructure link to purge
762 */
763 public void purgeSeenLink(Link link) {
764 seenLinks.remove(link);
765 }
766
767 /**
768 * Returns the status of a link as parallel link. A parallel link
769 * is defined as a link which has common src and dst switches as another
770 * seen-link that is currently enabled. It is not necessary for the link being
771 * queried to be a seen-link.
772 *
773 * @param link the infrastructure link being queried
774 * @return true if a seen-link exists that is up, and shares the
775 * same src and dst switches as the link being queried
776 */
777 public boolean isParallelLink(Link link) {
778 for (Entry<Link, Boolean> seen : seenLinks.entrySet()) {
779 Link seenLink = seen.getKey();
780 if (seenLink.equals(link)) {
781 continue;
782 }
783 if (seenLink.src().deviceId().equals(link.src().deviceId()) &&
784 seenLink.dst().deviceId().equals(link.dst().deviceId()) &&
785 seen.getValue()) {
786 return true;
787 }
788 }
789 return false;
790 }
791
sangho80f11cb2015-04-01 13:05:26 -0700792 private class InternalPacketProcessor implements PacketProcessor {
sangho80f11cb2015-04-01 13:05:26 -0700793 @Override
794 public void process(PacketContext context) {
795
796 if (context.isHandled()) {
797 return;
798 }
799
800 InboundPacket pkt = context.inPacket();
801 Ethernet ethernet = pkt.parsed();
Pier Luigi37a35432017-02-01 13:50:04 -0800802
803 if (ethernet == null) {
804 return;
805 }
806
Saurav Das2d94d312015-11-24 23:21:05 -0800807 log.trace("Rcvd pktin: {}", ethernet);
Pier Ventreadb4ae62016-11-23 09:57:42 -0800808 if (ethernet.getEtherType() == TYPE_ARP) {
Saurav Das62ae6792017-05-15 15:34:25 -0700809 log.warn("Received unexpected ARP packet on {}",
810 context.inPacket().receivedFrom());
Saurav Das368cf212017-03-15 15:15:14 -0700811 log.trace("{}", ethernet);
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800812 return;
sangho80f11cb2015-04-01 13:05:26 -0700813 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
Pier Ventreb6b81d52016-12-02 08:16:05 -0800814 IPv4 ipv4Packet = (IPv4) ethernet.getPayload();
815 //ipHandler.addToPacketBuffer(ipv4Packet);
816 if (ipv4Packet.getProtocol() == IPv4.PROTOCOL_ICMP) {
817 icmpHandler.processIcmp(ethernet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -0700818 } else {
Charles Chand041ad82017-01-13 17:20:44 -0800819 // NOTE: We don't support IP learning at this moment so this
820 // is not necessary. Also it causes duplication of DHCP packets.
Pier Ventre6b2c1b32016-12-09 17:26:04 -0800821 // ipHandler.processPacketIn(ipv4Packet, pkt.receivedFrom());
sangho80f11cb2015-04-01 13:05:26 -0700822 }
Pier Ventreb6a7f342016-11-26 21:05:22 -0800823 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV6) {
824 IPv6 ipv6Packet = (IPv6) ethernet.getPayload();
Pier Ventreb6b81d52016-12-02 08:16:05 -0800825 //ipHandler.addToPacketBuffer(ipv6Packet);
Pier Luigi37a35432017-02-01 13:50:04 -0800826 // We deal with the packet only if the packet is a ICMP6 ECHO/REPLY
Pier Ventreb6b81d52016-12-02 08:16:05 -0800827 if (ipv6Packet.getNextHeader() == IPv6.PROTOCOL_ICMP6) {
828 ICMP6 icmp6Packet = (ICMP6) ipv6Packet.getPayload();
829 if (icmp6Packet.getIcmpType() == ICMP6.ECHO_REQUEST ||
830 icmp6Packet.getIcmpType() == ICMP6.ECHO_REPLY) {
831 icmpHandler.processIcmpv6(ethernet, pkt.receivedFrom());
832 } else {
Saurav Das62ae6792017-05-15 15:34:25 -0700833 log.trace("Received ICMPv6 0x{} - not handled",
Charles Chand3727b72017-03-13 13:10:30 -0700834 Integer.toHexString(icmp6Packet.getIcmpType() & 0xff));
Pier Ventreb6b81d52016-12-02 08:16:05 -0800835 }
836 } else {
837 // NOTE: We don't support IP learning at this moment so this
838 // is not necessary. Also it causes duplication of DHCPv6 packets.
839 // ipHandler.processPacketIn(ipv6Packet, pkt.receivedFrom());
840 }
sangho80f11cb2015-04-01 13:05:26 -0700841 }
842 }
843 }
844
845 private class InternalLinkListener implements LinkListener {
846 @Override
847 public void event(LinkEvent event) {
Charles Chan0cff8aa2017-03-29 16:39:05 -0700848 if (event.type() == LinkEvent.Type.LINK_ADDED ||
849 event.type() == LinkEvent.Type.LINK_UPDATED ||
850 event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700851 log.debug("Event {} received from Link Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -0700852 scheduleEventHandlerIfNotScheduled(event);
853 }
854 }
855 }
856
857 private class InternalDeviceListener implements DeviceListener {
sangho80f11cb2015-04-01 13:05:26 -0700858 @Override
859 public void event(DeviceEvent event) {
sangho80f11cb2015-04-01 13:05:26 -0700860 switch (event.type()) {
861 case DEVICE_ADDED:
Saurav Dasf0f592d2016-11-18 15:21:57 -0800862 case PORT_UPDATED:
863 case PORT_ADDED:
sanghofb7c7292015-04-13 15:15:58 -0700864 case DEVICE_UPDATED:
865 case DEVICE_AVAILABILITY_CHANGED:
Saurav Das62ae6792017-05-15 15:34:25 -0700866 log.trace("Event {} received from Device Service", event.type());
sangho80f11cb2015-04-01 13:05:26 -0700867 scheduleEventHandlerIfNotScheduled(event);
868 break;
869 default:
870 }
871 }
872 }
873
Saurav Das2d94d312015-11-24 23:21:05 -0800874 @SuppressWarnings("rawtypes")
sangho80f11cb2015-04-01 13:05:26 -0700875 private void scheduleEventHandlerIfNotScheduled(Event event) {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700876 synchronized (threadSchedulerLock) {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700877 eventQueue.add(event);
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700878 numOfEventsQueued++;
879
880 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
881 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700882 eventHandlerFuture = executorService
883 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
884 numOfHandlerScheduled++;
885 }
Jonathan Hart54541d12016-04-12 15:39:44 -0700886 log.trace("numOfEventsQueued {}, numOfEventHandlerScheduled {}",
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700887 numOfEventsQueued,
888 numOfHandlerScheduled);
sangho80f11cb2015-04-01 13:05:26 -0700889 }
sangho80f11cb2015-04-01 13:05:26 -0700890 }
891
892 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli37a461b2015-04-07 15:12:32 -0700893 @Override
sangho80f11cb2015-04-01 13:05:26 -0700894 public void run() {
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700895 try {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700896 while (true) {
Saurav Das2d94d312015-11-24 23:21:05 -0800897 @SuppressWarnings("rawtypes")
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700898 Event event = null;
899 synchronized (threadSchedulerLock) {
900 if (!eventQueue.isEmpty()) {
901 event = eventQueue.poll();
902 numOfEventsExecuted++;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700903 } else {
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700904 numOfHandlerExecution++;
905 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
906 numOfHandlerExecution, numOfEventsExecuted);
907 break;
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700908 }
sanghofb7c7292015-04-13 15:15:58 -0700909 }
Charles Chan0cff8aa2017-03-29 16:39:05 -0700910 if (event.type() == LinkEvent.Type.LINK_ADDED ||
911 event.type() == LinkEvent.Type.LINK_UPDATED) {
Saurav Das62ae6792017-05-15 15:34:25 -0700912 // Note: do not update seenLinks here, otherwise every
913 // link, even one seen for the first time, will be appear
914 // to be a previously seen link
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700915 processLinkAdded((Link) event.subject());
916 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
Pier Ventre6d593892016-09-13 21:33:40 -0700917 Link linkRemoved = (Link) event.subject();
Saurav Das62ae6792017-05-15 15:34:25 -0700918 if (linkRemoved.type() == Link.Type.DIRECT) {
919 updateSeenLink(linkRemoved, false);
920 }
921 // device availability check helps to ensure that
922 // multiple link-removed events are actually treated as a
923 // single switch removed event. purgeSeenLink is necessary
924 // so we do rerouting (instead of rehashing) when switch
925 // comes back.
Pier Ventre6d593892016-09-13 21:33:40 -0700926 if (linkRemoved.src().elementId() instanceof DeviceId &&
927 !deviceService.isAvailable(linkRemoved.src().deviceId())) {
Saurav Das62ae6792017-05-15 15:34:25 -0700928 purgeSeenLink(linkRemoved);
Pier Ventre6d593892016-09-13 21:33:40 -0700929 continue;
930 }
931 if (linkRemoved.dst().elementId() instanceof DeviceId &&
932 !deviceService.isAvailable(linkRemoved.dst().deviceId())) {
Saurav Das62ae6792017-05-15 15:34:25 -0700933 purgeSeenLink(linkRemoved);
Pier Ventre6d593892016-09-13 21:33:40 -0700934 continue;
935 }
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700936 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700937 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
938 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
939 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
Saurav Das62af8802015-12-04 10:52:59 -0800940 DeviceId deviceId = ((Device) event.subject()).id();
941 if (deviceService.isAvailable(deviceId)) {
Saurav Dasc28b3432015-10-30 17:45:38 -0700942 log.info("Processing device event {} for available device {}",
943 event.type(), ((Device) event.subject()).id());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700944 processDeviceAdded((Device) event.subject());
Saurav Dasc3604f12016-03-23 11:22:49 -0700945 } else {
946 log.info("Processing device event {} for unavailable device {}",
947 event.type(), ((Device) event.subject()).id());
948 processDeviceRemoved((Device) event.subject());
949 }
Saurav Dasf0f592d2016-11-18 15:21:57 -0800950 } else if (event.type() == DeviceEvent.Type.PORT_ADDED) {
Saurav Dasd1872b02016-12-02 15:43:47 -0800951 // typically these calls come when device is added first time
952 // so port filtering rules are handled at the device_added event.
953 // port added calls represent all ports on the device,
954 // enabled or not.
Saurav Das62ae6792017-05-15 15:34:25 -0700955 log.trace("** PORT ADDED {}/{} -> {}",
Saurav Dasd1872b02016-12-02 15:43:47 -0800956 ((DeviceEvent) event).subject().id(),
957 ((DeviceEvent) event).port().number(),
958 event.type());
Saurav Dasf0f592d2016-11-18 15:21:57 -0800959 } else if (event.type() == DeviceEvent.Type.PORT_UPDATED) {
Saurav Dasd1872b02016-12-02 15:43:47 -0800960 // these calls happen for every subsequent event
961 // ports enabled, disabled, switch goes away, comes back
Saurav Dasf0f592d2016-11-18 15:21:57 -0800962 log.info("** PORT UPDATED {}/{} -> {}",
963 event.subject(),
964 ((DeviceEvent) event).port(),
965 event.type());
966 processPortUpdated(((Device) event.subject()),
967 ((DeviceEvent) event).port());
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -0700968 } else {
969 log.warn("Unhandled event type: {}", event.type());
970 }
sangho80f11cb2015-04-01 13:05:26 -0700971 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700972 } catch (Exception e) {
973 log.error("SegmentRouting event handler "
974 + "thread thrown an exception: {}", e);
sangho80f11cb2015-04-01 13:05:26 -0700975 }
sangho80f11cb2015-04-01 13:05:26 -0700976 }
977 }
978
sangho80f11cb2015-04-01 13:05:26 -0700979 private void processLinkAdded(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -0700980 log.info("** LINK ADDED {}", link.toString());
Charles Chan319d1a22015-11-03 10:42:14 -0800981 if (!deviceConfiguration.isConfigured(link.src().deviceId())) {
982 log.warn("Source device of this link is not configured.");
983 return;
984 }
Saurav Das62ae6792017-05-15 15:34:25 -0700985 if (link.type() != Link.Type.DIRECT) {
986 // NOTE: A DIRECT link might be transiently marked as INDIRECT
987 // if BDDP is received before LLDP. We can safely ignore that
988 // until the LLDP is received and the link is marked as DIRECT.
989 log.info("Ignore link {}->{}. Link type is {} instead of DIRECT.",
990 link.src(), link.dst(), link.type());
991 return;
992 }
993
994 //Irrespective of whether the local is a MASTER or not for this device,
995 //create group handler instance and push default TTP flow rules if needed,
996 //as in a multi-instance setup, instances can initiate groups for any device.
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -0700997 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
998 .deviceId());
999 if (groupHandler != null) {
Saurav Das62ae6792017-05-15 15:34:25 -07001000 groupHandler.portUpForLink(link);
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001001 } else {
1002 Device device = deviceService.getDevice(link.src().deviceId());
1003 if (device != null) {
1004 log.warn("processLinkAdded: Link Added "
1005 + "Notification without Device Added "
1006 + "event, still handling it");
1007 processDeviceAdded(device);
1008 groupHandler = groupHandlerMap.get(link.src()
1009 .deviceId());
Saurav Das62ae6792017-05-15 15:34:25 -07001010 groupHandler.portUpForLink(link);
sangho80f11cb2015-04-01 13:05:26 -07001011 }
1012 }
Srikanth Vavilapalli7cd16712015-05-04 09:48:09 -07001013
Srikanth Vavilapalli64d96c12015-05-14 20:22:47 -07001014 log.trace("Starting optimized route population process");
Saurav Das62ae6792017-05-15 15:34:25 -07001015 boolean seenBefore = isSeenLink(link);
1016 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null, link, null);
1017 if (mastershipService.isLocalMaster(link.src().deviceId())) {
1018 if (!seenBefore) {
1019 // if link seen first time, we need to ensure hash-groups have all ports
1020 groupHandler.retryHash(link, false, true);
1021 } else {
1022 //seen before-link
1023 if (isParallelLink(link)) {
1024 groupHandler.retryHash(link, false, false);
1025 }
1026 }
1027 }
Charles Chan72779502016-04-23 17:36:10 -07001028
1029 mcastHandler.init();
sangho80f11cb2015-04-01 13:05:26 -07001030 }
1031
1032 private void processLinkRemoved(Link link) {
Saurav Dasb149be12016-06-07 10:08:06 -07001033 log.info("** LINK REMOVED {}", link.toString());
Saurav Das62ae6792017-05-15 15:34:25 -07001034 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link, null, null);
1035
1036 // update local groupHandler stores
sangho2165d222015-05-01 09:38:25 -07001037 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
1038 if (groupHandler != null) {
Saurav Das62ae6792017-05-15 15:34:25 -07001039 if (mastershipService.isLocalMaster(link.src().deviceId()) &&
1040 isParallelLink(link)) {
1041 groupHandler.retryHash(link, true, false);
1042 }
1043 // ensure local stores are updated
1044 groupHandler.portDown(link.src().port());
1045 } else {
1046 log.warn("group handler not found for dev:{} when removing link: {}",
1047 link.src().deviceId(), link);
sangho2165d222015-05-01 09:38:25 -07001048 }
Charles Chan72779502016-04-23 17:36:10 -07001049
1050 mcastHandler.processLinkDown(link);
sangho80f11cb2015-04-01 13:05:26 -07001051 }
1052
1053 private void processDeviceAdded(Device device) {
Saurav Dasb149be12016-06-07 10:08:06 -07001054 log.info("** DEVICE ADDED with ID {}", device.id());
Charles Chan319d1a22015-11-03 10:42:14 -08001055 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
Saurav Das8ec0ec42015-11-03 14:39:27 -08001056 log.warn("Device configuration uploading. Device {} will be "
1057 + "processed after config completes.", device.id());
1058 return;
1059 }
Charles Chan72779502016-04-23 17:36:10 -07001060 processDeviceAddedInternal(device.id());
1061 }
1062
1063 private void processDeviceAddedInternal(DeviceId deviceId) {
Saurav Dasc28b3432015-10-30 17:45:38 -07001064 // Irrespective of whether the local is a MASTER or not for this device,
1065 // we need to create a SR-group-handler instance. This is because in a
1066 // multi-instance setup, any instance can initiate forwarding/next-objectives
1067 // for any switch (even if this instance is a SLAVE or not even connected
1068 // to the switch). To handle this, a default-group-handler instance is necessary
1069 // per switch.
Charles Chan72779502016-04-23 17:36:10 -07001070 log.debug("Current groupHandlerMap devs: {}", groupHandlerMap.keySet());
1071 if (groupHandlerMap.get(deviceId) == null) {
Charles Chan319d1a22015-11-03 10:42:14 -08001072 DefaultGroupHandler groupHandler;
1073 try {
1074 groupHandler = DefaultGroupHandler.
Charles Chan72779502016-04-23 17:36:10 -07001075 createGroupHandler(deviceId,
1076 appId,
1077 deviceConfiguration,
1078 linkService,
1079 flowObjectiveService,
1080 this);
Charles Chan319d1a22015-11-03 10:42:14 -08001081 } catch (DeviceConfigNotFoundException e) {
1082 log.warn(e.getMessage() + " Aborting processDeviceAdded.");
1083 return;
1084 }
Charles Chan72779502016-04-23 17:36:10 -07001085 log.debug("updating groupHandlerMap with new config for device: {}",
1086 deviceId);
1087 groupHandlerMap.put(deviceId, groupHandler);
Saurav Das8ec0ec42015-11-03 14:39:27 -08001088 }
Saurav Dasb149be12016-06-07 10:08:06 -07001089
Charles Chan72779502016-04-23 17:36:10 -07001090 if (mastershipService.isLocalMaster(deviceId)) {
Saurav Dasf9332192017-02-18 14:05:44 -08001091 defaultRoutingHandler.populatePortAddressingRules(deviceId);
Charles Chandebfea32016-10-24 14:52:01 -07001092 hostHandler.init(deviceId);
Charles Chan82f19972016-05-17 13:13:55 -07001093 xConnectHandler.init(deviceId);
Charles Chan72779502016-04-23 17:36:10 -07001094 DefaultGroupHandler groupHandler = groupHandlerMap.get(deviceId);
Charles Chan10b0fb72017-02-02 16:20:42 -08001095 groupHandler.createGroupsFromVlanConfig();
Charles Chan72779502016-04-23 17:36:10 -07001096 routingRulePopulator.populateSubnetBroadcastRule(deviceId);
Charles Chan77277672015-10-20 16:24:19 -07001097 }
Charles Chan82ab1932016-01-30 23:22:37 -08001098
Charles Chandebfea32016-10-24 14:52:01 -07001099 appCfgHandler.init(deviceId);
1100 routeHandler.init(deviceId);
sangho80f11cb2015-04-01 13:05:26 -07001101 }
1102
Saurav Dasc3604f12016-03-23 11:22:49 -07001103 private void processDeviceRemoved(Device device) {
1104 nsNextObjStore.entrySet().stream()
1105 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1106 .forEach(entry -> {
1107 nsNextObjStore.remove(entry.getKey());
1108 });
Charles Chan10b0fb72017-02-02 16:20:42 -08001109 vlanNextObjStore.entrySet().stream()
Saurav Dasc3604f12016-03-23 11:22:49 -07001110 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1111 .forEach(entry -> {
Charles Chan10b0fb72017-02-02 16:20:42 -08001112 vlanNextObjStore.remove(entry.getKey());
Saurav Dasc3604f12016-03-23 11:22:49 -07001113 });
Saurav Dasc3604f12016-03-23 11:22:49 -07001114 portNextObjStore.entrySet().stream()
1115 .filter(entry -> entry.getKey().deviceId().equals(device.id()))
1116 .forEach(entry -> {
1117 portNextObjStore.remove(entry.getKey());
1118 });
Charles Chan17d75d82017-06-15 00:44:51 -07001119
1120 seenLinks.keySet().removeIf(key -> key.src().deviceId().equals(device.id()) ||
1121 key.dst().deviceId().equals(device.id()));
1122
Saurav Dasc3604f12016-03-23 11:22:49 -07001123 groupHandlerMap.remove(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -07001124 defaultRoutingHandler.purgeEcmpGraph(device.id());
Saurav Das62ae6792017-05-15 15:34:25 -07001125 // Note that a switch going down is associated with all of its links
1126 // going down as well, but it is treated as a single switch down event
1127 // while the link-downs are ignored.
1128 defaultRoutingHandler
1129 .populateRoutingRulesForLinkStatusChange(null, null, device.id());
Charles Chan72779502016-04-23 17:36:10 -07001130 mcastHandler.removeDevice(device.id());
Charles Chan82f19972016-05-17 13:13:55 -07001131 xConnectHandler.removeDevice(device.id());
Saurav Dasc3604f12016-03-23 11:22:49 -07001132 }
1133
Saurav Dasf0f592d2016-11-18 15:21:57 -08001134 private void processPortUpdated(Device device, Port port) {
1135 if (deviceConfiguration == null || !deviceConfiguration.isConfigured(device.id())) {
1136 log.warn("Device configuration uploading. Not handling port event for"
1137 + "dev: {} port: {}", device.id(), port.number());
1138 return;
1139 }
Saurav Dasf9332192017-02-18 14:05:44 -08001140
1141 if (!mastershipService.isLocalMaster(device.id())) {
1142 log.debug("Not master for dev:{} .. not handling port updated event"
1143 + "for port {}", device.id(), port.number());
1144 return;
1145 }
1146
1147 // first we handle filtering rules associated with the port
1148 if (port.isEnabled()) {
1149 log.info("Switchport {}/{} enabled..programming filters",
1150 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001151 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), true);
Saurav Dasf9332192017-02-18 14:05:44 -08001152 } else {
1153 log.info("Switchport {}/{} disabled..removing filters",
1154 device.id(), port.number());
Charles Chan43be46b2017-02-26 22:59:35 -08001155 routingRulePopulator.processSinglePortFilters(device.id(), port.number(), false);
Saurav Dasf9332192017-02-18 14:05:44 -08001156 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001157
1158 // portUpdated calls are for ports that have gone down or up. For switch
1159 // to switch ports, link-events should take care of any re-routing or
1160 // group editing necessary for port up/down. Here we only process edge ports
1161 // that are already configured.
Saurav Das3fb28272017-03-04 16:08:47 -08001162 ConnectPoint cp = new ConnectPoint(device.id(), port.number());
1163 VlanId untaggedVlan = getUntaggedVlanId(cp);
1164 VlanId nativeVlan = getNativeVlanId(cp);
1165 Set<VlanId> taggedVlans = getTaggedVlanId(cp);
Charles Chan10b0fb72017-02-02 16:20:42 -08001166
Saurav Das3fb28272017-03-04 16:08:47 -08001167 if (untaggedVlan == null && nativeVlan == null && taggedVlans.isEmpty()) {
Saurav Das62ae6792017-05-15 15:34:25 -07001168 log.debug("Not handling port updated event for non-edge port (unconfigured) "
Saurav Dasf0f592d2016-11-18 15:21:57 -08001169 + "dev/port: {}/{}", device.id(), port.number());
1170 return;
1171 }
Saurav Das3fb28272017-03-04 16:08:47 -08001172 if (untaggedVlan != null) {
1173 processEdgePort(device, port, untaggedVlan, true);
1174 }
1175 if (nativeVlan != null) {
1176 processEdgePort(device, port, nativeVlan, true);
1177 }
1178 if (!taggedVlans.isEmpty()) {
1179 taggedVlans.forEach(tag -> processEdgePort(device, port, tag, false));
1180 }
Saurav Dasf0f592d2016-11-18 15:21:57 -08001181 }
1182
Saurav Das3fb28272017-03-04 16:08:47 -08001183 private void processEdgePort(Device device, Port port, VlanId vlanId,
1184 boolean popVlan) {
Saurav Dasf0f592d2016-11-18 15:21:57 -08001185 boolean portUp = port.isEnabled();
1186 if (portUp) {
Saurav Das3fb28272017-03-04 16:08:47 -08001187 log.info("Device:EdgePort {}:{} is enabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001188 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001189 } else {
Saurav Das3fb28272017-03-04 16:08:47 -08001190 log.info("Device:EdgePort {}:{} is disabled in vlan: {}", device.id(),
Charles Chan10b0fb72017-02-02 16:20:42 -08001191 port.number(), vlanId);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001192 }
1193
Srikanth Vavilapalli64505482015-04-21 13:04:13 -07001194 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sangho80f11cb2015-04-01 13:05:26 -07001195 if (groupHandler != null) {
Saurav Das3fb28272017-03-04 16:08:47 -08001196 groupHandler.processEdgePort(port.number(), vlanId, popVlan, portUp);
Saurav Dasf0f592d2016-11-18 15:21:57 -08001197 } else {
1198 log.warn("Group handler not found for dev:{}. Not handling edge port"
1199 + " {} event for port:{}", device.id(),
1200 (portUp) ? "UP" : "DOWN", port.number());
sangho80f11cb2015-04-01 13:05:26 -07001201 }
1202 }
sangho27462c62015-05-14 00:39:53 -07001203
Pier Ventreb6a7f342016-11-26 21:05:22 -08001204 /**
1205 * Registers the given connect point with the NRS, this is necessary
1206 * to receive the NDP and ARP packets from the NRS.
1207 *
1208 * @param portToRegister connect point to register
1209 */
1210 public void registerConnectPoint(ConnectPoint portToRegister) {
Charles Chan2e71ef32017-02-23 15:44:08 -08001211 neighbourResolutionService.registerNeighbourHandler(
Pier Ventreb6a7f342016-11-26 21:05:22 -08001212 portToRegister,
1213 neighbourHandler,
1214 appId
1215 );
1216 }
1217
Charles Chan72f556a2015-10-05 17:50:33 -07001218 private class InternalConfigListener implements NetworkConfigListener {
Charles Chan46fdfaf2016-11-09 20:51:44 -08001219 SegmentRoutingManager srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001220
Charles Chanb7f75ac2016-01-11 18:28:54 -08001221 /**
1222 * Constructs the internal network config listener.
1223 *
Charles Chan46fdfaf2016-11-09 20:51:44 -08001224 * @param srManager segment routing manager
Charles Chanb7f75ac2016-01-11 18:28:54 -08001225 */
Charles Chan46fdfaf2016-11-09 20:51:44 -08001226 public InternalConfigListener(SegmentRoutingManager srManager) {
1227 this.srManager = srManager;
Charles Chane7c61022015-10-07 14:21:45 -07001228 }
1229
Charles Chanb7f75ac2016-01-11 18:28:54 -08001230 /**
1231 * Reads network config and initializes related data structure accordingly.
1232 */
Charles Chane7c61022015-10-07 14:21:45 -07001233 public void configureNetwork() {
Pier Ventreb6a7f342016-11-26 21:05:22 -08001234
Charles Chan46fdfaf2016-11-09 20:51:44 -08001235 deviceConfiguration = new DeviceConfiguration(srManager);
Charles Chane7c61022015-10-07 14:21:45 -07001236
Charles Chan46fdfaf2016-11-09 20:51:44 -08001237 arpHandler = new ArpHandler(srManager);
1238 icmpHandler = new IcmpHandler(srManager);
1239 ipHandler = new IpHandler(srManager);
1240 routingRulePopulator = new RoutingRulePopulator(srManager);
1241 defaultRoutingHandler = new DefaultRoutingHandler(srManager);
Charles Chane7c61022015-10-07 14:21:45 -07001242
1243 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
1244 groupHandlerMap, tunnelStore);
1245 policyHandler = new PolicyHandler(appId, deviceConfiguration,
1246 flowObjectiveService,
1247 tunnelHandler, policyStore);
1248
Charles Chane7c61022015-10-07 14:21:45 -07001249 for (Device device : deviceService.getDevices()) {
Charles Chan72779502016-04-23 17:36:10 -07001250 processDeviceAddedInternal(device.id());
Charles Chane7c61022015-10-07 14:21:45 -07001251 }
1252
1253 defaultRoutingHandler.startPopulationProcess();
Charles Chan72779502016-04-23 17:36:10 -07001254 mcastHandler.init();
Charles Chane7c61022015-10-07 14:21:45 -07001255 }
1256
Charles Chan72f556a2015-10-05 17:50:33 -07001257 @Override
1258 public void event(NetworkConfigEvent event) {
Charles Chan82ab1932016-01-30 23:22:37 -08001259 // TODO move this part to NetworkConfigEventHandler
1260 if (event.configClass().equals(SegmentRoutingDeviceConfig.class)) {
1261 switch (event.type()) {
1262 case CONFIG_ADDED:
1263 log.info("Segment Routing Config added.");
1264 configureNetwork();
1265 break;
1266 case CONFIG_UPDATED:
1267 log.info("Segment Routing Config updated.");
1268 // TODO support dynamic configuration
1269 break;
1270 default:
1271 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001272 }
Charles Chan82ab1932016-01-30 23:22:37 -08001273 } else if (event.configClass().equals(SegmentRoutingAppConfig.class)) {
Charles Chan82f19972016-05-17 13:13:55 -07001274 checkState(appCfgHandler != null, "NetworkConfigEventHandler is not initialized");
Charles Chan82ab1932016-01-30 23:22:37 -08001275 switch (event.type()) {
1276 case CONFIG_ADDED:
Charles Chan82f19972016-05-17 13:13:55 -07001277 appCfgHandler.processAppConfigAdded(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001278 break;
1279 case CONFIG_UPDATED:
Charles Chan82f19972016-05-17 13:13:55 -07001280 appCfgHandler.processAppConfigUpdated(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001281 break;
1282 case CONFIG_REMOVED:
Charles Chan82f19972016-05-17 13:13:55 -07001283 appCfgHandler.processAppConfigRemoved(event);
1284 break;
1285 default:
1286 break;
1287 }
Charles Chandebfea32016-10-24 14:52:01 -07001288 configureNetwork();
Charles Chan82f19972016-05-17 13:13:55 -07001289 } else if (event.configClass().equals(XConnectConfig.class)) {
1290 checkState(xConnectHandler != null, "XConnectHandler is not initialized");
1291 switch (event.type()) {
1292 case CONFIG_ADDED:
1293 xConnectHandler.processXConnectConfigAdded(event);
1294 break;
1295 case CONFIG_UPDATED:
1296 xConnectHandler.processXConnectConfigUpdated(event);
1297 break;
1298 case CONFIG_REMOVED:
1299 xConnectHandler.processXConnectConfigRemoved(event);
Charles Chan82ab1932016-01-30 23:22:37 -08001300 break;
1301 default:
1302 break;
Charles Chan2b078ae2015-10-14 11:24:40 -07001303 }
Pier Ventre6b19e482016-11-07 16:21:04 -08001304 } else if (event.configClass().equals(PwaasConfig.class)) {
1305 checkState(l2TunnelHandler != null, "L2TunnelHandler is not initialized");
1306 switch (event.type()) {
1307 case CONFIG_ADDED:
1308 l2TunnelHandler.processPwaasConfigAdded(event);
1309 break;
1310 case CONFIG_UPDATED:
1311 l2TunnelHandler.processPwaasConfigUpdated(event);
1312 break;
1313 case CONFIG_REMOVED:
1314 l2TunnelHandler.processPwaasConfigRemoved(event);
1315 break;
1316 default:
1317 break;
1318 }
Charles Chan72f556a2015-10-05 17:50:33 -07001319 }
1320 }
1321 }
Charles Chanf4586112015-11-09 16:37:23 -08001322
1323 private class InternalHostListener implements HostListener {
Charles Chanf4586112015-11-09 16:37:23 -08001324 @Override
1325 public void event(HostEvent event) {
1326 // Do not proceed without mastership
1327 DeviceId deviceId = event.subject().location().deviceId();
1328 if (!mastershipService.isLocalMaster(deviceId)) {
1329 return;
1330 }
1331
1332 switch (event.type()) {
1333 case HOST_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001334 hostHandler.processHostAddedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001335 break;
1336 case HOST_MOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001337 hostHandler.processHostMovedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001338 break;
1339 case HOST_REMOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001340 hostHandler.processHostRemoveEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001341 break;
1342 case HOST_UPDATED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001343 hostHandler.processHostUpdatedEvent(event);
Charles Chanf4586112015-11-09 16:37:23 -08001344 break;
1345 default:
1346 log.warn("Unsupported host event type: {}", event.type());
1347 break;
1348 }
1349 }
1350 }
1351
Charles Chanc91c8782016-03-30 17:54:24 -07001352 private class InternalMcastListener implements McastListener {
1353 @Override
1354 public void event(McastEvent event) {
1355 switch (event.type()) {
1356 case SOURCE_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001357 mcastHandler.processSourceAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001358 break;
1359 case SINK_ADDED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001360 mcastHandler.processSinkAdded(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001361 break;
1362 case SINK_REMOVED:
Charles Chan1eaf4802016-04-18 13:44:03 -07001363 mcastHandler.processSinkRemoved(event);
Charles Chanc91c8782016-03-30 17:54:24 -07001364 break;
1365 case ROUTE_ADDED:
1366 case ROUTE_REMOVED:
1367 default:
1368 break;
1369 }
1370 }
1371 }
Charles Chan41f5ec02016-06-13 18:54:31 -07001372
Charles Chandebfea32016-10-24 14:52:01 -07001373 private class InternalRouteEventListener implements RouteListener {
1374 @Override
1375 public void event(RouteEvent event) {
1376 switch (event.type()) {
1377 case ROUTE_ADDED:
1378 routeHandler.processRouteAdded(event);
1379 break;
1380 case ROUTE_UPDATED:
1381 routeHandler.processRouteUpdated(event);
1382 break;
1383 case ROUTE_REMOVED:
1384 routeHandler.processRouteRemoved(event);
1385 break;
1386 default:
1387 break;
1388 }
1389 }
1390 }
Saurav Das62ae6792017-05-15 15:34:25 -07001391
sangho80f11cb2015-04-01 13:05:26 -07001392}