Search in sources :

Example 1 with ReconnectStrategyFactory

use of org.opendaylight.netconf.nettyutil.ReconnectStrategyFactory in project netconf by opendaylight.

the class RemoteDeviceConnectorImpl method getClientConfig.

@VisibleForTesting
NetconfReconnectingClientConfiguration getClientConfig(final NetconfClientSessionListener listener, final NetconfNode node) {
    // setup default values since default value is not supported in mdsal
    final long clientConnectionTimeoutMillis = node.getConnectionTimeoutMillis() == null ? NetconfTopologyUtils.DEFAULT_CONNECTION_TIMEOUT_MILLIS : node.getConnectionTimeoutMillis().toJava();
    final long maxConnectionAttempts = node.getMaxConnectionAttempts() == null ? NetconfTopologyUtils.DEFAULT_MAX_CONNECTION_ATTEMPTS : node.getMaxConnectionAttempts().toJava();
    final int betweenAttemptsTimeoutMillis = node.getBetweenAttemptsTimeoutMillis() == null ? NetconfTopologyUtils.DEFAULT_BETWEEN_ATTEMPTS_TIMEOUT_MILLIS : node.getBetweenAttemptsTimeoutMillis().toJava();
    final boolean isTcpOnly = node.getTcpOnly() == null ? NetconfTopologyUtils.DEFAULT_IS_TCP_ONLY : node.getTcpOnly();
    final BigDecimal sleepFactor = node.getSleepFactor() == null ? NetconfTopologyUtils.DEFAULT_SLEEP_FACTOR : node.getSleepFactor();
    final InetSocketAddress socketAddress = getSocketAddress(node.getHost(), node.getPort().getValue().toJava());
    final ReconnectStrategyFactory sf = new TimedReconnectStrategyFactory(netconfTopologyDeviceSetup.getEventExecutor(), maxConnectionAttempts, betweenAttemptsTimeoutMillis, sleepFactor);
    final NetconfReconnectingClientConfigurationBuilder reconnectingClientConfigurationBuilder;
    final Protocol protocol = node.getProtocol();
    if (isTcpOnly) {
        reconnectingClientConfigurationBuilder = NetconfReconnectingClientConfigurationBuilder.create().withProtocol(NetconfClientConfiguration.NetconfClientProtocol.TCP).withAuthHandler(getHandlerFromCredentials(node.getCredentials()));
    } else if (protocol == null || protocol.getName() == Protocol.Name.SSH) {
        reconnectingClientConfigurationBuilder = NetconfReconnectingClientConfigurationBuilder.create().withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH).withAuthHandler(getHandlerFromCredentials(node.getCredentials()));
    } else if (protocol.getName() == Protocol.Name.TLS) {
        reconnectingClientConfigurationBuilder = NetconfReconnectingClientConfigurationBuilder.create().withSslHandlerFactory(new SslHandlerFactoryImpl(keystoreAdapter, protocol.getSpecification())).withProtocol(NetconfClientConfiguration.NetconfClientProtocol.TLS);
    } else {
        throw new IllegalStateException("Unsupported protocol type: " + protocol.getName());
    }
    final List<Uri> odlHelloCapabilities = getOdlHelloCapabilities(node);
    if (odlHelloCapabilities != null) {
        reconnectingClientConfigurationBuilder.withOdlHelloCapabilities(odlHelloCapabilities);
    }
    return reconnectingClientConfigurationBuilder.withAddress(socketAddress).withConnectionTimeoutMillis(clientConnectionTimeoutMillis).withReconnectStrategy(sf.createReconnectStrategy()).withConnectStrategyFactory(sf).withSessionListener(listener).build();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) NetconfReconnectingClientConfigurationBuilder(org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfigurationBuilder) Uri(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri) BigDecimal(java.math.BigDecimal) TimedReconnectStrategyFactory(org.opendaylight.netconf.nettyutil.TimedReconnectStrategyFactory) TimedReconnectStrategyFactory(org.opendaylight.netconf.nettyutil.TimedReconnectStrategyFactory) ReconnectStrategyFactory(org.opendaylight.netconf.nettyutil.ReconnectStrategyFactory) SslHandlerFactoryImpl(org.opendaylight.netconf.sal.connect.util.SslHandlerFactoryImpl) Protocol(org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.parameters.Protocol) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with ReconnectStrategyFactory

