Bug 3112: Add SB registrations to MD-SAL datastore
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / LispMappingService.java
index cfc369cddded07f69f940cc37f880ddcf64cb362..7305a610ece0f510a4c41e3e3678b95368dc7329 100644 (file)
@@ -8,23 +8,28 @@
 
 package org.opendaylight.lispflowmapping.implementation;
 
-import java.util.concurrent.Future;
+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;
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
 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.config.ConfigIni;
+import org.opendaylight.lispflowmapping.implementation.dao.HashMapDb;
 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKey;
 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKeyUtil;
 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceNoMaskKey;
 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
+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.serializer.LispMessage;
 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
 import org.opendaylight.lispflowmapping.implementation.util.LispNotificationHelper;
@@ -54,42 +59,23 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.maprequestmessage.MapRequestBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.transportaddress.TransportAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.transportaddress.TransportAddressBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.AddKeyInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.AddKeyOutput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.AddMappingInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.AddMappingOutput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetKeyInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetKeyOutput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetKeyWithRefInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetKeyWithRefOutput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetMappingInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetMappingOutput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetMappingWithRefInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.GetMappingWithRefOutput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.LfmMappingDatabaseService;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.RemoveKeyInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.RemoveKeyWithRefInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.RemoveMappingInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.RemoveMappingWithRefInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.UpdateKeyInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.UpdateKeyWithRefInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.UpdateMappingInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.UpdateMappingWithRefInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mapping.database.rev150314.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.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
 import org.opendaylight.yangtools.yang.binding.Notification;
-import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.FrameworkUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class LispMappingService implements CommandProvider, IFlowMapping, IFlowMappingShell, BindingAwareProvider,
-        IMapRequestResultHandler, IMapNotifyHandler, LfmMappingDatabaseService {
+        IMapRequestResultHandler, IMapNotifyHandler {
     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;
@@ -104,7 +90,9 @@ public class LispMappingService implements CommandProvider, IFlowMapping, IFlowM
 
     private ProviderContext session;
 
+    private DataStoreBackEnd dsbe;
     private NotificationService notificationService;
+    private static LispMappingService lfmService = null;
 
     class LispIpv4AddressInMemoryConverter implements ILispTypeConverter<Ipv4Address, Integer> {
     }
@@ -118,14 +106,25 @@ public class LispMappingService implements CommandProvider, IFlowMapping, IFlowM
     class MappingServiceNoMaskKeyConvertor implements ILispTypeConverter<MappingServiceNoMaskKey, Integer> {
     }
 
+    public static LispMappingService getLispMappingService() {
+        return lfmService;
+    }
+
     void setBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
-        LOG.trace("BindingAwareBroker set!");
+        LOG.debug("BindingAwareBroker set!");
         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
         bindingAwareBroker.registerProvider(this, bundleContext);
+
+        // For the time being we initialize variables here. But once we remove the activator, these should
+        // be moved to the constructor.
+        lfmService = this;
+        setLispDao(new HashMapDb());
     }
 
     void unsetBindingAwareBroker(BindingAwareBroker bindingAwareBroker) {
         LOG.debug("BindingAwareBroker was unset in LispMappingService");
+        lfmService = null;
+        unsetLispDao(lispDao);
     }
 
     public void basicInit(ILispDAO dao) {
@@ -164,6 +163,8 @@ public class LispMappingService implements CommandProvider, IFlowMapping, IFlowM
         LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
         mapResolver = null;
         mapServer = null;
+        unsetLispDao(lispDao);
+        closeDataListeners();
     }
 
     public void _removeEid(final CommandInterpreter ci) {
@@ -229,6 +230,10 @@ public class LispMappingService implements CommandProvider, IFlowMapping, IFlowM
     }
 
     public MapReply handleMapRequest(MapRequest request, boolean smr) {
+        LOG.debug("DAO: Retrieving mapping for {}/{}",
+                LispAFIConvertor.toString(request.getEidRecord().get(0).getLispAddressContainer()),
+                request.getEidRecord().get(0).getMask());
+
         tlsMapReply.set(null);
         tlsMapRequest.set(null);
         mapResolver.handleMapRequest(request, smr, this);
@@ -252,6 +257,10 @@ public class LispMappingService implements CommandProvider, IFlowMapping, IFlowM
     }
 
     public MapNotify handleMapRegister(MapRegister mapRegister, boolean smr) {
+        LOG.debug("DAO: Adding mapping for {}/{}",
+                LispAFIConvertor.toString(mapRegister.getEidToLocatorRecord().get(0).getLispAddressContainer()),
+                mapRegister.getEidToLocatorRecord().get(0).getMaskLength());
+
         tlsMapNotify.set(null);
         mapServer.handleMapRegister(mapRegister, smr, this);
         // After this invocation we assume that the thread local is filled with
@@ -260,18 +269,22 @@ public class LispMappingService implements CommandProvider, IFlowMapping, IFlowM
     }
 
     public String getAuthenticationKey(LispAddressContainer address, int maskLen) {
+        LOG.debug("DAO: Retrieving authentication key for {}/{}", LispAFIConvertor.toString(address), maskLen);
         return mapServer.getAuthenticationKey(address, maskLen);
     }
 
     public void removeAuthenticationKey(LispAddressContainer address, int maskLen) {
+        LOG.debug("DAO: Removing authentication key for {}/{}", LispAFIConvertor.toString(address), maskLen);
         mapServer.removeAuthenticationKey(address, maskLen);
     }
 
     public void addAuthenticationKey(LispAddressContainer address, int maskLen, String key) {
+        LOG.debug("DAO: Adding authentication key '{}' for {}/{}", key, LispAFIConvertor.toString(address), maskLen);
         mapServer.addAuthenticationKey(address, maskLen, key);
     }
 
     public void removeMapping(LispAddressContainer address, int maskLen) {
+        LOG.debug("DAO: Removing mapping for {}/{}", LispAFIConvertor.toString(address), maskLen);
         mapServer.removeMapping(address, maskLen, smr, this);
     }
 
@@ -307,12 +320,23 @@ public class LispMappingService implements CommandProvider, IFlowMapping, IFlowM
     public void onSessionInitiated(ProviderContext session) {
         LOG.info("Lisp Consumer session initialized!");
         notificationService = session.getSALService(NotificationService.class);
+        dsbe = new DataStoreBackEnd(session.getSALService(DataBroker.class));
         registerNotificationListener(AddMapping.class, new MapRegisterNotificationHandler());
         registerNotificationListener(RequestMapping.class, new MapRequestNotificationHandler());
-        session.addRpcImplementation(LfmMappingDatabaseService.class, this);
+        registerDataListeners(session.getSALService(DataBroker.class));
         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);
     }
