Bug 8746: Multi-threading improvements 64/61164/7
authorLorand Jakab <lojakab@cisco.com>
Fri, 4 Aug 2017 12:10:24 +0000 (15:10 +0300)
committerLorand Jakab <lojakab@cisco.com>
Thu, 24 Aug 2017 06:16:12 +0000 (08:16 +0200)
Remove all state from SB handlers

This needs a significant rework of the LispSouthboundServiceTest test
class, moving even more functionality to Mockito, so eventually we can
remove the dependency on the old unittest.tools.

Change-Id: I901c5633cb60563ac5bc38a521796dad2186886a
Signed-off-by: Lorand Jakab <lojakab@cisco.com>
mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/LispSouthboundPlugin.java
mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/lisp/LispSouthboundHandler.java
mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/lisp/LispXtrSouthboundHandler.java
mappingservice/southbound/src/test/java/org/opendaylight/lispflowmapping/southbound/lisp/LispSouthboundServiceTest.java
mappingservice/southbound/src/test/java/org/opendaylight/lispflowmapping/southbound/lisp/LispXtrSouthboundHandlerTest.java
mappingservice/southbound/src/test/java/org/opendaylight/lispflowmapping/southbound/lisp/MapRegisterCacheTestUtil.java

index ff4d673fe61523f06f47cdcd9839b000e6f9d29d..6977e60773ff2afe3dc9ebb2fef1b74bef852e7e 100644 (file)
@@ -57,6 +57,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.ei
 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.transport.address.TransportAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
+import org.opendaylight.yangtools.yang.binding.Notification;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -66,6 +67,7 @@ public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCl
     public static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(
             LISPFLOWMAPPING_ENTITY_NAME);
 
+    private volatile boolean isMaster = false;
     private volatile String bindingAddress;
     private AuthKeyDb akdb;
     private MapRegisterCache mapRegisterCache = new MapRegisterCache();
@@ -116,17 +118,9 @@ public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCl
             restoreDaoFromDatastore();
 
             LispSouthboundHandler lispSouthboundHandler = new LispSouthboundHandler(this);
-            lispSouthboundHandler.setDataBroker(dataBroker);
-            lispSouthboundHandler.setNotificationProvider(notificationPublishService);
-            lispSouthboundHandler.setAuthKeyDb(akdb);
-            lispSouthboundHandler.setMapRegisterCache(mapRegisterCache);
-            lispSouthboundHandler.setMapRegisterCacheTimeout(mapRegisterCacheTimeout);
-            lispSouthboundHandler.setAuthenticationKeyDataListener(authenticationKeyDataListener);
-            lispSouthboundHandler.setStats(statistics);
             this.lispSouthboundHandler = lispSouthboundHandler;
 
-            LispXtrSouthboundHandler lispXtrSouthboundHandler = new LispXtrSouthboundHandler();
-            lispXtrSouthboundHandler.setNotificationProvider(notificationPublishService);
+            LispXtrSouthboundHandler lispXtrSouthboundHandler = new LispXtrSouthboundHandler(this);
             this.lispXtrSouthboundHandler = lispXtrSouthboundHandler;
 
             if (Epoll.isAvailable()) {
@@ -293,10 +287,6 @@ public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCl
         return null;
     }
 
-    public ConcurrentLispSouthboundStats getStats() {
-        return statistics;
-    }
-
     @Override
     @SuppressWarnings("checkstyle:IllegalCatch")
     public void setLispAddress(String address) {
@@ -365,24 +355,12 @@ public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCl
 
     @Override
     public void instantiateServiceInstance() {
-        if (lispSouthboundHandler != null) {
-            lispSouthboundHandler.setNotificationProvider(notificationPublishService);
-            lispSouthboundHandler.setIsMaster(true);
-        }
-        if (lispXtrSouthboundHandler != null) {
-            lispXtrSouthboundHandler.setNotificationProvider(notificationPublishService);
-        }
+        this.isMaster = true;
     }
 
     @Override
     public ListenableFuture<Void> closeServiceInstance() {
-        if (lispSouthboundHandler != null) {
-            lispSouthboundHandler.setNotificationProvider(null);
-            lispSouthboundHandler.setIsMaster(false);
-        }
-        if (lispXtrSouthboundHandler != null) {
-            lispXtrSouthboundHandler.setNotificationProvider(null);
-        }
+        this.isMaster = false;
         return Futures.<Void>immediateFuture(null);
     }
 
@@ -391,6 +369,31 @@ public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCl
         return SERVICE_GROUP_IDENTIFIER;
     }
 
+    public synchronized void sendNotificationIfPossible(final Notification notification) throws InterruptedException {
+        if (isMaster && notificationPublishService != null) {
+            notificationPublishService.putNotification(notification);
+            LOG.trace("Publishing notification: {}", notification);
+        } else if (notificationPublishService == null) {
+            LOG.warn("Can't publish notification because no reference to publication service exists!");
+        }
+    }
+
+    public AuthKeyDb getAkdb() {
+        return akdb;
+    }
+
+    public ConcurrentLispSouthboundStats getStats() {
+        return statistics;
+    }
+
+    public DataBroker getDataBroker() {
+        return dataBroker;
+    }
+
+    public AuthenticationKeyDataListener getAuthenticationKeyDataListener() {
+        return authenticationKeyDataListener;
+    }
+
     public MapRegisterCache getMapRegisterCache() {
         return mapRegisterCache;
     }
index 239852ea2fca3f0e457310fc650460a93597d4d3..6a9ae4250bbd296ccc50df47a8f7366a5050897b 100644 (file)
@@ -21,8 +21,6 @@ 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.config.ConfigIni;
 import org.opendaylight.lispflowmapping.lisp.authentication.ILispAuthentication;
 import org.opendaylight.lispflowmapping.lisp.authentication.LispAuthenticationUtil;
@@ -34,10 +32,8 @@ 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.AuthKeyDb;
 import org.opendaylight.lispflowmapping.southbound.ConcurrentLispSouthboundStats;
 import org.opendaylight.lispflowmapping.southbound.LispSouthboundPlugin;
-import org.opendaylight.lispflowmapping.southbound.lisp.cache.MapRegisterCache;
 import org.opendaylight.lispflowmapping.southbound.lisp.cache.MapRegisterPartialDeserializer;
 import org.opendaylight.lispflowmapping.southbound.lisp.exception.LispMalformedPacketException;
 import org.opendaylight.lispflowmapping.southbound.lisp.network.PacketHeader;
@@ -66,27 +62,17 @@ 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.yangtools.yang.binding.Notification;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 @ChannelHandler.Sharable
 public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramPacket>
         implements ILispSouthboundService, AutoCloseable {
-    private MapRegisterCache mapRegisterCache;
-    private long mapRegisterCacheTimeout;
-
-    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 = ConfigIni.getInstance().isAuthEnabled();
     private final LispSouthboundPlugin lispSbPlugin;
-    private ConcurrentLispSouthboundStats lispSbStats = null;
-    private AuthKeyDb akdb;
-    private AuthenticationKeyDataListener authenticationKeyDataListener;
     private boolean isReadFromChannelEnabled = true;
 
     private Channel channel;
@@ -146,12 +132,7 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
                     LispNotificationHelper.getIpAddressBinaryFromInetAddress(finalSourceAddress));
             transportAddressBuilder.setPort(new PortNumber(port));
             requestMappingBuilder.setTransportAddress(transportAddressBuilder.build());