use of org.opendaylight.netconf.nettyutil.ReconnectStrategyFactory in project netconf by opendaylight.

the class AbstractNetconfTopology method getClientConfig.

public NetconfReconnectingClientConfiguration getClientConfig(final NetconfClientSessionListener listener, final NetconfNode node) {
    final ReconnectStrategyFactory sf = new TimedReconnectStrategyFactory(eventExecutor, node.requireMaxConnectionAttempts().toJava(), node.requireBetweenAttemptsTimeoutMillis().toJava(), node.requireSleepFactor());
    final NetconfReconnectingClientConfigurationBuilder reconnectingClientConfigurationBuilder;
    final Protocol protocol = node.getProtocol();
    if (node.requireTcpOnly()) {
        reconnectingClientConfigurationBuilder = NetconfReconnectingClientConfigurationBuilder.create().withProtocol(NetconfClientConfiguration.NetconfClientProtocol.TCP).withAuthHandler(getHandlerFromCredentials(node.getCredentials()));
    } else if (protocol == null || protocol.getName() == Name.SSH) {
        reconnectingClientConfigurationBuilder = NetconfReconnectingClientConfigurationBuilder.create().withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH).withAuthHandler(getHandlerFromCredentials(node.getCredentials()));
    } else if (protocol.getName() == Name.TLS) {
        reconnectingClientConfigurationBuilder = NetconfReconnectingClientConfigurationBuilder.create().withSslHandlerFactory(new SslHandlerFactoryImpl(keystoreAdapter, protocol.getSpecification())).withProtocol(NetconfClientConfiguration.NetconfClientProtocol.TLS);
    } else {
        throw new IllegalStateException("Unsupported protocol type: " + protocol.getName());
    }
    if (node.getOdlHelloMessageCapabilities() != null) {
        reconnectingClientConfigurationBuilder.withOdlHelloCapabilities(node.getOdlHelloMessageCapabilities().getCapability());
    }
    return reconnectingClientConfigurationBuilder.withAddress(getSocketAddress(node.getHost(), node.getPort().getValue().toJava())).withConnectionTimeoutMillis(node.requireConnectionTimeoutMillis().toJava()).withReconnectStrategy(sf.createReconnectStrategy()).withConnectStrategyFactory(sf).withSessionListener(listener).build();
}
Also used : TimedReconnectStrategyFactory(org.opendaylight.netconf.nettyutil.TimedReconnectStrategyFactory) TimedReconnectStrategyFactory(org.opendaylight.netconf.nettyutil.TimedReconnectStrategyFactory) ReconnectStrategyFactory(org.opendaylight.netconf.nettyutil.ReconnectStrategyFactory) SslHandlerFactoryImpl(org.opendaylight.netconf.sal.connect.util.SslHandlerFactoryImpl) Protocol(org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.parameters.Protocol) NetconfReconnectingClientConfigurationBuilder(org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfigurationBuilder)

Example 3 with ReconnectStrategyFactory

use of org.opendaylight.netconf.nettyutil.ReconnectStrategyFactory in project netconf by opendaylight.

the class NetconfClientDispatcherImplTest method testNetconfClientDispatcherImpl.

