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.anyString;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
16 import io.netty.channel.ChannelFuture;
17 import io.netty.channel.EventLoopGroup;
18 import io.netty.channel.nio.NioEventLoopGroup;
19 import io.netty.util.concurrent.GlobalEventExecutor;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.lang.management.ManagementFactory;
23 import java.net.InetSocketAddress;
24 import java.util.Collection;
25 import java.util.List;
26 import junit.framework.Assert;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
31 import org.opendaylight.controller.config.spi.ModuleFactory;
32 import org.opendaylight.controller.netconf.api.NetconfMessage;
33 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
34 import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
35 import org.opendaylight.controller.netconf.client.SimpleNetconfClientSessionListener;
36 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration;
37 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfigurationBuilder;
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.nettyutil.handler.ssh.authentication.AuthenticationHandler;
45 import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.LoginPassword;
46 import org.opendaylight.controller.netconf.ssh.NetconfSSHServer;
47 import org.opendaylight.controller.netconf.ssh.authentication.AuthProvider;
48 import org.opendaylight.controller.netconf.ssh.authentication.AuthProviderImpl;
49 import org.opendaylight.controller.netconf.ssh.authentication.PEMGenerator;
50 import org.opendaylight.controller.netconf.util.messages.NetconfMessageUtil;
51 import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil;
52 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
53 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
54 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
56 public class NetconfITSecureTest extends AbstractNetconfConfigTest {
58 private static final InetSocketAddress tlsAddress = new InetSocketAddress("127.0.0.1", 12024);
60 private DefaultCommitNotificationProducer commitNot;
61 private NetconfSSHServer sshServer;
62 private NetconfMessage getConfig;
65 public void setUp() throws Exception {
66 this.getConfig = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
68 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, getModuleFactories().toArray(
69 new ModuleFactory[0])));
71 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
72 factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
74 commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
77 final NetconfServerDispatcher dispatchS = createDispatcher(factoriesListener);
78 ChannelFuture s = dispatchS.createLocalServer(NetconfConfigUtil.getNetconfLocalAddress());
80 EventLoopGroup bossGroup = new NioEventLoopGroup();
81 sshServer = NetconfSSHServer.start(tlsAddress.getPort(), NetconfConfigUtil.getNetconfLocalAddress(), getAuthProvider(), bossGroup);
84 private NetconfServerDispatcher createDispatcher(NetconfOperationServiceFactoryListenerImpl factoriesListener) {
85 return super.createDispatcher(factoriesListener, NetconfITTest.getNetconfMonitoringListenerService(), commitNot);
89 public void tearDown() throws Exception {
94 private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
95 final Collection<InputStream> yangDependencies = NetconfITTest.getBasicYangs();
96 return new HardcodedYangStoreService(yangDependencies);
99 protected List<ModuleFactory> getModuleFactories() {
100 return asList(NetconfITTest.FACTORIES);
104 public void testSecure() throws Exception {
105 NetconfClientDispatcher dispatch = new NetconfClientDispatcherImpl(getNettyThreadgroup(), getNettyThreadgroup(), getHashedWheelTimer());
106 try (TestingNetconfClient netconfClient = new TestingNetconfClient("testing-ssh-client", dispatch, getClientConfiguration())) {
107 NetconfMessage response = netconfClient.sendMessage(getConfig);
108 Assert.assertFalse("Unexpected error message " + XmlUtil.toString(response.getDocument()),
109 NetconfMessageUtil.isErrorMessage(response));
111 NetconfMessage gs = new NetconfMessage(XmlUtil.readXmlToDocument("<rpc message-id=\"2\"\n" +
112 " xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">\n" +
113 " <get-schema xmlns=\"urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring\">\n" +
114 " <identifier>config</identifier>\n" +
118 response = netconfClient.sendMessage(gs);
119 Assert.assertFalse("Unexpected error message " + XmlUtil.toString(response.getDocument()),
120 NetconfMessageUtil.isErrorMessage(response));
124 public NetconfClientConfiguration getClientConfiguration() throws IOException {
125 final NetconfClientConfigurationBuilder b = NetconfClientConfigurationBuilder.create();
126 b.withAddress(tlsAddress);
127 b.withSessionListener(new SimpleNetconfClientSessionListener());
128 b.withReconnectStrategy(new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000));
129 b.withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH);
130 b.withConnectionTimeoutMillis(5000);
131 b.withAuthHandler(getAuthHandler());
135 public AuthProvider getAuthProvider() throws Exception {
136 AuthProvider mock = mock(AuthProviderImpl.class);
137 doReturn(true).when(mock).authenticated(anyString(), anyString());
138 doReturn(PEMGenerator.generate().toCharArray()).when(mock).getPEMAsCharArray();
142 public AuthenticationHandler getAuthHandler() throws IOException {
143 return new LoginPassword("user", "pwd");