X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=mappingservice%2Fsouthbound%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Flispflowmapping%2Fsouthbound%2Flisp%2FLispSouthboundHandler.java;h=dba899a532b299af0db0a43e37ec82151807d430;hb=acfb335aef5783e74455751b7aa92f267cf4bce3;hp=49182246a1093802249307393be05765479386c0;hpb=02b48c593ece0f615354fedcbf847553ffb4d57f;p=lispflowmapping.git diff --git a/mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/lisp/LispSouthboundHandler.java b/mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/lisp/LispSouthboundHandler.java index 49182246a..dba899a53 100644 --- a/mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/lisp/LispSouthboundHandler.java +++ b/mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/lisp/LispSouthboundHandler.java @@ -9,11 +9,13 @@ package org.opendaylight.lispflowmapping.southbound.lisp; import com.google.common.base.Preconditions; + import io.netty.buffer.ByteBufUtil; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.socket.DatagramPacket; + import java.net.InetAddress; import java.nio.ByteBuffer; import java.util.ArrayList; @@ -21,11 +23,13 @@ import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Objects; + import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService; +import org.opendaylight.lispflowmapping.dsbackend.DataStoreBackEnd; import org.opendaylight.lispflowmapping.inmemorydb.HashMapDb; -import org.opendaylight.lispflowmapping.southbound.authentication.LispAuthenticationUtil; -import org.opendaylight.lispflowmapping.interfaces.lisp.ILispAuthentication; +import org.opendaylight.lispflowmapping.lisp.authentication.ILispAuthentication; +import org.opendaylight.lispflowmapping.lisp.authentication.LispAuthenticationUtil; import org.opendaylight.lispflowmapping.lisp.serializer.MapNotifySerializer; import org.opendaylight.lispflowmapping.lisp.serializer.MapRegisterSerializer; import org.opendaylight.lispflowmapping.lisp.serializer.MapReplySerializer; @@ -42,7 +46,7 @@ import org.opendaylight.lispflowmapping.southbound.lisp.cache.MapRegisterPartial import org.opendaylight.lispflowmapping.southbound.lisp.exception.LispMalformedPacketException; import org.opendaylight.lispflowmapping.southbound.lisp.network.PacketHeader; import org.opendaylight.lispflowmapping.southbound.util.LispNotificationHelper; -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.opendaylight.lfm.lisp.proto.rev151105.AddMappingBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapNotifyBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapReplyBuilder; @@ -65,7 +69,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.ma 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.list.MappingRecordItem; 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.mapping.authkey.container.MappingAuthkey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey; import org.opendaylight.yangtools.yang.binding.Notification; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -74,25 +79,21 @@ import org.slf4j.LoggerFactory; public class LispSouthboundHandler extends SimpleChannelInboundHandler implements ILispSouthboundService, AutoCloseable { private MapRegisterCache mapRegisterCache; + private boolean mapRegisterCacheEnabled = true; + private long mapRegisterCacheTimeout; - /** - * How long is record supposed to be relevant. After this time record isn't valid. - * - * If you modify this value, please update the LispSouthboundServiceTest class too. - */ - private static final long CACHE_RECORD_TIMEOUT = 90000; private DataBroker dataBroker; - private NotificationPublishService notificationPublishService; + protected static final Logger LOG = LoggerFactory.getLogger(LispSouthboundHandler.class); //TODO: think whether this field can be accessed through mappingservice or some other configuration parameter private boolean authenticationEnabled = true; - private final LispSouthboundPlugin lispSbPlugin; private LispSouthboundStats lispSbStats = null; private SimpleMapCache smc; private AuthenticationKeyDataListener authenticationKeyDataListener; + private DataStoreBackEnd dsbe; public LispSouthboundHandler(LispSouthboundPlugin lispSbPlugin) { this.lispSbPlugin = lispSbPlugin; @@ -113,7 +114,7 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler artificialEntry = MapRegisterPartialDeserializer - .deserializePartially(inBuffer, sourceAddress); - final MapRegisterCacheKey cacheKey = artificialEntry == null ? null : artificialEntry.getKey(); - - final MapRegisterCacheValue cacheValue = resolveCacheValue(artificialEntry); + Map.Entry artificialEntry = null; + MapRegisterCacheKey cacheKey = null; + MapRegisterCacheValue cacheValue = null; + if (mapRegisterCacheEnabled) { + artificialEntry = MapRegisterPartialDeserializer.deserializePartially(inBuffer, sourceAddress); + cacheKey = artificialEntry == null ? null : artificialEntry.getKey(); + cacheValue = resolveCacheValue(artificialEntry); + } if (cacheValue != null) { - final MapRegisterCacheMetadata mapRegisterValue = cacheValue.getMapRegisterCacheMetadata(); - LOG.debug("Map register message site-ID: {} xTR-ID: {} from cache.", mapRegisterValue.getSiteId(), - mapRegisterValue.getXtrId()); - mapRegisterCache.refreshEntry(cacheKey); - sendNotificationIfPossible(createMappingKeepAlive(cacheValue)); - if (mapRegisterValue.isWantMapNotify()) { - sendMapNotifyMsg(inBuffer, sourceAddress, port, mapRegisterValue.getEidLispAddress()); + MapRegisterCacheMetadata mapRegisterMeta = cacheValue.getMapRegisterCacheMetadata(); + LOG.debug("Map register message site-ID: {} xTR-ID: {} from cache.", mapRegisterMeta.getSiteId(), + mapRegisterMeta.getXtrId()); + cacheValue = refreshEntry(cacheKey); + if (cacheValue != null) { + sendNotificationIfPossible(createMappingKeepAlive(cacheValue)); + if (cacheValue.getMapRegisterCacheMetadata().isWantMapNotify()) { + sendMapNotifyMsg(inBuffer, sourceAddress, port, cacheValue); + } } lispSbStats.incrementCacheHits(); } else { MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(inBuffer, sourceAddress); - if (isAuthenticationSuccessful(mapRegister, inBuffer)) { + final MappingAuthkey mappingAuthkey = tryToAuthenticateMessage(mapRegister, inBuffer); + if (mappingAuthkey != null) { AddMappingBuilder addMappingBuilder = new AddMappingBuilder(); addMappingBuilder.setMapRegister(LispNotificationHelper.convertMapRegister(mapRegister)); TransportAddressBuilder transportAddressBuilder = new TransportAddressBuilder(); @@ -228,6 +235,7 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler mapRegisterCacheTimeout; + if (shouldAuthKeyRefreshingStop) { + authenticationKeyDataListener.setAuthKeyRefreshing(false); + } else { + final MappingAuthkey mappingAuthkey = provideAuthenticateKey(mapRegisterCacheValue + .getMapRegisterCacheMetadata().getEidLispAddress()); + + final MapRegisterCacheValueBuilder newMapRegisterCacheValueBuilder = new MapRegisterCacheValueBuilder + (mapRegisterCacheValue); + final MapRegisterCacheMetadataBuilder newMapRegisterCacheMetadataBuilder = + new MapRegisterCacheMetadataBuilder(mapRegisterCacheValue.getMapRegisterCacheMetadata()); + + newMapRegisterCacheValueBuilder.setMappingAuthkey(mappingAuthkey); + newMapRegisterCacheValueBuilder.setMapRegisterCacheMetadata(newMapRegisterCacheMetadataBuilder.build()); + return newMapRegisterCacheValueBuilder.build(); + } + } + + return mapRegisterCacheValue; + + } + private MapRegisterCacheValue resolveCacheValue(Map.Entry entry) { if (entry != null) { final MapRegisterCacheValue mapRegisterCacheValue = mapRegisterCache.getEntry(entry.getKey()); if (mapRegisterCacheValue != null) { final long creationTime = mapRegisterCacheValue.getMapRegisterCacheMetadata().getTimestamp(); final long currentTime = System.currentTimeMillis(); - if (currentTime - creationTime > CACHE_RECORD_TIMEOUT) { + if (currentTime - creationTime > mapRegisterCacheTimeout) { mapRegisterCache.removeEntry(entry.getKey()); return null; } else if (Arrays.equals(mapRegisterCacheValue.getPacketData(), entry.getValue())) { @@ -275,22 +318,10 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler cacheValue) { - - final String authKey = provideAuthenticateKey(cacheValue); - if (authKey != null) { - ByteBuffer outBuffer = transformMapRegisterToMapNotify(inBuffer); - outBuffer = calculateAndSetNewMAC(outBuffer, authKey); - outBuffer.position(0); - lispSbPlugin.handleSerializedLispBuffer(inetAddress, outBuffer, MessageType.MapNotify, portNumber); - } - } - /** * Returns null if not all of eids have the same value of authentication key */ - private String provideAuthenticateKey(final List eidLispAddresses) { + private MappingAuthkey provideAuthenticateKey(final List eidLispAddresses) { MappingAuthkey firstAuthKey = null; for (int i = 0; i < eidLispAddresses.size(); i++) { final Eid eid = eidLispAddresses.get(i).getEid(); @@ -303,10 +334,24 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler authKeys = dsbe.getAllAuthenticationKeys(); + LOG.info("Restoring {} keys from datastore into southbound DAO", authKeys.size()); + + for (AuthenticationKey authKey : authKeys) { + final Eid key = authKey.getEid(); + final MappingAuthkey mappingAuthkey = authKey.getMappingAuthkey(); + LOG.debug("Adding authentication key '{}' with key-ID {} for {}", mappingAuthkey.getKeyString(), + mappingAuthkey.getKeyType(), + LispAddressStringifier.getString(key)); + smc.addAuthenticationKey(key, mappingAuthkey); + } + } public void init() { Preconditions.checkNotNull(dataBroker); Preconditions.checkNotNull(smc); this.authenticationKeyDataListener = new AuthenticationKeyDataListener(dataBroker, smc); + dsbe = new DataStoreBackEnd(dataBroker); + } + + public void setMapRegisterCacheTimeout(long mapRegisterCacheTimeout) { + this.mapRegisterCacheTimeout = mapRegisterCacheTimeout; } }