Merge "Authentication to southbound"
authorLori Jakab <lorand.jakab@gmail.com>
Wed, 25 May 2016 14:47:48 +0000 (14:47 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Wed, 25 May 2016 14:47:48 +0000 (14:47 +0000)
23 files changed:
features/src/main/features/features.xml
integrationtest/src/test/java/org/opendaylight/lispflowmapping/integrationtest/MappingServiceIntegrationTest.java
mappingservice/api/src/main/java/org/opendaylight/lispflowmapping/interfaces/lisp/IGenericMapServer.java
mappingservice/api/src/main/java/org/opendaylight/lispflowmapping/interfaces/lisp/ILispAuthentication.java
mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/LispMappingService.java
mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/lisp/MapServer.java
mappingservice/implementation/src/test/java/org/opendaylight/lispflowmapping/implementation/LispMappingServiceTest.java
mappingservice/implementation/src/test/java/org/opendaylight/lispflowmapping/implementation/lisp/MapServerTest.java
mappingservice/southbound/pom.xml
mappingservice/southbound/src/main/config/default-config.xml
mappingservice/southbound/src/main/java/org/opendaylight/controller/config/yang/config/lisp_sb/impl/LfmMappingServiceSbModule.java
mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/LispSouthboundPlugin.java
mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/authentication/LispAuthenticationFactory.java [moved from mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/implementation/authentication/LispAuthenticationFactory.java with 95% similarity]
mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/authentication/LispAuthenticationUtil.java [moved from mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/implementation/authentication/LispAuthenticationUtil.java with 72% similarity]
mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/authentication/LispKeyIDEnum.java [moved from mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/implementation/authentication/LispKeyIDEnum.java with 93% similarity]
mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/authentication/LispMACAuthentication.java [moved from mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/implementation/authentication/LispMACAuthentication.java with 83% similarity]
mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/authentication/LispNoAuthentication.java [moved from mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/implementation/authentication/LispNoAuthentication.java with 82% similarity]
mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/lisp/AuthenticationKeyDataListener.java [new file with mode: 0644]
mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/lisp/LispSouthboundHandler.java
mappingservice/southbound/src/main/yang/odl-lisp-sb-impl.yang
mappingservice/southbound/src/test/java/org/opendaylight/lispflowmapping/southbound/authentication/LispAuthenticationTest.java [moved from mappingservice/southbound/src/test/java/org/opendaylight/lispflowmapping/implementation/authentication/LispAuthenticationTest.java with 80% similarity]
mappingservice/southbound/src/test/java/org/opendaylight/lispflowmapping/southbound/lisp/LispSouthboundServiceTest.java
mappingservice/southbound/src/test/java/org/opendaylight/lispflowmapping/southbound/lisp/MapRegisterCacheTestUtil.java

index 63188d8ac350f5aaad2fb9ac1591cdddac1437d6..fe3e7e15e2ee85d63eb0bf255cb9f193b3b7504a 100644 (file)
@@ -56,6 +56,7 @@
         <feature version="${restconf.version}">odl-restconf</feature>
         <feature version="${mdsal.version}">odl-mdsal-broker</feature>
         <feature version='${project.version}'>odl-lispflowmapping-models</feature>
+        <feature version="${project.version}">odl-lispflowmapping-inmemorydb</feature>
         <bundle>mvn:org.opendaylight.lispflowmapping/mappingservice.mapcache/{{VERSION}}</bundle>
         <bundle>mvn:org.opendaylight.lispflowmapping/mappingservice.southbound/{{VERSION}}</bundle>
         <configfile finalname="${configfile.directory}/lfm-mappingservice-sb.xml">
index 63805595f8491df073b23f9266eb17ae32b5563f..80e8ed7639f83616df8c996b160194fd0831f100 100644 (file)
@@ -341,8 +341,7 @@ public class MappingServiceIntegrationTest extends AbstractMdsalTestBase {
         mapRegisterWithMapNotifyAndMapRequest();
         registerAndQuery__MAC();
         mapRequestMapRegisterAndMapRequest();
-        //TODO uncomment followign test once authentication check will be moved to LispSouthboundHandler
-//        mapRegisterWithAuthenticationWithoutConfiguringAKey();
+        mapRegisterWithAuthenticationWithoutConfiguringAKey();
         mapRegisterWithoutMapNotify();
     }
 
index ae354d0d195c270f762d179d3ae17607f891208a..e1aa513c87e62e2a193d5069cadf3da4e3a686d6 100644 (file)
@@ -12,19 +12,6 @@ package org.opendaylight.lispflowmapping.interfaces.lisp;
  */
 public interface IGenericMapServer {
 
-    /**
-     * @return Should the Map Server use authentication.
-     */
-    boolean shouldAuthenticate();
-
-    /**
-     * Configure Map server to use authentication
-     *
-     * @param shouldAuthenticate
-     *            Set authentication
-     */
-    void setShouldAuthenticate(boolean shouldAuthenticate);
-
     /**
      * Configure Map Server to notify mapping subscribers on mapping updates
      *
index a02e7c727572f8b1550645bbc3d429335059f2a1..226341fe61f531810c4a07d367fb0f82d7e48322 100644 (file)
@@ -7,11 +7,12 @@
  */
 package org.opendaylight.lispflowmapping.interfaces.lisp;
 
+import java.nio.ByteBuffer;
 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;
 
 public interface ILispAuthentication {
-    boolean validate(MapRegister mapRegister, String key);
+
+    boolean validate(ByteBuffer mapRegisterBuffer, byte[] expectedAuthData, String key);
 
     byte[] getAuthenticationData(MapNotify mapNotify, String key);
 
index b870dc43ca9cf7e2e2b275c15aec03ac717dcd6e..6fd8489a599717f5dabab3c6e702960a89aba7f8 100644 (file)
@@ -62,8 +62,6 @@ public class LispMappingService implements IFlowMapping, BindingAwareProvider, I
         IMapNotifyHandler, OdlLispProtoListener, AutoCloseable {
     protected static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
 
-    private volatile boolean shouldAuthenticate = true;
-
     private volatile boolean smr = ConfigIni.getInstance().smrIsSet();
     private volatile String elpPolicy = ConfigIni.getInstance().getElpPolicy();
 
@@ -122,12 +120,12 @@ public class LispMappingService implements IFlowMapping, BindingAwareProvider, I
         broker.registerProvider(this);
         notificationService.registerNotificationListener(this);
         mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
-        mapServer = new MapServer(mapService, shouldAuthenticate, smr, this, notificationService);
+        mapServer = new MapServer(mapService, smr, this, notificationService);
     }
 
     public void basicInit() {
         mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
-        mapServer = new MapServer(mapService, shouldAuthenticate, smr, this, notificationService);
+        mapServer = new MapServer(mapService, smr, this, notificationService);
     }
 
     @Override
@@ -172,13 +170,7 @@ public class LispMappingService implements IFlowMapping, BindingAwareProvider, I
     }
 
     public void setShouldAuthenticate(boolean shouldAuthenticate) {
-        this.shouldAuthenticate = shouldAuthenticate;
         this.mapResolver.setShouldAuthenticate(shouldAuthenticate);
-        this.mapServer.setShouldAuthenticate(shouldAuthenticate);
-    }
-
-    public boolean shouldAuthenticate() {
-        return shouldAuthenticate;
     }
 
     private void sendMapNotify(MapNotify mapNotify, TransportAddress address) {
index 67614db80cd9b94dcd08d27442197b168e320bd9..4b2659c922c98de3d4d9b95ff517754ff66143f5 100644 (file)
@@ -64,16 +64,14 @@ public class MapServer implements IMapServerAsync, OdlMappingserviceListener {
 
     protected static final Logger LOG = LoggerFactory.getLogger(MapServer.class);
     private IMappingService mapService;
-    private boolean authenticate;
     private boolean subscriptionService;
     private IMapNotifyHandler notifyHandler;
     private NotificationService notificationService;
 
-    public MapServer(IMappingService mapService, boolean authenticate, boolean subscriptionService,
+    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;
@@ -87,16 +85,6 @@ 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;
index 08d35489f06984af7f3609df9c5d3549ff16dd64..ea4b215cf2e93917183b35cafd9a0a718944fd5e 100644 (file)
@@ -299,7 +299,6 @@ public class LispMappingServiceTest {
 
         lispMappingService.setShouldAuthenticate(value);
         Mockito.verify(mapResolverMock).setShouldAuthenticate(value);
-        Mockito.verify(mapServerMock).setShouldAuthenticate(value);
     }
 
     private static Pair<MapNotify, List<TransportAddress>> getDefaultMapNotifyPair() {
index 30303d96c951b1bce468b31b247d2e30745675bd..f1d96489f64b48d9f04282dbca3d8510ddd148d1 100644 (file)
@@ -140,7 +140,7 @@ public class MapServerTest {
 
     @Before
     public void init() throws NoSuchFieldException, IllegalAccessException  {
-        mapServer = new MapServer(mapService, true, true, notifyHandler, notificationService);
+        mapServer = new MapServer(mapService, true, notifyHandler, notificationService);
         subscriberSetMock_1.add(SUBSCRIBER_RLOC_1);
         subscriberSetMock_1.add(SUBSCRIBER_RLOC_2);
         subscriberSetMock_2.add(SUBSCRIBER_RLOC_3);
index c38c445cbd182cb839a55b9937af4a27fcbd9fd4..7530d049f72a64ca9afc2e7468e13cd9d5a45918 100644 (file)
       <groupId>${project.groupId}</groupId>
       <artifactId>mappingservice.api</artifactId>
     </dependency>
+    <dependency>
+      <groupId>${project.groupId}</groupId>
+      <artifactId>mappingservice.inmemorydb</artifactId>
+    </dependency>
     <dependency>
       <groupId>${project.groupId}</groupId>
       <artifactId>mappingservice.lisp-proto</artifactId>
     </dependency>
+    <dependency>
+      <groupId>${project.groupId}</groupId>
+      <artifactId>mappingservice.mapcache</artifactId>
+    </dependency>
     <dependency>
       <groupId>io.netty</groupId>
       <artifactId>netty-all</artifactId>
index df6e25e0e98510afed59f168dc13fbd45739c62f..83b7778bf6ee0c7da1596657804d1d31f97cbe48 100644 (file)
@@ -4,6 +4,7 @@
     <capability>urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding?module=opendaylight-md-sal-binding&amp;revision=2013-10-28</capability>
     <capability>urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding:impl?module=opendaylight-sal-binding-broker-impl&amp;revision=2013-10-28</capability>
     <capability>urn:opendaylight:params:xml:ns:yang:controller:config:lisp-sb:impl?module=odl-lisp-sb-impl&amp;revision=2015-05-17</capability>
+    <capability>urn:opendaylight:params:xml:ns:yang:controller:config:lfm:mappingservice-dao:inmemorydb?module=odl-mappingservice-dao-inmemorydb&amp;revision=2015-10-07</capability>
   </required-capabilities>
   <configuration>
     <data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
             </type>
             <name>binding-notification-publish-adapter</name>
           </notification-publish-service>
+
+          <data-broker>
+            <type xmlns:binding="urn:opendaylight:params:xml:ns:yang:controller:md:sal:binding">binding:binding-async-data-broker</type>
+            <name>pingpong-binding-data-broker</name>
+          </data-broker>
+
         </module>
       </modules>
 
index 228c68974ea9bfc70affb66b5bf898ea3a2a907e..4d0abfc54468e40460c3ea2acf7692c79a1da4de 100644 (file)
@@ -36,6 +36,7 @@ public class LfmMappingServiceSbModule extends org.opendaylight.controller.confi
         sbPlugin = new LispSouthboundPlugin();
         sbPlugin.setNotificationPublishService(getNotificationPublishServiceDependency());
         sbPlugin.setRpcRegistryDependency(getRpcRegistryDependency());
+        sbPlugin.setDataBroker(getDataBrokerDependency());
         sbPlugin.init();
 
         return sbPlugin;
index 1818f898f21b68bc42b1dc9eed1627a83ae3d65c..d278c34bc986cf1abbec4edb798b0ab3693147f9 100644 (file)
@@ -9,6 +9,8 @@
 package org.opendaylight.lispflowmapping.southbound;
 
 import static io.netty.buffer.Unpooled.wrappedBuffer;
+
+import com.google.common.base.Preconditions;
 import io.netty.bootstrap.Bootstrap;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.ByteBufUtil;
@@ -19,13 +21,12 @@ import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.channel.socket.DatagramPacket;
 import io.netty.channel.socket.nio.NioDatagramChannel;
 import io.netty.util.concurrent.DefaultThreadFactory;
-
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.UnknownHostException;
 import java.nio.ByteBuffer;
 import java.util.concurrent.ThreadFactory;
-
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.RpcRegistration;
 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
@@ -40,8 +41,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLi
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.base.Preconditions;
-
 public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCloseable {
     protected static final Logger LOG = LoggerFactory.getLogger(LispSouthboundPlugin.class);
 
@@ -59,6 +58,7 @@ public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCl
     private LispSouthboundStats statistics = new LispSouthboundStats();
     private ThreadFactory threadFactory = new DefaultThreadFactory("lisp-sb");
     private EventLoopGroup eventLoopGroup = new NioEventLoopGroup(0, threadFactory);
+    private DataBroker dataBroker;
 
 
     public void init() {
@@ -69,8 +69,11 @@ public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCl
 
         synchronized (startLock) {
             lispSouthboundHandler = new LispSouthboundHandler(this);
-            lispXtrSouthboundHandler = new LispXtrSouthboundHandler();
+            lispSouthboundHandler.setDataBroker(dataBroker);
             lispSouthboundHandler.setNotificationProvider(this.notificationPublishService);
+            lispSouthboundHandler.init();
+
+            lispXtrSouthboundHandler = new LispXtrSouthboundHandler();
             lispXtrSouthboundHandler.setNotificationProvider(this.notificationPublishService);
 
             start();
@@ -244,10 +247,15 @@ public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCl
         }
     }
 
+    public void setDataBroker(final DataBroker dataBroker) {
+        this.dataBroker = dataBroker;
+    }
+
     @Override
     public void close() throws Exception {
         unloadActions();
         eventLoopGroup.shutdownGracefully();
         sbRpcRegistration.close();
+        lispSouthboundHandler.close();
     }
 }
@@ -5,7 +5,7 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-package org.opendaylight.lispflowmapping.implementation.authentication;
+package org.opendaylight.lispflowmapping.southbound.authentication;
 
 import java.util.HashMap;
 import java.util.Map;
@@ -5,8 +5,9 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-package org.opendaylight.lispflowmapping.implementation.authentication;
+package org.opendaylight.lispflowmapping.southbound.authentication;
 
+import java.nio.ByteBuffer;
 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;
@@ -23,10 +24,11 @@ public final class LispAuthenticationUtil {
     private LispAuthenticationUtil() {
     }
 
-    public static boolean validate(MapRegister mapRegister, Eid eid, MappingAuthkey key) {
+    private static ILispAuthentication resolveAuthentication(final MapRegister mapRegister, final Eid eid, final
+            MappingAuthkey key) {
         if (key == null) {
             LOG.warn("Authentication failed: mapping authentication key is null");
-            return false;
+            return null;
         }
         short keyId = 0;
         if (mapRegister.getKeyId() != null) {
@@ -35,10 +37,16 @@ public final class LispAuthenticationUtil {
         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;
+            return null;
         }
-        ILispAuthentication authentication = LispAuthenticationFactory.getAuthentication(LispKeyIDEnum.valueOf(keyId));
-        return authentication.validate(mapRegister, key.getKeyString());
+        return LispAuthenticationFactory.getAuthentication(LispKeyIDEnum.valueOf(keyId));
+    }
+
+
+    public static boolean validate(MapRegister mapRegister, ByteBuffer byteBuffer, Eid eid, MappingAuthkey key) {
+        final ILispAuthentication authentication = resolveAuthentication(mapRegister, eid, key);
+        return authentication == null ? false : authentication.validate(byteBuffer, mapRegister.getAuthenticationData(),
+                key.getKeyString());
     }
 
     public static byte[] createAuthenticationData(MapNotify mapNotify, String key) {
@@ -47,5 +55,4 @@ public final class LispAuthenticationUtil {
                 LispKeyIDEnum.valueOf(mapNotify.getKeyId()));
         return authentication.getAuthenticationData(mapNotify, key);
     }
-
 }
@@ -5,7 +5,7 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-package org.opendaylight.lispflowmapping.implementation.authentication;
+package org.opendaylight.lispflowmapping.southbound.authentication;
 
 public enum LispKeyIDEnum {
     NONE(0, null), //
@@ -5,21 +5,17 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-package org.opendaylight.lispflowmapping.implementation.authentication;
+package org.opendaylight.lispflowmapping.southbound.authentication;
 
 import java.nio.ByteBuffer;
 import java.security.InvalidKeyException;
 import java.security.NoSuchAlgorithmException;
 import java.util.Arrays;
-
 import javax.crypto.Mac;
 import javax.crypto.spec.SecretKeySpec;
-
 import org.opendaylight.lispflowmapping.interfaces.lisp.ILispAuthentication;
 import org.opendaylight.lispflowmapping.lisp.serializer.MapNotifySerializer;
-import org.opendaylight.lispflowmapping.lisp.serializer.MapRegisterSerializer;
 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.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -41,21 +37,20 @@ public class LispMACAuthentication implements ILispAuthentication {
         }
     }
 
-    public boolean validate(MapRegister mapRegister, String key) {
+    @Override
+    public boolean validate(ByteBuffer mapRegisterBuffer, byte[] expectedAuthData, String key) {
         if (key == null) {
             LOG.warn("Authentication failed: mapping authentication password is null!");
             return false;
         }
-        ByteBuffer mapRegisterBuffer = MapRegisterSerializer.getInstance().serialize(mapRegister);
         if (mapRegisterBuffer == null) {
             return true;
         }
 
-        mapRegisterBuffer.position(MAP_REGISTER_AND_NOTIFY_AUTHENTICATION_POSITION);
+        mapRegisterBuffer.position(ILispAuthentication.MAP_REGISTER_AND_NOTIFY_AUTHENTICATION_POSITION);
         mapRegisterBuffer.put(tempAuthenticationData);
         mapRegisterBuffer.position(0);
-        return Arrays.equals(getAuthenticationData(mapRegisterBuffer.array(), key),
-                mapRegister.getAuthenticationData());
+        return Arrays.equals(getAuthenticationData(mapRegisterBuffer.array(), key), expectedAuthData);
     }
 
     protected byte[] getAuthenticationData(byte[] data, String key) {
@@ -5,11 +5,11 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-package org.opendaylight.lispflowmapping.implementation.authentication;
+package org.opendaylight.lispflowmapping.southbound.authentication;
 
+import java.nio.ByteBuffer;
 import org.opendaylight.lispflowmapping.interfaces.lisp.ILispAuthentication;
 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;
 
 public final class LispNoAuthentication implements ILispAuthentication {
 
@@ -33,7 +33,8 @@ public final class LispNoAuthentication implements ILispAuthentication {
         return authenticationData;
     }
 
-    public boolean validate(MapRegister mapRegister, String key) {
+    @Override
+    public boolean validate(ByteBuffer mapRegisterBuffer, byte[] expectedAuthData, String key) {
         return true;
     }
 
diff --git a/mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/lisp/AuthenticationKeyDataListener.java b/mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/lisp/AuthenticationKeyDataListener.java
new file mode 100644 (file)
index 0000000..79cc6b2
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc.  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,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.lispflowmapping.southbound.lisp;
+
+import java.util.Collection;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
+import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.lispflowmapping.mapcache.SimpleMapCache;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingDatabase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.database.VirtualNetworkIdentifier;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * DataListener for all AuthenticationKey modification events.
+ *
+ */
+public class AuthenticationKeyDataListener implements DataTreeChangeListener<AuthenticationKey> {
+    private static final Logger LOG = LoggerFactory.getLogger(AuthenticationKeyDataListener.class);
+
+    private final SimpleMapCache smc;
+    private final DataBroker broker;
+    private final InstanceIdentifier<AuthenticationKey> path;
+    private ListenerRegistration<DataTreeChangeListener<AuthenticationKey>> registration;
+
+
+    public AuthenticationKeyDataListener(final DataBroker broker, final SimpleMapCache smc) {
+        this.broker = broker;
+        this.smc = smc;
+        this.path = InstanceIdentifier.create(MappingDatabase.class).child(VirtualNetworkIdentifier.class)
+                .child(AuthenticationKey.class);
+        LOG.trace("Registering AuthenticationKey listener.");
+        final DataTreeIdentifier<AuthenticationKey> dataTreeIdentifier = new DataTreeIdentifier<>
+                (LogicalDatastoreType.CONFIGURATION, path);
+        registration = broker.registerDataTreeChangeListener(dataTreeIdentifier, this);
+    }
+
+    public void closeDataChangeListener() {
+        registration.close();
+    }
+
+    @Override
+    public void onDataTreeChanged(Collection<DataTreeModification<AuthenticationKey>> changes) {
+        for (DataTreeModification<AuthenticationKey> change : changes) {
+            final DataObjectModification<AuthenticationKey> mod = change.getRootNode();
+
+            if (ModificationType.DELETE == mod.getModificationType()) {
+                final AuthenticationKey authKey = mod.getDataBefore();
+
+                LOG.trace("Received deleted data");
+                LOG.trace("Key: {}", change.getRootPath().getRootIdentifier());
+                LOG.trace("Value: {}", authKey);
+
+                smc.removeAuthenticationKey(authKey.getEid());
+            } else if (ModificationType.WRITE == mod.getModificationType() || ModificationType.SUBTREE_MODIFIED == mod
+                    .getModificationType()) {
+                if (ModificationType.WRITE == mod.getModificationType()) {
+                    LOG.trace("Received created data");
+                } else {
+                    LOG.trace("Received updated data");
+                }
+                // Process newly created or updated authentication keys
+                final AuthenticationKey authKey = mod.getDataAfter();
+
+                LOG.trace("Key: {}", change.getRootPath().getRootIdentifier());
+                LOG.trace("Value: {}", authKey);
+
+                smc.addAuthenticationKey(authKey.getEid(), authKey.getMappingAuthkey());
+            } else {
+                LOG.warn("Ignoring unhandled modification type {}", mod.getModificationType());
+            }
+        }
+    }
+
+}
index 347a835ddb7d79d17c4210a14d00111836bb2dbe..d89914771097969c3a509d97079ff621072aa247 100644 (file)
@@ -8,6 +8,7 @@
 
 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;
@@ -19,7 +20,10 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
+import org.opendaylight.lispflowmapping.inmemorydb.HashMapDb;
+import org.opendaylight.lispflowmapping.southbound.authentication.LispAuthenticationUtil;
 import org.opendaylight.lispflowmapping.lisp.serializer.MapNotifySerializer;
 import org.opendaylight.lispflowmapping.lisp.serializer.MapRegisterSerializer;
 import org.opendaylight.lispflowmapping.lisp.serializer.MapReplySerializer;
@@ -28,6 +32,7 @@ import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
 import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
 import org.opendaylight.lispflowmapping.lisp.util.MapRequestUtil;
+import org.opendaylight.lispflowmapping.mapcache.SimpleMapCache;
 import org.opendaylight.lispflowmapping.southbound.LispSouthboundPlugin;
 import org.opendaylight.lispflowmapping.southbound.LispSouthboundStats;
 import org.opendaylight.lispflowmapping.southbound.lisp.cache.MapRegisterCache;
@@ -55,16 +60,18 @@ 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.map.register.cache.metadata.container.map.register.cache.metadata.EidLispAddressBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.value.grouping.MapRegisterCacheValue;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.value.grouping.MapRegisterCacheValueBuilder;
+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.yangtools.yang.binding.Notification;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @ChannelHandler.Sharable
 public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramPacket>
-        implements ILispSouthboundService {
-    private final MapRegisterCache mapRegisterCache;
+        implements ILispSouthboundService, AutoCloseable {
+    private MapRegisterCache mapRegisterCache;
 
     /**
      * How long is record supposed to be relevant. After this time record isn't valid.
@@ -72,27 +79,26 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
      * 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;
 
-    public LispSouthboundHandler(LispSouthboundPlugin lispSbPlugin, final MapRegisterCache mapRegisterCache) {
+    public LispSouthboundHandler(LispSouthboundPlugin lispSbPlugin) {
         this.lispSbPlugin = lispSbPlugin;
         if (lispSbPlugin != null) {
             this.lispSbStats = lispSbPlugin.getStats();
         }
-        this.mapRegisterCache = mapRegisterCache;
-    }
-
-    public LispSouthboundHandler(LispSouthboundPlugin lispSbPlugin) {
-        this(lispSbPlugin, new MapRegisterCache());
-    }
-
-    public void setNotificationProvider(NotificationPublishService nps) {
-        this.notificationPublishService = nps;
+        this.mapRegisterCache = new MapRegisterCache();
+        this.smc = new SimpleMapCache(new HashMapDb());
     }
 
     public void handlePacket(DatagramPacket msg) {
@@ -198,28 +204,31 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
                 }
             } else {
                 MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(inBuffer, sourceAddress);
-                AddMappingBuilder addMappingBuilder = new AddMappingBuilder();
-                addMappingBuilder.setMapRegister(LispNotificationHelper.convertMapRegister(mapRegister));
-                TransportAddressBuilder transportAddressBuilder = new TransportAddressBuilder();
-                transportAddressBuilder.setIpAddress(LispNotificationHelper.getIpAddressBinaryFromInetAddress(
-                        sourceAddress));
-                transportAddressBuilder.setPort(new PortNumber(port));
-                addMappingBuilder.setTransportAddress(transportAddressBuilder.build());
-                sendNotificationIfPossible(addMappingBuilder.build());
-                if (artificialEntry != null) {
-                    final MapRegisterCacheMetadataBuilder cacheMetadataBldNew = new MapRegisterCacheMetadataBuilder();
-                    cacheMetadataBldNew.setEidLispAddress(provideEidPrefixesFromMessage(mapRegister));
-                    cacheMetadataBldNew.setXtrId(mapRegister.getXtrId());
-                    cacheMetadataBldNew.setSiteId(mapRegister.getSiteId());
-                    cacheMetadataBldNew.setWantMapNotify(mapRegister.isWantMapNotify());
-                    cacheMetadataBldNew.setMergeEnabled(mapRegister.isMergeEnabled());
-                    cacheMetadataBldNew.setTimestamp(System.currentTimeMillis());
-
-                    final MapRegisterCacheValueBuilder cacheValueBldNew = new MapRegisterCacheValueBuilder();
-                    cacheValueBldNew.setPacketData(artificialEntry.getValue());
-                    cacheValueBldNew.setMapRegisterCacheMetadata(cacheMetadataBldNew.build());
-
-                    mapRegisterCache.addEntry(cacheKey, cacheValueBldNew.build());
+                if (isAuthenticationSuccessful(mapRegister, inBuffer)) {
+                    AddMappingBuilder addMappingBuilder = new AddMappingBuilder();
+                    addMappingBuilder.setMapRegister(LispNotificationHelper.convertMapRegister(mapRegister));
+                    TransportAddressBuilder transportAddressBuilder = new TransportAddressBuilder();
+                    transportAddressBuilder.setIpAddress(LispNotificationHelper.getIpAddressBinaryFromInetAddress(
+                            sourceAddress));
+                    transportAddressBuilder.setPort(new PortNumber(port));
+                    addMappingBuilder.setTransportAddress(transportAddressBuilder.build());
+                    sendNotificationIfPossible(addMappingBuilder.build());
+                    if (artificialEntry != null) {
+                        final MapRegisterCacheMetadataBuilder cacheMetadataBldNew = new
+                                MapRegisterCacheMetadataBuilder();
+                        cacheMetadataBldNew.setEidLispAddress(provideEidPrefixesFromMessage(mapRegister));
+                        cacheMetadataBldNew.setXtrId(mapRegister.getXtrId());
+                        cacheMetadataBldNew.setSiteId(mapRegister.getSiteId());
+                        cacheMetadataBldNew.setWantMapNotify(mapRegister.isWantMapNotify());
+                        cacheMetadataBldNew.setMergeEnabled(mapRegister.isMergeEnabled());
+                        cacheMetadataBldNew.setTimestamp(System.currentTimeMillis());
+
+                        final MapRegisterCacheValueBuilder cacheValueBldNew = new MapRegisterCacheValueBuilder();
+                        cacheValueBldNew.setPacketData(artificialEntry.getValue());
+                        cacheValueBldNew.setMapRegisterCacheMetadata(cacheMetadataBldNew.build());
+
+                        mapRegisterCache.addEntry(cacheKey, cacheValueBldNew.build());
+                    }
                 }
             }
         } catch (RuntimeException re) {
@@ -288,6 +297,51 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
         return eidsResult;
     }
 
+    /**
+     * Checks whether authentication data is valid.
+     *
+     * Methods pass through all records from map register message. For the EID of the first record it gets
+     * authentication key and does validation of authentication data again this authentication key. If it pass
+     * it just checks for remaining records (and its EID) whether they have the same authenticatin key stored in
+     * simple map cache (smc).
+     *
+     * @param mapRegister
+     * @param byteBuffer
+     * @return
+     */
+    private boolean isAuthenticationSuccessful(final MapRegister mapRegister, final ByteBuffer byteBuffer) {
+        if (!authenticationEnabled) {
+            return true;
+        }
+
+        if (smc == null) {
+            LOG.debug("Simple map cache wasn't instantieted and set.");
+            return false;
+        }
+
+        MappingAuthkey firstAuthKey = null;
+        final List<MappingRecordItem> mappingRecords = mapRegister.getMappingRecordItem();
+        for (int i = 0; i < mappingRecords.size(); i++) {
+            final MappingRecordItem recordItem = mappingRecords.get(i);
+            final MappingRecord mappingRecord = recordItem.getMappingRecord();
+            if (i == 0) {
+                firstAuthKey = smc.getAuthenticationKey(mappingRecord.getEid());
+                if (!LispAuthenticationUtil.validate(mapRegister, byteBuffer, mappingRecord.getEid(), firstAuthKey)) {
+                    return false;
+                }
+            } else {
+                final Eid eid = mappingRecord.getEid();
+                final MappingAuthkey authKey = smc.getAuthenticationKey(eid);
+                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 true;
+    }
+
     private void handleMapNotify(ByteBuffer inBuffer, InetAddress sourceAddress, int port) {
         try {
             MapNotify mapNotify = MapNotifySerializer.getInstance().deserialize(inBuffer);
@@ -312,6 +366,7 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
         }
     }
 
+
     private void handleMapReply(ByteBuffer inBuffer, InetAddress sourceAddress, int port) {
         try {
             MapReply mapReply = MapReplySerializer.getInstance().deserialize(inBuffer);
@@ -364,4 +419,32 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
         LOG.error("Error on channel: " + cause, cause);
     }
+
+    @Override
+    public void close() throws Exception {
+        authenticationKeyDataListener.closeDataChangeListener();
+    }
+
+    public void setSimpleMapCache(final SimpleMapCache smc) {
+        this.smc = smc;
+    }
+
+    public void setDataBroker(final DataBroker dataBroker) {
+        this.dataBroker = dataBroker;
+    }
+
+    public void setNotificationProvider(NotificationPublishService nps) {
+        this.notificationPublishService = nps;
+    }
+
+    public void setMapRegisterCache(final MapRegisterCache mapRegisterCache) {
+        this.mapRegisterCache = mapRegisterCache;
+    }
+
+
+    public void init() {
+        Preconditions.checkNotNull(dataBroker);
+        Preconditions.checkNotNull(smc);
+        this.authenticationKeyDataListener = new AuthenticationKeyDataListener(dataBroker, smc);
+    }
 }
index 8274ef668572061116bff99f745719af5cf72af4..893c9e19c6ff5bd1ef31f6e57ce79b7d860aa958 100644 (file)
@@ -9,6 +9,7 @@ module odl-lisp-sb-impl {
   import opendaylight-md-sal-binding { prefix mdsal; revision-date 2013-10-28; }
   import opendaylight-sal-binding-broker-impl { prefix sal-broker; revision-date 2013-10-28; }
   import odl-lisp-sb { prefix lisp-sb; revision-date 2015-09-04; }
+  import odl-mappingservice-dao { prefix ms-dao; revision-date 2015-10-07; }
 
   description
       "This module contains the base YANG definitions for
@@ -48,6 +49,16 @@ module odl-lisp-sb-impl {
           }
         }
       }
+
+      container data-broker {
+          uses config:service-ref {
+              refine type {
+                  mandatory true;
+                  config:required-identity mdsal:binding-async-data-broker;
+              }
+          }
+      }
+
     }
   }
 }
@@ -5,11 +5,12 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-package org.opendaylight.lispflowmapping.implementation.authentication;
+package org.opendaylight.lispflowmapping.southbound.authentication;
 
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.assertFalse;
 
+import java.nio.ByteBuffer;
 import java.util.ArrayList;
 
 import junitx.framework.ArrayAssert;
@@ -46,14 +47,14 @@ public class LispAuthenticationTest extends BaseTestCase {
         // No-Action
         // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
         // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
-        //
-        MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(hexToByteBuffer("38 00 01 01 FF BB "
-                + "00 00 00 00 00 00 00 01 00 14 e8 f5 0b c5 c5 f2 "
-                + "b0 21 27 a8 21 41 04 f3 46 5a 5a 5b 5c 5d 00 00 "
-                + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 "
-                + "ff 00 00 05 00 01 c0 a8 88 0a"), null);
-
-        assertFalse(validate(mapRegister, EID, 1, PASSWORD));
+        final ByteBuffer byteBuffer = hexToByteBuffer("38 00 01 01 FF BB "
+                        + "00 00 00 00 00 00 00 01 00 14 e8 f5 0b c5 c5 f2 "
+                        + "b0 21 27 a8 21 41 04 f3 46 5a 5a 5b 5c 5d 00 00 "
+                        + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 "
+                        + "ff 00 00 05 00 01 c0 a8 88 0a");
+        MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(byteBuffer, null);
+
+        assertFalse(validate(mapRegister, byteBuffer, EID, 1, PASSWORD));
     }
 
     @Test
@@ -69,13 +70,14 @@ public class LispAuthenticationTest extends BaseTestCase {
         // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
         // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
         //
-        MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(hexToByteBuffer("38 00 01 01 FF BB "
+        final ByteBuffer byteBuffer = hexToByteBuffer("38 00 01 01 FF BB "
                 + "00 00 00 00 00 00 00 01 00 14 2c 61 b9 c9 9a 20 ba d8 f5 40 d3 55 6f 5f 6e 5a b2 0a bf b5 00 00 "
                 + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 "
-                + "ff 00 00 05 00 01 c0 a8 88 0a"), null);
+                + "ff 00 00 05 00 01 c0 a8 88 0a");
+        MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(byteBuffer, null);
 
-        assertTrue(validate(mapRegister, EID, 1, PASSWORD));
-        assertFalse(validate(mapRegister, EID, 1, WRONG_PASSWORD));
+        assertTrue(validate(mapRegister, byteBuffer, EID, 1, PASSWORD));
+        assertFalse(validate(mapRegister, byteBuffer, EID, 1, WRONG_PASSWORD));
     }
 
     @Test
@@ -92,19 +94,19 @@ public class LispAuthenticationTest extends BaseTestCase {
         // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
         // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
         //
+        final ByteBuffer byteBuffer = hexToByteBuffer("38 00 01 01 FF BB "
+                + "00 00 00 00 00 00 00 02 00 20 "
+                + "70 30 d4 c6 10 44 0d 83 be 4d bf fd a9 8c 57 6d "
+                + "68 a5 bf 32 11 c9 7b 58 c4 b9 9f 06 11 23 b9 38 "
+                + "00 00 "
+                + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 "
+                + "ff 00 00 05 00 01 c0 a8 88 0a");
         MapRegister mapRegister = MapRegisterSerializer
                 .getInstance()
-                .deserialize(
-                        hexToByteBuffer("38 00 01 01 FF BB "
-                                + "00 00 00 00 00 00 00 02 00 20 "
-                                + "70 30 d4 c6 10 44 0d 83 be 4d bf fd a9 8c 57 6d "
-                                + "68 a5 bf 32 11 c9 7b 58 c4 b9 9f 06 11 23 b9 38 "
-                                + "00 00 "
-                                + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 "
-                                + "ff 00 00 05 00 01 c0 a8 88 0a"), null);
-
-        assertTrue(validate(mapRegister, EID, 2, PASSWORD));
-        assertFalse(validate(mapRegister, EID, 2, WRONG_PASSWORD));
+                .deserialize(byteBuffer, null);
+
+        assertTrue(validate(mapRegister, byteBuffer, EID, 2, PASSWORD));
+        assertFalse(validate(mapRegister, byteBuffer, EID, 2, WRONG_PASSWORD));
     }
 
     @Test
@@ -119,14 +121,15 @@ public class LispAuthenticationTest extends BaseTestCase {
         // Local RLOC: 192.168.136.10 (RLOC=0xC0A8880A), Reachable,
         // Priority/Weight: 1/100, Multicast Priority/Weight: 255/0
         //
-        MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(hexToByteBuffer("38 00 01 01 FF BB "
+        final ByteBuffer byteBuffer = hexToByteBuffer("38 00 01 01 FF BB "
                 + "00 00 00 00 00 00 00 00 00 00 "
                 + "00 00 "
                 + "00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 "
-                + "ff 00 00 05 00 01 c0 a8 88 0a"), null);
+                + "ff 00 00 05 00 01 c0 a8 88 0a");
+        MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(byteBuffer, null);
 
-        assertTrue(validate(mapRegister, EID, 0, PASSWORD));
-        assertTrue(validate(mapRegister, EID, 0, WRONG_PASSWORD));
+        assertTrue(validate(mapRegister, byteBuffer, EID, 0, PASSWORD));
+        assertTrue(validate(mapRegister, byteBuffer, EID, 0, WRONG_PASSWORD));
     }
 
     // @Test
@@ -187,8 +190,9 @@ public class LispAuthenticationTest extends BaseTestCase {
 
     }
 
-    private static boolean validate(MapRegister mapRegister, Eid eid, int keyId, String password) {
+    private static boolean validate(MapRegister mapRegister, ByteBuffer byteBuffer, Eid eid, int keyId, String
+            password) {
         MappingAuthkey key = new MappingAuthkeyBuilder().setKeyType(keyId).setKeyString(password).build();
-        return LispAuthenticationUtil.validate(mapRegister, eid, key);
+        return LispAuthenticationUtil.validate(mapRegister,byteBuffer, eid, key);
     }
 }
index 3db37c21ea34d0819e836e1c4cddbb3a64f9ffce..bcf7c42d61773320dd4e214947bd8403f80b9928 100644 (file)
@@ -13,7 +13,6 @@ import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Mockito.mock;
-import static org.opendaylight.lispflowmapping.southbound.lisp.MapRegisterCacheTestUtil.authenticationData;
 import static org.opendaylight.lispflowmapping.southbound.lisp.MapRegisterCacheTestUtil.data1;
 import static org.opendaylight.lispflowmapping.southbound.lisp.MapRegisterCacheTestUtil.data2;
 import static org.opendaylight.lispflowmapping.southbound.lisp.MapRegisterCacheTestUtil.data3;
@@ -36,11 +35,14 @@ import junitx.framework.ArrayAssert;
 import org.apache.commons.lang3.ArrayUtils;
 import org.jmock.api.Invocation;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Ignore;
 import org.junit.Test;
 import org.mockito.AdditionalMatchers;
 import org.mockito.InOrder;
+import org.mockito.Matchers;
 import org.mockito.Mockito;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
 import org.opendaylight.lispflowmapping.lisp.serializer.MapNotifySerializer;
 import org.opendaylight.lispflowmapping.lisp.serializer.MapReplySerializer;
@@ -49,11 +51,13 @@ import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
 import org.opendaylight.lispflowmapping.lisp.util.MapNotifyBuilderHelper;
 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
+import org.opendaylight.lispflowmapping.mapcache.SimpleMapCache;
 import org.opendaylight.lispflowmapping.southbound.LispSouthboundPlugin;
 import org.opendaylight.lispflowmapping.southbound.lisp.cache.MapRegisterCache;
 import org.opendaylight.lispflowmapping.southbound.lisp.exception.LispMalformedPacketException;
 import org.opendaylight.lispflowmapping.tools.junit.BaseTestCase;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana.afn.safi.rev130704.AddressFamily;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.InstanceIdType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv4BinaryAfi;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv4PrefixBinaryAfi;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv6PrefixBinaryAfi;
@@ -78,6 +82,7 @@ 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.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.mapreplymessage.MapReplyBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container.MappingAuthkeyBuilder;
 import org.opendaylight.yangtools.yang.binding.Notification;
 
 public class LispSouthboundServiceTest extends BaseTestCase {
@@ -96,6 +101,7 @@ public class LispSouthboundServiceTest extends BaseTestCase {
     private LispSouthboundPlugin mockLispSouthboundPlugin;
     private static final long CACHE_RECORD_TIMEOUT = 90000;
 
+    private static SimpleMapCache smc;
 
     private interface MapReplyIpv4SingleLocatorPos {
         int RECORD_COUNT = 3;
@@ -116,15 +122,41 @@ public class LispSouthboundServiceTest extends BaseTestCase {
         int LOCATOR = MapReplyIpv4SingleLocatorPos.LOCATOR + FIRST_LOCATOR_IPV4_LENGTH;
     }
 
+    @BeforeClass
+    public static void initTests() {
+        smc = Mockito.mock(SimpleMapCache.class);
+        Mockito.when(smc.getAuthenticationKey(Matchers.eq(LispAddressUtil.asIpv4PrefixBinaryEid("10.10.10.10/8"))))
+                .thenReturn(new MappingAuthkeyBuilder().setKeyType(1).setKeyString("password").build());
+        Mockito.when(smc.getAuthenticationKey(Matchers.eq(LispAddressUtil.asIpv6PrefixBinaryEid
+                ("2610:d0:ffff:192:0:0:0:1/128"))))
+                .thenReturn(new MappingAuthkeyBuilder().setKeyType(1).setKeyString("password").build());
+        Mockito.when(smc.getAuthenticationKey(Matchers.eq(LispAddressUtil.asIpv4PrefixBinaryEid("153.16.254.1/32"))))
+                .thenReturn(new MappingAuthkeyBuilder().setKeyType(1).setKeyString("password").build());
+        Mockito.when(smc.getAuthenticationKey(Matchers.eq(LispAddressUtil.asIpv4PrefixBinaryEid("125.124.123.122/8", new
+                InstanceIdType(21L)))))
+                .thenReturn(new MappingAuthkeyBuilder().setKeyType(1).setKeyString("password").build());
+        Mockito.when(smc.getAuthenticationKey(Matchers.eq(LispAddressUtil.asMacEid("0a:0b:0c:0d:0e:0f"))))
+                .thenReturn(new MappingAuthkeyBuilder().setKeyType(1).setKeyString("password").build());
+        Mockito.when(smc.getAuthenticationKey(Matchers.eq(LispAddressUtil.asIpv6PrefixBinaryEid
+                ("f0f:f0f:f0f:f0f:f0f:f0f:f0f:f0f/8"))))
+                .thenReturn(new MappingAuthkeyBuilder().setKeyType(1).setKeyString("password").build());
+        Mockito.when(smc.getAuthenticationKey(Matchers.eq(LispAddressUtil.asIpv4PrefixBinaryEid("172.1.1.2/32"))))
+                .thenReturn(new MappingAuthkeyBuilder().setKeyType(1).setKeyString("password").build());
+    }
+
     @Override
     @Before
     public void before() throws Exception {
         super.before();
         // mapResolver = context.mock(IMapResolver.class);
         // mapServer = context.mock(IMapServer.class);
-        mapRegisterCache = new MapRegisterCache();
         mockLispSouthboundPlugin = mock(LispSouthboundPlugin.class);
-        testedLispService = new LispSouthboundHandler(mockLispSouthboundPlugin, mapRegisterCache);
+        testedLispService = new LispSouthboundHandler(mockLispSouthboundPlugin);
+        mapRegisterCache = new MapRegisterCache();
+        testedLispService.setMapRegisterCache(mapRegisterCache);
+        testedLispService.setDataBroker(Mockito.mock(DataBroker.class));
+        testedLispService.setSimpleMapCache(smc);
+        testedLispService.init();
         nps = context.mock(NotificationPublishService.class);
         testedLispService.setNotificationProvider(nps);
         lispNotificationSaver = new ValueSaverAction<Notification>();
@@ -413,6 +445,10 @@ public class LispSouthboundServiceTest extends BaseTestCase {
     }
 
     void mapRegisterInvocationForCacheTest(byte[] eidPrefixAfi, byte[] eidPrefix) {
+        mapRegisterInvocationForCacheTest(eidPrefixAfi, eidPrefix, MapRegisterCacheTestUtil.authenticationData);
+    }
+
+    void mapRegisterInvocationForCacheTest(byte[] eidPrefixAfi, byte[] eidPrefix, byte[] authenticationData) {
         final byte[] mapRegisterMessage = MapRegisterCacheTestUtil.joinArrays(data1, nonce, keyId,
                 authenticationData, data2, eidPrefixAfi, eidPrefix, data3, xTRId, siteId);
         handlePacket(mapRegisterMessage);
@@ -430,7 +466,8 @@ public class LispSouthboundServiceTest extends BaseTestCase {
         byte[] eidPrefix = new byte[] {
                 0x0a, 0x0a, 0x0a, 0x0a     //ipv4 address
         };
-        cacheTest(eidPrefixAfi, eidPrefix);
+
+        cacheTest(eidPrefixAfi, eidPrefix, MapRegisterCacheTestUtil.authenticationData);
     }
 
     /**
@@ -448,7 +485,17 @@ public class LispSouthboundServiceTest extends BaseTestCase {
                 ,0x0f, 0x0f, 0x0f, 0x0f     //ipv6 address
                 ,0x0f, 0x0f, 0x0f, 0x0f     //ipv6 address
         };
-        cacheTest(eidPrefixAfi, eidPrefix);
+
+        byte[] authenticationData = new byte[]{
+                0x00, 0x14
+                ,0x41,(byte)0x83,0x13,0x7C
+                ,0x48,(byte)0xEE,0x75,(byte)0x9A
+                ,0x4,(byte)0x8C,0x46,(byte)0xA6
+                ,0x1B,0x13,(byte)0xC8,0x4D
+                ,(byte)0xA1,0x17,0x53,(byte)0xC3
+        };
+
+        cacheTest(eidPrefixAfi, eidPrefix, authenticationData);
     }
 
     /**
@@ -464,7 +511,15 @@ public class LispSouthboundServiceTest extends BaseTestCase {
                 0x0a, 0x0b, 0x0c, 0x0d     //mac address
                 ,0x0e, 0x0f                 //mac address
         };
-        cacheTest(eidPrefixAfi, eidPrefix);
+        byte[] authenticationData = new byte[]{
+                0x00, 0x14
+                ,(byte)0xB2,(byte)0x8E,0x6,(byte)0x9D
+                ,0x61,(byte)0xD8,0xC,0x24
+                ,(byte)0x80,0x61,0x5A,0x20
+                ,0xD,0x50,0x5E,(byte)0xAE
+                ,0x47,(byte)0xF7,(byte)0x86,0x36
+        };
+        cacheTest(eidPrefixAfi, eidPrefix, authenticationData);
     }
 
     /**
@@ -489,7 +544,18 @@ public class LispSouthboundServiceTest extends BaseTestCase {
 
         byte[] eidPrefix = joinArrays(lcafRsvd1, lcafFlags, lcafType, lcafIIDMaskLength, lcafLength, lcafInstanceId,
                 lcafAfi, lcafAddress);
-        cacheTest(eidPrefixAfi, eidPrefix);
+
+        byte[] authenticationData = new byte[]{
+                0x00, 0x14                 //authentication data length
+                ,0x68, 0x1d, (byte) 0x9e, 0x6e    //auth data
+                ,0x5e, 0x32, (byte) 0x88, 0x1a    //auth data
+                ,(byte) 0xae, 0x6b, (byte) 0xe3, 0x40    //auth data
+                ,0x30, (byte) 0x0b, (byte) 0xb6, (byte) 0xa0    //auth data
+                ,0x71, (byte) 0xf4, (byte) 0x8c, 0x5f    //auth data
+        };
+
+
+        cacheTest(eidPrefixAfi, eidPrefix, authenticationData);
     }
 
     /**
@@ -497,14 +563,15 @@ public class LispSouthboundServiceTest extends BaseTestCase {
      * @param eidPrefixAfi
      * @param eidPrefix
      */
-    public void cacheTest(byte[] eidPrefixAfi, byte[] eidPrefix) throws InterruptedException {
+    public void cacheTest(byte[] eidPrefixAfi, byte[] eidPrefix, byte[] authenticationData) throws
+            InterruptedException {
         final MapRegisterCacheKey mapRegisterCacheKey = MapRegisterCacheTestUtil.createMapRegisterCacheKey(eidPrefix);
 
         final NotificationPublishService mockedNotificationProvider = mock(NotificationPublishService.class);
         testedLispService.setNotificationProvider(mockedNotificationProvider);
 
         MapRegisterCacheTestUtil.beforeMapRegisterInvocationValidation(mapRegisterCacheKey, mapRegisterCache);
-        mapRegisterInvocationForCacheTest(eidPrefixAfi, eidPrefix);
+        mapRegisterInvocationForCacheTest(eidPrefixAfi, eidPrefix, authenticationData);
         MapRegisterCacheTestUtil.afterMapRegisterInvocationValidation(mockedNotificationProvider,
                 mapRegisterCacheKey, mapRegisterCache, eidPrefixAfi, eidPrefix);
     }
@@ -528,15 +595,14 @@ public class LispSouthboundServiceTest extends BaseTestCase {
         cacheRecordExpirationTest(false);
     }
 
-    private void cacheRecordExpirationTest(boolean cacheRecordTimeouted) {
-        mapRegisterCache = mock(MapRegisterCache.class);
-        testedLispService = new LispSouthboundHandler(mockLispSouthboundPlugin, mapRegisterCache);
+    private void cacheRecordExpirationTest(boolean cacheRecordTimeouted) throws InterruptedException {
+        mapRegisterCache = Mockito.mock(MapRegisterCache.class);
+        testedLispService.setMapRegisterCache(mapRegisterCache);
+        testedLispService.setNotificationProvider(Mockito.mock(NotificationPublishService.class));
 
         byte[] eidPrefixAfi = new byte[] {0x00, 0x01};
-
         byte[] eidPrefix = new byte[] {0x0a, 0x0a, 0x0a, 0x0a};
 
-
         MapRegisterCacheKeyBuilder cacheKeyBld = new MapRegisterCacheKeyBuilder();
         MapRegisterCacheValueBuilder cacheValueBld = new MapRegisterCacheValueBuilder();
         cacheKeyBld.setXtrId(xTRId);
index bc960c612db632c5102807ed6228dedabbbc918c..228ae6431920d32e64728e46ea37cae80c97c05d 100644 (file)
@@ -37,14 +37,16 @@ final class MapRegisterCacheTestUtil {
     };
 
     final static byte[] keyId = new byte[] {
-            0x10, 0x10                 //key ID
+            0x00, 0x01                 //key ID
     };
 
     final static byte[] authenticationData = new byte[]{
-            0x00, 0x0c                 //authentication data length
-            ,0x7f, 0x7f, 0x7f, 0x7f    //auth data
-            ,0x7f, 0x7f, 0x7f, 0x7f    //auth data
-            ,0x7f, 0x7f, 0x7f, 0x7f    //auth data
+            0x00, 0x14                 //authentication data length
+            ,(byte)0xfa, 0x50, 0x1d, (byte)0xd6    //auth data
+            ,0x63, 0x53, 0x67, 0x6c    //auth data
+            ,0x00, 0x3c, 0x61, 0x67    //auth data
+            ,0x35, (byte) 0xb8, (byte) 0xcf, 0x16    //auth data
+            ,(byte) 0x91, (byte) 0xcd, 0x6b, (byte) 0x95    //auth data
     };
 
     final static byte[] data2 = new byte[]{