2 * Copyright (c) 2013 Cisco Systems, Inc. and others. 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.controller.netconf.it;
11 import static java.util.Arrays.asList;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.anyString;
14 import static org.mockito.Mockito.doAnswer;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
18 import ch.ethz.ssh2.Connection;
19 import io.netty.channel.ChannelFuture;
20 import io.netty.channel.EventLoopGroup;
21 import io.netty.channel.nio.NioEventLoopGroup;
22 import io.netty.util.concurrent.GlobalEventExecutor;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.lang.management.ManagementFactory;
26 import java.net.InetSocketAddress;
27 import java.util.Collection;
28 import java.util.List;
29 import junit.framework.Assert;
30 import org.junit.After;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.invocation.InvocationOnMock;
34 import org.mockito.stubbing.Answer;
35 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
36 import org.opendaylight.controller.config.spi.ModuleFactory;
37 import org.opendaylight.controller.netconf.api.NetconfMessage;
38 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
39 import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
40 import org.opendaylight.controller.netconf.client.SimpleNetconfClientSessionListener;
41 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration;
42 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfigurationBuilder;
43 import org.opendaylight.controller.netconf.client.test.TestingNetconfClient;
44 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
45 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreException;
46 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
47 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
48 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
49 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler;
50 import org.opendaylight.controller.netconf.ssh.NetconfSSHServer;
51 import org.opendaylight.controller.netconf.ssh.authentication.AuthProvider;
52 import org.opendaylight.controller.netconf.ssh.authentication.PEMGenerator;
53 import org.opendaylight.controller.netconf.util.messages.NetconfMessageUtil;
54 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
55 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
56 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
57 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
59 public class NetconfITSecureTest extends AbstractNetconfConfigTest {
61 private static final InetSocketAddress tlsAddress = new InetSocketAddress("127.0.0.1", 12024);
63 private DefaultCommitNotificationProducer commitNot;
64 private NetconfSSHServer sshServer;
65 private NetconfMessage getConfig;
68 public void setUp() throws Exception {
69 this.getConfig = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
71 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, getModuleFactories().toArray(
72 new ModuleFactory[0])));
74 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
75 factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
77 commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
80 final NetconfServerDispatcher dispatchS = createDispatcher(factoriesListener);
81 ChannelFuture s = dispatchS.createLocalServer(NetconfConfigUtil.getNetconfLocalAddress());
83 EventLoopGroup bossGroup = new NioEventLoopGroup();
84 sshServer = NetconfSSHServer.start(tlsAddress.getPort(), NetconfConfigUtil.getNetconfLocalAddress(), getAuthProvider(), bossGroup);
87 private NetconfServerDispatcher createDispatcher(NetconfOperationServiceFactoryListenerImpl factoriesListener) {
88 return super.createDispatcher(factoriesListener, NetconfITTest.getNetconfMonitoringListenerService(), commitNot);
92 public void tearDown() throws Exception {
97 private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
98 final Collection<InputStream> yangDependencies = NetconfITTest.getBasicYangs();
99 return new HardcodedYangStoreService(yangDependencies);
102 protected List<ModuleFactory> getModuleFactories() {
103 return asList(NetconfITTest.FACTORIES);
107 public void testSecure() throws Exception {
108 NetconfClientDispatcher dispatch = new NetconfClientDispatcherImpl(getNettyThreadgroup(), getNettyThreadgroup(), getHashedWheelTimer());
109 try (TestingNetconfClient netconfClient = new TestingNetconfClient("testing-ssh-client", dispatch, getClientConfiguration())) {
110 NetconfMessage response = netconfClient.sendMessage(getConfig);
111 Assert.assertFalse("Unexpected error message " + XmlUtil.toString(response.getDocument()),
112 NetconfMessageUtil.isErrorMessage(response));
114 NetconfMessage gs = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc message-id=\"2\"\n" +
115 " xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
116 " <get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
117 " <identifier>config</identifier>\n" +
121 response = netconfClient.sendMessage(gs);
122 Assert.assertFalse("Unexpected error message " + XmlUtil.toString(response.getDocument()),
123 NetconfMessageUtil.isErrorMessage(response));
127 public NetconfClientConfiguration getClientConfiguration() throws IOException {
128 final NetconfClientConfigurationBuilder b = NetconfClientConfigurationBuilder.create();
129 b.withAddress(tlsAddress);
130 b.withSessionListener(new SimpleNetconfClientSessionListener());
131 b.withReconnectStrategy(new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000));
132 b.withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH);
133 b.withConnectionTimeoutMillis(5000);
134 b.withAuthHandler(getAuthHandler());
138 public AuthProvider getAuthProvider() throws Exception {
139 AuthProvider mock = mock(AuthProvider.class);
140 doReturn(true).when(mock).authenticated(anyString(), anyString());
141 doReturn(PEMGenerator.generate().toCharArray()).when(mock).getPEMAsCharArray();
145 public AuthenticationHandler getAuthHandler() throws IOException {
146 final AuthenticationHandler authHandler = mock(AuthenticationHandler.class);
147 doAnswer(new Answer() {
149 public Object answer(final InvocationOnMock invocation) throws Throwable {
150 Connection conn = (Connection) invocation.getArguments()[0];
151 conn.authenticateWithPassword("user", "pwd");
154 }).when(authHandler).authenticate(any(Connection.class));
155 doReturn("auth handler").when(authHandler).toString();