Cleanup mappingservice.yangmodel
[lispflowmapping.git] / mappingservice / southbound / src / main / java / org / opendaylight / lispflowmapping / southbound / LispSouthboundPlugin.java
index 9885afcb3971263e738958127ec7c3ab5dd66e69..5e220b4ffb5e25d49547dc7f3c0bf5a59b86d8d2 100644 (file)
@@ -16,36 +16,26 @@ import java.net.InetSocketAddress;
 import java.net.SocketException;
 import java.net.SocketTimeoutException;
 import java.nio.ByteBuffer;
-import java.util.concurrent.Future;
 
-import org.eclipse.osgi.framework.console.CommandProvider;
-import org.opendaylight.controller.sal.binding.api.AbstractBindingAwareProvider;
+import org.apache.commons.lang3.exception.ExceptionUtils;
+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.NotificationProviderService;
-import org.opendaylight.lispflowmapping.implementation.serializer.LispMessage;
-import org.opendaylight.lispflowmapping.implementation.serializer.MapNotifySerializer;
-import org.opendaylight.lispflowmapping.implementation.serializer.MapReplySerializer;
-import org.opendaylight.lispflowmapping.implementation.serializer.MapRequestSerializer;
+import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
+import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
 import org.opendaylight.lispflowmapping.southbound.lisp.ILispSouthboundService;
 import org.opendaylight.lispflowmapping.southbound.lisp.LispSouthboundService;
 import org.opendaylight.lispflowmapping.southbound.lisp.LispXtrSouthboundService;
 import org.opendaylight.lispflowmapping.type.sbplugin.IConfigLispSouthboundPlugin;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LfmControlPlaneService;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.SendMapNotifyInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.SendMapReplyInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.SendMapRequestInput;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.transportaddress.TransportAddress;
-import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.FrameworkUtil;
+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.transportaddress.TransportAddress;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import com.google.common.net.InetAddresses;
-import com.google.common.util.concurrent.Futures;
 
