X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=mappingservice%2Fimplementation%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Flispflowmapping%2Fimplementation%2Flisp%2FMapServer.java;h=ed5427d7c05664ba61c390ccd77a7bfaccc3825c;hb=75306b42dfad120cd804e7a6d7aa4381cf9c48ec;hp=313d7a89b6070919ad9edbdde8de79270298ecce;hpb=d87f4c7b3d6fc387b91b1c4838fcc507c9a963f0;p=lispflowmapping.git diff --git a/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/lisp/MapServer.java b/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/lisp/MapServer.java index 313d7a89b..ed5427d7c 100644 --- a/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/lisp/MapServer.java +++ b/mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/lisp/MapServer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Contextream, Inc. and others. All rights reserved. + * Copyright (c) 2014, 2017 Contextream, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, @@ -8,79 +8,92 @@ package org.opendaylight.lispflowmapping.implementation.lisp; +import com.google.common.base.Preconditions; +import com.google.common.collect.Maps; +import com.google.common.util.concurrent.ThreadFactoryBuilder; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.ArrayList; +import java.util.Arrays; import java.util.Enumeration; import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.Set; - +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; import org.apache.commons.lang3.BooleanUtils; -import org.apache.commons.lang3.exception.ExceptionUtils; import org.opendaylight.controller.md.sal.binding.api.NotificationService; -import org.opendaylight.lispflowmapping.implementation.authentication.LispAuthenticationUtil; -import org.opendaylight.lispflowmapping.implementation.config.ConfigIni; +import org.opendaylight.lispflowmapping.config.ConfigIni; +import org.opendaylight.lispflowmapping.implementation.util.LoggingUtil; +import org.opendaylight.lispflowmapping.implementation.util.MSNotificationInputUtil; import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys; -import org.opendaylight.lispflowmapping.interfaces.dao.SubscriberRLOC; +import org.opendaylight.lispflowmapping.interfaces.dao.Subscriber; import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler; import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync; +import org.opendaylight.lispflowmapping.interfaces.lisp.ISmrNotificationListener; +import org.opendaylight.lispflowmapping.interfaces.lisp.SmrEvent; import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService; +import org.opendaylight.lispflowmapping.lisp.authentication.LispAuthenticationUtil; import org.opendaylight.lispflowmapping.lisp.type.LispMessage; +import org.opendaylight.lispflowmapping.lisp.type.MappingData; import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier; import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil; import org.opendaylight.lispflowmapping.lisp.util.MapNotifyBuilderHelper; import org.opendaylight.lispflowmapping.lisp.util.MapRequestUtil; +import org.opendaylight.lispflowmapping.lisp.util.MappingRecordUtil; import org.opendaylight.lispflowmapping.lisp.util.SourceDestKeyHelper; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress; -import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber; import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItem; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItemBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord; -import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItem; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItemBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequestBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChange; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChanged; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.OdlMappingserviceListener; -import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container.MappingAuthkey; +import org.opendaylight.yangtools.concepts.ListenerRegistration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.google.common.base.Preconditions; - -public class MapServer implements IMapServerAsync, OdlMappingserviceListener { +public class MapServer implements IMapServerAsync, OdlMappingserviceListener, ISmrNotificationListener { protected static final Logger LOG = LoggerFactory.getLogger(MapServer.class); + 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}; private IMappingService mapService; - private boolean authenticate; private boolean subscriptionService; private IMapNotifyHandler notifyHandler; private NotificationService notificationService; + private ListenerRegistration mapServerListenerRegistration; + private SmrScheduler scheduler; - public MapServer(IMappingService mapService, boolean authenticate, boolean subscriptionService, - IMapNotifyHandler notifyHandler, NotificationService notificationService) { + public MapServer(IMappingService mapService, boolean subscriptionService, + IMapNotifyHandler notifyHandler, NotificationService notificationService) { Preconditions.checkNotNull(mapService); this.mapService = mapService; - this.authenticate = authenticate; this.subscriptionService = subscriptionService; this.notifyHandler = notifyHandler; this.notificationService = notificationService; if (notificationService != null) { notificationService.registerNotificationListener(this); } + scheduler = new SmrScheduler(); } @Override @@ -88,66 +101,75 @@ public class MapServer implements IMapServerAsync, OdlMappingserviceListener { this.subscriptionService = subscriptionService; } - @Override - public boolean shouldAuthenticate() { - return authenticate; - } - - @Override - public void setShouldAuthenticate(boolean shouldAuthenticate) { - authenticate = shouldAuthenticate; - } - @SuppressWarnings("unchecked") public void handleMapRegister(MapRegister mapRegister) { - boolean authFailed = false; boolean mappingUpdated = false; - MappingAuthkey authkey = null; - Set subscribers = null; + boolean oldMappingRemoved = false; + boolean merge = ConfigIni.getInstance().mappingMergeIsSet() && mapRegister.isMergeEnabled(); + Set subscribers = null; MappingRecord oldMapping; + if (merge) { + if (!mapRegister.isXtrSiteIdPresent() || mapRegister.getXtrId() == null) { + LOG.error("Merge bit is set in Map-Register, but xTR-ID is not present. Will not merge."); + merge = false; + } else if (Arrays.equals(mapRegister.getXtrId().getValue(), ALL_ZEROES_XTR_ID)) { + LOG.warn("Merge bit is set in Map-Register, but xTR-ID is all zeroes."); + } + } + for (MappingRecordItem record : mapRegister.getMappingRecordItem()) { MappingRecord mapping = record.getMappingRecord(); - if (authenticate) { - authkey = mapService.getAuthenticationKey(mapping.getEid()); - if (!LispAuthenticationUtil.validate(mapRegister, mapping.getEid(), authkey)) { - authFailed = true; - break; - } - } + Eid eid = mapping.getEid(); + MappingData mappingData = new MappingData(mapping, System.currentTimeMillis()); + mappingData.setMergeEnabled(merge); + mappingData.setXtrId(mapRegister.getXtrId()); - oldMapping = (MappingRecord) mapService.getMapping(MappingOrigin.Southbound, mapping.getEid()); - mapService.addMapping(MappingOrigin.Southbound, mapping.getEid(), getSiteId(mapRegister), mapping); + oldMapping = getMappingRecord(mapService.getMapping(MappingOrigin.Southbound, eid)); + mapService.addMapping(MappingOrigin.Southbound, eid, getSiteId(mapRegister), mappingData); + if (oldMapping != null && MappingRecordUtil.isNegativeMapping(oldMapping)) { + mapService.removeMapping(MappingOrigin.Southbound, oldMapping.getEid()); + oldMappingRemoved = true; + } if (subscriptionService) { - MappingRecord newMapping = ConfigIni.getInstance().mappingMergeIsSet() ? - (MappingRecord) mapService.getMapping(MappingOrigin.Southbound, mapping.getEid()) : mapping; + MappingRecord newMapping = merge + ? getMappingRecord(mapService.getMapping(MappingOrigin.Southbound, eid)) : mapping; if (mappingChanged(oldMapping, newMapping)) { if (LOG.isDebugEnabled()) { LOG.debug("Mapping update occured for {} SMRs will be sent for its subscribers.", LispAddressStringifier.getString(mapping.getEid())); } - subscribers = getSubscribers(mapping.getEid()); - sendSmrs(mapping, subscribers); + subscribers = getSubscribers(eid); + if (oldMapping != null && !oldMapping.getEid().equals(eid)) { + subscribers = addParentSubscribers(eid, subscribers); + } + LoggingUtil.logSubscribers(LOG, eid, subscribers); + handleSmr(eid, subscribers); + if (oldMapping != null && oldMappingRemoved && !oldMapping.getEid().equals(eid)) { + subscribers = getSubscribers(oldMapping.getEid()); + LoggingUtil.logSubscribers(LOG, oldMapping.getEid(), subscribers); + handleSmr(oldMapping.getEid(), subscribers); + } mappingUpdated = true; } } } - if (!authFailed && BooleanUtils.isTrue(mapRegister.isWantMapNotify())) { + if (BooleanUtils.isTrue(mapRegister.isWantMapNotify())) { LOG.trace("MapRegister wants MapNotify"); MapNotifyBuilder builder = new MapNotifyBuilder(); List rlocs = null; - if (ConfigIni.getInstance().mappingMergeIsSet()) { - Set notifyRlocs = new HashSet(); + if (merge) { + Set notifyRlocs = new HashSet(); List mergedMappings = new ArrayList(); for (MappingRecordItem record : mapRegister.getMappingRecordItem()) { MappingRecord mapping = record.getMappingRecord(); - MappingRecord currentRecord = (MappingRecord) mapService.getMapping(MappingOrigin.Southbound, - mapping.getEid()); + MappingRecord currentRecord = getMappingRecord(mapService.getMapping(MappingOrigin.Southbound, + mapping.getEid())); mergedMappings.add(new MappingRecordItemBuilder().setMappingRecord(currentRecord).build()); - Set sourceRlocs = (Set) mapService.getData(MappingOrigin.Southbound, - mapping.getEid(), SubKeys.SRC_RLOCS); + Set sourceRlocs = (Set) mapService.getData( + MappingOrigin.Southbound, mapping.getEid(), SubKeys.SRC_RLOCS); if (sourceRlocs != null) { notifyRlocs.addAll(sourceRlocs); } @@ -160,17 +182,22 @@ public class MapServer implements IMapServerAsync, OdlMappingserviceListener { } else { MapNotifyBuilderHelper.setFromMapRegister(builder, mapRegister); } - if (authenticate) { - builder.setAuthenticationData(LispAuthenticationUtil.createAuthenticationData(builder.build(), - authkey.getKeyString())); + List mappings = builder.getMappingRecordItem(); + if (mappings != null && mappings.get(0) != null && mappings.get(0).getMappingRecord() != null + && mappings.get(0).getMappingRecord().getEid() != null) { + MappingAuthkey authkey = mapService.getAuthenticationKey(mappings.get(0).getMappingRecord().getEid()); + if (authkey != null) { + builder.setAuthenticationData(LispAuthenticationUtil.createAuthenticationData(builder.build(), + authkey.getKeyString())); + } } notifyHandler.handleMapNotify(builder.build(), rlocs); } } - private static List getTransportAddresses(Set addresses) { + private static List getTransportAddresses(Set addresses) { List rlocs = new ArrayList(); - for (IpAddress address : addresses) { + for (IpAddressBinary address : addresses) { TransportAddressBuilder tab = new TransportAddressBuilder(); tab.setIpAddress(address); tab.setPort(new PortNumber(LispMessage.PORT_NUM)); @@ -179,23 +206,36 @@ public class MapServer implements IMapServerAsync, OdlMappingserviceListener { return rlocs; } - private SiteId getSiteId(MapRegister mapRegister) { + private static SiteId getSiteId(MapRegister mapRegister) { return (mapRegister.getSiteId() != null) ? new SiteId(mapRegister.getSiteId()) : null; } + private static MappingRecord getMappingRecord(MappingData mappingData) { + return (mappingData != null) ? mappingData.getRecord() : null; + } + @Override public void onMappingChanged(MappingChanged notification) { + LOG.trace("MappingChanged event of type: `{}'", notification.getChangeType()); if (subscriptionService) { - sendSmrs(notification.getMappingRecord(), getSubscribers(notification.getMappingRecord().getEid())); - if (notification.getChangeType().equals(MappingChange.Removed)) { - removeSubscribers(notification.getMappingRecord().getEid()); + Eid eid = notification.getMappingRecord().getEid(); + Set subscribers = MSNotificationInputUtil.toSubscriberSet(notification.getSubscriberItem()); + LoggingUtil.logSubscribers(LOG, eid, subscribers); + if (mapService.isMaster()) { + sendSmrs(eid, subscribers); + if (eid.getAddress() instanceof SourceDestKey) { + Set dstSubscribers = MSNotificationInputUtil.toSubscriberSetFromDst( + notification.getDstSubscriberItem()); + LoggingUtil.logSubscribers(LOG, SourceDestKeyHelper.getDstBinary(eid), dstSubscribers); + sendSmrs(SourceDestKeyHelper.getDstBinary(eid), dstSubscribers); + } } } } private static boolean mappingChanged(MappingRecord oldMapping, MappingRecord newMapping) { // We only check for fields we care about - // XXX: This code needs to be checked and updated when the YANG model is modified + // XXX: This code needs to be checked and updated when the YANG model for MappingRecord is modified Preconditions.checkNotNull(newMapping, "The new mapping should never be null"); if (oldMapping == null) { LOG.trace("mappingChanged(): old mapping is null"); @@ -219,58 +259,50 @@ public class MapServer implements IMapServerAsync, OdlMappingserviceListener { return false; } - private void sendSmrs(MappingRecord record, Set subscribers) { - Eid eid = record.getEid(); - handleSmr(eid, subscribers, notifyHandler); + private void handleSmr(Eid eid, Set subscribers) { + sendSmrs(eid, subscribers); // For SrcDst LCAF also send SMRs to Dst prefix if (eid.getAddress() instanceof SourceDestKey) { - Eid dstAddr = SourceDestKeyHelper.getDst(eid); - Set dstSubs = getSubscribers(dstAddr); - MappingRecord newRecord = new MappingRecordBuilder(record).setEid(dstAddr).build(); - handleSmr(newRecord.getEid(), dstSubs, notifyHandler); + Eid dstAddr = SourceDestKeyHelper.getDstBinary(eid); + Set dstSubs = getSubscribers(dstAddr); + sendSmrs(dstAddr, dstSubs); } } - private void handleSmr(Eid eid, Set subscribers, IMapNotifyHandler callback) { + private void sendSmrs(Eid eid, Set subscribers) { if (subscribers == null) { return; } - MapRequestBuilder mrb = MapRequestUtil.prepareSMR(eid, LispAddressUtil.toRloc(getLocalAddress())); + final MapRequestBuilder mrb = MapRequestUtil.prepareSMR(eid, LispAddressUtil.toRloc(getLocalAddress())); LOG.trace("Built SMR packet: " + mrb.build().toString()); - // Using Iterator ensures that we don't get a ConcurrentModificationException when removing a SubscriberRLOC - // from a Set. - Iterator iterator = subscribers.iterator(); - while (iterator.hasNext()) { - SubscriberRLOC subscriber = iterator.next(); - if (subscriber.timedOut()) { - LOG.trace("Lazy removing expired subscriber entry " + subscriber.toString()); - iterator.remove(); - } else { - try { - // The address stored in the SMR's EID record is used as Source EID in the SMR-invoked Map-Request. - // To ensure consistent behavior it is set to the value used to originally request a given mapping - mrb.setEidItem(new ArrayList()); - mrb.getEidItem().add(new EidItemBuilder().setEid(subscriber.getSrcEid()).build()); - callback.handleSMR(mrb.build(), subscriber.getSrcRloc()); - } catch (Exception e) { - LOG.error("Errors encountered while handling SMR:" + ExceptionUtils.getStackTrace(e)); - } - } - } - addSubscribers(eid, subscribers); + + scheduler.scheduleSmrs(mrb, subscribers.iterator()); } @SuppressWarnings("unchecked") - private Set getSubscribers(Eid address) { - return (Set) mapService.getData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS); + private Set getSubscribers(Eid address) { + return (Set) mapService.getData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS); } - private void removeSubscribers(Eid address) { - mapService.removeData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS); + private Set addParentSubscribers(Eid eid, Set subscribers) { + Eid parentPrefix = mapService.getParentPrefix(eid); + if (parentPrefix == null) { + return subscribers; + } + + Set parentSubscribers = getSubscribers(parentPrefix); + if (parentSubscribers != null) { + if (subscribers != null) { + subscribers.addAll(parentSubscribers); + } else { + subscribers = parentSubscribers; + } + } + return subscribers; } - private void addSubscribers(Eid address, Set subscribers) { + private void addSubscribers(Eid address, Set subscribers) { mapService.addData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS, subscribers); } @@ -279,24 +311,154 @@ public class MapServer implements IMapServerAsync, OdlMappingserviceListener { Enumeration interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface current = interfaces.nextElement(); - LOG.debug("Interface " + current.toString()); + LOG.trace("Interface " + current.toString()); if (!current.isUp() || current.isLoopback() || current.isVirtual()) { continue; } Enumeration addresses = current.getInetAddresses(); while (addresses.hasMoreElements()) { - InetAddress current_addr = addresses.nextElement(); + InetAddress currentAddr = addresses.nextElement(); // Skip loopback and link local addresses - if (current_addr.isLoopbackAddress() || current_addr.isLinkLocalAddress()) { + if (currentAddr.isLoopbackAddress() || currentAddr.isLinkLocalAddress()) { continue; } - LOG.debug(current_addr.getHostAddress()); - return current_addr; + LOG.debug(currentAddr.getHostAddress()); + return currentAddr; } } } catch (SocketException se) { - LOG.debug("Caught socket exceptio", se); + LOG.debug("Caught socket exception", se); } return null; } + + @Override + public void onSmrInvokedReceived(SmrEvent event) { + scheduler.smrReceived(event); + } + + /** + * Task scheduler is responsible for resending SMR messages to a subscriber (xTR) + * {@value ConfigIni#LISP_SMR_RETRY_COUNT} times, or until {@link ISmrNotificationListener#onSmrInvokedReceived} + * is triggered. + */ + private class SmrScheduler { + final int cpuCores = Runtime.getRuntime().availableProcessors(); + private final ThreadFactory threadFactory = new ThreadFactoryBuilder() + .setNameFormat("smr-executor-%d").build(); + private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(cpuCores * 2, threadFactory); + private final Map>> subscriberFutureMap = Maps.newConcurrentMap(); + + void scheduleSmrs(MapRequestBuilder mrb, Iterator subscribers) { + // Using Iterator ensures that we don't get a ConcurrentModificationException when removing a Subscriber + // from a Set. + while (subscribers.hasNext()) { + Subscriber subscriber = subscribers.next(); + if (subscriber.timedOut()) { + LOG.debug("Lazy removing expired subscriber entry " + subscriber.getString()); + subscribers.remove(); + } else { + final Eid srcEid = mrb.getSourceEid().getEid(); + final ScheduledFuture future = executor.scheduleAtFixedRate(new CancellableRunnable( + mrb, subscriber), 0L, ConfigIni.getInstance().getSmrTimeout(), TimeUnit.MILLISECONDS); + final IpAddressBinary subscriberAddress = LispAddressUtil + .addressBinaryFromAddress(subscriber.getSrcRloc().getAddress()); + + if (subscriberFutureMap.containsKey(subscriberAddress)) { + subscriberFutureMap.get(subscriberAddress).put(srcEid, future); + } else { + final Map> eidFutureMap = Maps.newConcurrentMap(); + eidFutureMap.put(srcEid, future); + subscriberFutureMap.put(subscriberAddress, eidFutureMap); + } + } + } + } + + void smrReceived(SmrEvent event) { + final List subscriberAddressList = event.getSubscriberAddressList(); + for (IpAddressBinary subscriberAddress : subscriberAddressList) { + final Map> eidFutureMap = subscriberFutureMap.get(subscriberAddress); + if (eidFutureMap != null) { + final ScheduledFuture future = eidFutureMap.get(event.getEid()); + if (future != null && !future.isCancelled()) { + future.cancel(true); + LOG.debug("SMR-invoked MapRequest received, scheduled task for subscriber {}, EID {} with" + + " nonce {} has been cancelled", subscriberAddress.toString(), + LispAddressStringifier.getString(event.getEid()), event.getNonce()); + eidFutureMap.remove(event.getEid()); + } + if (eidFutureMap.isEmpty()) { + subscriberFutureMap.remove(subscriberAddress); + } + } + } + } + + private final class CancellableRunnable implements Runnable { + private MapRequestBuilder mrb; + private Subscriber subscriber; + private int executionCount = 1; + + CancellableRunnable(MapRequestBuilder mrb, Subscriber subscriber) { + this.mrb = mrb; + this.subscriber = subscriber; + } + + @SuppressWarnings("checkstyle:IllegalCatch") + @Override + public void run() { + final IpAddressBinary subscriberAddress = LispAddressUtil + .addressBinaryFromAddress(subscriber.getSrcRloc().getAddress()); + final Eid srcEid = mrb.getSourceEid().getEid(); + + try { + // The address stored in the SMR's EID record is used as Source EID in the SMR-invoked + // Map-Request. To ensure consistent behavior it is set to the value used to originally request + // a given mapping. + if (executionCount <= ConfigIni.getInstance().getSmrRetryCount()) { + synchronized (mrb) { + mrb.setEidItem(new ArrayList()); + mrb.getEidItem().add(new EidItemBuilder().setEid(subscriber.getSrcEid()).build()); + notifyHandler.handleSMR(mrb.build(), subscriber.getSrcRloc()); + if (LOG.isTraceEnabled()) { + LOG.trace("Attempt #{} to send SMR for EID {} to subscriber {}, source EID {}", + executionCount, + LispAddressStringifier.getString(mrb.getSourceEid().getEid()), + LispAddressStringifier.getString(subscriber.getSrcRloc()), + LispAddressStringifier.getString(mrb.getEidItem().get(0).getEid())); + } + } + } else { + LOG.trace("Cancelling execution of a SMR Map-Request after {} failed attempts.", + executionCount - 1); + cancelAndRemove(subscriberAddress, srcEid); + return; + } + } catch (Exception e) { + LOG.error("Errors encountered while handling SMR:", e); + cancelAndRemove(subscriberAddress, srcEid); + return; + } + executionCount++; + } + + private void cancelAndRemove(IpAddressBinary subscriberAddress, Eid eid) { + final Map> eidFutureMap = subscriberFutureMap.get(subscriberAddress); + if (eidFutureMap == null) { + LOG.warn("Couldn't find subscriber {} in SMR scheduler internal list", subscriberAddress); + return; + } + + if (eidFutureMap.containsKey(eid)) { + ScheduledFuture eidFuture = eidFutureMap.get(eid); + eidFutureMap.remove(eid); + eidFuture.cancel(false); + } + if (eidFutureMap.isEmpty()) { + subscriberFutureMap.remove(subscriberAddress); + } + } + } + } }