Update DAO API
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / LispMappingService.java
index 5befc148690cdf7d0e6e92c126939f47f4c14211..cd4bfebb3349bdc8e61a3ad6798d74a3de1d7604 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013 Contextream, Inc. and others.  All rights reserved.
+ * Copyright (c) 2014 Contextream, Inc. and others.  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,
 
 package org.opendaylight.lispflowmapping.implementation;
 
-import java.net.InetAddress;
-import java.net.UnknownHostException;
+import java.util.List;
 
 import org.apache.commons.lang3.tuple.MutablePair;
 import org.apache.commons.lang3.tuple.Pair;
-import org.eclipse.osgi.framework.console.CommandInterpreter;
-import org.eclipse.osgi.framework.console.CommandProvider;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
-import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
-import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
+import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
 import org.opendaylight.controller.sal.binding.api.NotificationListener;
 import org.opendaylight.controller.sal.binding.api.NotificationService;
-import org.opendaylight.lispflowmapping.implementation.dao.InMemoryDAO;
-import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKey;
-import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceNoMaskKey;
+import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
+import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
-import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
+import org.opendaylight.lispflowmapping.implementation.mdsal.AuthenticationKeyDataListener;
+import org.opendaylight.lispflowmapping.implementation.mdsal.DataStoreBackEnd;
+import org.opendaylight.lispflowmapping.implementation.mdsal.MappingDataListener;
 import org.opendaylight.lispflowmapping.implementation.util.LispNotificationHelper;
+import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
-import org.opendaylight.lispflowmapping.interfaces.dao.ILispTypeConverter;
-import org.opendaylight.lispflowmapping.interfaces.dao.IQueryAll;
 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