-public class LispSouthboundPlugin extends AbstractBindingAwareProvider implements IConfigLispSouthboundPlugin, CommandProvider, LfmControlPlaneService {
+public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCloseable, BindingAwareProvider {
     protected static final Logger LOG = LoggerFactory.getLogger(LispSouthboundPlugin.class);
 
     private static Object startLock = new Object();
@@ -53,25 +43,46 @@ public class LispSouthboundPlugin extends AbstractBindingAwareProvider implement
     private LispIoThread xtrThread;
     private LispSouthboundService lispSouthboundService;
     private LispXtrSouthboundService lispXtrSouthboundService;
+    private NotificationProviderService notificationService;
+    private RpcProviderRegistry rpcRegistry;
+    private BindingAwareBroker broker;
     private volatile DatagramSocket socket = null;
-    private final String MAP_NOTIFY = "MapNotify";
-    private final String MAP_REPlY = "MapReply";
-    private final String MAP_REQUEST = "MapRequest";
     private volatile String bindingAddress = null;
-    private volatile boolean alreadyInit = false;
     private volatile int xtrPort = LispMessage.XTR_PORT_NUM;
     private volatile boolean listenOnXtrPort = false;
-
+    private BindingAwareBroker.RpcRegistration<LispProtoService> controlPlaneRpc;
     private DatagramSocket xtrSocket;
 
-    private void registerWithOSGIConsole() {
-        BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
-        bundleContext.registerService(CommandProvider.class.getName(), this, null);
-        bundleContext.registerService(IConfigLispSouthboundPlugin.class.getName(), this, null);
+    public void init() {
+        LOG.info("LISP (RFC6830) Mapping Service is up!");
+        final LfmControlPlaneRpc lfmCpRpc = new LfmControlPlaneRpc(this);
+
+        controlPlaneRpc = rpcRegistry.addRpcImplementation(LispProtoService.class, lfmCpRpc);
+        broker.registerProvider(this);
+
+        synchronized (startLock) {
+            lispSouthboundService = new LispSouthboundService();
+            lispXtrSouthboundService = new LispXtrSouthboundService();
+            lispSouthboundService.setNotificationProvider(this.notificationService);
+            lispXtrSouthboundService.setNotificationProvider(this.notificationService);
+            LOG.trace("Provider Session initialized");
+            if (bindingAddress == null) {
+                setLispAddress("0.0.0.0");
+            }
+            LOG.info("LISP (RFC6830) Mapping Service is up!");
+        }
     }
 
-    protected void stopImpl(BundleContext context) {
-        unloadActions();
+    public void setNotificationProviderService(NotificationProviderService notificationService) {
+        this.notificationService = notificationService;
+    }
+
+    public void setRpcRegistryDependency(RpcProviderRegistry rpcRegistry) {
+        this.rpcRegistry = rpcRegistry;
+    }
+
+    public void setBindingAwareBroker(BindingAwareBroker broker) {
+        this.broker = broker;
     }
 
     private void unloadActions() {
@@ -82,6 +93,7 @@ public class LispSouthboundPlugin extends AbstractBindingAwareProvider implement
         lispXtrSouthboundService = null;
         lispThread = null;
         xtrThread = null;
+        bindingAddress = null;
         LOG.info("LISP (RFC6830) Mapping Service is down!");
         try {
             Thread.sleep(1100);
@@ -89,10 +101,6 @@ public class LispSouthboundPlugin extends AbstractBindingAwareProvider implement
         }
     }
 
-    public void destroy() {
-        unloadActions();
-    }
-
     private class LispIoThread extends Thread {
         private volatile boolean shouldRun;
         private volatile DatagramSocket threadSocket = null;
@@ -112,8 +120,8 @@ public class LispSouthboundPlugin extends AbstractBindingAwareProvider implement
 
             int lispReceiveTimeout = 1000;
 
-            LOG.info("LISP (RFC6830) Mapping Service is running and listening on address: " + bindingAddress + " port: "
-                    + threadSocket.getLocalPort());
+            LOG.info("LISP (RFC6830) Mapping Service is running and listening on address: " + bindingAddress
+                    + " port: " + threadSocket.getLocalPort());
             try {
 
                 threadSocket.setSoTimeout(lispReceiveTimeout);
@@ -133,8 +141,8 @@ public class LispSouthboundPlugin extends AbstractBindingAwareProvider implement
                 } catch (IOException e) {
                     LOG.warn("IO Exception while trying to recieve packet", e);
                 }
-                LOG.trace(String.format("Handling packet from {%s}:{%d} (len={%d})", packet.getAddress().getHostAddress(), packet.getPort(),
-                        packet.getLength()));
+                LOG.trace(String.format("Handling packet from {%s}:{%d} (len={%d})", packet.getAddress()
+                        .getHostAddress(), packet.getPort(), packet.getLength()));
 
                 try {
                     this.service.handlePacket(packet);
@@ -171,6 +179,14 @@ public class LispSouthboundPlugin extends AbstractBindingAwareProvider implement
     }
 
     private void startIOThread() {
+        if (socket != null) {
+            while (!socket.isClosed()) {
+                try {
+                    Thread.sleep(500);
+                } catch (InterruptedException e) {
+                }
+            }
+        }
         try {
             socket = new DatagramSocket(new InetSocketAddress(bindingAddress, LispMessage.PORT_NUM));
             lispThread = new LispIoThread(socket, lispSouthboundService);
@@ -180,7 +196,7 @@ public class LispSouthboundPlugin extends AbstractBindingAwareProvider implement
                 restartXtrThread();
             }
         } catch (SocketException e) {
-            LOG.error("couldn't start socket {}", e.getMessage());
+            LOG.error("couldn't start socket: {}", ExceptionUtils.getStackTrace(e));
         }
     }
 
@@ -192,42 +208,14 @@ public class LispSouthboundPlugin extends AbstractBindingAwareProvider implement
             xtrThread.start();
             LOG.info("xTR Southbound Plugin is up!");
         } catch (SocketException e) {
-            LOG.warn("failed to start xtr thread: {}", e.getMessage());
+            LOG.warn("failed to start xtr thread: {}", ExceptionUtils.getStackTrace(e));
         }
     }
 
-    public void onSessionInitiated(ProviderContext session) {
-        LOG.info("LISP (RFC6830) Mapping Service is up!");
-        synchronized (startLock) {
-            if (!alreadyInit) {
-                alreadyInit = true;
-                lispSouthboundService = new LispSouthboundService();
-                lispXtrSouthboundService = new LispXtrSouthboundService();
-                registerWithOSGIConsole();
-                registerRPCs(session);
-                LOG.trace("Provider Session initialized");
-                if (bindingAddress == null) {
-                    setLispAddress("0.0.0.0");
-                }
-            }
-
-        }
-    }
-
-    private void registerRPCs(ProviderContext session) {
-        try {
-            lispSouthboundService.setNotificationProvider(session.getSALService(NotificationProviderService.class));
-            lispXtrSouthboundService.setNotificationProvider(session.getSALService(NotificationProviderService.class));
-            session.addRpcImplementation(LfmControlPlaneService.class, this);
-        } catch (Exception e) {
-            LOG.error(e.getMessage(), e);
-        }
-    }
-
-    private void handleSerializedLispBuffer(TransportAddress address, ByteBuffer outBuffer, String packetType) {
+    public void handleSerializedLispBuffer(TransportAddress address, ByteBuffer outBuffer, String packetType) {
         DatagramPacket packet = new DatagramPacket(outBuffer.array(), outBuffer.limit());
         packet.setPort(address.getPort().getValue());
-        InetAddress ip = InetAddresses.forString(address.getIpAddress().getIpv4Address().getValue());
+        InetAddress ip = InetAddresses.forString(new String(address.getIpAddress().getValue()));
         packet.setAddress(ip);
         try {
             if (LOG.isDebugEnabled()) {
@@ -274,45 +262,6 @@ public class LispSouthboundPlugin extends AbstractBindingAwareProvider implement
         }
     }
 
-    @Override
-    public Future<RpcResult<Void>> sendMapNotify(SendMapNotifyInput mapNotifyInput) {
-        LOG.trace("sendMapNotify called!!");
-        if (mapNotifyInput != null) {
-            ByteBuffer outBuffer = MapNotifySerializer.getInstance().serialize(mapNotifyInput.getMapNotify());
-            handleSerializedLispBuffer(mapNotifyInput.getTransportAddress(), outBuffer, MAP_NOTIFY);
-        } else {
-            LOG.warn("MapNotify was null");
-            return Futures.immediateFuture(RpcResultBuilder.<Void> failed().build());
-        }
-        return Futures.immediateFuture(RpcResultBuilder.<Void> success().build());
-    }
-
-    @Override
-    public Future<RpcResult<Void>> sendMapReply(SendMapReplyInput mapReplyInput) {
-        LOG.trace("sendMapReply called!!");
-        if (mapReplyInput != null) {
-            ByteBuffer outBuffer = MapReplySerializer.getInstance().serialize(mapReplyInput.getMapReply());
-            handleSerializedLispBuffer(mapReplyInput.getTransportAddress(), outBuffer, MAP_REPlY);
-        } else {
-            LOG.warn("MapReply was null");
-            return Futures.immediateFuture(RpcResultBuilder.<Void> failed().build());
-        }
-        return Futures.immediateFuture(RpcResultBuilder.<Void> success().build());
-    }
-
-    @Override
-    public Future<RpcResult<Void>> sendMapRequest(SendMapRequestInput mapRequestInput) {
-        LOG.trace("sendMapRequest called!!");
-        if (mapRequestInput != null) {
-            ByteBuffer outBuffer = MapRequestSerializer.getInstance().serialize(mapRequestInput.getMapRequest());
-            handleSerializedLispBuffer(mapRequestInput.getTransportAddress(), outBuffer, MAP_REQUEST);
-        } else {
-            LOG.debug("MapRequest was null");
-            return Futures.immediateFuture(RpcResultBuilder.<Void> failed().build());
-        }
-        return Futures.immediateFuture(RpcResultBuilder.<Void> success().build());
-    }
-
     @Override
     public void shouldListenOnXtrPort(boolean shouldListenOnXtrPort) {
         listenOnXtrPort = shouldListenOnXtrPort;
@@ -332,4 +281,15 @@ public class LispSouthboundPlugin extends AbstractBindingAwareProvider implement
             restartXtrThread();
         }
     }
+
+    @Override
+    public void close() throws Exception {
+        unloadActions();
+        controlPlaneRpc.close();
+    }
+
+    @Override
+    public void onSessionInitiated(ProviderContext session) {
+        LOG.debug("LispSouthboundPlugin Provider Session Initiated");
+    }
 }