Search in sources :

Example 1 with NetconfReconnectingClientConfiguration

use of org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration in project netconf by opendaylight.

the class RemoteDeviceConnectorImplTest method testGetClientConfig.

@Test
public void testGetClientConfig() {
    final NetconfClientSessionListener listener = mock(NetconfClientSessionListener.class);
    final Host host = new Host(new IpAddress(new Ipv4Address("127.0.0.1")));
    final PortNumber portNumber = new PortNumber(Uint16.valueOf(9999));
    final NetconfNode testingNode = new NetconfNodeBuilder().setConnectionTimeoutMillis(Uint32.valueOf(1000)).setDefaultRequestTimeoutMillis(Uint32.valueOf(2000)).setHost(host).setPort(portNumber).setCredentials(new LoginPasswordBuilder().setUsername("testuser").setPassword("testpassword").build()).setTcpOnly(true).build();
    final RemoteDeviceConnectorImpl remoteDeviceConnection = new RemoteDeviceConnectorImpl(builder.build(), remoteDeviceId, deviceActionFactory);
    final NetconfReconnectingClientConfiguration defaultClientConfig = remoteDeviceConnection.getClientConfig(listener, testingNode);
    assertEquals(defaultClientConfig.getConnectionTimeoutMillis().longValue(), 1000L);
    assertEquals(defaultClientConfig.getAddress(), new InetSocketAddress(InetAddresses.forString("127.0.0.1"), 9999));
    assertSame(defaultClientConfig.getSessionListener(), listener);
    assertEquals(defaultClientConfig.getAuthHandler().getUsername(), "testuser");
    assertEquals(defaultClientConfig.getProtocol(), NetconfClientConfiguration.NetconfClientProtocol.TCP);
}
Also used : NetconfClientSessionListener(org.opendaylight.netconf.client.NetconfClientSessionListener) NetconfNodeBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder) InetSocketAddress(java.net.InetSocketAddress) NetconfNode(org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode) Host(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host) IpAddress(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress) LoginPasswordBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.netconf.node.credentials.credentials.LoginPasswordBuilder) PortNumber(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber) NetconfReconnectingClientConfiguration(org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration) Ipv4Address(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address) Test(org.junit.Test)

Example 2 with NetconfReconnectingClientConfiguration

use of org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration 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 3 with NetconfReconnectingClientConfiguration

use of org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration 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)

Example 4 with NetconfReconnectingClientConfiguration

use of org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration in project netconf by opendaylight.

the class NetconfDeviceCommunicatorTest method testNetconfDeviceReconnectInCommunicator.

/**
 * Test whether reconnect is scheduled properly.
 */
@Test
public void testNetconfDeviceReconnectInCommunicator() {
    final RemoteDevice<NetconfSessionPreferences, NetconfMessage, NetconfDeviceCommunicator> device = mock(RemoteDevice.class);
    final TimedReconnectStrategy timedReconnectStrategy = new TimedReconnectStrategy(GlobalEventExecutor.INSTANCE, 10000, 0, 1.0, null, 100L, null);
    final ReconnectStrategy reconnectStrategy = spy(new ReconnectStrategy() {

        @Override
        @Deprecated
        public int getConnectTimeout() throws Exception {
            return timedReconnectStrategy.getConnectTimeout();
        }

        @Override
        @Deprecated
        public Future<Void> scheduleReconnect(final Throwable cause) {
            return timedReconnectStrategy.scheduleReconnect(cause);
        }

        @Override
        @Deprecated
        public void reconnectSuccessful() {
            timedReconnectStrategy.reconnectSuccessful();
        }
    });
    final EventLoopGroup group = new NioEventLoopGroup();
    final Timer time = new HashedWheelTimer();
    try {
        final NetconfDeviceCommunicator listener = new NetconfDeviceCommunicator(new RemoteDeviceId("test", InetSocketAddress.createUnresolved("localhost", 22)), device, 10);
        final NetconfReconnectingClientConfiguration cfg = NetconfReconnectingClientConfigurationBuilder.create().withAddress(new InetSocketAddress("localhost", 65000)).withReconnectStrategy(reconnectStrategy).withConnectStrategyFactory(() -> reconnectStrategy).withAuthHandler(new LoginPasswordHandler("admin", "admin")).withConnectionTimeoutMillis(10000).withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH).withSessionListener(listener).build();
        listener.initializeRemoteConnection(new NetconfClientDispatcherImpl(group, group, time), cfg);
        verify(reconnectStrategy, timeout(TimeUnit.MINUTES.toMillis(4)).times(101)).scheduleReconnect(any(Throwable.class));
    } finally {
        time.stop();
        group.shutdownGracefully();
    }
}
Also used : TimedReconnectStrategy(org.opendaylight.netconf.nettyutil.TimedReconnectStrategy) ReconnectStrategy(org.opendaylight.netconf.nettyutil.ReconnectStrategy) InetSocketAddress(java.net.InetSocketAddress) HashedWheelTimer(io.netty.util.HashedWheelTimer) NetconfClientDispatcherImpl(org.opendaylight.netconf.client.NetconfClientDispatcherImpl) TimeoutException(java.util.concurrent.TimeoutException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) RemoteDeviceId(org.opendaylight.netconf.sal.connect.util.RemoteDeviceId) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) LoginPasswordHandler(org.opendaylight.netconf.nettyutil.handler.ssh.authentication.LoginPasswordHandler) HashedWheelTimer(io.netty.util.HashedWheelTimer) Timer(io.netty.util.Timer) NetconfMessage(org.opendaylight.netconf.api.NetconfMessage) TimedReconnectStrategy(org.opendaylight.netconf.nettyutil.TimedReconnectStrategy) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future) NetconfReconnectingClientConfiguration(org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.Test)

