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 org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyString;
13 import static org.mockito.Mockito.doAnswer;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.lang.management.ManagementFactory;
21 import java.net.InetSocketAddress;
22 import java.nio.file.Files;
23 import java.util.Collection;
24 import java.util.List;
26 import junit.framework.Assert;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.invocation.InvocationOnMock;
32 import org.mockito.stubbing.Answer;
33 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
34 import org.opendaylight.controller.config.spi.ModuleFactory;
35 import org.opendaylight.controller.netconf.api.NetconfMessage;
36 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
37 import org.opendaylight.controller.netconf.client.NetconfSshClientDispatcher;
38 import org.opendaylight.controller.netconf.client.test.TestingNetconfClient;
39 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
40 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreException;
41 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
42 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
43 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
44 import org.opendaylight.controller.netconf.ssh.NetconfSSHServer;
45 import org.opendaylight.controller.netconf.ssh.authentication.AuthProvider;
46 import org.opendaylight.controller.netconf.ssh.authentication.PEMGenerator;
47 import org.opendaylight.controller.netconf.util.handler.ssh.authentication.AuthenticationHandler;
48 import org.opendaylight.controller.netconf.util.messages.NetconfMessageUtil;
49 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
50 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
51 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
52 import org.opendaylight.controller.usermanager.IUserManager;
54 import ch.ethz.ssh2.Connection;
55 import io.netty.channel.ChannelFuture;
57 public class NetconfITSecureTest extends AbstractNetconfConfigTest {
59 private static final InetSocketAddress tlsAddress = new InetSocketAddress("127.0.0.1", 12024);
60 private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023);
62 private DefaultCommitNotificationProducer commitNot;
63 private NetconfServerDispatcher dispatchS;
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 dispatchS = createDispatcher(factoriesListener);
81 ChannelFuture s = dispatchS.createServer(tcpAddress);
84 sshServer = NetconfSSHServer.start(tlsAddress.getPort(), tcpAddress, getAuthProvider());
85 Thread thread = new Thread(sshServer);
86 thread.setDaemon(true);
90 private NetconfServerDispatcher createDispatcher(NetconfOperationServiceFactoryListenerImpl factoriesListener) {
91 return super.createDispatcher(factoriesListener, NetconfITTest.getNetconfMonitoringListenerService(), commitNot);
95 public void tearDown() throws Exception {
100 private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
101 final Collection<InputStream> yangDependencies = NetconfITTest.getBasicYangs();
102 return new HardcodedYangStoreService(yangDependencies);
105 protected List<ModuleFactory> getModuleFactories() {
106 return NetconfITTest.getModuleFactoriesS();
110 public void testSecure() throws Exception {
111 NetconfClientDispatcher dispatch = new NetconfSshClientDispatcher(getAuthHandler(), nettyThreadgroup, nettyThreadgroup, 5000);
112 try (TestingNetconfClient netconfClient = new TestingNetconfClient("tls-client", tlsAddress, 4000, dispatch)) {
113 NetconfMessage response = netconfClient.sendMessage(getConfig);
114 Assert.assertFalse("Unexpected error message " + XmlUtil.toString(response.getDocument()),
115 NetconfMessageUtil.isErrorMessage(response));
121 public AuthProvider getAuthProvider() throws Exception {
122 final IUserManager userManager = mock(IUserManager.class);
123 doReturn(AuthResultEnum.AUTH_ACCEPT).when(userManager).authenticate(anyString(), anyString());
125 final File privateKeyFile = Files.createTempFile("tmp-netconf-test", "pk").toFile();
126 privateKeyFile.deleteOnExit();
127 String privateKeyPEMString = PEMGenerator.generateTo(privateKeyFile);
128 return new AuthProvider(userManager, privateKeyPEMString);
131 public AuthenticationHandler getAuthHandler() throws IOException {
132 final AuthenticationHandler authHandler = mock(AuthenticationHandler.class);
133 doAnswer(new Answer() {
135 public Object answer(final InvocationOnMock invocation) throws Throwable {
136 Connection conn = (Connection) invocation.getArguments()[0];
137 conn.authenticateWithPassword("user", "pwd");
140 }).when(authHandler).authenticate(any(Connection.class));