@Test
public void testNetconfClientDispatcherImpl() throws Exception {
    EventLoopGroup bossGroup = Mockito.mock(EventLoopGroup.class);
    EventLoopGroup workerGroup = Mockito.mock(EventLoopGroup.class);
    Timer timer = new HashedWheelTimer();
    ChannelFuture chf = Mockito.mock(ChannelFuture.class);
    Channel ch = Mockito.mock(Channel.class);
    doReturn(ch).when(chf).channel();
    Throwable thr = Mockito.mock(Throwable.class);
    doReturn(chf).when(workerGroup).register(any(Channel.class));
    ChannelPromise promise = Mockito.mock(ChannelPromise.class);
    doReturn(promise).when(chf).addListener(any(GenericFutureListener.class));
    doReturn(thr).when(chf).cause();
    doReturn(true).when(chf).isDone();
    doReturn(false).when(chf).isSuccess();
    Long timeout = 200L;
    NetconfHelloMessageAdditionalHeader header = new NetconfHelloMessageAdditionalHeader("a", "host", "port", "trans", "id");
    NetconfClientSessionListener listener = new SimpleNetconfClientSessionListener();
    InetSocketAddress address = InetSocketAddress.createUnresolved("host", 830);
    ReconnectStrategyFactory reconnectStrategyFactory = Mockito.mock(ReconnectStrategyFactory.class);
    AuthenticationHandler handler = Mockito.mock(AuthenticationHandler.class);
    ReconnectStrategy reconnect = Mockito.mock(ReconnectStrategy.class);
    doReturn(5).when(reconnect).getConnectTimeout();
    doReturn("").when(reconnect).toString();
    doReturn("").when(handler).toString();
    doReturn("").when(reconnectStrategyFactory).toString();
    doReturn(reconnect).when(reconnectStrategyFactory).createReconnectStrategy();
    NetconfReconnectingClientConfiguration cfg = NetconfReconnectingClientConfigurationBuilder.create().withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH).withAddress(address).withConnectionTimeoutMillis(timeout).withReconnectStrategy(reconnect).withAdditionalHeader(header).withSessionListener(listener).withConnectStrategyFactory(reconnectStrategyFactory).withAuthHandler(handler).build();
    NetconfReconnectingClientConfiguration cfg2 = NetconfReconnectingClientConfigurationBuilder.create().withProtocol(NetconfClientConfiguration.NetconfClientProtocol.TCP).withAddress(address).withConnectionTimeoutMillis(timeout).withReconnectStrategy(reconnect).withAdditionalHeader(header).withSessionListener(listener).withConnectStrategyFactory(reconnectStrategyFactory).withAuthHandler(handler).build();
    NetconfClientDispatcherImpl dispatcher = new NetconfClientDispatcherImpl(bossGroup, workerGroup, timer);
    Future<NetconfClientSession> sshSession = dispatcher.createClient(cfg);
    Future<NetconfClientSession> tcpSession = dispatcher.createClient(cfg2);
    ReconnectFuture sshReconn = dispatcher.createReconnectingClient(cfg);
    final ReconnectFuture tcpReconn = dispatcher.createReconnectingClient(cfg2);
    assertNotNull(sshSession);
    assertNotNull(tcpSession);
    assertNotNull(sshReconn);
    assertNotNull(tcpReconn);
    SslHandlerFactory sslHandlerFactory = Mockito.mock(SslHandlerFactory.class);
    NetconfReconnectingClientConfiguration cfg3 = NetconfReconnectingClientConfigurationBuilder.create().withProtocol(NetconfClientConfiguration.NetconfClientProtocol.TLS).withAddress(address).withConnectionTimeoutMillis(timeout).withReconnectStrategy(reconnect).withAdditionalHeader(header).withSessionListener(listener).withConnectStrategyFactory(reconnectStrategyFactory).withSslHandlerFactory(sslHandlerFactory).build();
    Future<NetconfClientSession> tlsSession = dispatcher.createClient(cfg3);
    ReconnectFuture tlsReconn = dispatcher.createReconnectingClient(cfg3);
    assertNotNull(tlsSession);
    assertNotNull(tlsReconn);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NetconfHelloMessageAdditionalHeader(org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader) ReconnectStrategy(org.opendaylight.netconf.nettyutil.ReconnectStrategy) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) HashedWheelTimer(io.netty.util.HashedWheelTimer) ChannelPromise(io.netty.channel.ChannelPromise) EventLoopGroup(io.netty.channel.EventLoopGroup) ReconnectStrategyFactory(org.opendaylight.netconf.nettyutil.ReconnectStrategyFactory) HashedWheelTimer(io.netty.util.HashedWheelTimer) Timer(io.netty.util.Timer) ReconnectFuture(org.opendaylight.netconf.nettyutil.ReconnectFuture) AuthenticationHandler(org.opendaylight.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) NetconfReconnectingClientConfiguration(org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration) Test(org.junit.Test)

Example 4 with ReconnectStrategyFactory

use of org.opendaylight.netconf.nettyutil.ReconnectStrategyFactory in project netconf by opendaylight.

the class NetconfReconnectingClientConfigurationTest method testNetconfReconnectingClientConfiguration.

