blob: eb2cf5694cb19dfcd281249f451fec69795a1ac6 [file] [log] [blame]
sanghob35a6192015-04-01 13:05:26 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
sangho1e575652015-05-14 00:39:53 -070023import org.apache.felix.scr.annotations.Service;
sanghob35a6192015-04-01 13:05:26 -070024import org.onlab.packet.Ethernet;
25import org.onlab.packet.IPv4;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070026import org.onlab.util.KryoNamespace;
sanghob35a6192015-04-01 13:05:26 -070027import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
29import org.onosproject.event.Event;
Charles Chand6832882015-10-05 17:50:33 -070030import org.onosproject.net.config.ConfigFactory;
31import org.onosproject.net.config.NetworkConfigEvent;
32import org.onosproject.net.config.NetworkConfigRegistry;
33import org.onosproject.net.config.NetworkConfigListener;
34import org.onosproject.net.config.basics.SubjectFactories;
35import org.onosproject.segmentrouting.config.SegmentRoutingConfig;
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -070036import org.onosproject.segmentrouting.grouphandler.DefaultGroupHandler;
37import org.onosproject.segmentrouting.grouphandler.NeighborSet;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070038import org.onosproject.segmentrouting.grouphandler.NeighborSetNextObjectiveStoreKey;
sanghob35a6192015-04-01 13:05:26 -070039import org.onosproject.mastership.MastershipService;
40import org.onosproject.net.Device;
41import org.onosproject.net.DeviceId;
42import org.onosproject.net.Link;
sanghob35a6192015-04-01 13:05:26 -070043import org.onosproject.net.Port;
44import org.onosproject.net.device.DeviceEvent;
45import org.onosproject.net.device.DeviceListener;
46import org.onosproject.net.device.DeviceService;
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070047import org.onosproject.net.flowobjective.FlowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -070048import org.onosproject.net.group.GroupKey;
sanghob35a6192015-04-01 13:05:26 -070049import org.onosproject.net.host.HostService;
50import org.onosproject.net.intent.IntentService;
51import org.onosproject.net.link.LinkEvent;
52import org.onosproject.net.link.LinkListener;
53import org.onosproject.net.link.LinkService;
54import org.onosproject.net.packet.InboundPacket;
55import org.onosproject.net.packet.PacketContext;
56import org.onosproject.net.packet.PacketProcessor;
57import org.onosproject.net.packet.PacketService;
58import org.onosproject.net.topology.TopologyService;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070059import org.onosproject.store.service.EventuallyConsistentMap;
60import org.onosproject.store.service.EventuallyConsistentMapBuilder;
61import org.onosproject.store.service.StorageService;
62import org.onosproject.store.service.WallClockTimestamp;
sanghob35a6192015-04-01 13:05:26 -070063import org.slf4j.Logger;
64import org.slf4j.LoggerFactory;
65
Srikanth Vavilapalli23181912015-05-04 09:48:09 -070066import java.net.URI;
67import java.util.HashSet;
sangho1e575652015-05-14 00:39:53 -070068import java.util.List;
sanghob35a6192015-04-01 13:05:26 -070069import java.util.Map;
70import java.util.concurrent.ConcurrentHashMap;
71import java.util.concurrent.ConcurrentLinkedQueue;
72import java.util.concurrent.Executors;
73import java.util.concurrent.ScheduledExecutorService;
74import java.util.concurrent.ScheduledFuture;
75import java.util.concurrent.TimeUnit;
76
77@SuppressWarnings("ALL")
sangho1e575652015-05-14 00:39:53 -070078@Service
sanghob35a6192015-04-01 13:05:26 -070079@Component(immediate = true)
sangho1e575652015-05-14 00:39:53 -070080public class SegmentRoutingManager implements SegmentRoutingService {
sanghob35a6192015-04-01 13:05:26 -070081
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -070082 private static Logger log = LoggerFactory
83 .getLogger(SegmentRoutingManager.class);
sanghob35a6192015-04-01 13:05:26 -070084
85 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 protected CoreService coreService;
87
88 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
89 protected TopologyService topologyService;
90
91 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
92 protected PacketService packetService;
93
94 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
95 protected IntentService intentService;
96
97 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
98 protected HostService hostService;
99
100 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
101 protected DeviceService deviceService;
102
103 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700104 protected FlowObjectiveService flowObjectiveService;
sanghob35a6192015-04-01 13:05:26 -0700105
106 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
107 protected LinkService linkService;
108
109 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
sanghob35a6192015-04-01 13:05:26 -0700110 protected MastershipService mastershipService;
sangho1e575652015-05-14 00:39:53 -0700111
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700112 protected ArpHandler arpHandler = null;
113 protected IcmpHandler icmpHandler = null;
114 protected IpHandler ipHandler = null;
115 protected RoutingRulePopulator routingRulePopulator = null;
sanghob35a6192015-04-01 13:05:26 -0700116 protected ApplicationId appId;
sangho666cd6d2015-04-14 16:27:13 -0700117 protected DeviceConfiguration deviceConfiguration = null;
sanghob35a6192015-04-01 13:05:26 -0700118
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700119 private DefaultRoutingHandler defaultRoutingHandler = null;
sangho1e575652015-05-14 00:39:53 -0700120 private TunnelHandler tunnelHandler = null;
121 private PolicyHandler policyHandler = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700122 private InternalPacketProcessor processor = null;
123 private InternalLinkListener linkListener = null;
124 private InternalDeviceListener deviceListener = null;
sanghob35a6192015-04-01 13:05:26 -0700125 private InternalEventHandler eventHandler = new InternalEventHandler();
126
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700127 private ScheduledExecutorService executorService = Executors
128 .newScheduledThreadPool(1);
sanghob35a6192015-04-01 13:05:26 -0700129
130 private static ScheduledFuture<?> eventHandlerFuture = null;
131 private ConcurrentLinkedQueue<Event> eventQueue = new ConcurrentLinkedQueue<Event>();
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700132 private Map<DeviceId, DefaultGroupHandler> groupHandlerMap = new ConcurrentHashMap<DeviceId, DefaultGroupHandler>();
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700133 // Per device next objective ID store with (device id + neighbor set) as key
134 private EventuallyConsistentMap<NeighborSetNextObjectiveStoreKey,
135 Integer> nsNextObjStore = null;
sangho0b2b6d12015-05-20 22:16:38 -0700136 private EventuallyConsistentMap<String, Tunnel> tunnelStore = null;
137 private EventuallyConsistentMap<String, Policy> policyStore = null;
138
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700139 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
140 protected StorageService storageService;
sanghob35a6192015-04-01 13:05:26 -0700141
Charles Chand6832882015-10-05 17:50:33 -0700142 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
143 protected NetworkConfigRegistry cfgService;
144
Charles Chan4636be02015-10-07 14:21:45 -0700145 private final InternalConfigListener cfgListener =
146 new InternalConfigListener(this);
147
Charles Chand6832882015-10-05 17:50:33 -0700148 private final ConfigFactory cfgFactory =
149 new ConfigFactory(SubjectFactories.DEVICE_SUBJECT_FACTORY,
150 SegmentRoutingConfig.class,
151 "segmentrouting") {
152 @Override
153 public SegmentRoutingConfig createConfig() {
154 return new SegmentRoutingConfig();
155 }
156 };
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700157
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700158 private Object threadSchedulerLock = new Object();
159 private static int numOfEventsQueued = 0;
160 private static int numOfEventsExecuted = 0;
sanghob35a6192015-04-01 13:05:26 -0700161 private static int numOfHandlerExecution = 0;
162 private static int numOfHandlerScheduled = 0;
163
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700164 private KryoNamespace.Builder kryoBuilder = null;
165
sanghob35a6192015-04-01 13:05:26 -0700166 @Activate
167 protected void activate() {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700168 appId = coreService
169 .registerApplication("org.onosproject.segmentrouting");
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700170
171 kryoBuilder = new KryoNamespace.Builder()
172 .register(NeighborSetNextObjectiveStoreKey.class,
sangho0b2b6d12015-05-20 22:16:38 -0700173 NeighborSet.class,
174 DeviceId.class,
175 URI.class,
176 WallClockTimestamp.class,
177 org.onosproject.cluster.NodeId.class,
178 HashSet.class,
179 Tunnel.class,
180 DefaultTunnel.class,
181 Policy.class,
182 TunnelPolicy.class,
183 Policy.Type.class
184 );
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700185
186 log.debug("Creating EC map nsnextobjectivestore");
187 EventuallyConsistentMapBuilder<NeighborSetNextObjectiveStoreKey, Integer>
188 nsNextObjMapBuilder = storageService.eventuallyConsistentMapBuilder();
189
190 nsNextObjStore = nsNextObjMapBuilder
191 .withName("nsnextobjectivestore")
192 .withSerializer(kryoBuilder)
Madan Jampanibcf1a482015-06-24 19:05:56 -0700193 .withTimestampProvider((k, v) -> new WallClockTimestamp())
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700194 .build();
195 log.trace("Current size {}", nsNextObjStore.size());
196
sangho0b2b6d12015-05-20 22:16:38 -0700197 EventuallyConsistentMapBuilder<String, Tunnel> tunnelMapBuilder =
198 storageService.eventuallyConsistentMapBuilder();
199
200 tunnelStore = tunnelMapBuilder
201 .withName("tunnelstore")
202 .withSerializer(kryoBuilder)
Madan Jampanibcf1a482015-06-24 19:05:56 -0700203 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700204 .build();
205
206 EventuallyConsistentMapBuilder<String, Policy> policyMapBuilder =
207 storageService.eventuallyConsistentMapBuilder();
208
209 policyStore = policyMapBuilder
210 .withName("policystore")
211 .withSerializer(kryoBuilder)
Madan Jampanibcf1a482015-06-24 19:05:56 -0700212 .withTimestampProvider((k, v) -> new WallClockTimestamp())
sangho0b2b6d12015-05-20 22:16:38 -0700213 .build();
214
Charles Chand6832882015-10-05 17:50:33 -0700215 cfgService.addListener(cfgListener);
216 cfgService.registerConfigFactory(cfgFactory);
Charles Chand6832882015-10-05 17:50:33 -0700217
Charles Chanb8e10c82015-10-14 11:24:40 -0700218 processor = new InternalPacketProcessor();
219 linkListener = new InternalLinkListener();
220 deviceListener = new InternalDeviceListener();
221
222 packetService.addProcessor(processor, PacketProcessor.director(2));
223 linkService.addListener(linkListener);
224 deviceService.addListener(deviceListener);
225
226 cfgListener.configureNetwork();
227
sanghob35a6192015-04-01 13:05:26 -0700228 log.info("Started");
229 }
230
231 @Deactivate
232 protected void deactivate() {
Charles Chand6832882015-10-05 17:50:33 -0700233 cfgService.removeListener(cfgListener);
234 cfgService.unregisterConfigFactory(cfgFactory);
235
sanghob35a6192015-04-01 13:05:26 -0700236 packetService.removeProcessor(processor);
Charles Chanb8e10c82015-10-14 11:24:40 -0700237 linkService.removeListener(linkListener);
238 deviceService.removeListener(deviceListener);
sanghob35a6192015-04-01 13:05:26 -0700239 processor = null;
Charles Chanb8e10c82015-10-14 11:24:40 -0700240 linkListener = null;
241 deviceService = null;
242
243 groupHandlerMap.clear();
244
sanghob35a6192015-04-01 13:05:26 -0700245 log.info("Stopped");
246 }
247
sangho1e575652015-05-14 00:39:53 -0700248
249 @Override
250 public List<Tunnel> getTunnels() {
251 return tunnelHandler.getTunnels();
252 }
253
254 @Override
sangho71abe1b2015-06-29 14:58:47 -0700255 public TunnelHandler.Result createTunnel(Tunnel tunnel) {
256 return tunnelHandler.createTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700257 }
258
259 @Override
sangho71abe1b2015-06-29 14:58:47 -0700260 public TunnelHandler.Result removeTunnel(Tunnel tunnel) {
sangho1e575652015-05-14 00:39:53 -0700261 for (Policy policy: policyHandler.getPolicies()) {
262 if (policy.type() == Policy.Type.TUNNEL_FLOW) {
263 TunnelPolicy tunnelPolicy = (TunnelPolicy) policy;
264 if (tunnelPolicy.tunnelId().equals(tunnel.id())) {
265 log.warn("Cannot remove the tunnel used by a policy");
sangho71abe1b2015-06-29 14:58:47 -0700266 return TunnelHandler.Result.TUNNEL_IN_USE;
sangho1e575652015-05-14 00:39:53 -0700267 }
268 }
269 }
sangho71abe1b2015-06-29 14:58:47 -0700270 return tunnelHandler.removeTunnel(tunnel);
sangho1e575652015-05-14 00:39:53 -0700271 }
272
273 @Override
sangho71abe1b2015-06-29 14:58:47 -0700274 public PolicyHandler.Result removePolicy(Policy policy) {
275 return policyHandler.removePolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700276 }
277
278 @Override
sangho71abe1b2015-06-29 14:58:47 -0700279 public PolicyHandler.Result createPolicy(Policy policy) {
280 return policyHandler.createPolicy(policy);
sangho1e575652015-05-14 00:39:53 -0700281 }
282
283 @Override
284 public List<Policy> getPolicies() {
285 return policyHandler.getPolicies();
286 }
287
sanghof9d0bf12015-05-19 11:57:42 -0700288 /**
289 * Returns the tunnel object with the tunnel ID.
290 *
291 * @param tunnelId Tunnel ID
292 * @return Tunnel reference
293 */
sangho1e575652015-05-14 00:39:53 -0700294 public Tunnel getTunnel(String tunnelId) {
295 return tunnelHandler.getTunnel(tunnelId);
296 }
297
sanghob35a6192015-04-01 13:05:26 -0700298 /**
Saurav Dasa07f2032015-10-19 14:37:36 -0700299 * Returns the GroupKey object for the device and the NeighborSet given.
300 * XXX is this called
sanghob35a6192015-04-01 13:05:26 -0700301 *
302 * @param ns NeightborSet object for the GroupKey
303 * @return GroupKey object for the NeighborSet
304 */
305 public GroupKey getGroupKey(NeighborSet ns) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700306 for (DefaultGroupHandler groupHandler : groupHandlerMap.values()) {
sanghob35a6192015-04-01 13:05:26 -0700307 return groupHandler.getGroupKey(ns);
308 }
309
310 return null;
311 }
312
sangho1e575652015-05-14 00:39:53 -0700313 /**
sanghof9d0bf12015-05-19 11:57:42 -0700314 * Returns the next objective ID for the NeighborSet given. If the nextObjectiveID does not exist,
315 * a new one is created and returned.
sangho1e575652015-05-14 00:39:53 -0700316 *
sanghof9d0bf12015-05-19 11:57:42 -0700317 * @param deviceId Device ID
318 * @param ns NegighborSet
319 * @return next objective ID
sangho1e575652015-05-14 00:39:53 -0700320 */
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700321 public int getNextObjectiveId(DeviceId deviceId, NeighborSet ns) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700322 if (groupHandlerMap.get(deviceId) != null) {
323 log.trace("getNextObjectiveId query in device {}", deviceId);
324 return groupHandlerMap
325 .get(deviceId).getNextObjectiveId(ns);
326 } else {
327 log.warn("getNextObjectiveId query in device {} not found", deviceId);
328 return -1;
329 }
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700330 }
331
sanghob35a6192015-04-01 13:05:26 -0700332 private class InternalPacketProcessor implements PacketProcessor {
sanghob35a6192015-04-01 13:05:26 -0700333 @Override
334 public void process(PacketContext context) {
335
336 if (context.isHandled()) {
337 return;
338 }
339
340 InboundPacket pkt = context.inPacket();
341 Ethernet ethernet = pkt.parsed();
342
343 if (ethernet.getEtherType() == Ethernet.TYPE_ARP) {
344 arpHandler.processPacketIn(pkt);
345 } else if (ethernet.getEtherType() == Ethernet.TYPE_IPV4) {
346 IPv4 ipPacket = (IPv4) ethernet.getPayload();
347 ipHandler.addToPacketBuffer(ipPacket);
348 if (ipPacket.getProtocol() == IPv4.PROTOCOL_ICMP) {
349 icmpHandler.processPacketIn(pkt);
350 } else {
351 ipHandler.processPacketIn(pkt);
352 }
353 }
354 }
355 }
356
357 private class InternalLinkListener implements LinkListener {
358 @Override
359 public void event(LinkEvent event) {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700360 if (event.type() == LinkEvent.Type.LINK_ADDED
361 || event.type() == LinkEvent.Type.LINK_REMOVED) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700362 log.debug("Event {} received from Link Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700363 scheduleEventHandlerIfNotScheduled(event);
364 }
365 }
366 }
367
368 private class InternalDeviceListener implements DeviceListener {
sanghob35a6192015-04-01 13:05:26 -0700369 @Override
370 public void event(DeviceEvent event) {
sanghob35a6192015-04-01 13:05:26 -0700371 switch (event.type()) {
372 case DEVICE_ADDED:
373 case PORT_REMOVED:
sangho20eff1d2015-04-13 15:15:58 -0700374 case DEVICE_UPDATED:
375 case DEVICE_AVAILABILITY_CHANGED:
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700376 log.debug("Event {} received from Device Service", event.type());
sanghob35a6192015-04-01 13:05:26 -0700377 scheduleEventHandlerIfNotScheduled(event);
378 break;
379 default:
380 }
381 }
382 }
383
sanghob35a6192015-04-01 13:05:26 -0700384 private void scheduleEventHandlerIfNotScheduled(Event event) {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700385 synchronized (threadSchedulerLock) {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700386 eventQueue.add(event);
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700387 numOfEventsQueued++;
388
389 if ((numOfHandlerScheduled - numOfHandlerExecution) == 0) {
390 //No pending scheduled event handling threads. So start a new one.
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700391 eventHandlerFuture = executorService
392 .schedule(eventHandler, 100, TimeUnit.MILLISECONDS);
393 numOfHandlerScheduled++;
394 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700395 log.trace("numOfEventsQueued {}, numOfEventHanlderScheduled {}",
396 numOfEventsQueued,
397 numOfHandlerScheduled);
sanghob35a6192015-04-01 13:05:26 -0700398 }
sanghob35a6192015-04-01 13:05:26 -0700399 }
400
401 private class InternalEventHandler implements Runnable {
Srikanth Vavilapalli4db76e32015-04-07 15:12:32 -0700402 @Override
sanghob35a6192015-04-01 13:05:26 -0700403 public void run() {
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700404 try {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700405 while (true) {
406 Event event = null;
407 synchronized (threadSchedulerLock) {
408 if (!eventQueue.isEmpty()) {
409 event = eventQueue.poll();
410 numOfEventsExecuted++;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700411 } else {
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700412 numOfHandlerExecution++;
413 log.debug("numOfHandlerExecution {} numOfEventsExecuted {}",
414 numOfHandlerExecution, numOfEventsExecuted);
415 break;
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700416 }
sangho20eff1d2015-04-13 15:15:58 -0700417 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700418 if (event.type() == LinkEvent.Type.LINK_ADDED) {
419 processLinkAdded((Link) event.subject());
420 } else if (event.type() == LinkEvent.Type.LINK_REMOVED) {
421 processLinkRemoved((Link) event.subject());
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700422 } else if (event.type() == DeviceEvent.Type.DEVICE_ADDED ||
423 event.type() == DeviceEvent.Type.DEVICE_AVAILABILITY_CHANGED ||
424 event.type() == DeviceEvent.Type.DEVICE_UPDATED) {
425 if (deviceService.isAvailable(((Device) event.subject()).id())) {
426 processDeviceAdded((Device) event.subject());
427 }
428 } else if (event.type() == DeviceEvent.Type.PORT_REMOVED) {
429 processPortRemoved((Device) event.subject(),
430 ((DeviceEvent) event).port());
431 } else {
432 log.warn("Unhandled event type: {}", event.type());
433 }
sanghob35a6192015-04-01 13:05:26 -0700434 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700435 } catch (Exception e) {
436 log.error("SegmentRouting event handler "
437 + "thread thrown an exception: {}", e);
sanghob35a6192015-04-01 13:05:26 -0700438 }
sanghob35a6192015-04-01 13:05:26 -0700439 }
440 }
441
sanghob35a6192015-04-01 13:05:26 -0700442 private void processLinkAdded(Link link) {
443 log.debug("A new link {} was added", link.toString());
444
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700445 //Irrespective whether the local is a MASTER or not for this device,
446 //create group handler instance and push default TTP flow rules.
447 //Because in a multi-instance setup, instances can initiate
448 //groups for any devices. Also the default TTP rules are needed
449 //to be pushed before inserting any IP table entries for any device
450 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src()
451 .deviceId());
452 if (groupHandler != null) {
453 groupHandler.linkUp(link);
454 } else {
455 Device device = deviceService.getDevice(link.src().deviceId());
456 if (device != null) {
457 log.warn("processLinkAdded: Link Added "
458 + "Notification without Device Added "
459 + "event, still handling it");
460 processDeviceAdded(device);
461 groupHandler = groupHandlerMap.get(link.src()
462 .deviceId());
sanghob35a6192015-04-01 13:05:26 -0700463 groupHandler.linkUp(link);
464 }
465 }
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700466
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700467 log.trace("Starting optimized route population process");
468 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(null);
469 //log.trace("processLinkAdded: re-starting route population process");
470 //defaultRoutingHandler.startPopulationProcess();
sanghob35a6192015-04-01 13:05:26 -0700471 }
472
473 private void processLinkRemoved(Link link) {
474 log.debug("A link {} was removed", link.toString());
sangho834e4b02015-05-01 09:38:25 -0700475 DefaultGroupHandler groupHandler = groupHandlerMap.get(link.src().deviceId());
476 if (groupHandler != null) {
477 groupHandler.portDown(link.src().port());
478 }
Srikanth Vavilapalli5428b6c2015-05-14 20:22:47 -0700479 log.trace("Starting optimized route population process");
480 defaultRoutingHandler.populateRoutingRulesForLinkStatusChange(link);
481 //log.trace("processLinkRemoved: re-starting route population process");
482 //defaultRoutingHandler.startPopulationProcess();
sanghob35a6192015-04-01 13:05:26 -0700483 }
484
485 private void processDeviceAdded(Device device) {
486 log.debug("A new device with ID {} was added", device.id());
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700487 //Irrespective whether the local is a MASTER or not for this device,
488 //create group handler instance and push default TTP flow rules.
489 //Because in a multi-instance setup, instances can initiate
490 //groups for any devices. Also the default TTP rules are needed
491 //to be pushed before inserting any IP table entries for any device
492 DefaultGroupHandler dgh = DefaultGroupHandler.
493 createGroupHandler(device.id(),
494 appId,
495 deviceConfiguration,
496 linkService,
497 flowObjectiveService,
498 nsNextObjStore);
sanghob35a6192015-04-01 13:05:26 -0700499 groupHandlerMap.put(device.id(), dgh);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700500 defaultRoutingHandler.populateTtpRules(device.id());
sanghob35a6192015-04-01 13:05:26 -0700501 }
502
503 private void processPortRemoved(Device device, Port port) {
504 log.debug("Port {} was removed", port.toString());
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700505 DefaultGroupHandler groupHandler = groupHandlerMap.get(device.id());
sanghob35a6192015-04-01 13:05:26 -0700506 if (groupHandler != null) {
507 groupHandler.portDown(port.number());
508 }
509 }
sangho1e575652015-05-14 00:39:53 -0700510
Charles Chand6832882015-10-05 17:50:33 -0700511 private class InternalConfigListener implements NetworkConfigListener {
Charles Chan4636be02015-10-07 14:21:45 -0700512 SegmentRoutingManager segmentRoutingManager;
513
514 public InternalConfigListener(SegmentRoutingManager srMgr) {
515 this.segmentRoutingManager = srMgr;
516 }
517
518 public void configureNetwork() {
519 deviceConfiguration = new DeviceConfiguration(segmentRoutingManager.cfgService);
520
521 arpHandler = new ArpHandler(segmentRoutingManager);
522 icmpHandler = new IcmpHandler(segmentRoutingManager);
523 ipHandler = new IpHandler(segmentRoutingManager);
524 routingRulePopulator = new RoutingRulePopulator(segmentRoutingManager);
525 defaultRoutingHandler = new DefaultRoutingHandler(segmentRoutingManager);
526
527 tunnelHandler = new TunnelHandler(linkService, deviceConfiguration,
528 groupHandlerMap, tunnelStore);
529 policyHandler = new PolicyHandler(appId, deviceConfiguration,
530 flowObjectiveService,
531 tunnelHandler, policyStore);
532
Charles Chan4636be02015-10-07 14:21:45 -0700533 for (Device device : deviceService.getDevices()) {
534 //Irrespective whether the local is a MASTER or not for this device,
535 //create group handler instance and push default TTP flow rules.
536 //Because in a multi-instance setup, instances can initiate
537 //groups for any devices. Also the default TTP rules are needed
538 //to be pushed before inserting any IP table entries for any device
539 DefaultGroupHandler groupHandler = DefaultGroupHandler
540 .createGroupHandler(device.id(), appId,
541 deviceConfiguration, linkService,
542 flowObjectiveService,
543 nsNextObjStore);
544 groupHandlerMap.put(device.id(), groupHandler);
545 defaultRoutingHandler.populateTtpRules(device.id());
546 }
547
548 defaultRoutingHandler.startPopulationProcess();
549 }
550
Charles Chand6832882015-10-05 17:50:33 -0700551 @Override
552 public void event(NetworkConfigEvent event) {
Charles Chanb8e10c82015-10-14 11:24:40 -0700553 if (event.configClass().equals(SegmentRoutingConfig.class)) {
554 if (event.type() == NetworkConfigEvent.Type.CONFIG_ADDED) {
555 log.info("Network configuration added.");
556 configureNetwork();
557 }
558 if (event.type() == NetworkConfigEvent.Type.CONFIG_UPDATED) {
559 log.info("Network configuration updated.");
560 // TODO support dynamic configuration
561 }
Charles Chand6832882015-10-05 17:50:33 -0700562 }
563 }
564 }
sanghob35a6192015-04-01 13:05:26 -0700565}