@@ -334,6 +358,10 @@ public class LispMappingService implements CommandProvider, IFlowMapping, IFlowM
                 LOG.warn("got null map notify");
             }
 
+            List<Mapping> mappings = LispNotificationHelper.getMapping(mapRegisterNotification);
+            for (Mapping mapping : mappings) {
+                dsbe.updateMapping(mapping);
+            }
         }
     }
 
@@ -396,92 +424,4 @@ public class LispMappingService implements CommandProvider, IFlowMapping, IFlowM
     public void setOverwrite(boolean overwrite) {
         mapServer.setOverwrite(overwrite);
     }
-
-    @Override
-    public Future<RpcResult<AddKeyOutput>> addKey(AddKeyInput input) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public Future<RpcResult<AddMappingOutput>> addMapping(AddMappingInput input) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public Future<RpcResult<GetKeyOutput>> getKey(GetKeyInput input) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public Future<RpcResult<GetKeyWithRefOutput>> getKeyWithRef(
-            GetKeyWithRefInput input) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public Future<RpcResult<GetMappingOutput>> getMapping(GetMappingInput input) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public Future<RpcResult<GetMappingWithRefOutput>> getMappingWithRef(
-            GetMappingWithRefInput input) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public Future<RpcResult<Void>> removeKey(RemoveKeyInput input) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public Future<RpcResult<Void>> removeKeyWithRef(RemoveKeyWithRefInput input) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public Future<RpcResult<Void>> removeMapping(RemoveMappingInput input) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public Future<RpcResult<Void>> removeMappingWithRef(
-            RemoveMappingWithRefInput input) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public Future<RpcResult<Void>> updateKey(UpdateKeyInput input) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public Future<RpcResult<Void>> updateKeyWithRef(UpdateKeyWithRefInput input) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public Future<RpcResult<Void>> updateMapping(UpdateMappingInput input) {
-        // TODO Auto-generated method stub
-        return null;
-    }
-
-    @Override
-    public Future<RpcResult<Void>> updateMappingWithRef(
-            UpdateMappingWithRefInput input) {
-        // TODO Auto-generated method stub
-        return null;
-    }
 }