+import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
+import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMappingShell;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
-import org.opendaylight.lispflowmapping.type.sbplugin.ILispSouthboundPlugin;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.AddMapping;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapNotify;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRegister;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapReply;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRequest;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.RequestMapping;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainer;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainerBuilder;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.Address;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Ipv4Builder;
+import org.opendaylight.lispflowmapping.lisp.util.LispAFIConvertor;
+import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
+import org.opendaylight.lispflowmapping.lisp.util.MapServerMapResolverUtil;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.LispProtoService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.AddMapping;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.MapNotify;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.MapRegister;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.MapReply;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.MapRequest;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.RequestMapping;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.SendMapNotifyInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.SendMapReplyInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.SendMapRequestInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.lispaddress.LispAddressContainer;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.lispaddress.lispaddresscontainer.address.ipv4.Ipv4AddressBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.mapnotifymessage.MapNotifyBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.mapreplymessage.MapReplyBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.maprequestmessage.MapRequestBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.transportaddress.TransportAddress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.transportaddress.TransportAddressBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.MappingserviceService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.MappingOrigin;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.db.instance.AuthenticationKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150820.db.instance.Mapping;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.FrameworkUtil;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
+import org.opendaylight.yangtools.yang.binding.Notification;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.net.InetAddresses;
-
-public class LispMappingService implements CommandProvider, IFlowMapping, BindingAwareConsumer, //
-        IMapRequestResultHandler, IMapNotifyHandler {
-    protected static final Logger logger = LoggerFactory.getLogger(LispMappingService.class);
+public class LispMappingService implements IFlowMapping, IFlowMappingShell, BindingAwareProvider,
+        IMapRequestResultHandler, IMapNotifyHandler, AutoCloseable {
+    protected static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
 
+    private static final ConfigIni configIni = new ConfigIni();
+    private AuthenticationKeyDataListener keyListener;
+    private MappingDataListener mappingListener;
     private ILispDAO lispDao = null;
     private IMapResolverAsync mapResolver;
     private IMapServerAsync mapServer;
     private volatile boolean shouldIterateMask;
     private volatile boolean shouldAuthenticate;
+    private volatile boolean smr = configIni.smrIsSet();
     private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
     private ThreadLocal<MapNotify> tlsMapNotify = new ThreadLocal<MapNotify>();
-    private ThreadLocal<Pair<MapRequest, InetAddress>> tlsMapRequest = new ThreadLocal<Pair<MapRequest, InetAddress>>();
+    private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest = new ThreadLocal<Pair<MapRequest, TransportAddress>>();
 
-    private ILispSouthboundPlugin lispSB = null;
+    private LispProtoService lispSB = null;
+    private ProviderContext session;
 
-    private ConsumerContext session;
+    private DataStoreBackEnd dsbe;
+    private NotificationService notificationService;
+    private static LispMappingService lfmService = null;
+    private BindingAwareBroker.RpcRegistration<MappingserviceService> lfmDbRpc;
+    private DataBroker dataBrokerService;
+    private RpcProviderRegistry rpcRegistry;
+    private BindingAwareBroker broker;
 
-    public static void main(String[] args) throws Exception {
-        LispMappingService serv = new LispMappingService();
-        serv.setLispDao(new InMemoryDAO());
-        serv.init();
+    public LispMappingService() {
+        LOG.debug("LispMappingService Module starting!");
+        lfmService = this;
     }
 
-    class LispIpv4AddressInMemoryConverter implements ILispTypeConverter<Ipv4Address, Integer> {
+    public void setDataBrokerService(DataBroker dataBrokerService) {
+        this.dataBrokerService = dataBrokerService;
     }
 
-    class LispIpv6AddressInMemoryConverter implements ILispTypeConverter<Ipv6Address, Integer> {
+    public void setRpcProviderRegistry(RpcProviderRegistry rpcRegistry) {
+        this.rpcRegistry = rpcRegistry;
     }
 
-    class MappingServiceKeyConvertor implements ILispTypeConverter<MappingServiceKey, Integer> {
+    public void setBindingAwareBroker(BindingAwareBroker broker) {
+        this.broker = broker;
     }
 
-    class MappingServiceNoMaskKeyConvertor implements ILispTypeConverter<MappingServiceNoMaskKey, Integer> {
+    public void initialize() {
+        broker.registerProvider(this);
+
+        LfmMappingDatabaseRpc mappingDbProviderRpc = new LfmMappingDatabaseRpc(dataBrokerService);
+        lfmDbRpc = rpcRegistry.addRpcImplementation(MappingserviceService.class, mappingDbProviderRpc);
+        dsbe = new DataStoreBackEnd(dataBrokerService);
+        restoreDaoFromDatastore();
     }
 
-    void setBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
-        logger.debug("BindingAwareBroker set!");
-        BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
-        bindingAwareBroker.registerConsumer(this, bundleContext);
+    @Override
+    public void onSessionInitiated(ProviderContext session) {
+        LOG.info("Lisp Consumer session initialized!");
+        notificationService = session.getSALService(NotificationService.class);
+        registerNotificationListener(AddMapping.class, new MapRegisterNotificationHandler());
+        registerNotificationListener(RequestMapping.class, new MapRequestNotificationHandler());
+        registerDataListeners(session.getSALService(DataBroker.class));
+        this.session = session;
+        LOG.info("LISP (RFC6830) Mapping Service init finished");
     }
 
-    void unsetBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
-        logger.debug("BindingAwareBroker was unset in LispMappingService");
+    public static LispMappingService getLispMappingService() {
+        return lfmService;
     }
 
     public void basicInit(ILispDAO dao) {
@@ -107,95 +141,115 @@ public class LispMappingService implements CommandProvider, IFlowMapping, Bindin
         mapServer = new MapServer(dao);
     }
 
-    void setLispDao(ILispDAO dao) {
-        logger.debug("LispDAO set in LispMappingService");
+    public void setLispDao(ILispDAO dao) {
+        LOG.trace("LispDAO set in LispMappingService");
         basicInit(dao);
-        logger.debug("Registering LispIpv4Address");
-        lispDao.register(LispIpv4AddressInMemoryConverter.class);
-        logger.debug("Registering LispIpv6Address");
-        lispDao.register(LispIpv6AddressInMemoryConverter.class);
-        logger.debug("Registering MappingServiceKey");
-        lispDao.register(MappingServiceKeyConvertor.class);
-        logger.debug("Registering MappingServiceNoMaskKey");
-        lispDao.register(MappingServiceNoMaskKeyConvertor.class);
     }
 
     void unsetLispDao(ILispDAO dao) {
-        logger.debug("LispDAO was unset in LispMappingService");
+        LOG.trace("LispDAO was unset in LispMappingService");
         mapServer = null;
         mapResolver = null;
         lispDao = null;
     }
 
-    public void init() {
-        try {
-            registerWithOSGIConsole();
-            logger.info("LISP (RFC6830) Mapping Service init finished");
-        } catch (Throwable t) {
-            t.printStackTrace();
+    private void restoreDaoFromDatastore() {
+        List<Mapping> mappings = dsbe.getAllMappings();
+        List<AuthenticationKey> authKeys = dsbe.getAllAuthenticationKeys();
+
+        LOG.info("Restoring {} mappings and {} keys from datastore into DAO", mappings.size(), authKeys.size());
+
+        // restore southbound registered entries first ...
+        for (Mapping mapping : mappings) {
+            if (mapping.getOrigin() == MappingOrigin.Southbound) {
+                MapRegister register = MapServerMapResolverUtil.getMapRegister(mapping);
+                handleMapRegister(register, false);
+            }
         }
-    }
 
-    private void registerWithOSGIConsole() {
-        BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
-        bundleContext.registerService(CommandProvider.class.getName(), this, null);
+        // because northbound registrations have priority
+        for (Mapping mapping : mappings) {
+            if (mapping.getOrigin() == MappingOrigin.Northbound) {
+                MapRegister register = MapServerMapResolverUtil.getMapRegister(mapping);
+                handleMapRegister(register, false);
+            }
+        }
+
+        for (AuthenticationKey authKey : authKeys) {
+            addAuthenticationKey(authKey.getLispAddressContainer(), authKey.getAuthkey());
+        }
     }
 
     public void destroy() {
-        logger.debug("LISP (RFC6830) Mapping Service is destroyed!");
+        LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
         mapResolver = null;
         mapServer = null;
+        closeDataListeners();
     }
 
-    public void _removeEid(final CommandInterpreter ci) {
-        lispDao.remove(LispAFIConvertor.asIPAfiAddress(ci.nextArgument()));
-    }
-
-    public void _dumpAll(final CommandInterpreter ci) {
-        ci.println("EID\tRLOCs");
-        if (lispDao instanceof IQueryAll) {
-            ((IQueryAll) lispDao).getAll(new IRowVisitor() {
-                String lastKey = "";
+    public String printMappings() {
+        final StringBuffer sb = new StringBuffer();
+        sb.append("EID\tRLOCs\n");
+        final IRowVisitor innerVisitor = (new IRowVisitor() {
+            String lastKey = "";
 
-                public void visitRow(Class<?> keyType, Object keyId, String valueKey, Object value) {
-                    String key = keyType.getSimpleName() + "#" + keyId;
-                    if (!lastKey.equals(key)) {
-                        ci.println();
-                        ci.print(key + "\t");
-                    }
-                    ci.print(valueKey + "=" + value + "\t");
-                    lastKey = key;
+            public void visitRow(Object keyId, String valueKey, Object value) {
+                String key = keyId.getClass().getSimpleName() + "#" + keyId;
+                if (!lastKey.equals(key)) {
+                    sb.append(key + "\t");
                 }
-            });
-            ci.println();
-        } else {
-            ci.println("Not implemented by this DAO");
-        }
-        return;
+                if (!(valueKey.equals(SubKeys.LCAF_SRCDST))) {
+                    sb.append(valueKey + "=" + value + "\t");
+                }
+                lastKey = key;
+            }
+        });
+        lispDao.getAll(new IRowVisitor() {
+            String lastKey = "";
+
+            public void visitRow(Object keyId, String valueKey, Object value) {
+                String key = keyId.getClass().getSimpleName() + "#" + keyId;
+                if (!lastKey.equals(key)) {
+                    sb.append("\n" + key + "\t");
+                }
+                if (valueKey.equals(SubKeys.LCAF_SRCDST)) {
+                    sb.append(valueKey + "= { ");
+                    ((ILispDAO)value).getAll(innerVisitor);
+                    sb.append("}\t");
+                } else {
+                    sb.append(valueKey + "=" + value + "\t");
+                }
+                lastKey = key;
+            }
+        });
+        sb.append("\n");
+        return sb.toString();
     }
 
-    public void _addDefaultPassword(final CommandInterpreter ci) {
-        LispAddressContainerBuilder builder = new LispAddressContainerBuilder();
-        builder.setAddress((Address) (new Ipv4Builder().setIpv4Address(new Ipv4Address("0.0.0.0")).build()));
-        addAuthenticationKey(builder.build(), 0, "password");
+    public void addDefaultKeyIPv4() {
+        LispAddressContainer address = LispAFIConvertor.toContainer(
+                new Ipv4AddressBuilder().setIpv4Address(new Ipv4Address("0.0.0.0")).setMask((short)0).build());
+        addAuthenticationKey(address, "password");
     }
 
-    public String getHelp() {
-        StringBuffer help = new StringBuffer();
-        help.append("---LISP Mapping Service---\n");
-        help.append("\t dumpAll        - Dump all current EID -> RLOC mapping\n");
-        help.append("\t removeEid      - Remove a single LispIPv4Address Eid\n");
-        return help.toString();
+    public MapReply handleMapRequest(MapRequest request) {
+        return handleMapRequest(request, smr);
     }
 
-    public MapReply handleMapRequest(MapRequest request) {
+    public MapReply handleMapRequest(MapRequest request, boolean smr) {
+        LOG.debug("DAO: Retrieving mapping for {}",
+                LispAddressStringifier.getString(request.getEidRecord().get(0).getLispAddressContainer()));
+
         tlsMapReply.set(null);
         tlsMapRequest.set(null);
-        mapResolver.handleMapRequest(request, this);
-        // After this invocation we assume that the thread local is filled with
-        // the reply
+        mapResolver.handleMapRequest(request, smr, this);
+        // After this invocation we assume that the thread local is filled with the reply
         if (tlsMapRequest.get() != null) {
-            getLispSB().handleMapRequest(tlsMapRequest.get().getLeft(), tlsMapRequest.get().getRight());
+            SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
+            new MapRequestBuilder(tlsMapRequest.get().getLeft());
+            smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
+            smrib.setTransportAddress(tlsMapRequest.get().getRight());
+            getLispSB().sendMapRequest(smrib.build());
             return null;
         } else {
             return tlsMapReply.get();
@@ -204,29 +258,52 @@ public class LispMappingService implements CommandProvider, IFlowMapping, Bindin
     }
 
     public MapNotify handleMapRegister(MapRegister mapRegister) {
+        return handleMapRegister(mapRegister, smr);
+    }
+
+    public MapNotify handleMapRegister(MapRegister mapRegister, boolean smr) {
+        LOG.debug("DAO: Adding mapping for {}",
+                LispAddressStringifier.getString(mapRegister.getEidToLocatorRecord().get(0).getLispAddressContainer()));
+
         tlsMapNotify.set(null);
-        mapServer.handleMapRegister(mapRegister, this);
-        // After this invocation we assume that the thread local is filled with
-        // the reply
+        mapServer.handleMapRegister(mapRegister, smr, this);
+        // After this invocation we assume that the thread local is filled with the reply
         return tlsMapNotify.get();
     }
 
-    public String getAuthenticationKey(LispAddressContainer address, int maskLen) {
-        return mapServer.getAuthenticationKey(address, maskLen);
+    public String getAuthenticationKey(LispAddressContainer address) {
+        LOG.debug("DAO: Retrieving authentication key for {}", LispAddressStringifier.getString(address));
+        return mapServer.getAuthenticationKey(address);
     }
 
-    public boolean removeAuthenticationKey(LispAddressContainer address, int maskLen) {
-        return mapServer.removeAuthenticationKey(address, maskLen);
+    public void removeAuthenticationKey(LispAddressContainer address) {
+        LOG.debug("DAO: Removing authentication key for {}", LispAddressStringifier.getString(address));
+        mapServer.removeAuthenticationKey(address);
     }
 
-    public boolean addAuthenticationKey(LispAddressContainer address, int maskLen, String key) {
-        return mapServer.addAuthenticationKey(address, maskLen, key);
+    public void addAuthenticationKey(LispAddressContainer address, String key) {
+        LOG.debug("DAO: Adding authentication key '{}' for {}", key,
+                LispAddressStringifier.getString(address));
+        mapServer.addAuthenticationKey(address, key);
+    }
+
+    public void removeMapping(LispAddressContainer address) {
+        LOG.debug("DAO: Removing mapping for {}", LispAddressStringifier.getString(address));
+        mapServer.removeMapping(address, smr, this);
     }
 
     public boolean shouldIterateMask() {
         return this.shouldIterateMask;
     }
 
+    public boolean shouldUseSmr() {
+        return this.smr;
+    }
+
+    public void setShouldUseSmr(boolean smr) {
+        this.smr = smr;
+    }
+
     public void setShouldIterateMask(boolean shouldIterateMask) {
         this.shouldIterateMask = shouldIterateMask;
         this.mapResolver.setShouldIterateMask(shouldIterateMask);
@@ -243,23 +320,45 @@ public class LispMappingService implements CommandProvider, IFlowMapping, Bindin
         return shouldAuthenticate;
     }
 
-    public void onSessionInitialized(ConsumerContext session) {
-        logger.debug("Lisp Consumer session initialized!");
-        NotificationService notificationService = session.getSALService(NotificationService.class);
-        // notificationService.registerNotificationListener(LispNotification.class,
-        // this);
-        notificationService.registerNotificationListener(AddMapping.class, new MapRegisterNotificationHandler());
-        notificationService.registerNotificationListener(RequestMapping.class, new MapRequestNotificationHandler());
-        this.session = session;
+    private void registerDataListeners(DataBroker broker) {
+        keyListener = new AuthenticationKeyDataListener(broker, this);
+        mappingListener = new MappingDataListener(broker, this);
+    }
+
+    private void closeDataListeners() {
+        keyListener.closeDataChangeListener();
+        mappingListener.closeDataChangeListener();
+    }
+
+    public <T extends Notification> void registerNotificationListener(Class<T> notificationType, NotificationListener<T> listener) {
+        notificationService.registerNotificationListener(notificationType, listener);
     }
 
     private class MapRegisterNotificationHandler implements NotificationListener<AddMapping> {
 
         @Override
         public void onNotification(AddMapping mapRegisterNotification) {
-            MapNotify mapNotify = handleMapRegister(mapRegisterNotification.getMapRegister());
-            getLispSB().handleMapNotify(mapNotify,
-                    LispNotificationHelper.getInetAddressFromIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress()));
+            MapNotify mapNotify = handleMapRegister(mapRegisterNotification.getMapRegister(), smr);
+            if (mapNotify != null) {
+                // store mappings in md-sal datastore only if we have a MapNotify
+                // XXX: this assumes that null MapNotifys are equivalent to authentication/registration errors
+                //      however notifies may be disabled with a flag (by the registering router). This should
+                //      be solved by moving southbound authentication of registrations out of handleMapRegister().
+                List<Mapping> mappings = LispNotificationHelper.getMapping(mapRegisterNotification);
+                for (Mapping mapping : mappings) {
+                    dsbe.updateMapping(mapping);
+                }
+
+                TransportAddressBuilder tab = new TransportAddressBuilder();
+                tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
+                tab.setPort(new PortNumber(LispMessage.PORT_NUM));
+                SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
+                smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
+                smnib.setTransportAddress(tab.build());
+                getLispSB().sendMapNotify(smnib.build());
+            } else {
+                LOG.warn("got null map notify");
+            }
 
         }
     }
@@ -269,15 +368,20 @@ public class LispMappingService implements CommandProvider, IFlowMapping, Bindin
         @Override
         public void onNotification(RequestMapping mapRequestNotification) {
             MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
-            getLispSB().handleMapReply(mapReply,
-                    LispNotificationHelper.getInetAddressFromIpAddress(mapRequestNotification.getTransportAddress().getIpAddress()));
+            if (mapReply != null) {
+                SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
+                smrib.setMapReply((new MapReplyBuilder(mapReply).build()));
+                smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
+                getLispSB().sendMapReply(smrib.build());
+            } else {
+                LOG.warn("got null map reply");
+            }
         }
-
     }
 
-    private ILispSouthboundPlugin getLispSB() {
+    private LispProtoService getLispSB() {
         if (lispSB == null) {
-            lispSB = session.getRpcService(ILispSouthboundPlugin.class);
+            lispSB = session.getRpcService(LispProtoService.class);
         }
         return lispSB;
     }
@@ -290,9 +394,41 @@ public class LispMappingService implements CommandProvider, IFlowMapping, Bindin
         tlsMapNotify.set(notify);
     }
 
+    public void handleSMR(MapRequest smr, LispAddressContainer subscriber) {
+        LOG.debug("Sending SMR to {} with Source-EID {} and EID Record {}",
+                LispAddressStringifier.getString(subscriber),
+                LispAddressStringifier.getString(smr.getSourceEid().getLispAddressContainer()),
+                LispAddressStringifier.getString(smr.getEidRecord().get(0).getLispAddressContainer()));
+        SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
+        smrib.setMapRequest(new MapRequestBuilder(smr).build());
+        smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromContainer(subscriber));
+        getLispSB().sendMapRequest(smrib.build());
+
+    }
+
+    @Override
+    public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
+        tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
+    }
+
+    @Override
+    public void clean() {
+        lispDao.removeAll();
+    }
+
+    @Override
+    public boolean shouldOverwrite() {
+        return mapServer.shouldOverwrite();
+    }
+
     @Override
-    public void handleNonProxyMapRequest(MapRequest mapRequest, InetAddress targetAddress) {
-        tlsMapRequest.set(new MutablePair<MapRequest, InetAddress>(mapRequest, targetAddress));
+    public void setOverwrite(boolean overwrite) {
+        mapServer.setOverwrite(overwrite);
     }
 
+    @Override
+    public void close() throws Exception {
+        lfmDbRpc.close();
+        destroy();
+    }
 }