Revert removal of deprecated constructor in AbstractDispatcher
[bgpcep.git] / pcep / impl / src / main / java / org / opendaylight / protocol / pcep / impl / PCEPDispatcherImpl.java
index 715e153bae5dce004b86cdfe8865ce1c36f3cf86..686c0d86ba84b66d74b15eedb61819d9ee96b1af 100644 (file)
@@ -7,72 +7,56 @@
  */
 package org.opendaylight.protocol.pcep.impl;
 
+import com.google.common.base.Preconditions;
+import io.netty.channel.ChannelFuture;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.socket.SocketChannel;
+import io.netty.util.concurrent.Promise;
+import org.opendaylight.protocol.framework.AbstractDispatcher;
+import org.opendaylight.protocol.framework.SessionListenerFactory;
+import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
+import org.opendaylight.protocol.pcep.PCEPDispatcher;
+import org.opendaylight.protocol.pcep.PCEPSessionListener;
+import org.opendaylight.protocol.pcep.spi.MessageHandlerRegistry;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.Message;
+
 import java.io.IOException;
 import java.net.InetSocketAddress;
-import java.util.concurrent.ExecutionException;
-
-import org.opendaylight.protocol.framework.Dispatcher;
-import org.opendaylight.protocol.framework.ProtocolServer;
-import org.opendaylight.protocol.pcep.PCEPConnection;
-import org.opendaylight.protocol.pcep.PCEPConnectionFactory;
-import org.opendaylight.protocol.pcep.PCEPDispatcher;
-import org.opendaylight.protocol.pcep.PCEPSession;
-import org.opendaylight.protocol.pcep.PCEPSessionProposalFactory;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Implementation of PCEPDispatcher.
  */
-public class PCEPDispatcherImpl implements PCEPDispatcher {
-
-       private final static Logger logger = LoggerFactory.getLogger(PCEPDispatcherImpl.class);
+public class PCEPDispatcherImpl extends AbstractDispatcher<PCEPSessionImpl, PCEPSessionListener> implements PCEPDispatcher, AutoCloseable {
 
-       public static final int DEFAULT_MAX_UNKNOWN_MSG = 5;
-
-       private int maxUnknownMessages = DEFAULT_MAX_UNKNOWN_MSG;
-
-       private final Dispatcher dispatcher;
-
-       private final PCEPSessionProposalFactory proposalFactory;
+       private final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> snf;
+       private final PCEPHandlerFactory hf;
 
        /**
         * Creates an instance of PCEPDispatcherImpl, gets the default selector and opens it.
-        * 
+        *
         * @throws IOException if some error occurred during opening the selector
         */
-       public PCEPDispatcherImpl(final Dispatcher dispatcher, final PCEPSessionProposalFactory proposalFactory) {
-               this.dispatcher = dispatcher;
-               this.proposalFactory = proposalFactory;
+       public PCEPDispatcherImpl(final MessageHandlerRegistry registry,
+                       final SessionNegotiatorFactory<Message, PCEPSessionImpl, PCEPSessionListener> negotiatorFactory,
+                       EventLoopGroup bossGroup, EventLoopGroup workerGroup) {
+               super(bossGroup, workerGroup);
+               this.snf = Preconditions.checkNotNull(negotiatorFactory);
+               this.hf = new PCEPHandlerFactory(registry);
        }
 
        @Override
-       public ProtocolServer createServer(final InetSocketAddress address, final PCEPConnectionFactory connectionFactory) throws IOException {
-               connectionFactory.setProposal(this.proposalFactory, address, 0);
-               return this.dispatcher.createServer(address, connectionFactory, new PCEPSessionFactoryImpl(this.maxUnknownMessages));
+       public ChannelFuture createServer(final InetSocketAddress address, final SessionListenerFactory<PCEPSessionListener> listenerFactory) {
+               return super.createServer(address, new PipelineInitializer<PCEPSessionImpl>() {
+                       @Override
+                       public void initializeChannel(final SocketChannel ch, final Promise<PCEPSessionImpl> promise) {
+                               ch.pipeline().addLast(PCEPDispatcherImpl.this.hf.getDecoders());
+                               ch.pipeline().addLast("negotiator", PCEPDispatcherImpl.this.snf.getSessionNegotiator(listenerFactory, ch, promise));
+                               ch.pipeline().addLast(PCEPDispatcherImpl.this.hf.getEncoders());
+                       }
+               });
        }
 
-       /**
-        * Create client is used for mock purposes only.
-        * 
-        * @throws ExecutionException
-        * @throws InterruptedException
-        */
-       @Override
-       public PCEPSession createClient(final PCEPConnection connection) throws IOException {
-               return (PCEPSession) this.dispatcher.createClient(connection, new PCEPSessionFactoryImpl(this.maxUnknownMessages));
-       }
-
-       @Override
-       public void setMaxUnknownMessages(final int limit) {
-               this.maxUnknownMessages = limit;
-       }
-
-       public int getMaxUnknownMessages() {
-               return this.maxUnknownMessages;
-       }
-
-       public Dispatcher getDispatcher() {
-               return this.dispatcher;
-       }
+    @Override
+    public void close() {
+    }
 }