Checkstyle: enforce 120 character limit
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / authentication / LispAuthenticationUtil.java
index d7244016dcee2a566ecc312f761b59a42945ebeb..9d90c1ad7d5a7a717c4954951d35b154b2471c1b 100644 (file)
@@ -8,25 +8,43 @@
 package org.opendaylight.lispflowmapping.implementation.authentication;
 
 import org.opendaylight.lispflowmapping.interfaces.lisp.ILispAuthentication;
+import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapNotify;
 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.eid.container.Eid;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container.MappingAuthkey;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public final class LispAuthenticationUtil {
+    protected static final Logger LOG = LoggerFactory.getLogger(LispAuthenticationUtil.class);
+
     // Utility class, should not be instantiated
     private LispAuthenticationUtil() {
     }
 
-    public static boolean validate(MapRegister mapRegister, String key) {
+    public static boolean validate(MapRegister mapRegister, Eid eid, MappingAuthkey key) {
+        if (key == null) {
+            LOG.warn("Authentication failed: mapping authentication key is null");
+            return false;
+        }
         short keyId = 0;
         if (mapRegister.getKeyId() != null) {
             keyId = mapRegister.getKeyId();
         }
+        if (keyId != key.getKeyType().shortValue()) {
+            LOG.warn("Authentication failed: key-ID in Map-Register is different from the one on file for {}",
+                    LispAddressStringifier.getString(eid));
+            return false;
+        }
         ILispAuthentication authentication = LispAuthenticationFactory.getAuthentication(LispKeyIDEnum.valueOf(keyId));
-        return authentication.validate(mapRegister, key);
+        return authentication.validate(mapRegister, key.getKeyString());
     }
 
     public static byte[] createAuthenticationData(MapNotify mapNotify, String key) {
-        ILispAuthentication authentication = LispAuthenticationFactory.getAuthentication(LispKeyIDEnum.valueOf(mapNotify.getKeyId()));
+        // We assume that the key-ID is correctly set in the Map-Notify message
+        ILispAuthentication authentication = LispAuthenticationFactory.getAuthentication(
+                LispKeyIDEnum.valueOf(mapNotify.getKeyId()));
         return authentication.getAuthenticationData(mapNotify, key);
     }