-            if (notificationPublishService != null) {
-                notificationPublishService.putNotification(requestMappingBuilder.build());
-                LOG.trace("MapRequest was published!");
-            } else {
-                LOG.warn("Notification Provider is null!");
-            }
+            lispSbPlugin.sendNotificationIfPossible(requestMappingBuilder.build());
         } catch (RuntimeException re) {
             throw new LispMalformedPacketException("Couldn't deserialize Map-Request (len="
                     + inBuffer.capacity() + ")", re);
@@ -195,19 +176,19 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
                 cacheValue = resolveCacheValue(artificialEntry);
             }
             if (cacheValue != null) {
-                lispSbStats.incrementCacheHits();
+                lispSbPlugin.getStats().incrementCacheHits();
                 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));
+                    lispSbPlugin.sendNotificationIfPossible(createMappingKeepAlive(cacheValue));
                     if (cacheValue.getMapRegisterCacheMetadata().isWantMapNotify()) {
                         sendMapNotifyMsg(inBuffer, sourceAddress, port, cacheValue);
                     }
                 }
             } else {
-                lispSbStats.incrementCacheMisses();
+                lispSbPlugin.getStats().incrementCacheMisses();
                 MapRegister mapRegister = MapRegisterSerializer.getInstance().deserialize(inBuffer, sourceAddress);
 
                 MappingAuthkey mappingAuthkey = null;
@@ -225,7 +206,7 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
                         sourceAddress));
                 transportAddressBuilder.setPort(new PortNumber(port));
                 addMappingBuilder.setTransportAddress(transportAddressBuilder.build());
