BUG-58: refactor to take advantage of netty
[bgpcep.git] / framework / src / main / java / org / opendaylight / protocol / framework / ChannelInitializerImpl.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.framework;
9
10
11 import io.netty.channel.ChannelInitializer;
12 import io.netty.channel.socket.SocketChannel;
13 import io.netty.util.concurrent.Promise;
14
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 import com.google.common.base.Preconditions;
19
20 final class ChannelInitializerImpl<M extends ProtocolMessage, S extends ProtocolSession<M>, L extends SessionListener<M, ?, ?>> extends ChannelInitializer<SocketChannel> {
21         private static final Logger logger = LoggerFactory.getLogger(ChannelInitializerImpl.class);
22         private final SessionNegotiatorFactory<M, S, L> negotiatorFactory;
23         private final SessionListenerFactory<L> listenerFactory;
24         private final ProtocolHandlerFactory<?> factory;
25         private final Promise<S> promise;
26
27         ChannelInitializerImpl(final SessionNegotiatorFactory<M, S, L> negotiatorFactory, final SessionListenerFactory<L> listenerFactory,
28                         final ProtocolHandlerFactory<?> factory, final Promise<S> promise) {
29                 this.negotiatorFactory = Preconditions.checkNotNull(negotiatorFactory);
30                 this.listenerFactory = Preconditions.checkNotNull(listenerFactory);
31                 this.promise = Preconditions.checkNotNull(promise);
32                 this.factory = Preconditions.checkNotNull(factory);
33         }
34
35         @Override
36         protected void initChannel(final SocketChannel ch) {
37                 logger.debug("Initializing channel {}", ch);
38                 ch.pipeline().addLast("decoder", factory.getDecoder());
39                 ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(listenerFactory, ch, promise));
40                 ch.pipeline().addLast("encoder", factory.getEncoder());
41                 logger.debug("Channel {} initialized", ch);
42         }
43 }