Example 5 with NetconfReconnectingClientConfiguration

use of org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration in project netconf by opendaylight.

the class AbstractNetconfTopology method setupConnection.

protected ListenableFuture<NetconfDeviceCapabilities> setupConnection(final NodeId nodeId, final Node configNode) {
    final NetconfNode netconfNode = configNode.augmentation(NetconfNode.class);
    final NetconfNodeAugmentedOptional nodeOptional = configNode.augmentation(NetconfNodeAugmentedOptional.class);
    requireNonNull(netconfNode.getHost());
    requireNonNull(netconfNode.getPort());
    final NetconfConnectorDTO deviceCommunicatorDTO = createDeviceCommunicator(nodeId, netconfNode, nodeOptional);
    final NetconfDeviceCommunicator deviceCommunicator = deviceCommunicatorDTO.getCommunicator();
    final NetconfClientSessionListener netconfClientSessionListener = deviceCommunicatorDTO.getSessionListener();
    final NetconfReconnectingClientConfiguration clientConfig = getClientConfig(netconfClientSessionListener, netconfNode);
    final ListenableFuture<NetconfDeviceCapabilities> future = deviceCommunicator.initializeRemoteConnection(clientDispatcher, clientConfig);
    activeConnectors.put(nodeId, deviceCommunicatorDTO);
    Futures.addCallback(future, new FutureCallback<NetconfDeviceCapabilities>() {

        @Override
        public void onSuccess(final NetconfDeviceCapabilities result) {
            LOG.debug("Connector for {} started succesfully", nodeId.getValue());
        }

        @Override
        public void onFailure(final Throwable throwable) {
            LOG.error("Connector for {} failed", nodeId.getValue(), throwable);
        // remove this node from active connectors?
        }
    }, MoreExecutors.directExecutor());
    return future;
}
Also used : NetconfClientSessionListener(org.opendaylight.netconf.client.NetconfClientSessionListener) NetconfNodeAugmentedOptional(org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.optional.rev190614.NetconfNodeAugmentedOptional) NetconfDeviceCommunicator(org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator) NetconfDeviceCapabilities(org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities) NetconfNode(org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode) NetconfReconnectingClientConfiguration(org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration)

Aggregations

NetconfReconnectingClientConfiguration (org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration)7 Test (org.junit.Test)5 InetSocketAddress (java.net.InetSocketAddress)4 NetconfClientSessionListener (org.opendaylight.netconf.client.NetconfClientSessionListener)4 ReconnectStrategy (org.opendaylight.netconf.nettyutil.ReconnectStrategy)3 NetconfNode (org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode)3 ChannelFuture (io.netty.channel.ChannelFuture)2 EventLoopGroup (io.netty.channel.EventLoopGroup)2 HashedWheelTimer (io.netty.util.HashedWheelTimer)2 Timer (io.netty.util.Timer)2 NetconfHelloMessageAdditionalHeader (org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader)2 ReconnectStrategyFactory (org.opendaylight.netconf.nettyutil.ReconnectStrategyFactory)2 AuthenticationHandler (org.opendaylight.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler)2 NetconfDeviceCapabilities (org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities)2 NetconfDeviceCommunicator (org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator)2 Host (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host)2 IpAddress (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress)2 Ipv4Address (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address)2 PortNumber (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber)2 NetconfNodeBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNodeBuilder)2