Bug 8112: IT: send SMR-invoked Map-Request on SMR
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / lisp / MapServer.java
1 /*
2  * Copyright (c) 2014, 2017 Contextream, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.lispflowmapping.implementation.lisp;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Maps;
13 import com.google.common.collect.Sets;
14 import com.google.common.util.concurrent.ThreadFactoryBuilder;
15 import java.net.InetAddress;
16 import java.net.NetworkInterface;
17 import java.net.SocketException;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.Enumeration;
21 import java.util.HashSet;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Objects;
26 import java.util.Set;
27 import java.util.concurrent.Executors;
28 import java.util.concurrent.ScheduledExecutorService;
29 import java.util.concurrent.ScheduledFuture;
30 import java.util.concurrent.ThreadFactory;
31 import java.util.concurrent.TimeUnit;
32 import org.apache.commons.lang3.BooleanUtils;
33 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
34 import org.opendaylight.lispflowmapping.config.ConfigIni;
35 import org.opendaylight.lispflowmapping.implementation.util.LoggingUtil;
36 import org.opendaylight.lispflowmapping.implementation.util.MSNotificationInputUtil;
37 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
38 import org.opendaylight.lispflowmapping.interfaces.dao.Subscriber;
39 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
40 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
41 import org.opendaylight.lispflowmapping.interfaces.lisp.ISmrNotificationListener;
42 import org.opendaylight.lispflowmapping.interfaces.lisp.SmrEvent;
43 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
44 import org.opendaylight.lispflowmapping.lisp.authentication.LispAuthenticationUtil;
45 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
46 import org.opendaylight.lispflowmapping.lisp.type.MappingData;
47 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
48 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
49 import org.opendaylight.lispflowmapping.lisp.util.MapNotifyBuilderHelper;
50 import org.opendaylight.lispflowmapping.lisp.util.MapRequestUtil;
51 import org.opendaylight.lispflowmapping.lisp.util.MappingRecordUtil;
52 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
53 import org.opendaylight.lispflowmapping.lisp.util.SourceDestKeyHelper;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
55 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
56 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4PrefixBinary;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4PrefixBinaryBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv6PrefixBinary;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv6PrefixBinaryBuilder;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItem;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItemBuilder;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItem;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItemBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequestBuilder;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChanged;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.OdlMappingserviceListener;
79 import org.opendaylight.yangtools.concepts.ListenerRegistration;
80 import org.slf4j.Logger;
81 import org.slf4j.LoggerFactory;
82
83 public class MapServer implements IMapServerAsync, OdlMappingserviceListener, ISmrNotificationListener {
84
85     protected static final Logger LOG = LoggerFactory.getLogger(MapServer.class);
86     private static final byte[] ALL_ZEROES_XTR_ID = new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0};
87     private IMappingService mapService;
88     private boolean subscriptionService;
89     private IMapNotifyHandler notifyHandler;
90     private NotificationService notificationService;
91     private ListenerRegistration<MapServer> mapServerListenerRegistration;
92     private SmrScheduler scheduler;
93
94     public MapServer(IMappingService mapService, boolean subscriptionService,
95                      IMapNotifyHandler notifyHandler, NotificationService notificationService) {
96         Preconditions.checkNotNull(mapService);
97         this.mapService = mapService;
98         this.subscriptionService = subscriptionService;
99         this.notifyHandler = notifyHandler;
100         this.notificationService = notificationService;
101         if (notificationService != null) {
102             notificationService.registerNotificationListener(this);
103         }
104         scheduler = new SmrScheduler();
105     }
106
107     @Override
108     public void setSubscriptionService(boolean subscriptionService) {
109         this.subscriptionService = subscriptionService;
110     }
111
112     @SuppressWarnings("unchecked")
113     public void handleMapRegister(MapRegister mapRegister) {
114         boolean mappingUpdated = false;
115         boolean merge = ConfigIni.getInstance().mappingMergeIsSet() && mapRegister.isMergeEnabled();
116         Set<Subscriber> subscribers = Sets.newConcurrentHashSet();
117         MappingRecord oldMapping;
118
119         if (merge) {
120             if (!mapRegister.isXtrSiteIdPresent() || mapRegister.getXtrId() == null) {
121                 LOG.error("Merge bit is set in Map-Register, but xTR-ID is not present. Will not merge.");
122                 merge = false;
123             } else if (Arrays.equals(mapRegister.getXtrId().getValue(), ALL_ZEROES_XTR_ID)) {
124                 LOG.warn("Merge bit is set in Map-Register, but xTR-ID is all zeroes.");
125             }
126         }
127
128         for (MappingRecordItem record : mapRegister.getMappingRecordItem()) {
129             MappingRecord mapping = record.getMappingRecord();
130             Eid eid = mapping.getEid();
131             MappingData mappingData = new MappingData(mapping, System.currentTimeMillis());
132             mappingData.setMergeEnabled(merge);
133             mappingData.setXtrId(mapRegister.getXtrId());
134
135             oldMapping = getMappingRecord(mapService.getMapping(MappingOrigin.Southbound, eid));
136             mapService.addMapping(MappingOrigin.Southbound, eid, getSiteId(mapRegister), mappingData);
137             if (oldMapping != null
138                     && MappingRecordUtil.isNegativeMapping(oldMapping)
139                     && !oldMapping.getEid().equals(eid)) {
140                 if (subscriptionService) {
141                     // Here we save the subscribers of the OLD mapping before removing. We will add to this set the
142                     // subscribers of the NEW mapping below (since the EIDs are different, the result of
143                     // mappingChanged() will be true, and then send an SMR to all subscribers with the EID of the NEW
144                     // mapping only.
145                     Set<Subscriber> oldMappingSubscribers = getSubscribers(oldMapping.getEid());
146                     if (oldMappingSubscribers != null) {
147                         subscribers.addAll(oldMappingSubscribers);
148                         LoggingUtil.logSubscribers(LOG, oldMapping.getEid(), subscribers);
149                     }
150                 }
151                 mapService.removeMapping(MappingOrigin.Southbound, oldMapping.getEid());
152             }
153
154             if (subscriptionService) {
155                 MappingRecord newMapping = merge
156                         ? getMappingRecord(mapService.getMapping(MappingOrigin.Southbound, eid)) : mapping;
157
158                 if (mappingChanged(oldMapping, newMapping)) {
159                     if (LOG.isDebugEnabled()) {
160                         LOG.debug("Mapping update occured for {} SMRs will be sent for its subscribers.",
161                                 LispAddressStringifier.getString(eid));
162                     }
163                     Set<Subscriber> newMappingSubscribers = getSubscribers(eid);
164                     if (oldMapping != null && !oldMapping.getEid().equals(eid)) {
165                         newMappingSubscribers = addParentSubscribers(eid, newMappingSubscribers);
166                     }
167                     if (newMappingSubscribers != null) {
168                         subscribers.addAll(newMappingSubscribers);
169                         LoggingUtil.logSubscribers(LOG, eid, subscribers);
170                     }
171                     handleSmr(eid, subscribers);
172                     mappingUpdated = true;
173                 }
174             }
175         }
176         if (BooleanUtils.isTrue(mapRegister.isWantMapNotify())) {
177             LOG.trace("MapRegister wants MapNotify");
178             MapNotifyBuilder builder = new MapNotifyBuilder();
179             List<TransportAddress> rlocs = null;
180             if (merge) {
181                 Set<IpAddressBinary> notifyRlocs = new HashSet<IpAddressBinary>();
182                 List<MappingRecordItem> mergedMappings = new ArrayList<MappingRecordItem>();
183                 for (MappingRecordItem record : mapRegister.getMappingRecordItem()) {
184                     MappingRecord mapping = record.getMappingRecord();
185                     MappingRecord currentRecord = getMappingRecord(mapService.getMapping(MappingOrigin.Southbound,
186                             mapping.getEid()));
187                     mergedMappings.add(new MappingRecordItemBuilder().setMappingRecord(currentRecord).build());
188                     Set<IpAddressBinary> sourceRlocs = (Set<IpAddressBinary>) mapService.getData(
189                             MappingOrigin.Southbound, mapping.getEid(), SubKeys.SRC_RLOCS);
190                     if (sourceRlocs != null) {
191                         notifyRlocs.addAll(sourceRlocs);
192                     }
193                 }
194                 MapNotifyBuilderHelper.setFromMapRegisterAndMappingRecordItems(builder, mapRegister, mergedMappings);
195                 // send map-notify to merge group only when mapping record is changed
196                 if (mappingUpdated) {
197                     rlocs = getTransportAddresses(notifyRlocs);
198                 }
199             } else {
200                 MapNotifyBuilderHelper.setFromMapRegister(builder, mapRegister);
201             }
202             List<MappingRecordItem> mappings = builder.getMappingRecordItem();
203             if (mappings != null && mappings.get(0) != null && mappings.get(0).getMappingRecord() != null
204                     && mappings.get(0).getMappingRecord().getEid() != null) {
205                 MappingAuthkey authkey = mapService.getAuthenticationKey(mappings.get(0).getMappingRecord().getEid());
206                 if (authkey != null) {
207                     builder.setAuthenticationData(LispAuthenticationUtil.createAuthenticationData(builder.build(),
208                             authkey.getKeyString()));
209                 }
210             }
211             notifyHandler.handleMapNotify(builder.build(), rlocs);
212         }
213     }
214
215     private static List<TransportAddress> getTransportAddresses(Set<IpAddressBinary> addresses) {
216         List<TransportAddress> rlocs = new ArrayList<TransportAddress>();
217         for (IpAddressBinary address : addresses) {
218             TransportAddressBuilder tab = new TransportAddressBuilder();
219             tab.setIpAddress(address);
220             tab.setPort(new PortNumber(LispMessage.PORT_NUM));
221             rlocs.add(tab.build());
222         }
223         return rlocs;
224     }
225
226     private static SiteId getSiteId(MapRegister mapRegister) {
227         return (mapRegister.getSiteId() != null) ? new SiteId(mapRegister.getSiteId()) : null;
228     }
229
230     private static MappingRecord getMappingRecord(MappingData mappingData) {
231         return (mappingData != null) ? mappingData.getRecord() : null;
232     }
233
234     @Override
235     public void onMappingChanged(MappingChanged notification) {
236         LOG.trace("MappingChanged event of type: `{}'", notification.getChangeType());
237         if (subscriptionService) {
238             Eid eid = notification.getMappingRecord().getEid();
239             Set<Subscriber> subscribers = MSNotificationInputUtil.toSubscriberSet(notification.getSubscriberItem());
240             LoggingUtil.logSubscribers(LOG, eid, subscribers);
241             if (mapService.isMaster()) {
242                 sendSmrs(eid, subscribers);
243                 if (eid.getAddress() instanceof SourceDestKey) {
244                     Set<Subscriber> dstSubscribers = MSNotificationInputUtil.toSubscriberSetFromDst(
245                             notification.getDstSubscriberItem());
246                     LoggingUtil.logSubscribers(LOG, SourceDestKeyHelper.getDstBinary(eid), dstSubscribers);
247                     sendSmrs(SourceDestKeyHelper.getDstBinary(eid), dstSubscribers);
248                 }
249             }
250         }
251     }
252
253     private static boolean mappingChanged(MappingRecord oldMapping, MappingRecord newMapping) {
254         // We only check for fields we care about
255         // XXX: This code needs to be checked and updated when the YANG model for MappingRecord is modified
256         Preconditions.checkNotNull(newMapping, "The new mapping should never be null");
257         if (oldMapping == null) {
258             LOG.trace("mappingChanged(): old mapping is null");
259             return true;
260         } else if (!Objects.equals(oldMapping.getEid(), newMapping.getEid())) {
261             LOG.trace("mappingChanged(): EID");
262             return true;
263         } else if (!Objects.equals(oldMapping.getLocatorRecord(), newMapping.getLocatorRecord())) {
264             LOG.trace("mappingChanged(): RLOC");
265             return true;
266         } else if (!Objects.equals(oldMapping.getAction(), newMapping.getAction())) {
267             LOG.trace("mappingChanged(): action");
268             return true;
269         } else if (!Objects.equals(oldMapping.getRecordTtl(), newMapping.getRecordTtl())) {
270             LOG.trace("mappingChanged(): TTL");
271             return true;
272         } else if (!Objects.equals(oldMapping.getMapVersion(), newMapping.getMapVersion())) {
273             LOG.trace("mappingChanged(): mapping version");
274             return true;
275         }
276         return false;
277     }
278
279     private void handleSmr(Eid eid, Set<Subscriber> subscribers) {
280         sendSmrs(eid, subscribers);
281
282         // For SrcDst LCAF also send SMRs to Dst prefix
283         if (eid.getAddress() instanceof SourceDestKey) {
284             Eid dstAddr = SourceDestKeyHelper.getDstBinary(eid);
285             Set<Subscriber> dstSubs = getSubscribers(dstAddr);
286             sendSmrs(dstAddr, dstSubs);
287         }
288     }
289
290     private void sendSmrs(Eid eid, Set<Subscriber> subscribers) {
291         if (subscribers == null) {
292             return;
293         }
294         final MapRequestBuilder mrb = MapRequestUtil.prepareSMR(eid, LispAddressUtil.toRloc(getLocalAddress()));
295         LOG.trace("Built SMR packet: " + mrb.build().toString());
296
297         scheduler.scheduleSmrs(mrb, subscribers.iterator());
298     }
299
300     @SuppressWarnings("unchecked")
301     private Set<Subscriber> getSubscribers(Eid address) {
302         return (Set<Subscriber>) mapService.getData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
303     }
304
305     private Set<Subscriber> addParentSubscribers(Eid eid, Set<Subscriber> subscribers) {
306         Eid parentPrefix = mapService.getParentPrefix(eid);
307         if (parentPrefix == null) {
308             return subscribers;
309         }
310
311         Set<Subscriber> parentSubscribers = getSubscribers(parentPrefix);
312         if (parentSubscribers != null) {
313             if (subscribers != null) {
314                 subscribers.addAll(parentSubscribers);
315             } else {
316                 subscribers = parentSubscribers;
317             }
318         }
319         return subscribers;
320     }
321
322     private void addSubscribers(Eid address, Set<Subscriber> subscribers) {
323         mapService.addData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS, subscribers);
324     }
325
326     private static InetAddress getLocalAddress() {
327         try {
328             Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
329             while (interfaces.hasMoreElements()) {
330                 NetworkInterface current = interfaces.nextElement();
331                 LOG.trace("Interface " + current.toString());
332                 if (!current.isUp() || current.isLoopback() || current.isVirtual()) {
333                     continue;
334                 }
335                 Enumeration<InetAddress> addresses = current.getInetAddresses();
336                 while (addresses.hasMoreElements()) {
337                     InetAddress currentAddr = addresses.nextElement();
338                     // Skip loopback and link local addresses
339                     if (currentAddr.isLoopbackAddress() || currentAddr.isLinkLocalAddress()) {
340                         continue;
341                     }
342                     LOG.debug(currentAddr.getHostAddress());
343                     return currentAddr;
344                 }
345             }
346         } catch (SocketException se) {
347             LOG.debug("Caught socket exception", se);
348         }
349         return null;
350     }
351
352     @Override
353     public void onSmrInvokedReceived(SmrEvent event) {
354         scheduler.smrReceived(event);
355     }
356
357     /**
358      * Task scheduler is responsible for resending SMR messages to a subscriber (xTR)
359      * {@value ConfigIni#LISP_SMR_RETRY_COUNT} times, or until {@link ISmrNotificationListener#onSmrInvokedReceived}
360      * is triggered.
361      */
362     private class SmrScheduler {
363         final int cpuCores = Runtime.getRuntime().availableProcessors();
364         private final ThreadFactory threadFactory = new ThreadFactoryBuilder()
365                 .setNameFormat("smr-executor-%d").build();
366         private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(cpuCores * 2, threadFactory);
367         private final Map<Eid, Map<Subscriber, ScheduledFuture<?>>> eidFutureMap = Maps.newConcurrentMap();
368
369         void scheduleSmrs(MapRequestBuilder mrb, Iterator<Subscriber> subscribers) {
370             final Eid srcEid = fixSrcEidMask(mrb.getSourceEid().getEid());
371             cancelExistingFuturesForEid(srcEid);
372
373             final Map<Subscriber, ScheduledFuture<?>> subscriberFutureMap = Maps.newConcurrentMap();
374
375             // Using Iterator ensures that we don't get a ConcurrentModificationException when removing a Subscriber
376             // from a Set.
377             while (subscribers.hasNext()) {
378                 Subscriber subscriber = subscribers.next();
379                 if (subscriber.timedOut()) {
380                     LOG.debug("Lazy removing expired subscriber entry " + subscriber.getString());
381                     subscribers.remove();
382                 } else {
383                     final ScheduledFuture<?> future = executor.scheduleAtFixedRate(new CancellableRunnable(
384                             mrb, subscriber), 0L, ConfigIni.getInstance().getSmrTimeout(), TimeUnit.MILLISECONDS);
385                     subscriberFutureMap.put(subscriber, future);
386                 }
387             }
388
389             if (subscriberFutureMap.isEmpty()) {
390                 return;
391             }
392             eidFutureMap.put(srcEid, subscriberFutureMap);
393         }
394
395         void smrReceived(SmrEvent event) {
396             final List<Subscriber> subscriberList = event.getSubscriberList();
397             for (Subscriber subscriber : subscriberList) {
398                 if (LOG.isTraceEnabled()) {
399                     LOG.trace("SMR-invoked event, EID {}, subscriber {}",
400                             LispAddressStringifier.getString(event.getEid()),
401                             subscriber.getString());
402                     LOG.trace("eidFutureMap: {}", eidFutureMap);
403                 }
404                 final Map<Subscriber, ScheduledFuture<?>> subscriberFutureMap = eidFutureMap.get(event.getEid());
405                 if (subscriberFutureMap != null) {
406                     final ScheduledFuture<?> future = subscriberFutureMap.get(subscriber);
407                     if (future != null && !future.isCancelled()) {
408                         future.cancel(true);
409                         if (LOG.isDebugEnabled()) {
410                             LOG.debug("SMR-invoked MapRequest received, scheduled task for subscriber {}, EID {} with"
411                                     + " nonce {} has been cancelled", subscriber.getString(),
412                                     LispAddressStringifier.getString(event.getEid()), event.getNonce());
413                         }
414                         subscriberFutureMap.remove(subscriber);
415                     } else {
416                         if (future == null) {
417                             LOG.trace("No outstanding SMR tasks for EID {}, subscriber {}",
418                                     LispAddressStringifier.getString(event.getEid()), subscriber.getString());
419                         } else {
420                             LOG.trace("Future {} is cancelled", future);
421                         }
422                     }
423                     if (subscriberFutureMap.isEmpty()) {
424                         eidFutureMap.remove(event.getEid());
425                     }
426                 } else {
427                     if (LOG.isTraceEnabled()) {
428                         LOG.trace("No outstanding SMR tasks for EID {}",
429                                 LispAddressStringifier.getString(event.getEid()));
430                     }
431                 }
432             }
433         }
434
435         private void cancelExistingFuturesForEid(Eid eid) {
436             synchronized (eidFutureMap) {
437                 if (eidFutureMap.containsKey(eid)) {
438                     final Map<Subscriber, ScheduledFuture<?>> subscriberFutureMap = eidFutureMap.get(eid);
439                     Iterator<Subscriber> oldSubscribers = subscriberFutureMap.keySet().iterator();
440                     while (oldSubscribers.hasNext()) {
441                         Subscriber subscriber = oldSubscribers.next();
442                         ScheduledFuture<?> subscriberFuture = subscriberFutureMap.get(subscriber);
443                         subscriberFuture.cancel(true);
444                     }
445                     eidFutureMap.remove(eid);
446                 }
447             }
448         }
449
450         /*
451          * See https://bugs.opendaylight.org/show_bug.cgi?id=8469#c1 why this is necessary.
452          *
453          * TL;DR  The sourceEid field in the MapRequestBuilder object will be serialized to a packet on the wire, and
454          * a Map-Request can't store the prefix length in the source EID.
455          *
456          * Since we store all prefixes as binary internally, we only care about and fix those address types.
457          */
458         private Eid fixSrcEidMask(Eid eid) {
459             Address address = eid.getAddress();
460             if (address instanceof Ipv4PrefixBinary) {
461                 return new EidBuilder(eid).setAddress(new Ipv4PrefixBinaryBuilder((Ipv4PrefixBinary) address)
462                         .setIpv4MaskLength(MaskUtil.IPV4_MAX_MASK).build()).build();
463             } else if (address instanceof Ipv6PrefixBinary) {
464                 return new EidBuilder(eid).setAddress(new Ipv6PrefixBinaryBuilder((Ipv6PrefixBinary) address)
465                         .setIpv6MaskLength(MaskUtil.IPV6_MAX_MASK).build()).build();
466             }
467             return eid;
468         }
469
470         private final class CancellableRunnable implements Runnable {
471             private MapRequestBuilder mrb;
472             private Subscriber subscriber;
473             private int executionCount = 1;
474
475             CancellableRunnable(MapRequestBuilder mrb, Subscriber subscriber) {
476                 this.mrb = mrb;
477                 this.subscriber = subscriber;
478             }
479
480             @SuppressWarnings("checkstyle:IllegalCatch")
481             @Override
482             public void run() {
483                 final Eid srcEid = mrb.getSourceEid().getEid();
484
485                 try {
486                     // The address stored in the SMR's EID record is used as Source EID in the SMR-invoked
487                     // Map-Request. To ensure consistent behavior it is set to the value used to originally request
488                     // a given mapping.
489                     if (executionCount <= ConfigIni.getInstance().getSmrRetryCount()) {
490                         synchronized (mrb) {
491                             mrb.setEidItem(new ArrayList<EidItem>());
492                             mrb.getEidItem().add(new EidItemBuilder().setEid(subscriber.getSrcEid()).build());
493                             notifyHandler.handleSMR(mrb.build(), subscriber.getSrcRloc());
494                             if (LOG.isTraceEnabled()) {
495                                 LOG.trace("Attempt #{} to send SMR to subscriber {} for EID {}",
496                                         executionCount,
497                                         subscriber.getString(),
498                                         LispAddressStringifier.getString(mrb.getSourceEid().getEid()));
499                             }
500                         }
501                     } else {
502                         LOG.trace("Cancelling execution of a SMR Map-Request after {} failed attempts.",
503                                 executionCount - 1);
504                         cancelAndRemove(subscriber, srcEid);
505                         return;
506                     }
507                 } catch (Exception e) {
508                     LOG.error("Errors encountered while handling SMR:", e);
509                     cancelAndRemove(subscriber, srcEid);
510                     return;
511                 }
512                 executionCount++;
513             }
514
515             private void cancelAndRemove(Subscriber subscriber, Eid eid) {
516                 final Map<Subscriber, ScheduledFuture<?>> subscriberFutureMap = eidFutureMap.get(eid);
517                 if (subscriberFutureMap == null) {
518                     LOG.warn("Couldn't find subscriber {} in SMR scheduler internal list", subscriber);
519                     return;
520                 }
521
522                 if (subscriberFutureMap.containsKey(subscriber)) {
523                     ScheduledFuture<?> eidFuture = subscriberFutureMap.get(subscriber);
524                     subscriberFutureMap.remove(subscriber);
525                     eidFuture.cancel(false);
526                 }
527                 if (subscriberFutureMap.isEmpty()) {
528                     eidFutureMap.remove(eid);
529                 }
530             }
531         }
532     }
533 }