Split out odl-netconf-device.yang
[netconf.git] / netconf / netconf-topology-singleton / src / test / java / org / opendaylight / netconf / topology / singleton / impl / RemoteDeviceConnectorImplTest.java
1 /*
2  * Copyright (c) 2016 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.netconf.topology.singleton.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertSame;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16
17 import akka.actor.ActorSystem;
18 import akka.util.Timeout;
19 import com.google.common.net.InetAddresses;
20 import com.google.common.util.concurrent.ListeningExecutorService;
21 import io.netty.util.concurrent.EventExecutor;
22 import java.net.InetSocketAddress;
23 import java.util.concurrent.ScheduledExecutorService;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.opendaylight.mdsal.binding.api.DataBroker;
30 import org.opendaylight.mdsal.binding.api.TransactionChain;
31 import org.opendaylight.mdsal.binding.api.WriteTransaction;
32 import org.opendaylight.mdsal.dom.api.DOMRpcProviderService;
33 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
34 import org.opendaylight.netconf.client.NetconfClientDispatcher;
35 import org.opendaylight.netconf.client.NetconfClientSessionListener;
36 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
37 import org.opendaylight.netconf.client.conf.NetconfReconnectingClientConfiguration;
38 import org.opendaylight.netconf.sal.connect.api.DeviceActionFactory;
39 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
40 import org.opendaylight.netconf.sal.connect.impl.DefaultSchemaResourceManager;
41 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCommunicator;
42 import org.opendaylight.netconf.sal.connect.netconf.sal.KeepaliveSalFacade;
43 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
44 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
45 import org.opendaylight.netconf.topology.spi.NetconfConnectorDTO;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Host;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.credentials.Credentials;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.device.rev221225.credentials.credentials.LoginPasswordBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.NetconfNode;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev221225.NetconfNodeBuilder;
54 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
55 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeBuilder;
56 import org.opendaylight.yangtools.yang.common.Uint16;
57 import org.opendaylight.yangtools.yang.common.Uint32;
58 import org.opendaylight.yangtools.yang.parser.impl.DefaultYangParserFactory;
59 import scala.concurrent.duration.Duration;
60
61 @RunWith(MockitoJUnitRunner.StrictStubs.class)
62 public class RemoteDeviceConnectorImplTest extends AbstractBaseSchemasTest {
63
64     private static final NodeId NODE_ID = new NodeId("testing-node");
65     private static final String TOPOLOGY_ID = "testing-topology";
66     private static final Timeout TIMEOUT = new Timeout(Duration.create(5, "seconds"));
67
68     @Mock
69     private DataBroker dataBroker;
70
71     @Mock
72     private DOMRpcProviderService rpcProviderRegistry;
73
74     @Mock
75     private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
76
77     @Mock
78     private ScheduledExecutorService keepaliveExecutor;
79
80     @Mock
81     private ListeningExecutorService processingExecutor;
82
83     @Mock
84     private ActorSystem actorSystem;
85
86     @Mock
87     private EventExecutor eventExecutor;
88
89     @Mock
90     private NetconfClientDispatcher clientDispatcher;
91
92     @Mock
93     private TransactionChain txChain;
94
95     @Mock
96     private WriteTransaction writeTx;
97
98     @Mock
99     private DeviceActionFactory deviceActionFactory;
100
101     private NetconfTopologySetup.NetconfTopologySetupBuilder builder;
102     private RemoteDeviceId remoteDeviceId;
103
104     @Before
105     public void setUp() {
106         remoteDeviceId = new RemoteDeviceId(TOPOLOGY_ID,
107                 new InetSocketAddress(InetAddresses.forString("127.0.0.1"), 9999));
108
109         builder = new NetconfTopologySetup.NetconfTopologySetupBuilder()
110                 .setBaseSchemas(BASE_SCHEMAS)
111                 .setDataBroker(dataBroker)
112                 .setRpcProviderRegistry(rpcProviderRegistry)
113                 .setClusterSingletonServiceProvider(clusterSingletonServiceProvider)
114                 .setKeepaliveExecutor(keepaliveExecutor)
115                 .setProcessingExecutor(processingExecutor)
116                 .setActorSystem(actorSystem)
117                 .setEventExecutor(eventExecutor)
118                 .setNetconfClientDispatcher(clientDispatcher)
119                 .setTopologyId(TOPOLOGY_ID);
120     }
121
122     @Test
123     public void testStopRemoteDeviceConnection() {
124         builder.setNode(new NodeBuilder().setNodeId(NODE_ID)
125             .addAugmentation(new NetconfNodeBuilder()
126                 .setHost(new Host(new IpAddress(new Ipv4Address("127.0.0.1"))))
127                 .setPort(new PortNumber(Uint16.valueOf(9999)))
128                 .setReconnectOnChangedSchema(true)
129                 .setDefaultRequestTimeoutMillis(Uint32.valueOf(1000))
130                 .setBetweenAttemptsTimeoutMillis(Uint16.valueOf(100))
131                 .setSchemaless(false)
132                 .setTcpOnly(false)
133                 .setCredentials(new LoginPasswordBuilder().setPassword("admin").setUsername("admin").build())
134                 .build())
135             .build());
136
137         final NetconfDeviceCommunicator communicator = mock(NetconfDeviceCommunicator.class);
138         final RemoteDeviceHandler salFacade = mock(RemoteDeviceHandler.class);
139
140         final TestingRemoteDeviceConnectorImpl remoteDeviceConnection = new TestingRemoteDeviceConnectorImpl(
141             builder.build(), remoteDeviceId, communicator, deviceActionFactory);
142
143         remoteDeviceConnection.startRemoteDeviceConnection(salFacade);
144
145         remoteDeviceConnection.stopRemoteDeviceConnection();
146
147         verify(communicator, times(1)).close();
148         verify(salFacade, times(1)).close();
149     }
150
151     @Test
152     public void testKeapAliveFacade() {
153         final Credentials credentials = new LoginPasswordBuilder()
154                 .setPassword("admin").setUsername("admin").build();
155         final NetconfNode netconfNode = new NetconfNodeBuilder()
156                 .setHost(new Host(new IpAddress(new Ipv4Address("127.0.0.1"))))
157                 .setPort(new PortNumber(Uint16.valueOf(9999)))
158                 .setReconnectOnChangedSchema(true)
159                 .setDefaultRequestTimeoutMillis(Uint32.valueOf(1000))
160                 .setBetweenAttemptsTimeoutMillis(Uint16.valueOf(100))
161                 .setSchemaless(false)
162                 .setTcpOnly(false)
163                 .setCredentials(credentials)
164                 .setKeepaliveDelay(Uint32.ONE)
165                 .build();
166
167         builder.setSchemaResourceDTO(new DefaultSchemaResourceManager(new DefaultYangParserFactory())
168             .getSchemaResources(netconfNode.getSchemaCacheDirectory(), "foo"));
169
170         final RemoteDeviceConnectorImpl remoteDeviceConnection =
171                 new RemoteDeviceConnectorImpl(builder.build(), remoteDeviceId, deviceActionFactory);
172
173         final RemoteDeviceHandler salFacade = mock(RemoteDeviceHandler.class);
174
175         final NetconfConnectorDTO connectorDTO =
176                 remoteDeviceConnection.createDeviceCommunicator(NODE_ID, netconfNode, salFacade);
177
178         assertTrue(connectorDTO.getFacade() instanceof KeepaliveSalFacade);
179     }
180
181     @Test
182     public void testGetClientConfig() {
183         final NetconfClientSessionListener listener = mock(NetconfClientSessionListener.class);
184         final Host host = new Host(new IpAddress(new Ipv4Address("127.0.0.1")));
185         final PortNumber portNumber = new PortNumber(Uint16.valueOf(9999));
186         final NetconfNode testingNode = new NetconfNodeBuilder()
187                 .setConnectionTimeoutMillis(Uint32.valueOf(1000))
188                 .setDefaultRequestTimeoutMillis(Uint32.valueOf(2000))
189                 .setHost(host)
190                 .setPort(portNumber)
191                 .setCredentials(new LoginPasswordBuilder()
192                         .setUsername("testuser")
193                         .setPassword("testpassword").build())
194                 .setTcpOnly(true)
195                 .build();
196
197         final RemoteDeviceConnectorImpl remoteDeviceConnection =
198                 new RemoteDeviceConnectorImpl(builder.build(), remoteDeviceId, deviceActionFactory);
199
200         final NetconfReconnectingClientConfiguration defaultClientConfig =
201                 remoteDeviceConnection.getClientConfig(listener, testingNode);
202
203         assertEquals(defaultClientConfig.getConnectionTimeoutMillis().longValue(), 1000L);
204         assertEquals(defaultClientConfig.getAddress(), new InetSocketAddress(InetAddresses.forString("127.0.0.1"),
205             9999));
206         assertSame(defaultClientConfig.getSessionListener(), listener);
207         assertEquals(defaultClientConfig.getAuthHandler().getUsername(), "testuser");
208         assertEquals(defaultClientConfig.getProtocol(), NetconfClientConfiguration.NetconfClientProtocol.TCP);
209     }
210 }