From: Robert Gallas Date: Mon, 11 Nov 2013 14:56:29 +0000 (+0100) Subject: Channel initializer class hierarchy refactor X-Git-Tag: jenkins-controller-bulk-release-prepare-only-2-1~426^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=1dde13b64526fd68ba052882bc9e00522fe381bc Channel initializer class hierarchy refactor Refactored netconf channel initializer class hierarchy to enable SSH and other transport implementation. Change-Id: Ibf7567ab47adf46ec3bfdca352a7188d465f699e Signed-off-by: Robert Gallas --- diff --git a/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientDispatcher.java b/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientDispatcher.java index 4df8235441..d18f0208d4 100644 --- a/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientDispatcher.java +++ b/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfClientDispatcher.java @@ -17,7 +17,7 @@ import io.netty.util.concurrent.Promise; import org.opendaylight.controller.netconf.api.NetconfMessage; import org.opendaylight.controller.netconf.api.NetconfSession; import org.opendaylight.controller.netconf.api.NetconfTerminationReason; -import org.opendaylight.controller.netconf.util.AbstractChannelInitializer; +import org.opendaylight.controller.netconf.util.AbstractSslChannelInitializer; import org.opendaylight.protocol.framework.AbstractDispatcher; import org.opendaylight.protocol.framework.ReconnectStrategy; import org.opendaylight.protocol.framework.SessionListener; @@ -48,18 +48,18 @@ public class NetconfClientDispatcher extends AbstractDispatcher promise) { - new ClientChannelInitializer(maybeContext, negotatorFactory, sessionListener).initialize(ch, promise); + new ClientSslChannelInitializer(maybeContext, negotatorFactory, sessionListener).initialize(ch, promise); } }); } - private static class ClientChannelInitializer extends AbstractChannelInitializer { + private static class ClientSslChannelInitializer extends AbstractSslChannelInitializer { private final NetconfClientSessionNegotiatorFactory negotiatorFactory; private final NetconfClientSessionListener sessionListener; - private ClientChannelInitializer(Optional maybeContext, - NetconfClientSessionNegotiatorFactory negotiatorFactory, NetconfClientSessionListener sessionListener) { + private ClientSslChannelInitializer(Optional maybeContext, + NetconfClientSessionNegotiatorFactory negotiatorFactory, NetconfClientSessionListener sessionListener) { super(maybeContext); this.negotiatorFactory = negotiatorFactory; this.sessionListener = sessionListener; diff --git a/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfSshClientDispatcher.java b/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfSshClientDispatcher.java new file mode 100644 index 0000000000..a426181183 --- /dev/null +++ b/opendaylight/netconf/netconf-client/src/main/java/org/opendaylight/controller/netconf/client/NetconfSshClientDispatcher.java @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2013 Cisco Systems, 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.controller.netconf.client; + +public class NetconfSshClientDispatcher extends NetconfClientDispatcher { + public NetconfSshClientDispatcher() { + super(null); + } +} diff --git a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerDispatcher.java b/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerDispatcher.java index 324da56ca5..c73840132f 100644 --- a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerDispatcher.java +++ b/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerDispatcher.java @@ -14,7 +14,7 @@ import io.netty.channel.socket.SocketChannel; import io.netty.util.concurrent.Promise; import org.opendaylight.controller.netconf.api.NetconfSession; import org.opendaylight.controller.netconf.impl.util.DeserializerExceptionHandler; -import org.opendaylight.controller.netconf.util.AbstractChannelInitializer; +import org.opendaylight.controller.netconf.util.AbstractSslChannelInitializer; import org.opendaylight.protocol.framework.AbstractDispatcher; import javax.net.ssl.SSLContext; @@ -23,12 +23,12 @@ import java.net.InetSocketAddress; public class NetconfServerDispatcher extends AbstractDispatcher { - private final ServerChannelInitializer initializer; + private final ServerSslChannelInitializer initializer; public NetconfServerDispatcher(final Optional maybeContext, NetconfServerSessionNegotiatorFactory serverNegotiatorFactory, NetconfServerSessionListenerFactory listenerFactory) { - this.initializer = new ServerChannelInitializer(maybeContext, serverNegotiatorFactory, listenerFactory); + this.initializer = new ServerSslChannelInitializer(maybeContext, serverNegotiatorFactory, listenerFactory); } // FIXME change headers for all new source code files @@ -44,14 +44,14 @@ public class NetconfServerDispatcher extends AbstractDispatcher maybeContext, - NetconfServerSessionNegotiatorFactory negotiatorFactory, - NetconfServerSessionListenerFactory listenerFactory) { + private ServerSslChannelInitializer(Optional maybeContext, + NetconfServerSessionNegotiatorFactory negotiatorFactory, + NetconfServerSessionListenerFactory listenerFactory) { super(maybeContext); this.negotiatorFactory = negotiatorFactory; this.listenerFactory = listenerFactory; diff --git a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/AbstractChannelInitializer.java b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/AbstractChannelInitializer.java index 5d082c92cd..caee542152 100644 --- a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/AbstractChannelInitializer.java +++ b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/AbstractChannelInitializer.java @@ -8,73 +8,14 @@ package org.opendaylight.controller.netconf.util; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLEngine; - -import org.opendaylight.controller.netconf.api.NetconfMessage; -import org.opendaylight.controller.netconf.api.NetconfSession; -import org.opendaylight.controller.netconf.util.handler.FramingMechanismHandlerFactory; -import org.opendaylight.controller.netconf.util.handler.NetconfMessageAggregator; -import org.opendaylight.controller.netconf.util.messages.FramingMechanism; -import org.opendaylight.controller.netconf.util.messages.NetconfMessageFactory; -import org.opendaylight.protocol.framework.ProtocolHandlerFactory; -import org.opendaylight.protocol.framework.ProtocolMessageDecoder; -import org.opendaylight.protocol.framework.ProtocolMessageEncoder; - -import com.google.common.base.Optional; - -import io.netty.channel.ChannelHandler; import io.netty.channel.socket.SocketChannel; -import io.netty.handler.ssl.SslHandler; import io.netty.util.concurrent.Promise; +import org.opendaylight.controller.netconf.api.NetconfSession; public abstract class AbstractChannelInitializer { - private final Optional maybeContext; - private final NetconfHandlerFactory handlerFactory; - - public AbstractChannelInitializer(Optional maybeContext) { - this.maybeContext = maybeContext; - this.handlerFactory = new NetconfHandlerFactory(new NetconfMessageFactory()); - } - - public void initialize(SocketChannel ch, Promise promise) { - if (maybeContext.isPresent()) { - initSsl(ch); - } - - ch.pipeline().addLast("aggregator", new NetconfMessageAggregator(FramingMechanism.EOM)); - ch.pipeline().addLast(handlerFactory.getDecoders()); - initializeAfterDecoder(ch, promise); - ch.pipeline().addLast("frameEncoder", FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM)); - ch.pipeline().addLast(handlerFactory.getEncoders()); - } + public abstract void initialize(SocketChannel ch, Promise promise); protected abstract void initializeAfterDecoder(SocketChannel ch, Promise promise); - private void initSsl(SocketChannel ch) { - SSLEngine sslEngine = maybeContext.get().createSSLEngine(); - initSslEngine(sslEngine); - final SslHandler handler = new SslHandler(sslEngine); - ch.pipeline().addLast("ssl", handler); - } - - protected abstract void initSslEngine(SSLEngine sslEngine); - - private static final class NetconfHandlerFactory extends ProtocolHandlerFactory { - - public NetconfHandlerFactory(final NetconfMessageFactory msgFactory) { - super(msgFactory); - } - - @Override - public ChannelHandler[] getEncoders() { - return new ChannelHandler[] { new ProtocolMessageEncoder(this.msgFactory) }; - } - - @Override - public ChannelHandler[] getDecoders() { - return new ChannelHandler[] { new ProtocolMessageDecoder(this.msgFactory) }; - } - } } diff --git a/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/AbstractSslChannelInitializer.java b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/AbstractSslChannelInitializer.java new file mode 100644 index 0000000000..d490eb2be6 --- /dev/null +++ b/opendaylight/netconf/netconf-util/src/main/java/org/opendaylight/controller/netconf/util/AbstractSslChannelInitializer.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2013 Cisco Systems, 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.controller.netconf.util; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLEngine; + +import org.opendaylight.controller.netconf.api.NetconfMessage; +import org.opendaylight.controller.netconf.api.NetconfSession; +import org.opendaylight.controller.netconf.util.handler.FramingMechanismHandlerFactory; +import org.opendaylight.controller.netconf.util.handler.NetconfMessageAggregator; +import org.opendaylight.controller.netconf.util.messages.FramingMechanism; +import org.opendaylight.controller.netconf.util.messages.NetconfMessageFactory; +import org.opendaylight.protocol.framework.ProtocolHandlerFactory; +import org.opendaylight.protocol.framework.ProtocolMessageDecoder; +import org.opendaylight.protocol.framework.ProtocolMessageEncoder; + +import com.google.common.base.Optional; + +import io.netty.channel.ChannelHandler; +import io.netty.channel.socket.SocketChannel; +import io.netty.handler.ssl.SslHandler; +import io.netty.util.concurrent.Promise; + +public abstract class AbstractSslChannelInitializer extends AbstractChannelInitializer { + + private final Optional maybeContext; + private final NetconfHandlerFactory handlerFactory; + + public AbstractSslChannelInitializer(Optional maybeContext) { + this.maybeContext = maybeContext; + this.handlerFactory = new NetconfHandlerFactory(new NetconfMessageFactory()); + } + + @Override + public void initialize(SocketChannel ch, Promise promise) { + if (maybeContext.isPresent()) { + initSsl(ch); + } + + ch.pipeline().addLast("aggregator", new NetconfMessageAggregator(FramingMechanism.EOM)); + ch.pipeline().addLast(handlerFactory.getDecoders()); + initializeAfterDecoder(ch, promise); + ch.pipeline().addLast("frameEncoder", FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM)); + ch.pipeline().addLast(handlerFactory.getEncoders()); + } + + private void initSsl(SocketChannel ch) { + SSLEngine sslEngine = maybeContext.get().createSSLEngine(); + initSslEngine(sslEngine); + final SslHandler handler = new SslHandler(sslEngine); + ch.pipeline().addLast("ssl", handler); + } + + protected abstract void initSslEngine(SSLEngine sslEngine); + + private static final class NetconfHandlerFactory extends ProtocolHandlerFactory { + + public NetconfHandlerFactory(final NetconfMessageFactory msgFactory) { + super(msgFactory); + } + + @Override + public ChannelHandler[] getEncoders() { + return new ChannelHandler[] { new ProtocolMessageEncoder(this.msgFactory) }; + } + + @Override + public ChannelHandler[] getDecoders() { + return new ChannelHandler[] { new ProtocolMessageDecoder(this.msgFactory) }; + } + } +}