-                sendNotificationIfPossible(addMappingBuilder.build());
+                lispSbPlugin.sendNotificationIfPossible(addMappingBuilder.build());
                 if (artificialEntry != null) {
                     final MapRegisterCacheMetadataBuilder cacheMetadataBldNew = new
                             MapRegisterCacheMetadataBuilder();
@@ -241,7 +222,7 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
                     cacheValueBldNew.setMappingAuthkey(mappingAuthkey);
                     cacheValueBldNew.setMapRegisterCacheMetadata(cacheMetadataBldNew.build());
 
-                    mapRegisterCache.addEntry(cacheKey, cacheValueBldNew.build());
+                    lispSbPlugin.getMapRegisterCache().addEntry(cacheKey, cacheValueBldNew.build());
                 }
             }
         } catch (RuntimeException re) {
@@ -253,10 +234,10 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
     }
 
     private MapRegisterCacheValue refreshEntry(final MapRegisterCacheKey cacheKey) {
-        MapRegisterCacheValue mapRegisterCacheValue = mapRegisterCache.refreshEntry(cacheKey);
+        MapRegisterCacheValue mapRegisterCacheValue = lispSbPlugin.getMapRegisterCache().refreshEntry(cacheKey);
         if (mapRegisterCacheValue != null) {
             mapRegisterCacheValue = refreshAuthKeyIfNecessary(mapRegisterCacheValue);
-            mapRegisterCache.addEntry(cacheKey, mapRegisterCacheValue);
+            lispSbPlugin.getMapRegisterCache().addEntry(cacheKey, mapRegisterCacheValue);
             return mapRegisterCacheValue;
         }
         return null;
@@ -265,7 +246,8 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
     private MapRegisterCacheValue refreshAuthKeyIfNecessary(MapRegisterCacheValue mapRegisterCacheValue) {
         final List<EidLispAddress> eids = mapRegisterCacheValue.getMapRegisterCacheMetadata().getEidLispAddress();
 
-        if (authenticationKeyDataListener.authKeysForEidsUnchanged(eids, mapRegisterCacheTimeout)) {
+        if (lispSbPlugin.getAuthenticationKeyDataListener().authKeysForEidsUnchanged(
+                eids, lispSbPlugin.getMapRegisterCacheTimeout())) {
             return mapRegisterCacheValue;
         }
 
@@ -282,12 +264,13 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
 
     private MapRegisterCacheValue resolveCacheValue(Map.Entry<MapRegisterCacheKey, byte[]> entry) {
         if (entry != null) {
-            final MapRegisterCacheValue mapRegisterCacheValue = mapRegisterCache.getEntry(entry.getKey());
+            final MapRegisterCacheValue mapRegisterCacheValue =
+                    lispSbPlugin.getMapRegisterCache().getEntry(entry.getKey());
             if (mapRegisterCacheValue != null) {
                 final long creationTime = mapRegisterCacheValue.getMapRegisterCacheMetadata().getTimestamp();
                 final long currentTime = System.currentTimeMillis();
-                if (currentTime - creationTime > mapRegisterCacheTimeout) {
-                    mapRegisterCache.removeEntry(entry.getKey());
+                if (currentTime - creationTime > lispSbPlugin.getMapRegisterCacheTimeout()) {
+                    lispSbPlugin.getMapRegisterCache().removeEntry(entry.getKey());
                     return null;
                 } else if (Arrays.equals(mapRegisterCacheValue.getPacketData(), entry.getValue())) {
                     return mapRegisterCacheValue;
@@ -297,15 +280,6 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
         return null;
     }
 
-    private void sendNotificationIfPossible(final Notification notification) throws InterruptedException {
-        if (notificationPublishService != null) {
-            notificationPublishService.putNotification(notification);
-            LOG.trace("{} was published.", notification.getClass());
-        } else {
-            LOG.warn("Notification Provider is null!");
-        }
-    }
-
     private MappingKeepAlive createMappingKeepAlive(final MapRegisterCacheValue value) {
         MappingKeepAliveBuilder mappingKeepAliveBuilder = new MappingKeepAliveBuilder();
         mappingKeepAliveBuilder.setMapRegisterCacheMetadata(value.getMapRegisterCacheMetadata());
@@ -320,9 +294,9 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
         for (int i = 0; i < eidLispAddresses.size(); i++) {
             final Eid eid = eidLispAddresses.get(i).getEid();
             if (i == 0) {
-                firstAuthKey = akdb.getAuthenticationKey(eid);
+                firstAuthKey = lispSbPlugin.getAkdb().getAuthenticationKey(eid);
             } else {
-                final MappingAuthkey authKey = akdb.getAuthenticationKey(eid);
+                final MappingAuthkey authKey = lispSbPlugin.getAkdb().getAuthenticationKey(eid);
                 if (!Objects.equals(firstAuthKey, authKey)) {
                     return null;
                 }
@@ -389,13 +363,13 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
      *
      * <p>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
-     * the authentication key database (akdb).
+     * it just checks for remaining records (and its EID) whether they have the same authentication key stored in
+     * the authentication key database.
      *
      * @return Returns authentication key if all of EIDs have the same authentication key or null otherwise
      */
     private MappingAuthkey tryToAuthenticateMessage(final MapRegister mapRegister, final ByteBuffer byteBuffer) {
-        if (akdb == null) {
+        if (lispSbPlugin.getAkdb() == null) {
             LOG.debug("Simple map cache wasn't instantieted and set.");
             return null;
         }
@@ -406,13 +380,13 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
             final MappingRecordItem recordItem = mappingRecords.get(i);
             final MappingRecord mappingRecord = recordItem.getMappingRecord();
             if (i == 0) {
-                firstAuthKey = akdb.getAuthenticationKey(mappingRecord.getEid());
+                firstAuthKey = lispSbPlugin.getAkdb().getAuthenticationKey(mappingRecord.getEid());
                 if (!LispAuthenticationUtil.validate(mapRegister, byteBuffer, mappingRecord.getEid(), firstAuthKey)) {
                     return null;
                 }
             } else {
                 final Eid eid = mappingRecord.getEid();
-                final MappingAuthkey authKey = akdb.getAuthenticationKey(eid);
+                final MappingAuthkey authKey = lispSbPlugin.getAkdb().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));
@@ -434,12 +408,7 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
                     .getIpAddressBinaryFromInetAddress(sourceAddress));
             transportAddressBuilder.setPort(new PortNumber(port));
             gotMapNotifyBuilder.setTransportAddress(transportAddressBuilder.build());
-            if (notificationPublishService != null) {
-                notificationPublishService.putNotification(gotMapNotifyBuilder.build());
-                LOG.trace("MapNotify was published!");
-            } else {
-                LOG.warn("Notification Provider is null!");
-            }
+            lispSbPlugin.sendNotificationIfPossible(gotMapNotifyBuilder.build());
         } catch (RuntimeException re) {
             throw new LispMalformedPacketException("Couldn't deserialize Map-Notify (len="
                     + inBuffer.capacity() + ")", re);
@@ -459,12 +428,7 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
                     .getIpAddressBinaryFromInetAddress(sourceAddress));
             transportAddressBuilder.setPort(new PortNumber(port));
             gotMapReplyBuilder.setTransportAddress(transportAddressBuilder.build());
-            if (notificationPublishService != null) {
-                notificationPublishService.putNotification(gotMapReplyBuilder.build());
-                LOG.trace("MapReply was published!");
-            } else {
-                LOG.warn("Notification Provider is null!");
-            }
+            lispSbPlugin.sendNotificationIfPossible(gotMapReplyBuilder.build());
         } catch (RuntimeException re) {
             throw new LispMalformedPacketException("Couldn't deserialize Map-Reply (len="
                     + inBuffer.capacity() + ")", re);
@@ -474,11 +438,11 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
     }
 
     private void handleStats(int type) {
-        if (lispSbStats != null) {
+        if (lispSbPlugin.getStats() != null) {
             if (type <= ConcurrentLispSouthboundStats.MAX_LISP_TYPES) {
-                lispSbStats.incrementRx(type);
+                lispSbPlugin.getStats().incrementRx(type);
             } else {
-                lispSbStats.incrementRxUnknown();
+                lispSbPlugin.getStats().incrementRxUnknown();
             }
         }
     }
@@ -508,36 +472,4 @@ public class LispSouthboundHandler extends SimpleChannelInboundHandler<DatagramP
     @Override
     public void close() throws Exception {
     }
-
-    public void setAuthKeyDb(final AuthKeyDb smc) {
-        this.akdb = 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 setAuthenticationKeyDataListener(AuthenticationKeyDataListener authenticationKeyDataListener) {
-        this.authenticationKeyDataListener = authenticationKeyDataListener;
-    }
-
-    public void setStats(ConcurrentLispSouthboundStats lispSbStats) {
-        this.lispSbStats = lispSbStats;
-    }
-
-    public void setIsMaster(boolean isReadFromChannelEnabled) {
-        this.isReadFromChannelEnabled = isReadFromChannelEnabled;
-    }
-
-    public void setMapRegisterCacheTimeout(long mapRegisterCacheTimeout) {
-        this.mapRegisterCacheTimeout = mapRegisterCacheTimeout;
-    }
 }
index 66d6c94441f58730cfc07d484ae189d43e347792..96193fc25328ad1f6c882352830cd1515aeff147 100644 (file)
@@ -14,12 +14,12 @@ import io.netty.channel.SimpleChannelInboundHandler;
 import io.netty.channel.socket.DatagramPacket;
 import java.net.InetAddress;
 import java.nio.ByteBuffer;
-import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
 import org.opendaylight.lispflowmapping.lisp.serializer.MapReplySerializer;
 import org.opendaylight.lispflowmapping.lisp.serializer.MapRequestSerializer;
 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
 import org.opendaylight.lispflowmapping.lisp.util.ByteUtil;
 import org.opendaylight.lispflowmapping.lisp.util.MapRequestUtil;
+import org.opendaylight.lispflowmapping.southbound.LispSouthboundPlugin;
 import org.opendaylight.lispflowmapping.southbound.lisp.exception.LispMalformedPacketException;
 import org.opendaylight.lispflowmapping.southbound.util.LispNotificationHelper;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
@@ -34,11 +34,11 @@ import org.slf4j.LoggerFactory;
 
 public class LispXtrSouthboundHandler extends SimpleChannelInboundHandler<DatagramPacket>
         implements ILispSouthboundService {
-    private NotificationPublishService notificationPublishService;
+    private final LispSouthboundPlugin lispSbPlugin;
     protected static final Logger LOG = LoggerFactory.getLogger(LispXtrSouthboundHandler.class);
 
-    public void setNotificationProvider(NotificationPublishService nps) {
-        this.notificationPublishService = nps;
+    public LispXtrSouthboundHandler(LispSouthboundPlugin lispSbPlugin) {
+        this.lispSbPlugin = lispSbPlugin;
     }
 
     public void handlePacket(DatagramPacket packet) {
@@ -71,12 +71,7 @@ public class LispXtrSouthboundHandler extends SimpleChannelInboundHandler<Datagr
                     LispNotificationHelper.getIpAddressBinaryFromInetAddress(finalSourceAddress));
             transportAddressBuilder.setPort(new PortNumber(LispMessage.PORT_NUM));
             requestMappingBuilder.setTransportAddress(transportAddressBuilder.build());
-            if (notificationPublishService != null) {
-                notificationPublishService.putNotification(requestMappingBuilder.build());
-                LOG.trace("MapRequest was published!");
-            } else {
-                LOG.warn("Notification Provider is null!");
-            }
+            lispSbPlugin.sendNotificationIfPossible(requestMappingBuilder.build());
         } catch (RuntimeException re) {
             throw new LispMalformedPacketException(
                     "Couldn't deserialize Map-Request (len=" + inBuffer.capacity() + ")", re);
@@ -92,13 +87,7 @@ public class LispXtrSouthboundHandler extends SimpleChannelInboundHandler<Datagr
 
             XtrReplyMappingBuilder replyMappingBuilder = new XtrReplyMappingBuilder();
             replyMappingBuilder.setMapReply(LispNotificationHelper.convertMapReply(reply));
-
-            if (notificationPublishService != null) {
-                notificationPublishService.putNotification(replyMappingBuilder.build());
-                LOG.trace("MapReply was published!");
-            } else {
-                LOG.warn("Notification Provider is null!");
-            }
+            lispSbPlugin.sendNotificationIfPossible(replyMappingBuilder.build());
         } catch (RuntimeException re) {
             throw new LispMalformedPacketException(
                     "Couldn't deserialize Map-Reply (len=" + buffer.capacity() + ")", re);
index 6dff007db8be23e07be8a2f46ee2ffda09993c4d..2fad936c2fc04456e5e2824c68d73fdd12f6fcf1 100644 (file)
@@ -12,7 +12,6 @@ import static io.netty.buffer.Unpooled.wrappedBuffer;
 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.DATA1;
 import static org.opendaylight.lispflowmapping.southbound.lisp.MapRegisterCacheTestUtil.DATA2;
 import static org.opendaylight.lispflowmapping.southbound.lisp.MapRegisterCacheTestUtil.DATA3;
@@ -31,23 +30,21 @@ import java.util.Arrays;
 import java.util.List;
 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.ArgumentCaptor;
 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;
 import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
 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.AuthKeyDb;
 import org.opendaylight.lispflowmapping.southbound.ConcurrentLispSouthboundStats;
@@ -61,8 +58,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.typ
 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;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.AddMapping;
-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.MapRequest;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.RequestMapping;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
@@ -87,18 +82,14 @@ import org.opendaylight.yangtools.yang.binding.Notification;
 public class LispSouthboundServiceTest extends BaseTestCase {
 
     private LispSouthboundHandler testedLispService;
-    private NotificationPublishService nps;
     private byte[] mapRequestPacket;
     private byte[] mapRegisterPacket;
-    private ValueSaverAction<Notification> lispNotificationSaver;
-    // private ValueSaverAction<MapRegister> mapRegisterSaver;
-    // private ValueSaverAction<MapRequest> mapRequestSaver;
     private MapNotifyBuilder mapNotifyBuilder;
     private MapReplyBuilder mapReplyBuilder;
     private MappingRecordBuilder mappingRecordBuilder;
     private MapRegisterCache mapRegisterCache;
     private LispSouthboundPlugin mockLispSouthboundPlugin;
-    private ConcurrentLispSouthboundStats lispSouthboundStats;
+    private LispSouthboundPlugin contextMockLispSouthboundPlugin;
     private static final long CACHE_RECORD_TIMEOUT = 90000;
 
     private static AuthKeyDb akdb;
@@ -123,6 +114,7 @@ public class LispSouthboundServiceTest extends BaseTestCase {
         int LOCATOR = MapReplyIpv4SingleLocatorPos.LOCATOR + FIRST_LOCATOR_IPV4_LENGTH;
     }
 
+    @SuppressWarnings("unchecked")
     @BeforeClass
     public static void initTests() {
         akdb = Mockito.mock(AuthKeyDb.class);
@@ -152,24 +144,19 @@ public class LispSouthboundServiceTest extends BaseTestCase {
     @Before
     public void before() throws Exception {
         super.before();
-        // mapResolver = context.mock(IMapResolver.class);
-        // mapServer = context.mock(IMapServer.class);
-        mockLispSouthboundPlugin = mock(LispSouthboundPlugin.class);
+        mockLispSouthboundPlugin = Mockito.mock(LispSouthboundPlugin.class);
+        contextMockLispSouthboundPlugin = context.mock(LispSouthboundPlugin.class);
         Mockito.when(mockLispSouthboundPlugin.isMapRegisterCacheEnabled()).thenReturn(true);
-        testedLispService = new LispSouthboundHandler(mockLispSouthboundPlugin);
-        testedLispService.setMapRegisterCacheTimeout(90000);
+        Mockito.when(mockLispSouthboundPlugin.getMapRegisterCacheTimeout()).thenReturn(CACHE_RECORD_TIMEOUT);
         mapRegisterCache = new MapRegisterCache();
-        testedLispService.setMapRegisterCache(mapRegisterCache);
-        testedLispService.setDataBroker(Mockito.mock(DataBroker.class));
-        testedLispService.setAuthKeyDb(akdb);
-        testedLispService.setAuthenticationKeyDataListener(akdl);
-        nps = context.mock(NotificationPublishService.class);
-        testedLispService.setNotificationProvider(nps);
-        lispSouthboundStats = new ConcurrentLispSouthboundStats();
-        testedLispService.setStats(lispSouthboundStats);
-        lispNotificationSaver = new ValueSaverAction<Notification>();
-        // mapRegisterSaver = new ValueSaverAction<MapRegister>();
-        // mapRequestSaver = new ValueSaverAction<MapRequest>();
+        Mockito.when(mockLispSouthboundPlugin.getMapRegisterCache()).thenReturn(mapRegisterCache);
+        Mockito.when(mockLispSouthboundPlugin.getDataBroker()).thenReturn(Mockito.mock(DataBroker.class));
+        Mockito.when(mockLispSouthboundPlugin.getAkdb()).thenReturn(akdb);
+        Mockito.when(mockLispSouthboundPlugin.getAuthenticationKeyDataListener()).thenReturn(akdl);
+        ConcurrentLispSouthboundStats lispSouthboundStats = new ConcurrentLispSouthboundStats();
+        Mockito.when(mockLispSouthboundPlugin.getStats()).thenReturn(lispSouthboundStats);
+        testedLispService = new LispSouthboundHandler(mockLispSouthboundPlugin);
+
         // SRC: 127.0.0.1:58560 to 127.0.0.1:4342
         // LISP(Type = 8 - Encapsulated)
         // IP: 192.168.136.10 -> 1.2.3.4
@@ -272,17 +259,6 @@ public class LispSouthboundServiceTest extends BaseTestCase {
         handleMapRequestPacket(mapRequestPacket);
     }
 
-    private MapRegister lastMapRegister() {
-        assertTrue(lispNotificationSaver.lastValue instanceof AddMapping);
-        AddMapping lastValue = (AddMapping) lispNotificationSaver.lastValue;
-        return lastValue.getMapRegister();
-    }
-
-    private MapRequest lastMapRequest() {
-        RequestMapping lastValue = (RequestMapping) lispNotificationSaver.lastValue;
-        return lastValue.getMapRequest();
-    }
-
     @Test
     public void mapRegister__TwoRlocs() throws Exception {
         // P Bit & M Bit set
@@ -301,11 +277,11 @@ public class LispSouthboundServiceTest extends BaseTestCase {
                 + "0060   ff 00 00 05 00 01 0a 01 00 6e 06 64 ff 00 00 05 "
                 + "0070   00 01 c0 a8 88 33"));
 
-        oneOf(nps).putNotification(with(lispNotificationSaver));
-
+        ArgumentCaptor<AddMapping> captor = ArgumentCaptor.forClass(AddMapping.class);
         handleMapRegisterPacket(mapRegisterPacket);
+        Mockito.verify(mockLispSouthboundPlugin).sendNotificationIfPossible(captor.capture());
 
-        List<MappingRecordItem> eidRecords = lastMapRegister().getMappingRecordItem();
+        List<MappingRecordItem> eidRecords = captor.getValue().getMapRegister().getMappingRecordItem();
         assertEquals(1, eidRecords.size());
         MappingRecord eidRecord = eidRecords.get(0).getMappingRecord();
         assertEquals(2, eidRecord.getLocatorRecord().size());
@@ -337,11 +313,12 @@ public class LispSouthboundServiceTest extends BaseTestCase {
                 + "0060   01 92 00 00 00 00 00 00 00 01 01 64 ff 00 00 05 "
                 + "0070   00 01 0a 00 3a 9c"));
 
-        oneOf(nps).putNotification(with(lispNotificationSaver));
-
+        ArgumentCaptor<AddMapping> captor = ArgumentCaptor.forClass(AddMapping.class);
         handleMapRegisterPacket(mapRegisterPacket);
+        Mockito.verify(mockLispSouthboundPlugin).sendNotificationIfPossible(captor.capture());
+        MappingRecord eidToLocatorRecord =
+                captor.getValue().getMapRegister().getMappingRecordItem().get(0).getMappingRecord();
 
-        MappingRecord eidToLocatorRecord = lastMapRegister().getMappingRecordItem().get(0).getMappingRecord();
         assertEquals(LispAddressUtil.asIpv6PrefixBinaryEid("2610:d0:ffff:192:0:0:0:1/128"),
                 eidToLocatorRecord.getEid());
         assertEquals(Ipv6PrefixBinaryAfi.class, eidToLocatorRecord.getEid().getAddressType());
@@ -351,22 +328,20 @@ public class LispSouthboundServiceTest extends BaseTestCase {
 
     @Test
     public void mapRegister__VerifyBasicFields() throws Exception {
-        oneOf(nps).putNotification(with(lispNotificationSaver));
+        ArgumentCaptor<AddMapping> captor = ArgumentCaptor.forClass(AddMapping.class);
         handleMapRegisterPacket(mapRegisterPacket);
+        Mockito.verify(mockLispSouthboundPlugin).sendNotificationIfPossible(captor.capture());
+        MappingRecord eidToLocator =
+                captor.getValue().getMapRegister().getMappingRecordItem().get(0).getMappingRecord();
 
-        MappingRecord eidToLocator = lastMapRegister().getMappingRecordItem().get(0).getMappingRecord();
         assertEquals(LispAddressUtil.asIpv4PrefixBinaryEid("153.16.254.1/32"), eidToLocator.getEid());
-
         assertEquals(1, eidToLocator.getLocatorRecord().size());
         assertEquals(LispAddressUtil.asIpv4Rloc("192.168.136.10"), eidToLocator.getLocatorRecord().get(0).getRloc());
     }
 
     @Test
-    @Ignore
     public void mapRegister__NoResponseFromMapServerShouldReturnNullPacket() throws Exception {
-        oneOf(nps).putNotification(with(lispNotificationSaver));
         mapNotifyBuilder = null;
-
         assertNull(handleMapRegisterPacket(mapRegisterPacket));
     }
 
@@ -380,11 +355,11 @@ public class LispSouthboundServiceTest extends BaseTestCase {
                 + "0040   83 63 a7 79 6e f0 40 97 54 26 3a 44 b4 eb 00 00 "
                 + "0050   00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 "
                 + "0060   ff 00 00 05 00 01 c0 a8 88 0a"));
-        stubMapRegister(true);
 
+        ArgumentCaptor<AddMapping> captor = ArgumentCaptor.forClass(AddMapping.class);
         handleMapRegisterPacket(registerWithNonSetMBit);
-
-        assertFalse(lastMapRegister().isWantMapNotify());
+        Mockito.verify(mockLispSouthboundPlugin).sendNotificationIfPossible(captor.capture());
+        assertFalse(captor.getValue().getMapRegister().isWantMapNotify());
     }
 
     @Test
@@ -397,10 +372,11 @@ public class LispSouthboundServiceTest extends BaseTestCase {
                 + "0040   e7 20 25 3d e8 b2 07 e2 63 de 62 2b 7a 20 00 00 "
                 + "0050   00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 "
                 + "0060   ff 00 00 05 00 01 c0 a8 88 0a"));
-        stubMapRegister(true);
 
+        ArgumentCaptor<AddMapping> captor = ArgumentCaptor.forClass(AddMapping.class);
         handleMapRegisterPacket(registerWithNonSetMBit);
-        assertFalse(lastMapRegister().isWantMapNotify());
+        Mockito.verify(mockLispSouthboundPlugin).sendNotificationIfPossible(captor.capture());
+        assertFalse(captor.getValue().getMapRegister().isWantMapNotify());
     }
 
     @Test
@@ -413,10 +389,11 @@ public class LispSouthboundServiceTest extends BaseTestCase {
                 + "0040   4e 6b e2 e5 e1 01 40 8a c9 e1 d1 80 cb 72 00 00 "
                 + "0050   00 0a 01 20 10 00 00 00 00 01 99 10 fe 01 01 64 "
                 + "0060   ff 00 00 05 00 01 c0 a8 88 0a"));
-        stubMapRegister(true);
 
+        ArgumentCaptor<AddMapping> captor = ArgumentCaptor.forClass(AddMapping.class);
         handleMapRegisterPacket(registerWithNonSetMBit);
-        assertTrue(lastMapRegister().isWantMapNotify());
+        Mockito.verify(mockLispSouthboundPlugin).sendNotificationIfPossible(captor.capture());
+        assertTrue(captor.getValue().getMapRegister().isWantMapNotify());
     }
 
     /**
@@ -433,22 +410,23 @@ public class LispSouthboundServiceTest extends BaseTestCase {
             0x0a, 0x0a, 0x0a, 0x0a     //ipv4 address
         };
 
-        NotificationPublishService notifServiceMock = MapRegisterCacheTestUtil.resetMockForNotificationProvider(
-                testedLispService);
-
         //send stream of byte -> map register message
+        InOrder inOrder = Mockito.inOrder(mockLispSouthboundPlugin);
         final MapRegisterCacheKey cacheKey = MapRegisterCacheTestUtil.createMapRegisterCacheKey(eidPrefix);
         MapRegisterCacheTestUtil.beforeMapRegisterInvocationValidation(cacheKey, mapRegisterCache);
+        ArgumentCaptor<AddMapping> captor = ArgumentCaptor.forClass(AddMapping.class);
         mapRegisterInvocationForCacheTest(eidPrefixAfi, eidPrefix);
-        MapRegisterCacheTestUtil.afterMapRegisterInvocationValidation(notifServiceMock,
+        inOrder.verify(mockLispSouthboundPlugin).sendNotificationIfPossible(captor.capture());
+        MapRegisterCacheTestUtil.afterMapRegisterInvocationValidation(
                 cacheKey, mapRegisterCache, eidPrefixAfi, eidPrefix);
 
         //sending the same byte stream -> map register second time
-        notifServiceMock = MapRegisterCacheTestUtil.resetMockForNotificationProvider(testedLispService);
+        captor = ArgumentCaptor.forClass(AddMapping.class);
         mapRegisterInvocationForCacheTest(eidPrefixAfi, eidPrefix);
+        inOrder.verify(mockLispSouthboundPlugin).sendNotificationIfPossible(captor.capture());
 
         //mapping-keep-alive message should be generated
-        MapRegisterCacheTestUtil.afterSecondMapRegisterInvocationValidation(notifServiceMock,
+        MapRegisterCacheTestUtil.afterSecondMapRegisterInvocationValidation(
                 mockLispSouthboundPlugin, eidPrefixAfi, eidPrefix);
     }
 
@@ -573,12 +551,11 @@ public class LispSouthboundServiceTest extends BaseTestCase {
             InterruptedException {
         final MapRegisterCacheKey mapRegisterCacheKey = MapRegisterCacheTestUtil.createMapRegisterCacheKey(eidPrefix);
 
-        final NotificationPublishService mockedNotificationProvider = mock(NotificationPublishService.class);
-        testedLispService.setNotificationProvider(mockedNotificationProvider);
-
         MapRegisterCacheTestUtil.beforeMapRegisterInvocationValidation(mapRegisterCacheKey, mapRegisterCache);
+        ArgumentCaptor<AddMapping> captor = ArgumentCaptor.forClass(AddMapping.class);
         mapRegisterInvocationForCacheTest(eidPrefixAfi, eidPrefix, authenticationData);
-        MapRegisterCacheTestUtil.afterMapRegisterInvocationValidation(mockedNotificationProvider,
+        Mockito.verify(mockLispSouthboundPlugin).sendNotificationIfPossible(captor.capture());
+        MapRegisterCacheTestUtil.afterMapRegisterInvocationValidation(
                 mapRegisterCacheKey, mapRegisterCache, eidPrefixAfi, eidPrefix);
     }
 
@@ -603,8 +580,7 @@ public class LispSouthboundServiceTest extends BaseTestCase {
 
     private void cacheRecordExpirationTest(boolean cacheRecordTimeouted) throws InterruptedException {
         mapRegisterCache = Mockito.mock(MapRegisterCache.class);
-        testedLispService.setMapRegisterCache(mapRegisterCache);
-        testedLispService.setNotificationProvider(Mockito.mock(NotificationPublishService.class));
+        Mockito.when(mockLispSouthboundPlugin.getMapRegisterCache()).thenReturn(mapRegisterCache);
 
         final byte[] eidPrefixAfi = new byte[] {0x00, 0x01};
         final byte[] eidPrefix = new byte[] {0x0a, 0x0a, 0x0a, 0x0a};
@@ -632,6 +608,8 @@ public class LispSouthboundServiceTest extends BaseTestCase {
         Mockito.when(mapRegisterCache.refreshEntry(Mockito.eq(cacheKey))).thenReturn(cacheValue);
 
         mapRegisterInvocationForCacheTest(eidPrefixAfi, eidPrefix);
+        Mockito.verify(mockLispSouthboundPlugin, Mockito.atLeastOnce()).sendNotificationIfPossible(
+                Mockito.any(AddMapping.class));
 
         InOrder inOrder = Mockito.inOrder(mapRegisterCache);
         inOrder.verify(mapRegisterCache).getEntry(Mockito.eq(cacheKey));
@@ -650,7 +628,6 @@ public class LispSouthboundServiceTest extends BaseTestCase {
         byte[] extraDataPacket = new byte[mapRegisterPacket.length + 3];
         extraDataPacket[mapRegisterPacket.length] = 0x9;
         System.arraycopy(mapRegisterPacket, 0, extraDataPacket, 0, mapRegisterPacket.length);
-        stubMapRegister(true);
 
         DatagramPacket dp = new DatagramPacket(wrappedBuffer(extraDataPacket), new InetSocketAddress(0),
                 new InetSocketAddress(0));
@@ -687,8 +664,6 @@ public class LispSouthboundServiceTest extends BaseTestCase {
         byte registerType = mapRegisterPacket[0];
         assertEquals(MessageType.MapRegister.getIntValue(), registerType >> 4);
 
-        stubMapRegister(true);
-
         byte[] result = handleMapRegisterAsByteArray(mapRegisterPacket);
 
         assertEquals(mapRegisterPacket.length, result.length);
@@ -710,22 +685,21 @@ public class LispSouthboundServiceTest extends BaseTestCase {
     @Ignore
     @Test
     public void mapNotify__VerifyPort() throws Exception {
-        stubMapRegister(true);
-
         DatagramPacket notifyPacket = handleMapRegisterPacket(mapRegisterPacket);
         assertEquals(LispMessage.PORT_NUM, notifyPacket.recipient().getPort());
     }
 
     @Test
     public void mapRequest__VerifyBasicFields() throws Exception {
-        oneOf(nps).putNotification(with(lispNotificationSaver));
+        ArgumentCaptor<RequestMapping> captor = ArgumentCaptor.forClass(RequestMapping.class);
         handleMapRequestAsByteArray(mapRequestPacket);
-        List<EidItem> eids = lastMapRequest().getEidItem();
+        Mockito.verify(mockLispSouthboundPlugin).sendNotificationIfPossible(captor.capture());
+        List<EidItem> eids = captor.getValue().getMapRequest().getEidItem();
         assertEquals(1, eids.size());
         Eid lispAddress = eids.get(0).getEid();
         assertEquals(Ipv4PrefixBinaryAfi.class, lispAddress.getAddressType());
         assertEquals(LispAddressUtil.asIpv4PrefixBinaryEid("1.2.3.4/32"), lispAddress);
-        assertEquals(0x3d8d2acd39c8d608L, lastMapRequest().getNonce().longValue());
+        assertEquals(0x3d8d2acd39c8d608L, captor.getValue().getMapRequest().getNonce().longValue());
     }
 
     @Test
@@ -755,13 +729,13 @@ public class LispSouthboundServiceTest extends BaseTestCase {
                 + "00a0   00 02 26 10 00 d0 ff ff 01 92 00 00 00 00 00 00 "
                 + "00b0   00 01 01 64 ff 00 00 05 00 01 0a 00 3a 9c"));
 
-        oneOf(nps).putNotification(with(lispNotificationSaver));
-        // ret(mapReply);
-
+        ArgumentCaptor<RequestMapping> captor = ArgumentCaptor.forClass(RequestMapping.class);
         handleMapRequestAsByteArray(mapRequestPacket);
-        assertEquals(LispAddressUtil.asIpv6Eid("2610:d0:ffff:192:0:0:0:1"), lastMapRequest().getSourceEid().getEid());
+        Mockito.verify(mockLispSouthboundPlugin).sendNotificationIfPossible(captor.capture());
+        assertEquals(LispAddressUtil.asIpv6Eid("2610:d0:ffff:192:0:0:0:1"),
+                captor.getValue().getMapRequest().getSourceEid().getEid());
         assertEquals(LispAddressUtil.asIpv6PrefixBinaryEid("2610:d0:ffff:192:0:0:0:2/128"),
-                lastMapRequest().getEidItem().get(0).getEid());
+                captor.getValue().getMapRequest().getEidItem().get(0).getEid());
     }
 
     @Ignore
@@ -785,10 +759,10 @@ public class LispSouthboundServiceTest extends BaseTestCase {
                 + "0090   00 00 00 00 00 02 00 00 00 0a 01 80 10 00 00 00 "
                 + "00a0   00 02 26 10 00 d0 ff ff 01 92 00 00 00 00 00 00 "
                 + "00b0   00 01 01 64 ff 00 00 05 00 01 0a 00 3a 9c"));
-        oneOf(nps).putNotification(with(lispNotificationSaver));
-        // ret(mapReply);
 
+        ArgumentCaptor<RequestMapping> captor = ArgumentCaptor.forClass(RequestMapping.class);
         DatagramPacket replyPacket = handleMapRequestPacket(mapRequestPacket);
+        Mockito.verify(mockLispSouthboundPlugin).sendNotificationIfPossible(Mockito.any());
         assertEquals(4342, replyPacket.recipient().getPort());
     }
 
@@ -808,11 +782,10 @@ public class LispSouthboundServiceTest extends BaseTestCase {
                 + "0070   10 00 00 00 00 01 99 10 fe 01 01 64 ff 00 00 05 "
                 + "0080   00 01 0a 00 01 26"));
 
-        oneOf(nps).putNotification(with(lispNotificationSaver));
-        // ret(mapReply);
-
+        ArgumentCaptor<RequestMapping> captor = ArgumentCaptor.forClass(RequestMapping.class);
         handleMapRequestAsByteArray(mapRequestPacket);
-        assertEquals(Ipv4BinaryAfi.class, lastMapRequest().getSourceEid().getEid().getAddressType());
+        Mockito.verify(mockLispSouthboundPlugin).sendNotificationIfPossible(captor.capture());
+        assertEquals(Ipv4BinaryAfi.class, captor.getValue().getMapRequest().getSourceEid().getEid().getAddressType());
 
     }
 
@@ -843,7 +816,7 @@ public class LispSouthboundServiceTest extends BaseTestCase {
 
     @Test
     @Ignore
-    public void mapReply__VerifyBasicIPv6() throws Exception {
+    public void mapReply_q_VerifyBasicIPv6() throws Exception {
         mappingRecordBuilder.setEid(LispAddressUtil.asIpv6PrefixEid("0:0:0:0:0:0:0:1/128"));
 
         stubHandleRequest();
@@ -987,32 +960,13 @@ public class LispSouthboundServiceTest extends BaseTestCase {
                 + "0080   01 64 ff 00 00 05 00 01 0a 01 00 6f 06 64 ff 00 "
                 + "0090   00 05 00 01 c0 a8 88 33"));
 
-        oneOf(nps).putNotification(with(lispNotificationSaver));
         handleMapRequestAsByteArray(mapRequestPacket);
-
-    }
-
-    private void stubMapRegister(final boolean setNotifyFromRegister) {
-        try {
-            allowing(nps).putNotification(with(lispNotificationSaver));
-        } catch (InterruptedException e) {
-            LOG.debug("Interrupted", e);
-        }
-        will(new SimpleAction() {
-
-            @Override
-            public Object invoke(Invocation invocation) throws Throwable {
-                if (setNotifyFromRegister) {
-                    MapNotifyBuilderHelper.setFromMapRegister(mapNotifyBuilder, lastMapRegister());
-                }
-                return null;
-            }
-        });
+        Mockito.verify(mockLispSouthboundPlugin).sendNotificationIfPossible(Mockito.any(RequestMapping.class));
     }
 
     private void stubHandleRequest() {
         try {
-            allowing(nps).putNotification(wany(Notification.class));
+            allowing(contextMockLispSouthboundPlugin).sendNotificationIfPossible(wany(Notification.class));
         } catch (InterruptedException e) {
             LOG.debug("Interrupted", e);
         }
@@ -1088,39 +1042,4 @@ public class LispSouthboundServiceTest extends BaseTestCase {
                 + "00 20 00 01 01 02 03 04").array();
         handleMapRequestPacket(mapRequestPacket);
     }
-
-    // @Ignore
-    // @Test
-    // public void mapRequest__IPITRRLOCIsSecond() throws Exception {
-    // mapRequestPacket = hexToByteBuffer("10 00 " //
-    // + "01 " // This means 3 ITR - RLOCs
-    // + "01 3d 8d 2a cd 39 c8 d6 08 00 00 " //
-    // + "40 05 c0 a8 88 0a 01 02 " // MAC (ITR-RLOC #1 of 2)
-    // + "00 01 01 02 03 04 " // IP (ITR-RLOC #2 of 2)
-    // + "00 20 00 01 01 02 03 04").array();
-    // oneOf(nps).putNotification(with(lispNotificationSaver));
-    // // ret(mapReply);
-    // DatagramPacket packet = handleMapRequestPacket(mapRequestPacket);
-    // assertEquals(2, lastMapRequest().getItrRlocs().size());
-    // assertEquals((new LispIpv4Address("1.2.3.4")).getAddress(),
-    // packet.getAddress());
-    // }
-    //
-    // @Ignore
-    // @Test
-    // public void mapRequest__MULTIPLEIPITRRLOCs() throws Exception {
-    // mapRequestPacket = hexToByteBuffer("10 00 " //
-    // + "01 " // This means 3 ITR - RLOCs
-    // + "01 3d 8d 2a cd 39 c8 d6 08 00 00 " //
-    // + "00 01 01 02 03 04 " // IP (ITR-RLOC #1 of 2)
-    // + "00 01 c0 a8 88 0a " // MAC (ITR-RLOC #2 of 2)
-    // + "00 20 00 01 01 02 03 04").array();
-    // oneOf(nps).putNotification(with(lispNotificationSaver));
-    // // ret(mapReply);
-    // DatagramPacket packet = handleMapRequestPacket(mapRequestPacket);
-    // assertEquals(2, lastMapRequest().getItrRloc().size());
-    // assertEquals((new LispIpv4Address("1.2.3.4")).getAddress(),
-    // packet.getAddress());
-    // }
-
 }
index e650c0302144d5acae0c67f5fe25e24af4e48a1a..00ed431bc342279a37c5dc4b98987949713b2c1a 100644 (file)
@@ -18,15 +18,14 @@ import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.nio.ByteBuffer;
 import java.util.Arrays;
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.ArgumentCaptor;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.runners.MockitoJUnitRunner;
-import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
+import org.opendaylight.lispflowmapping.southbound.LispSouthboundPlugin;
 import org.opendaylight.lispflowmapping.southbound.lisp.exception.LispMalformedPacketException;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.Ipv4AddressBinary;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv4BinaryAfi;
@@ -45,9 +44,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rl
 @RunWith(MockitoJUnitRunner.class)
 public class LispXtrSouthboundHandlerTest {
 
-    @Mock(name = "notificationPublishService") private static NotificationPublishService notificationPublishServiceMock;
-    @InjectMocks private static LispXtrSouthboundHandler handler;
-
     private static final String IPV4_STRING_1 =      "1.2.3.4";
     private static final String IPV4_STRING_2 =      "127.0.0.1";
     private static final String IPV4_STRING_PREFIX = "/32";
@@ -115,6 +111,15 @@ public class LispXtrSouthboundHandlerTest {
             + "0060   00 01 01 02 03 04 00 00 00 00 00 00 00 01 fe fe "
             + "0070   fe fe 0d e3 70 40";
 
+    private LispSouthboundPlugin lispSbPluginMock;
+    private LispXtrSouthboundHandler handler;
+
+    @Before
+    public void initTest() {
+        lispSbPluginMock = Mockito.mock(LispSouthboundPlugin.class);
+        handler = new LispXtrSouthboundHandler(lispSbPluginMock);
+    }
+
     /**
      * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Request.
      */
@@ -127,22 +132,11 @@ public class LispXtrSouthboundHandlerTest {
 
         handler.handlePacket(extractLispPacket(MAP_REQUEST_PACKET_STRING, HEADER_LENGTH,
                 LISP_MAP_REQUEST_PACKET_LENGTH));
-        Mockito.verify(notificationPublishServiceMock).putNotification(captor.capture());
+        Mockito.verify(lispSbPluginMock).sendNotificationIfPossible(captor.capture());
 
         assertEquals(expectedRequest, captor.getValue().getMapRequest());
     }
 
-    /**
-     * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Request, null NotificationPublishService.
-     */
-    @Test
-    public void handlePacketTest_withMapRequest_withNullNotifPublishService() throws InterruptedException {
-        final LispXtrSouthboundHandler handler = new LispXtrSouthboundHandler();
-        handler.handlePacket(extractLispPacket(MAP_REQUEST_PACKET_STRING, HEADER_LENGTH,
-                LISP_MAP_REQUEST_PACKET_LENGTH));
-        Mockito.verifyZeroInteractions(notificationPublishServiceMock);
-    }
-
     /**
      * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Request, no Itr Rlocs.
      */
@@ -161,22 +155,10 @@ public class LispXtrSouthboundHandlerTest {
         handler.handlePacket(extractLispPacket(MAP_REPLY_PACKET_STRING, HEADER_LENGTH,
                 LISP_MAP_REPLY_PACKET_LENGTH));
 
-        Mockito.verify(notificationPublishServiceMock).putNotification(captor.capture());
+        Mockito.verify(lispSbPluginMock).sendNotificationIfPossible(captor.capture());
         assertNotNull(captor.getValue().getMapReply());
     }
 
-    /**
-     * Tests {@link LispXtrSouthboundHandler#handlePacket} method with Map-Reply over channelRead0 method.
-     */
-    @Test
-    public void handlePacketTest_withMapReply_withNullNotifPublishService() throws Exception {
-        final LispXtrSouthboundHandler handler = new LispXtrSouthboundHandler();
-        handler.channelRead0(Mockito.mock(ChannelHandlerContext.class),
-                extractLispPacket(MAP_REPLY_PACKET_STRING, HEADER_LENGTH, LISP_MAP_REPLY_PACKET_LENGTH));
-
-        Mockito.verifyZeroInteractions(notificationPublishServiceMock);
-    }
-
     /**
      * Tests {@link LispXtrSouthboundHandler#channelReadComplete} method.
      */
@@ -206,7 +188,6 @@ public class LispXtrSouthboundHandlerTest {
                 + "0070   fe fe 0d e3 70 40";
 
         handler.exceptionCaught(Mockito.mock(ChannelHandlerContext.class), Mockito.mock(Throwable.class));
-        handler.setNotificationProvider(notificationPublishServiceMock);
         handler.handlePacket(extractLispPacket(mapNotificationPacket, HEADER_LENGTH, LISP_MAP_REPLY_PACKET_LENGTH));
     }
 
@@ -252,4 +233,4 @@ public class LispXtrSouthboundHandlerTest {
                 .setSmr(false)
                 .setSmrInvoked(false);
     }
-}
+}
\ No newline at end of file
index 4f31b7a4b1d9d272795fd02a06f241ec9ed9baee..966441400a942ae6041f720173596b66355c5c0a 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.lispflowmapping.southbound.lisp;
 
-import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
 
 import java.net.Inet4Address;
@@ -16,11 +15,8 @@ import java.nio.ByteBuffer;
 import org.apache.commons.lang3.ArrayUtils;
 import org.junit.Assert;
 import org.mockito.Mockito;
-import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
 import org.opendaylight.lispflowmapping.southbound.LispSouthboundPlugin;
 import org.opendaylight.lispflowmapping.southbound.lisp.cache.MapRegisterCache;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.AddMapping;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MappingKeepAlive;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MessageType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.key.container.MapRegisterCacheKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.map.register.cache.key.container.MapRegisterCacheKeyBuilder;
@@ -86,19 +82,9 @@ final class MapRegisterCacheTestUtil {
         return result;
     }
 
-    static NotificationPublishService resetMockForNotificationProvider(final LispSouthboundHandler testedLispService) {
-        NotificationPublishService mockedNotificationProvider = mock(NotificationPublishService.class);
-        testedLispService.setNotificationProvider(mockedNotificationProvider);
-        return mockedNotificationProvider;
-    }
-
-    static void afterSecondMapRegisterInvocationValidation(final NotificationPublishService
-                                                                    mockedNotificationProvider,
-                                                            final LispSouthboundPlugin mockLispSouthboundPlugin,
+    static void afterSecondMapRegisterInvocationValidation(final LispSouthboundPlugin mockLispSouthboundPlugin,
                                                             byte[] eidPrefixAfi, byte[] eidPrefix) throws
             InterruptedException, UnknownHostException {
-        verify(mockedNotificationProvider).putNotification(Mockito.any(MappingKeepAlive.class));
-
         final byte[] resetForMapNotify = new byte[]{
             0x4C, 0x00, 0x00, 0x01
         };
@@ -123,11 +109,9 @@ final class MapRegisterCacheTestUtil {
                 Mockito.eq(null));
     }
 
-    static void afterMapRegisterInvocationValidation(final NotificationPublishService mockedNotificationProvider, final
-        MapRegisterCacheKey mapRegisterCacheKey, final MapRegisterCache mapRegisterCache, final byte[] eidPrefixAfi,
-                                                     byte[] eidPrefix) throws InterruptedException {
-        Mockito.verify(mockedNotificationProvider).putNotification(Mockito.any(AddMapping.class));
-
+    static void afterMapRegisterInvocationValidation(final MapRegisterCacheKey mapRegisterCacheKey,
+            final MapRegisterCache mapRegisterCache, final byte[] eidPrefixAfi, byte[] eidPrefix)
+                    throws InterruptedException {
         Assert.assertEquals(1, mapRegisterCache.cacheSize());
         final MapRegisterCacheValue currentMapRegisterCacheValue = mapRegisterCache.getEntry(mapRegisterCacheKey);
         Assert.assertNotNull(currentMapRegisterCacheValue);