Bump upstreams
[lispflowmapping.git] / mappingservice / southbound / src / main / java / org / opendaylight / lispflowmapping / southbound / LispSouthboundPlugin.java
index f4c8760170ff0dc4b53585b24f20f2df3f2b5360..662030455bd17736ff228dddfc90775d12c2e193 100644 (file)
 /*
- * 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,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.lispflowmapping.southbound;
 
-import java.io.IOException;
-import java.net.DatagramPacket;
-import java.net.DatagramSocket;
+import static io.netty.buffer.Unpooled.wrappedBuffer;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
+import io.netty.bootstrap.Bootstrap;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufUtil;
+import io.netty.buffer.PooledByteBufAllocator;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelOption;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.epoll.Epoll;
+import io.netty.channel.epoll.EpollChannelOption;
+import io.netty.channel.epoll.EpollDatagramChannel;
+import io.netty.channel.epoll.EpollEventLoopGroup;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.DatagramChannel;
+import io.netty.channel.socket.DatagramPacket;
+import io.netty.channel.socket.nio.NioDatagramChannel;
+import io.netty.util.concurrent.DefaultThreadFactory;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
-import java.net.SocketException;
-import java.net.SocketTimeoutException;
+import java.net.UnknownHostException;
 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.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
-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.lispflowmapping.southbound.lisp.LispSouthboundService;
-import org.opendaylight.lispflowmapping.type.sbplugin.IConfigLispPlugin;
-import org.opendaylight.lispflowmapping.type.sbplugin.ILispSouthboundPlugin;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapNotify;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapReply;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRequest;
-import org.opendaylight.yangtools.yang.common.RpcResult;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.FrameworkUtil;
+import java.util.List;
+import java.util.concurrent.ThreadFactory;
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import org.opendaylight.lispflowmapping.dsbackend.DataStoreBackEnd;
+import org.opendaylight.lispflowmapping.inmemorydb.HashMapDb;
+import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
+import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
+import org.opendaylight.lispflowmapping.mapcache.AuthKeyDb;
+import org.opendaylight.lispflowmapping.southbound.lisp.AuthenticationKeyDataListener;
+import org.opendaylight.lispflowmapping.southbound.lisp.LispSouthboundHandler;
+import org.opendaylight.lispflowmapping.southbound.lisp.LispXtrSouthboundHandler;
+import org.opendaylight.lispflowmapping.southbound.lisp.cache.MapRegisterCache;
+import org.opendaylight.lispflowmapping.type.sbplugin.IConfigLispSouthboundPlugin;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.NotificationPublishService;
+import org.opendaylight.mdsal.singleton.api.ClusterSingletonService;
+import org.opendaylight.mdsal.singleton.api.ClusterSingletonServiceProvider;
+import org.opendaylight.mdsal.singleton.api.ServiceGroupIdentifier;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
+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.eid.container.Eid;
+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.concepts.Registration;
+import org.opendaylight.yangtools.yang.binding.Notification;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.metatype.annotations.AttributeDefinition;
+import org.osgi.service.metatype.annotations.Designate;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class LispSouthboundPlugin extends AbstractBindingAwareProvider implements ILispSouthboundPlugin, IConfigLispPlugin, CommandProvider {
-    protected static final Logger logger = LoggerFactory.getLogger(LispSouthboundPlugin.class);
+@Singleton
+@Component(immediate = true, property = "type=default", configurationPid = "org.opendaylight.lispflowmapping",
+           service = { IConfigLispSouthboundPlugin.class, LispSouthboundPlugin.class })
+@Designate(ocd = LispSouthboundPlugin.Configuration.class)
+public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCloseable, ClusterSingletonService {
+    @ObjectClassDefinition
+    public @interface Configuration {
+        @AttributeDefinition()
+        String bindingAddress() default DEFAULT_BINDING_ADDRESS;
+
+        @AttributeDefinition()
+        boolean mapRegisterCacheEnabled() default true;
+
+        @AttributeDefinition()
+        long mapRegisterCacheTimeout() default DEFAULT_MAP_REGISTER_CACHE_TIMEOUT;
+    }
+
+    protected static final Logger LOG = LoggerFactory.getLogger(LispSouthboundPlugin.class);
+    public static final String LISPFLOWMAPPING_ENTITY_NAME = "lispflowmapping";
+    public static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER =
+        new ServiceGroupIdentifier(LISPFLOWMAPPING_ENTITY_NAME);
+
+    private static final String DEFAULT_BINDING_ADDRESS = "0.0.0.0";
+    private static final long DEFAULT_MAP_REGISTER_CACHE_TIMEOUT = 90000;
+
+    private volatile boolean isMaster = false;
+    private volatile String bindingAddress;
+    private AuthKeyDb akdb;
+    private final MapRegisterCache mapRegisterCache = new MapRegisterCache();
+    private final boolean mapRegisterCacheEnabled;
+    private final long mapRegisterCacheTimeout;
 
     private static Object startLock = new Object();
-    private LispIoThread thread;
-    private LispSouthboundService lispSouthboundService;
-    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 stillRunning = false;
-    private volatile boolean alreadyInit = false;
-
-    private void registerWithOSGIConsole() {
-        BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
-        bundleContext.registerService(CommandProvider.class.getName(), this, null);
-        bundleContext.registerService(IConfigLispPlugin.class.getName(), this, null);
-    }
-
-    protected void stopImpl(BundleContext context) {
-        unloadActions();
+
+    private final DataBroker dataBroker;
+    private final NotificationPublishService notificationPublishService;
+    private final ClusterSingletonServiceProvider clusterSingletonService;
+
+    private LispSouthboundHandler lispSouthboundHandler;
+    private int numChannels = 1;
+    private final Channel[] channel;
+    private Channel xtrChannel;
+    private volatile int xtrPort = LispMessage.XTR_PORT_NUM;
+    private volatile boolean listenOnXtrPort = false;
+    private final ConcurrentLispSouthboundStats statistics = new ConcurrentLispSouthboundStats();
+    private final Bootstrap bootstrap = new Bootstrap();
+    private final Bootstrap xtrBootstrap = new Bootstrap();
+    private final ThreadFactory threadFactory = new DefaultThreadFactory("lisp-sb");
+    private EventLoopGroup eventLoopGroup;
+    private AuthenticationKeyDataListener authenticationKeyDataListener;
+    private DataStoreBackEnd dsbe;
+    private Registration cssReg;
+
+    @Inject
+    public LispSouthboundPlugin(final DataBroker dataBroker,
+            final NotificationPublishService notificationPublishService,
+            final ClusterSingletonServiceProvider clusterSingletonService) {
+        this(dataBroker, notificationPublishService, clusterSingletonService, DEFAULT_BINDING_ADDRESS, true,
+            DEFAULT_MAP_REGISTER_CACHE_TIMEOUT);
     }
 
-    private void unloadActions() {
-        if (thread != null) {
-            thread.stopRunning();
-        }
-        lispSouthboundService = null;
-        thread = null;
-        logger.info("LISP (RFC6830) Mapping Service is down!");
-        try {
-            Thread.sleep(1100);
-        } catch (InterruptedException e) {
-        }
+    @Activate
+    public LispSouthboundPlugin(@Reference final DataBroker dataBroker,
+            @Reference final NotificationPublishService notificationPublishService,
+            @Reference final ClusterSingletonServiceProvider clusterSingletonService,
+            final Configuration configuration) {
+        this(dataBroker, notificationPublishService, clusterSingletonService, configuration.bindingAddress(),
+            configuration.mapRegisterCacheEnabled(), configuration.mapRegisterCacheTimeout());
+        init();
     }
 
-    public void destroy() {
-        unloadActions();
+    public LispSouthboundPlugin(final DataBroker dataBroker,
+            final NotificationPublishService notificationPublishService,
+            final ClusterSingletonServiceProvider clusterSingletonService,
+            final String bindingAddress, final boolean mapRegisterCacheEnabled, final long mapRegisterCacheTimeout) {
+        LOG.info("LISP (RFC6830) Southbound Plugin is initializing...");
+        this.dataBroker = dataBroker;
+        this.notificationPublishService = notificationPublishService;
+        this.clusterSingletonService = clusterSingletonService;
+        this.bindingAddress = bindingAddress;
+        this.mapRegisterCacheEnabled = mapRegisterCacheEnabled;
+        this.mapRegisterCacheTimeout = mapRegisterCacheTimeout;
+
+        if (Epoll.isAvailable()) {
+            // When lispflowmapping is under heavy load, there are usually two threads nearing 100% CPU core
+            // utilization. In order to have some headroom, we reserve 3 cores for "other" tasks, and allow the
+            // rest to be used for southbound packet processing, which is the most CPU intensive work done in lfm
+            numChannels = Math.max(1, Runtime.getRuntime().availableProcessors() - 3);
+        }
+        channel = new Channel[numChannels];
     }
 
-    private class LispIoThread extends Thread {
-        private volatile boolean running;
+    @PostConstruct
+    public void init() {
+        synchronized (startLock) {
+            akdb = new AuthKeyDb(new HashMapDb());
+            authenticationKeyDataListener = new AuthenticationKeyDataListener(dataBroker, akdb);
+            dsbe = new DataStoreBackEnd(dataBroker);
+            restoreDaoFromDatastore();
+
+            final Class<? extends DatagramChannel> channelType;
+            if (Epoll.isAvailable()) {
+                eventLoopGroup = new EpollEventLoopGroup(numChannels, threadFactory);
+                channelType = EpollDatagramChannel.class;
+                bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
+                bootstrap.option(EpollChannelOption.SO_REUSEPORT, true);
+                LOG.debug("Using Netty Epoll for UDP sockets");
+            } else {
+                eventLoopGroup = new NioEventLoopGroup(0, threadFactory);
+                channelType = NioDatagramChannel.class;
+                LOG.debug("Using Netty I/O (non-Epoll) for UDP sockets");
+            }
+
+            bootstrap.group(eventLoopGroup);
+            bootstrap.channel(channelType);
+            lispSouthboundHandler = new LispSouthboundHandler(this);
+            bootstrap.handler(lispSouthboundHandler);
 
-        public LispIoThread() {
-            super("Lisp Thread");
-            running = true;
+            xtrBootstrap.group(eventLoopGroup);
+            xtrBootstrap.channel(channelType);
+            xtrBootstrap.handler(new LispXtrSouthboundHandler(this));
+
+            start();
+            startXtr();
+
+            cssReg = clusterSingletonService.registerClusterSingletonService(this);
         }
 
-        @Override
-        public void run() {
-            stillRunning = true;
+        LOG.info("LISP (RFC6830) Southbound Plugin is up!");
+    }
 
-            int lispPortNumber = LispMessage.PORT_NUM;
-            int lispReceiveTimeout = 1000;
+    @SuppressWarnings("checkstyle:IllegalCatch")
+    private void start() {
+        try {
+            for (int i = 0; i < numChannels; ++i) {
+                channel[i] = bootstrap.bind(bindingAddress, LispMessage.PORT_NUM).sync().channel();
+            }
+            LOG.debug("Binding LISP UDP listening socket to {}:{}", bindingAddress, LispMessage.PORT_NUM);
+        } catch (Exception e) {
+            LOG.error("Failed to open main socket ", e);
+        }
+    }
 
-            logger.info("LISP (RFC6830) Mapping Service is running and listening on " + bindingAddress);
+    @SuppressWarnings("checkstyle:IllegalCatch")
+    private void startXtr() {
+        if (listenOnXtrPort) {
             try {
-                socket = new DatagramSocket(new InetSocketAddress(bindingAddress, lispPortNumber));
-                socket.setSoTimeout(lispReceiveTimeout);
-            } catch (SocketException e) {
-                logger.warn("Cannot open socket on UDP port " + lispPortNumber, e);
-                return;
+                xtrChannel = xtrBootstrap.bind(bindingAddress, xtrPort).sync().channel();
+                LOG.debug("Binding LISP xTR UDP listening socket to {}:{}", bindingAddress, xtrPort);
+            } catch (Exception e) {
+                LOG.error("Failed to open xTR socket ", e);
             }
+        }
+    }
 
-            while (running) {
-                byte[] buffer = new byte[4096];
-                DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
-                try {
-                    socket.receive(packet);
-                    logger.debug("Received a packet!");
-                } catch (SocketTimeoutException ste) {
-                    continue;
-                } catch (IOException e) {
-                    logger.error("IO Exception while trying to recieve packet", e);
-                }
-                logger.debug("Handling packet from {}:{} (len={})", packet.getAddress().getHostAddress(), packet.getPort(), packet.getLength());
-
-                try {
-                    lispSouthboundService.handlePacket(packet);
-                } catch (Throwable t) {
-                    logger.error("Error while handling packet", t);
-                }
+    @SuppressWarnings("checkstyle:IllegalCatch")
+    private void stop() {
+        try {
+            for (int i = 0; i < numChannels; ++i) {
+                channel[i].close().sync();
+                channel[i] = null;
             }
-
-            socket.close();
-            logger.info("Socket closed");
-            stillRunning = false;
+        } catch (Exception e) {
+            LOG.error("Failed to close main socket ", e);
         }
+    }
 
-        public void stopRunning() {
-            running = false;
+    @SuppressWarnings("checkstyle:IllegalCatch")
+    private void stopXtr() {
+        if (listenOnXtrPort) {
+            try {
+                xtrChannel.close().sync();
+                xtrChannel = null;
+            } catch (Exception e) {
+                LOG.error("Failed to close xTR socket ", e);
+            }
         }
     }
 
-    public static String intToIpv4(int address) {
-        return ((address >> 24) & 0xff) + "." + //
-                ((address >> 16) & 0xff) + "." + //
-                ((address >> 8) & 0xff) + "." + //
-                ((address >> 0) & 0xff);
+    private void restart() {
+        LOG.info("Reloading");
+        stop();
+        start();
     }
 
-    public String getHelp() {
-        StringBuffer help = new StringBuffer();
-        help.append("---LISP Southbound Plugin---\n");
-        return help.toString();
+    private void restartXtr() {
+        LOG.info("Reloading xTR");
+        stopXtr();
+        startXtr();
     }
 
-    private void startIOThread() {
-        thread = new LispIoThread();
-        logger.info("LISP (RFC6830) Mapping Service Southbound Plugin is up!");
-        thread.start();
+    private void unloadActions() {
+        lispSouthboundHandler = null;
+
+        stop();
+        stopXtr();
+
+        LOG.info("LISP (RFC6830) Southbound Plugin is down!");
     }
 
-    public void onSessionInitiated(ProviderContext session) {
-        logger.info("LISP (RFC6830) Mapping Service is up!");
-        synchronized (startLock) {
-            if (!alreadyInit) {
-                alreadyInit = true;
-                lispSouthboundService = new LispSouthboundService();
-                registerWithOSGIConsole();
-                registerRPCs(session);
-                logger.debug("Provider Session initialized");
-                if (bindingAddress == null) {
-                    setLispAddress("0.0.0.0");
-                }
-            }
+    /**
+     * Restore all keys from MDSAL datastore.
+     */
+    public void restoreDaoFromDatastore() {
+        final List<AuthenticationKey> authKeys = dsbe.getAllAuthenticationKeys();
+        LOG.info("Restoring {} keys from datastore into southbound DAO", authKeys.size());
 
+        for (AuthenticationKey authKey : authKeys) {
+            final Eid key = authKey.getEid();
+            final MappingAuthkey mappingAuthkey = authKey.getMappingAuthkey();
+            LOG.debug("Adding authentication key '{}' with key-ID {} for {}", mappingAuthkey.getKeyString(),
+                    mappingAuthkey.getKeyType(),
+                    LispAddressStringifier.getString(key));
+            akdb.addAuthenticationKey(key, mappingAuthkey);
         }
     }
 