@Test
public void testNetconfReconnectingClientConfiguration() throws Exception {
    Long timeout = 200L;
    NetconfHelloMessageAdditionalHeader header = new NetconfHelloMessageAdditionalHeader("a", "host", "port", "trans", "id");
    NetconfClientSessionListener listener = new SimpleNetconfClientSessionListener();
    InetSocketAddress address = InetSocketAddress.createUnresolved("host", 830);
    ReconnectStrategyFactory strategy = Mockito.mock(ReconnectStrategyFactory.class);
    AuthenticationHandler handler = Mockito.mock(AuthenticationHandler.class);
    ReconnectStrategy reconnect = Mockito.mock(ReconnectStrategy.class);
    NetconfReconnectingClientConfiguration cfg = NetconfReconnectingClientConfigurationBuilder.create().withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH).withAddress(address).withConnectionTimeoutMillis(timeout).withReconnectStrategy(reconnect).withAdditionalHeader(header).withSessionListener(listener).withConnectStrategyFactory(strategy).withAuthHandler(handler).build();
    Assert.assertEquals(timeout, cfg.getConnectionTimeoutMillis());
    Assert.assertEquals(Optional.of(header), cfg.getAdditionalHeader());
    Assert.assertEquals(listener, cfg.getSessionListener());
    Assert.assertEquals(handler, cfg.getAuthHandler());
    Assert.assertEquals(strategy, cfg.getConnectStrategyFactory());
    Assert.assertEquals(NetconfClientConfiguration.NetconfClientProtocol.SSH, cfg.getProtocol());
    Assert.assertEquals(address, cfg.getAddress());
    Assert.assertEquals(reconnect, cfg.getReconnectStrategy());
    SslHandlerFactory sslHandlerFactory = Mockito.mock(SslHandlerFactory.class);
    NetconfReconnectingClientConfiguration cfg2 = NetconfReconnectingClientConfigurationBuilder.create().withProtocol(NetconfClientConfiguration.NetconfClientProtocol.TLS).withAddress(address).withConnectionTimeoutMillis(timeout).withReconnectStrategy(reconnect).withAdditionalHeader(header).withSessionListener(listener).withConnectStrategyFactory(strategy).withSslHandlerFactory(sslHandlerFactory).build();
    Assert.assertEquals(timeout, cfg2.getConnectionTimeoutMillis());
    Assert.assertEquals(Optional.of(header), cfg2.getAdditionalHeader());
    Assert.assertEquals(listener, cfg2.getSessionListener());
    Assert.assertEquals(sslHandlerFactory, cfg2.getSslHandlerFactory());
    Assert.assertEquals(strategy, cfg2.getConnectStrategyFactory());
    Assert.assertEquals(NetconfClientConfiguration.NetconfClientProtocol.TLS, cfg2.getProtocol());
    Assert.assertEquals(address, cfg2.getAddress());
    Assert.assertEquals(reconnect, cfg2.getReconnectStrategy());
}
Also used : NetconfHelloMessageAdditionalHeader(org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader) ReconnectStrategyFactory(org.opendaylight.netconf.nettyutil.ReconnectStrategyFactory) ReconnectStrategy(org.opendaylight.netconf.nettyutil.ReconnectStrategy) InetSocketAddress(java.net.InetSocketAddress) AuthenticationHandler(org.opendaylight.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler) NetconfReconnectingClientConfiguration(org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration) Test(org.junit.Test)

Aggregations

ReconnectStrategyFactory (org.opendaylight.netconf.nettyutil.ReconnectStrategyFactory)4 InetSocketAddress (java.net.InetSocketAddress)3 Test (org.junit.Test)2 NetconfHelloMessageAdditionalHeader (org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader)2 NetconfReconnectingClientConfiguration (org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration)2 NetconfReconnectingClientConfigurationBuilder (org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfigurationBuilder)2 ReconnectStrategy (org.opendaylight.netconf.nettyutil.ReconnectStrategy)2 TimedReconnectStrategyFactory (org.opendaylight.netconf.nettyutil.TimedReconnectStrategyFactory)2 AuthenticationHandler (org.opendaylight.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler)2 SslHandlerFactoryImpl (org.opendaylight.netconf.sal.connect.util.SslHandlerFactoryImpl)2 Protocol (org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.connection.parameters.Protocol)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Channel (io.netty.channel.Channel)1 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelPromise (io.netty.channel.ChannelPromise)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 HashedWheelTimer (io.netty.util.HashedWheelTimer)1 Timer (io.netty.util.Timer)1 GenericFutureListener (io.netty.util.concurrent.GenericFutureListener)1 BigDecimal (java.math.BigDecimal)1