2 * Copyright (C) 2019 Ericsson Software Technology AB. All rights reserved.
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
9 package org.opendaylight.netconf.test.tool;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.xmlunit.assertj.XmlAssert.assertThat;
15 import com.google.common.collect.ImmutableMap;
16 import io.netty.channel.nio.NioEventLoopGroup;
17 import io.netty.util.HashedWheelTimer;
18 import io.netty.util.concurrent.DefaultThreadFactory;
19 import io.netty.util.concurrent.GlobalEventExecutor;
21 import java.net.InetSocketAddress;
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.concurrent.TimeUnit;
26 import java.util.regex.Pattern;
27 import org.junit.AfterClass;
28 import org.junit.BeforeClass;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.opendaylight.netconf.api.NetconfMessage;
32 import org.opendaylight.netconf.api.xml.XmlUtil;
33 import org.opendaylight.netconf.auth.AuthProvider;
34 import org.opendaylight.netconf.client.NetconfClientDispatcher;
35 import org.opendaylight.netconf.client.NetconfClientDispatcherImpl;
36 import org.opendaylight.netconf.client.NetconfClientSession;
37 import org.opendaylight.netconf.client.NetconfClientSessionListener;
38 import org.opendaylight.netconf.client.SimpleNetconfClientSessionListener;
39 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration;
40 import org.opendaylight.netconf.client.conf.NetconfClientConfiguration.NetconfClientProtocol;
41 import org.opendaylight.netconf.client.conf.NetconfClientConfigurationBuilder;
42 import org.opendaylight.netconf.nettyutil.NeverReconnectStrategy;
43 import org.opendaylight.netconf.nettyutil.handler.ssh.authentication.LoginPasswordHandler;
44 import org.opendaylight.netconf.test.tool.config.Configuration;
45 import org.opendaylight.netconf.test.tool.config.ConfigurationBuilder;
46 import org.opendaylight.netconf.test.tool.config.YangResource;
47 import org.w3c.dom.Document;
49 @SuppressWarnings("SameParameterValue")
50 public class TestToolTest {
52 private static final long RECEIVE_TIMEOUT_MS = 5_000;
53 private static final int RANDOM_PORT = 0;
55 private static final User ADMIN_USER = new User("admin", "admin");
56 private static final File CUSTOM_RPC_CONFIG = new File("src/test/resources/customrpc.xml");
57 private static final Configuration SSH_SIMULATOR_CONFIG = getSimulatorConfig(NetconfClientProtocol.SSH,
59 private static final Configuration TCP_SIMULATOR_CONFIG = getSimulatorConfig(NetconfClientProtocol.SSH,
62 private static NioEventLoopGroup nettyGroup;
63 private static NetconfClientDispatcherImpl dispatcher;
67 public LogPropertyCatcher logPropertyCatcher =
68 new LogPropertyCatcher(Pattern.compile("(start\\(\\) listen on auto-allocated port="
69 + "|Simulated TCP device started on (/0:0:0:0:0:0:0:0|/0.0.0.0):)(\\d+)"));
71 private static final String XML_REQUEST_RFC7950_SECTION_4_2_9 = "<rpc message-id=\"101\"\n"
72 + " xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
73 + " <activate-software-image xmlns=\"http://example.com/system\">\n"
74 + " <image-name>example-fw-2.3</image-name>\n"
75 + " </activate-software-image>\n"
77 private static final String EXPECTED_XML_RESPONSE_RFC7950_SECTION_4_2_9 = "<rpc-reply message-id=\"101\"\n"
78 + " xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
79 + " <status xmlns=\"http://example.com/system\">\n"
80 + " The image example-fw-2.3 is being installed.\n"
83 private static final String XML_REQUEST_RFC7950_SECTION_7_15_3 = "<rpc message-id=\"101\"\n"
84 + " xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
85 + " <action xmlns=\"urn:ietf:params:xml:ns:yang:1\">\n"
86 + " <server xmlns=\"urn:example:server-farm\">\n"
87 + " <name>apache-1</name>\n"
89 + " <reset-at>2014-07-29T13:42:00Z</reset-at>\n"
94 private static final String EXPECTED_XML_RESPONSE_RFC7950_SECTION_7_15_3 = "<rpc-reply message-id=\"101\"\n"
95 + " xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n"
96 + " <reset-finished-at xmlns=\"urn:example:server-farm\">\n"
97 + " 2014-07-29T13:42:12Z\n"
98 + " </reset-finished-at>\n"
100 private static final Map<String, String> PREFIX_2_URI = ImmutableMap.of(
101 "base10", "urn:ietf:params:xml:ns:netconf:base:1.0",
102 "ncmon", "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring"
106 public static void setUpClass() {
107 HashedWheelTimer hashedWheelTimer = new HashedWheelTimer();
108 nettyGroup = new NioEventLoopGroup(1, new DefaultThreadFactory(NetconfClientDispatcher.class));
109 dispatcher = new NetconfClientDispatcherImpl(nettyGroup, nettyGroup, hashedWheelTimer);
113 public static void cleanUpClass()
114 throws InterruptedException {
115 nettyGroup.shutdownGracefully().sync();
119 public void customRpcOverSsh()
121 Document docResponse = invokeRpc(SSH_SIMULATOR_CONFIG, XML_REQUEST_RFC7950_SECTION_7_15_3);
122 assertThat(docResponse)
123 .and(EXPECTED_XML_RESPONSE_RFC7950_SECTION_7_15_3)
129 public void customRpcOverTcp()
131 Document docResponse = invokeRpc(TCP_SIMULATOR_CONFIG, XML_REQUEST_RFC7950_SECTION_4_2_9);
132 assertThat(docResponse)
133 .and(EXPECTED_XML_RESPONSE_RFC7950_SECTION_4_2_9)
139 public void shouldSupportGetSchema()
141 String getSchema = "<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"m-0\">\n"
143 + " <filter xmlns:ns0=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ns0:type=\"subtree\">\n"
144 + " <netconf-state xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n"
146 + " </netconf-state>\n"
150 Document docResponse = invokeRpc(TCP_SIMULATOR_CONFIG, getSchema);
151 Set<YangResource> expectedYangResources = Configuration.DEFAULT_YANG_RESOURCES;
152 assertEquals(4, expectedYangResources.size());
153 assertThat(docResponse)
154 .withNamespaceContext(PREFIX_2_URI)
155 .valueByXPath("count(//base10:rpc-reply/base10:data/ncmon:netconf-state/ncmon:schemas/ncmon:schema)")
156 .isEqualTo(expectedYangResources.size());
159 private Document invokeRpc(final Configuration simulatorConfig, final String xmlRequest)
162 int localPort = launchSimulator(simulatorConfig);
163 SimpleNetconfClientSessionListener sessionListener = new SimpleNetconfClientSessionListener();
164 NetconfClientConfiguration clientConfig = getClientConfig("localhost", localPort,
165 simulatorConfig, sessionListener);
166 Document docRequest = XmlUtil.readXmlToDocument(xmlRequest);
167 NetconfMessage request = new NetconfMessage(docRequest);
170 NetconfMessage response;
171 try (NetconfClientSession ignored = dispatcher.createClient(clientConfig).get()) {
172 response = sessionListener.sendRequest(request)
173 .get(RECEIVE_TIMEOUT_MS, TimeUnit.MILLISECONDS);
177 assertNotNull(response);
178 return response.getDocument();
181 private static final ConcurrentHashMap<Configuration, Integer> CACHED_SIMULATORS = new ConcurrentHashMap<>();
184 * Retrieves a previously launched simulator or launches a new one using the given configuration.
186 * @param configuration The simulator configuration.
187 * @return The TCP port number to access the launched simulator.
189 private int launchSimulator(final Configuration configuration) {
190 return CACHED_SIMULATORS.computeIfAbsent(configuration, cfg -> {
191 NetconfDeviceSimulator simulator = new NetconfDeviceSimulator(cfg);
193 return logPropertyCatcher.getLastValue()
194 .map(Integer::parseInt)
195 .orElseThrow(() -> new IllegalArgumentException("Unable to capture auto-allocated port from log"));
199 @SuppressWarnings("deprecation")
200 private static Configuration getSimulatorConfig(final NetconfClientProtocol protocol, final User user) {
201 return new ConfigurationBuilder()
202 .setStartingPort(RANDOM_PORT)
203 .setRpcConfigFile(CUSTOM_RPC_CONFIG)
204 .setSsh(protocol == NetconfClientProtocol.SSH)
205 .setAuthProvider(new InMemoryAuthenticationProvider(user))
209 @SuppressWarnings("deprecation")
210 private static NetconfClientConfiguration getClientConfig(final String host, final int port,
211 final Configuration simulatorConfig,
212 final NetconfClientSessionListener sessionListener) {
213 User user = ((InMemoryAuthenticationProvider) simulatorConfig.getAuthProvider()).user;
214 return NetconfClientConfigurationBuilder.create()
215 .withAddress(new InetSocketAddress(host, port))
216 .withSessionListener(sessionListener)
217 .withReconnectStrategy(new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE,
218 NetconfClientConfigurationBuilder.DEFAULT_CONNECTION_TIMEOUT_MILLIS))
219 .withProtocol(simulatorConfig.isSsh() ? NetconfClientProtocol.SSH : NetconfClientProtocol.TCP)
220 .withAuthHandler(new LoginPasswordHandler(user.username, user.password))
224 private static final class User {
225 private final String username;
226 private final String password;
228 private User(final String username, final String password) {
229 this.username = username;
230 this.password = password;
234 private static final class InMemoryAuthenticationProvider implements AuthProvider {
236 private final User user;
238 private InMemoryAuthenticationProvider(final User user) {
243 public boolean authenticated(final String username, final String password) {
244 return user.username.equals(username) && user.password.equals(password);