Adding authentication data to map register cache.
[lispflowmapping.git] / mappingservice / southbound / src / main / java / org / opendaylight / lispflowmapping / southbound / lisp / LispSouthboundHandler.java
index 97406d3dafb848af1fe7628dc40779b5ec43c329..f892094f854adb5ff87b6700850bfa7afa3e926e 100644 (file)
@@ -203,18 +203,20 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
                 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);
+                MapRegisterCacheMetadata mapRegisterMeta = cacheValue.getMapRegisterCacheMetadata();
+                LOG.debug("Map register message site-ID: {} xTR-ID: {} from cache.", mapRegisterMeta.getSiteId(),
+                        mapRegisterMeta.getXtrId());
+                mapRegisterMeta = refreshEntry(cacheKey);
                 sendNotificationIfPossible(createMappingKeepAlive(cacheValue));
-                if (mapRegisterValue.isWantMapNotify()) {
-                    sendMapNotifyMsg(inBuffer, sourceAddress, port, mapRegisterValue.getEidLispAddress());
+
+                if (mapRegisterMeta.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();
@@ -235,6 +237,8 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
 
                         final MapRegisterCacheValueBuilder cacheValueBldNew = new MapRegisterCacheValueBuilder();
                         cacheValueBldNew.setPacketData(artificialEntry.getValue());
+                        cacheValueBldNew.setAuthKeyValue(mappingAuthkey.getKeyString());
+                        cacheValueBldNew.setAuthKeyType(mappingAuthkey.getKeyType());
                         cacheValueBldNew.setMapRegisterCacheMetadata(cacheMetadataBldNew.build());
 
                         mapRegisterCache.addEntry(cacheKey, cacheValueBldNew.build());
@@ -250,6 +254,42 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
         }
     }
 
+    private MapRegisterCacheMetadata refreshEntry(final MapRegisterCacheKey cacheKey) {
+        MapRegisterCacheValue mapRegisterCacheValue = mapRegisterCache.refreshEntry(cacheKey);
+        if (mapRegisterCacheValue != null) {
+            mapRegisterCacheValue = refreshAuthKeyIfNecessary(mapRegisterCacheValue);
+            mapRegisterCache.addEntry(cacheKey, mapRegisterCacheValue);
+            return mapRegisterCacheValue.getMapRegisterCacheMetadata();
+        }
+        return null;
+    }
+
+    private MapRegisterCacheValue refreshAuthKeyIfNecessary(MapRegisterCacheValue mapRegisterCacheValue) {
+        if (authenticationKeyDataListener.isAuthKeyRefreshing()) {
+            final boolean shouldAuthKeyRefreshingStop = System.currentTimeMillis() - authenticationKeyDataListener
+                    .getAuthKeyRefreshingDate() > CACHE_RECORD_TIMEOUT;
+            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.setAuthKeyValue(mappingAuthkey.getKeyString());
+                newMapRegisterCacheValueBuilder.setAuthKeyType(mappingAuthkey.getKeyType());
+                newMapRegisterCacheValueBuilder.setMapRegisterCacheMetadata(newMapRegisterCacheMetadataBuilder.build());
+                return newMapRegisterCacheValueBuilder.build();
+            }
+        }
+
+        return mapRegisterCacheValue;
+
+    }
+
     private MapRegisterCacheValue resolveCacheValue(Map.Entry<MapRegisterCacheKey, byte[]> entry) {
         if (entry != null) {
             final MapRegisterCacheValue mapRegisterCacheValue = mapRegisterCache.getEntry(entry.getKey());
@@ -282,27 +322,6 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
         return mappingKeepAliveBuilder.build();
     }
 
-    private void sendMapNotifyMsg(final ByteBuffer inBuffer, final InetAddress inetAddress, int portNumber,
-                                  List<EidLispAddress> eids) {
-
-        if (eids == null) {
-            LOG.warn("Map-Register Cache: missing EID list when trying to send Map-Notify!");
-            return;
-        }
-
-        final MappingAuthkey authKey = provideAuthenticateKey(eids);
-        if (authKey != null) {
-            ByteBuffer outBuffer = transformMapRegisterToMapNotify(inBuffer);
-            if (authKey.getKeyType() != 0) {
-                outBuffer = calculateAndSetNewMAC(outBuffer, authKey.getKeyString());
-            }
-            outBuffer.position(0);
-            lispSbPlugin.handleSerializedLispBuffer(inetAddress, outBuffer, MessageType.MapNotify, portNumber);
-        } else {
-            LOG.error("Map-Register Cache: authentication succeeded, but can't find auth key for sending Map-Notify");
-        }
-    }
-
     /**
      * Returns null if not all of eids have the same value of authentication key
      */
@@ -323,6 +342,20 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
 
     }
 
+    private void sendMapNotifyMsg(final ByteBuffer inBuffer, final InetAddress inetAddress, int portNumber,
+                                  MapRegisterCacheValue mapRegisterValue) {
+        if (mapRegisterValue.getAuthKeyType() != null) {
+            ByteBuffer outBuffer = transformMapRegisterToMapNotify(inBuffer);
+            if (mapRegisterValue.getAuthKeyType() != 0) {
+                outBuffer = calculateAndSetNewMAC(outBuffer, mapRegisterValue.getAuthKeyValue());
+            }
+            outBuffer.position(0);
+            lispSbPlugin.handleSerializedLispBuffer(inetAddress, outBuffer, MessageType.MapNotify, portNumber);
+        } else {
+            LOG.error("Map-Register Cache: authentication succeeded, but can't find auth key for sending Map-Notify");
+        }
+    }
+
     /**
      * Calculates new message authentication code (MAC) for notify message.
      *
@@ -373,16 +406,16 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
      *
      * @param mapRegister
      * @param byteBuffer
-     * @return
+     * @return Returns authentication key if all of EIDs have the same authentication key or null otherwise
      */
-    private boolean isAuthenticationSuccessful(final MapRegister mapRegister, final ByteBuffer byteBuffer) {
+    private MappingAuthkey tryToAuthenticateMessage(final MapRegister mapRegister, final ByteBuffer byteBuffer) {
         if (!authenticationEnabled) {
-            return true;
+            return null;
         }
 
         if (smc == null) {
             LOG.debug("Simple map cache wasn't instantieted and set.");
-            return false;
+            return null;
         }
 
         MappingAuthkey firstAuthKey = null;
@@ -393,7 +426,7 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
             if (i == 0) {
                 firstAuthKey = smc.getAuthenticationKey(mappingRecord.getEid());
                 if (!LispAuthenticationUtil.validate(mapRegister, byteBuffer, mappingRecord.getEid(), firstAuthKey)) {
-                    return false;
+                    return null;
                 }
             } else {
                 final Eid eid = mappingRecord.getEid();
@@ -401,11 +434,11 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
                 if (!firstAuthKey.equals(authKey)) {
                     LOG.debug("Map register packet contained several eids. Authentication keys for first one and for " +
                             "{} are different.",LispAddressStringifier.getString(eid));
-                    return false;
+                    return null;
                 }
             }
         }
-        return true;
+        return firstAuthKey;
     }
 
     private void handleMapNotify(ByteBuffer inBuffer, InetAddress sourceAddress, int port) {