Merge branch 'master' into topic/ietf_yang
[lispflowmapping.git] / mappingservice / southbound / src / main / java / org / opendaylight / lispflowmapping / southbound / LispSouthboundPlugin.java
index dcaf0915370dbd6049ed756646fb0c056507d082..e6ffffebd9036f9be2dc5295fcb7ebfc5237d269 100644 (file)
@@ -17,18 +17,20 @@ import java.net.SocketException;
 import java.net.SocketTimeoutException;
 import java.nio.ByteBuffer;
 
+import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
 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.controller.sal.binding.api.RpcProviderRegistry;
-import org.opendaylight.lispflowmapping.implementation.serializer.LispMessage;
+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.transportaddress.TransportAddress;
+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.transportaddress.TransportAddress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.LispSbService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -42,38 +44,38 @@ public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCl
     private LispIoThread xtrThread;
     private LispSouthboundService lispSouthboundService;
     private LispXtrSouthboundService lispXtrSouthboundService;
-    private NotificationProviderService notificationService;
+    private NotificationPublishService notificationPublishService;
     private RpcProviderRegistry rpcRegistry;
     private BindingAwareBroker broker;
     private volatile DatagramSocket socket = null;
     private volatile String bindingAddress = null;
     private volatile int xtrPort = LispMessage.XTR_PORT_NUM;
     private volatile boolean listenOnXtrPort = false;
-    private BindingAwareBroker.RpcRegistration<LfmControlPlaneService> controlPlaneRpc;
+    private BindingAwareBroker.RpcRegistration<LispSbService> sbRpcRegistration;
     private DatagramSocket xtrSocket;
+    private LispSouthboundStats statistics = new LispSouthboundStats();
 
     public void init() {
-        LOG.info("LISP (RFC6830) Mapping Service is up!");
-        final LfmControlPlaneRpc lfmCpRpc = new LfmControlPlaneRpc(this);
+        LOG.info("LISP (RFC6830) southbound plugin is initializing...");
+        final LispSouthboundRPC sbRpcHandler = new LispSouthboundRPC(this);
 
-        controlPlaneRpc = rpcRegistry.addRpcImplementation(LfmControlPlaneService.class, lfmCpRpc);
+        sbRpcRegistration = rpcRegistry.addRpcImplementation(LispSbService.class, sbRpcHandler);
         broker.registerProvider(this);
 
         synchronized (startLock) {
-            lispSouthboundService = new LispSouthboundService();
+            lispSouthboundService = new LispSouthboundService(this);
             lispXtrSouthboundService = new LispXtrSouthboundService();
-            lispSouthboundService.setNotificationProvider(this.notificationService);
-            lispXtrSouthboundService.setNotificationProvider(this.notificationService);
-            LOG.trace("Provider Session initialized");
+            lispSouthboundService.setNotificationProvider(this.notificationPublishService);
+            lispXtrSouthboundService.setNotificationProvider(this.notificationPublishService);
             if (bindingAddress == null) {
                 setLispAddress("0.0.0.0");
             }
-            LOG.info("LISP (RFC6830) Mapping Service is up!");
+            LOG.info("LISP (RFC6830) southbound plugin is up!");
         }
     }
 
-    public void setNotificationProviderService(NotificationProviderService notificationService) {
-        this.notificationService = notificationService;
+    public void setNotificationPublishService(NotificationPublishService notificationService) {
+        this.notificationPublishService = notificationService;
     }
 
     public void setRpcRegistryDependency(RpcProviderRegistry rpcRegistry) {
@@ -93,7 +95,7 @@ public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCl
         lispThread = null;
         xtrThread = null;
         bindingAddress = null;
-        LOG.info("LISP (RFC6830) Mapping Service is down!");
+        LOG.info("LISP (RFC6830) southbound plugin is down!");
         try {
             Thread.sleep(1100);
         } catch (InterruptedException e) {
@@ -119,7 +121,7 @@ public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCl
 
             int lispReceiveTimeout = 1000;
 
-            LOG.info("LISP (RFC6830) Mapping Service is running and listening on address: " + bindingAddress
+            LOG.info("LISP (RFC6830) southbound plugin is running and listening on address: " + bindingAddress
                     + " port: " + threadSocket.getLocalPort());
             try {
 
@@ -171,12 +173,6 @@ public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCl
                 ((address >> 0) & 0xff);
     }
 
-    public String getHelp() {
-        StringBuffer help = new StringBuffer();
-        help.append("---LISP Southbound Plugin---\n");
-        return help.toString();
-    }
-
     private void startIOThread() {
         if (socket != null) {
             while (!socket.isClosed()) {
@@ -190,12 +186,12 @@ public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCl
             socket = new DatagramSocket(new InetSocketAddress(bindingAddress, LispMessage.PORT_NUM));
             lispThread = new LispIoThread(socket, lispSouthboundService);
             lispThread.start();
-            LOG.info("LISP (RFC6830) Mapping Service Southbound Plugin is up!");
+            LOG.info("LISP (RFC6830) southbound plugin is listening for control packets!");
             if (listenOnXtrPort) {
                 restartXtrThread();
             }
         } catch (SocketException e) {
-            LOG.error("couldn't start socket {}", e.getMessage());
+            LOG.error("couldn't start socket: {}", ExceptionUtils.getStackTrace(e));
         }
     }
 
@@ -205,13 +201,13 @@ public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCl
             xtrSocket = new DatagramSocket(new InetSocketAddress(bindingAddress, xtrPort));
             xtrThread = new LispIoThread(xtrSocket, lispXtrSouthboundService);
             xtrThread.start();
-            LOG.info("xTR Southbound Plugin is up!");
+            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 handleSerializedLispBuffer(TransportAddress address, ByteBuffer outBuffer, String packetType) {
+    public void handleSerializedLispBuffer(TransportAddress address, ByteBuffer outBuffer, MessageType packetType) {
         DatagramPacket packet = new DatagramPacket(outBuffer.array(), outBuffer.limit());
         packet.setPort(address.getPort().getValue());
         InetAddress ip = InetAddresses.forString(new String(address.getIpAddress().getValue()));
@@ -221,11 +217,17 @@ public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCl
                 LOG.trace("Sending " + packetType + " on port " + address.getPort().getValue() + " to address: " + ip);
             }
             socket.send(packet);
+            this.statistics.incrementTx(packetType.getIntValue());
         } catch (IOException e) {
             LOG.warn("Failed to send " + packetType, e);
+            this.statistics.incrementTxErrors();
         }
     }
 
+    public LispSouthboundStats getStats() {
+        return statistics;
+    }
+
     public void setLispAddress(String address) {
         synchronized (startLock) {
             if (bindingAddress != null && bindingAddress.equals(address)) {
@@ -284,7 +286,7 @@ public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCl
     @Override
     public void close() throws Exception {
         unloadActions();
-        controlPlaneRpc.close();
+        sbRpcRegistration.close();
     }
 
     @Override