-    private void registerRPCs(ProviderContext session) {
-        try {
-            lispSouthboundService.setNotificationProvider(session.getSALService(NotificationProviderService.class));
-            session.addRpcImplementation(ILispSouthboundPlugin.class, this);
-        } catch (Throwable t) {
-            logger.error(t.getMessage(), t);
+    public void handleSerializedLispBuffer(final TransportAddress address, final ByteBuffer outBuffer,
+                                           final MessageType packetType) {
+        InetAddress ip = getInetAddress(address);
+        handleSerializedLispBuffer(ip, outBuffer, packetType, address.getPort().getValue().toJava(), null);
+    }
+
+    public void handleSerializedLispBuffer(final InetAddress address, final ByteBuffer outBuffer,
+            final MessageType packetType, final int portNumber, Channel senderChannel) {
+        if (senderChannel == null) {
+            senderChannel = channel[0];
+        }
+        InetSocketAddress recipient = new InetSocketAddress(address, portNumber);
+        outBuffer.position(0);
+        ByteBuf data = wrappedBuffer(outBuffer);
+        DatagramPacket packet = new DatagramPacket(data, recipient);
+        LOG.debug("Sending {} on port {} to address: {}", packetType, portNumber, address);
+        if (LOG.isTraceEnabled()) {
+            LOG.trace("Buffer:\n{}", ByteBufUtil.prettyHexDump(data));
         }
+        senderChannel.write(packet).addListener(future -> {
+            if (future.isSuccess()) {
+                LOG.trace("Success");
+                statistics.incrementTx(packetType.getIntValue());
+            } else {
+                LOG.warn("Failed to send packet");
+                statistics.incrementTxErrors();
+            }
+        });
+        senderChannel.flush();
     }
 
-    public Future<RpcResult<Void>> handleMapNotify(MapNotify mapNotify, InetAddress address) {
-        logger.trace("handleMapNotify called!!");
-        if (mapNotify != null) {
-            ByteBuffer outBuffer = MapNotifySerializer.getInstance().serialize(mapNotify);
-            handleSerializedLispBuffer(address, outBuffer, MAP_NOTIFY);
-        } else {
-            logger.debug("MapNotify was null");
+    private static InetAddress getInetAddress(final TransportAddress address) {
+        requireNonNull(address, "TransportAddress must not be null");
+        IpAddressBinary ip = address.getIpAddress();
+        try {
+            if (ip.getIpv4AddressBinary() != null) {
+                return InetAddress.getByAddress(ip.getIpv4AddressBinary().getValue());
+            } else if (ip.getIpv6AddressBinary() != null) {
+                return InetAddress.getByAddress(ip.getIpv6AddressBinary().getValue());
+            }
+        } catch (UnknownHostException e) {
+            LOG.debug("Could not convert TransportAddress {} to InetAddress", address, e);
         }
         return null;
     }
 
-    private void handleSerializedLispBuffer(InetAddress address, ByteBuffer outBuffer, String packetType) {
-        DatagramPacket packet = new DatagramPacket(outBuffer.array(), outBuffer.limit());
-        packet.setPort(LispMessage.PORT_NUM);
-        packet.setAddress(address);
-        try {
-            if (logger.isDebugEnabled()) {
-                logger.debug("Sending " + packetType + " on port " + LispMessage.PORT_NUM + " to address: " + address);
+    @Override
+    @SuppressWarnings("checkstyle:IllegalCatch")
+    public void setLispAddress(final String address) {
+        synchronized (startLock) {
+            if (bindingAddress.equals(address)) {
+                LOG.debug("Configured LISP binding address didn't change.");
+            } else {
+                LOG.debug("Setting LISP binding address to {}", address);
+                bindingAddress = address;
+                if (channel != null) {
+                    try {
+                        restart();
+                        restartXtr();
+                    } catch (Exception e) {
+                        LOG.error("Failed to set LISP binding address: ", e);
+                    }
+                }
             }
-            socket.send(packet);
-        } catch (IOException e) {
-            logger.error("Failed to send " + packetType, e);
         }
     }
 
-    public Future<RpcResult<Void>> handleMapReply(MapReply mapReply, InetAddress address) {
-        logger.trace("handleMapReply called!!");
-        if (mapReply != null) {
-            ByteBuffer outBuffer = MapReplySerializer.getInstance().serialize(mapReply);
-            handleSerializedLispBuffer(address, outBuffer, MAP_REPlY);
+    @Override
+    public void shouldListenOnXtrPort(final boolean shouldListenOnXtrPort) {
+        listenOnXtrPort = shouldListenOnXtrPort;
+        if (listenOnXtrPort) {
+            restartXtr();
         } else {
-            logger.debug("MapReply was null");
+            LOG.info("Shutting down xTR");
+            stopXtr();
         }
-        return null;
     }
 
-    public Future<RpcResult<Void>> handleMapRequest(MapRequest mapRequest, InetAddress address) {
-        logger.trace("handleMapRequest called!!");
-        if (mapRequest != null) {
-            ByteBuffer outBuffer = MapRequestSerializer.getInstance().serialize(mapRequest);
-            handleSerializedLispBuffer(address, outBuffer, MAP_REQUEST);
-        } else {
-            logger.debug("MapRequest was null");
+    @Override
+    public void setXtrPort(final int port) {
+        xtrPort = port;
+        if (listenOnXtrPort) {
+            restartXtr();
         }
-        return null;
     }
 
-    public void setLispAddress(String address) {
-        synchronized (startLock) {
-            if (bindingAddress != null && bindingAddress.equals(address)) {
-                logger.debug("configured lisp binding address didn't change.");
-            } else {
-                String action = (bindingAddress == null ? "Setting" : "Resetting");
-                logger.info(action + " lisp binding address to: " + address);
-                bindingAddress = address;
-                if (thread != null) {
-                    thread.stopRunning();
-                    while (stillRunning) {
-                        try {
-                            Thread.sleep(500);
-                        } catch (InterruptedException e) {
-                            e.printStackTrace();
-                        }
-                    }
-                }
-                startIOThread();
-            }
+    @Deactivate
+    @PreDestroy
+    @Override
+    public void close() throws Exception {
+        eventLoopGroup.shutdownGracefully();
+        lispSouthboundHandler.close();
+        unloadActions();
+        if (cssReg != null) {
+            cssReg.close();
         }
+        dsbe.closeTransactionChain();
+    }
+
+    @Override
+    public void instantiateServiceInstance() {
+        isMaster = true;
+    }
+
+    @Override
+    public ListenableFuture<Void> closeServiceInstance() {
+        isMaster = false;
+        return Futures.<Void>immediateFuture(null);
+    }
+
+    @Override
+    public ServiceGroupIdentifier getIdentifier() {
+        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;
+    }
+
+    public boolean isMapRegisterCacheEnabled() {
+        return mapRegisterCacheEnabled;
+    }
+
+    public long getMapRegisterCacheTimeout() {
+        return mapRegisterCacheTimeout;
     }
 }