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