- remove TLS/SSL support from netconf server and client
[controller.git] / opendaylight / netconf / netconf-it / src / test / java / org / opendaylight / controller / netconf / it / NetconfITTest.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 ch.ethz.ssh2.Connection;
12 import ch.ethz.ssh2.Session;
13 import com.google.common.base.Optional;
14 import com.google.common.collect.Lists;
15 import com.google.common.collect.Sets;
16 import io.netty.channel.ChannelFuture;
17 import io.netty.channel.EventLoopGroup;
18 import io.netty.channel.nio.NioEventLoopGroup;
19 import io.netty.util.HashedWheelTimer;
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.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.List;
28 import java.util.Set;
29 import java.util.concurrent.TimeUnit;
30 import java.util.regex.Pattern;
31 import javax.management.ObjectName;
32 import javax.xml.parsers.ParserConfigurationException;
33 import junit.framework.Assert;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Ignore;
37 import org.junit.Test;
38 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
39 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
40 import org.opendaylight.controller.config.persist.api.Persister;
41 import org.opendaylight.controller.config.spi.ModuleFactory;
42 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
43 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
44 import org.opendaylight.controller.config.yang.store.impl.HardcodedYangStoreService;
45 import org.opendaylight.controller.config.yang.test.impl.DepTestImplModuleFactory;
46 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleFactory;
47 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleMXBean;
48 import org.opendaylight.controller.config.yang.test.impl.TestImplModuleFactory;
49 import org.opendaylight.controller.netconf.api.NetconfMessage;
50 import org.opendaylight.controller.netconf.client.NetconfClient;
51 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
52 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
53 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
54 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
55 import org.opendaylight.controller.netconf.impl.NetconfServerSessionListenerFactory;
56 import org.opendaylight.controller.netconf.impl.NetconfServerSessionNegotiatorFactory;
57 import org.opendaylight.controller.netconf.impl.SessionIdProvider;
58 import org.opendaylight.controller.netconf.impl.mapping.ExiDecoderHandler;
59 import org.opendaylight.controller.netconf.impl.mapping.ExiEncoderHandler;
60 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
61 import org.opendaylight.controller.netconf.persist.impl.ConfigPersisterNotificationHandler;
62 import org.opendaylight.controller.netconf.persist.impl.osgi.ConfigPersisterActivator;
63 import org.opendaylight.controller.netconf.ssh.NetconfSSHServer;
64 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
65 import org.opendaylight.controller.netconf.util.xml.ExiParameters;
66 import org.opendaylight.controller.netconf.util.xml.XmlElement;
67 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
68 import org.slf4j.Logger;
69 import org.slf4j.LoggerFactory;
70 import org.w3c.dom.Document;
71 import org.w3c.dom.Element;
72 import org.w3c.dom.NamedNodeMap;
73 import org.w3c.dom.Node;
74 import org.xml.sax.SAXException;
75 import static java.util.Collections.emptyList;
76 import static junit.framework.Assert.assertEquals;
77 import static junit.framework.Assert.assertNotNull;
78 import static junit.framework.Assert.assertTrue;
79 import static org.mockito.Mockito.doReturn;
80 import static org.mockito.Mockito.mock;
81
82 public class NetconfITTest extends AbstractConfigTest {
83
84      private static final Logger logger =  LoggerFactory.getLogger(NetconfITTest.class);
85     //
86
87     private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023);
88     private static final InetSocketAddress sshAddress = new InetSocketAddress("127.0.0.1", 10830);
89     private static final String USERNAME = "netconf";
90     private static final String PASSWORD = "netconf";
91
92     private NetconfMessage getConfig, getConfigCandidate, editConfig,
93             closeSession, startExi, stopExi;
94     private DefaultCommitNotificationProducer commitNot;
95     private NetconfServerDispatcher dispatch;
96     private EventLoopGroup nettyThreadgroup;
97
98     private NetconfClientDispatcher clientDispatcher;
99
100
101     @Before
102     public void setUp() throws Exception {
103         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(getModuleFactories().toArray(
104                 new ModuleFactory[0])));
105
106         loadMessages();
107
108         NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
109         factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
110
111         nettyThreadgroup = new NioEventLoopGroup();
112
113         commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
114
115         dispatch = createDispatcher(factoriesListener);
116         ChannelFuture s = dispatch.createServer(tcpAddress);
117         s.await();
118
119         clientDispatcher = new NetconfClientDispatcher( nettyThreadgroup, nettyThreadgroup);
120     }
121
122     private NetconfServerDispatcher createDispatcher(NetconfOperationServiceFactoryListenerImpl factoriesListener) {
123         SessionIdProvider idProvider = new SessionIdProvider();
124         NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
125                 new HashedWheelTimer(5000, TimeUnit.MILLISECONDS), factoriesListener, idProvider);
126
127         NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory(
128                 factoriesListener, commitNot, idProvider);
129
130         NetconfServerDispatcher.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcher.ServerChannelInitializer(
131                 serverNegotiatorFactory, listenerFactory);
132         return new NetconfServerDispatcher(serverChannelInitializer, nettyThreadgroup, nettyThreadgroup);
133     }
134
135     @After
136     public void tearDown() throws Exception {
137         commitNot.close();
138         nettyThreadgroup.shutdownGracefully();
139         clientDispatcher.close();
140     }
141
142     private void loadMessages() throws IOException, SAXException, ParserConfigurationException {
143         this.editConfig = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/edit_config.xml");
144         this.getConfig = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
145         this.getConfigCandidate = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig_candidate.xml");
146         this.startExi = XmlFileLoader
147                 .xmlFileToNetconfMessage("netconfMessages/startExi.xml");
148         this.stopExi = XmlFileLoader
149                 .xmlFileToNetconfMessage("netconfMessages/stopExi.xml");
150         this.closeSession = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/closeSession.xml");
151     }
152
153     private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
154         final Collection<InputStream> yangDependencies = getBasicYangs();
155         return new HardcodedYangStoreService(yangDependencies);
156     }
157
158     static Collection<InputStream> getBasicYangs() throws IOException {
159         List<String> paths = Arrays.asList("/META-INF/yang/config.yang", "/META-INF/yang/rpc-context.yang",
160                 "/META-INF/yang/config-test.yang", "/META-INF/yang/config-test-impl.yang", "/META-INF/yang/test-types.yang",
161                 "/META-INF/yang/ietf-inet-types.yang");
162         final Collection<InputStream> yangDependencies = new ArrayList<>();
163         List<String> failedToFind = new ArrayList<>();
164         for (String path : paths) {
165             InputStream resourceAsStream = NetconfITTest.class.getResourceAsStream(path);
166             if (resourceAsStream == null) {
167                 failedToFind.add(path);
168             } else {
169                 yangDependencies.add(resourceAsStream);
170             }
171         }
172         assertEquals("Some yang files were not found",emptyList(), failedToFind);
173         return yangDependencies;
174     }
175
176     protected List<ModuleFactory> getModuleFactories() {
177         return getModuleFactoriesS();
178     }
179     static List<ModuleFactory> getModuleFactoriesS() {
180         return Lists.newArrayList(new TestImplModuleFactory(), new DepTestImplModuleFactory(),
181                 new NetconfTestImplModuleFactory());
182     }
183
184     @Test
185     public void testNetconfClientDemonstration() throws Exception {
186         try (NetconfClient netconfClient = new NetconfClient("client", tcpAddress, 4000, clientDispatcher)) {
187
188             Set<String> capabilitiesFromNetconfServer = netconfClient.getCapabilities();
189             long sessionId = netconfClient.getSessionId();
190
191             // NetconfMessage can be created :
192             // new NetconfMessage(XmlUtil.readXmlToDocument("<xml/>"));
193
194             NetconfMessage response = netconfClient.sendMessage(getConfig);
195             response.getDocument();
196         }
197     }
198
199     @Test
200     public void testTwoSessions() throws Exception {
201         try (NetconfClient netconfClient = new NetconfClient("1", tcpAddress, 10000, clientDispatcher))  {
202             try (NetconfClient netconfClient2 = new NetconfClient("2", tcpAddress, 10000, clientDispatcher)) {
203             }
204         }
205     }
206
207     @Test(timeout = 10000)
208     public void testPersister() throws Exception {
209         Persister persister = mock(Persister.class);
210         doReturn("mockPersister").when(persister).toString();
211         doReturn(Optional.absent()).when(persister).loadLastConfig();
212         ConfigPersisterNotificationHandler h =
213                 new ConfigPersisterNotificationHandler(persister, tcpAddress, ManagementFactory.getPlatformMBeanServer(), Pattern.compile(ConfigPersisterActivator.DEFAULT_IGNORED_REGEX));
214         h.init();
215     }
216
217     @Ignore
218     @Test
219     public void waitingTest() throws Exception {
220         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
221         transaction.createModule(DepTestImplModuleFactory.NAME, "eb");
222         transaction.commit();
223         Thread.currentThread().suspend();
224     }
225
226     @Test
227     public void rpcReplyContainsAllAttributesTest() throws Exception {
228         try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
229             final String rpc = "<rpc message-id=\"5\" a=\"a\" b=\"44\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
230                     + "<get/>" + "</rpc>";
231             final Document doc = XmlUtil.readXmlToDocument(rpc);
232             final NetconfMessage message = netconfClient.sendMessage(new NetconfMessage(doc));
233             assertNotNull(message);
234             final NamedNodeMap expectedAttributes = doc.getDocumentElement().getAttributes();
235             final NamedNodeMap returnedAttributes = message.getDocument().getDocumentElement().getAttributes();
236
237             assertSameAttributes(expectedAttributes, returnedAttributes);
238         }
239     }
240
241     private void assertSameAttributes(final NamedNodeMap expectedAttributes, final NamedNodeMap returnedAttributes) {
242         assertNotNull("Expecting 4 attributes", returnedAttributes);
243         assertEquals(expectedAttributes.getLength(), returnedAttributes.getLength());
244
245         for (int i = 0; i < expectedAttributes.getLength(); i++) {
246             final Node expAttr = expectedAttributes.item(i);
247             final Node attr = returnedAttributes.item(i);
248             assertEquals(expAttr.getNodeName(), attr.getNodeName());
249             assertEquals(expAttr.getNamespaceURI(), attr.getNamespaceURI());
250             assertEquals(expAttr.getTextContent(), attr.getTextContent());
251         }
252     }
253
254     @Test
255     public void rpcReplyErrorContainsAllAttributesTest() throws Exception {
256         try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
257             final String rpc = "<rpc message-id=\"1\" a=\"adada\" b=\"4\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
258                     + "<commit/>" + "</rpc>";
259             final Document doc = XmlUtil.readXmlToDocument(rpc);
260             final NetconfMessage message = netconfClient.sendMessage(new NetconfMessage(doc));
261             final NamedNodeMap expectedAttributes = doc.getDocumentElement().getAttributes();
262             final NamedNodeMap returnedAttributes = message.getDocument().getDocumentElement().getAttributes();
263
264             assertSameAttributes(expectedAttributes, returnedAttributes);
265         }
266     }
267
268     @Test
269     public void rpcOutputContainsCorrectNamespace() throws Exception {
270         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
271         ObjectName dep = transaction.createModule(DepTestImplModuleFactory.NAME, "instanceD");
272         ObjectName impl = transaction.createModule(NetconfTestImplModuleFactory.NAME, "instance");
273         NetconfTestImplModuleMXBean proxy = configRegistryClient
274                 .newMXBeanProxy(impl, NetconfTestImplModuleMXBean.class);
275         proxy.setTestingDep(dep);
276         proxy.setSimpleShort((short)0);
277
278         transaction.commit();
279
280         try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
281             final String expectedNamespace = "urn:opendaylight:params:xml:ns:yang:controller:test:impl";
282
283             final String rpc = "<rpc message-id=\"5\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
284                     + "<no-arg xmlns=\""
285                     + expectedNamespace
286                     + "\">    "
287                     + "<context-instance>/modules/module[type='impl-netconf'][name='instance']</context-instance>"
288                     + "<arg1>argument1</arg1>" + "</no-arg>" + "</rpc>";
289             final Document doc = XmlUtil.readXmlToDocument(rpc);
290             final NetconfMessage message = netconfClient.sendMessage(new NetconfMessage(doc));
291
292             final Element rpcReply = message.getDocument().getDocumentElement();
293             final XmlElement resultElement = XmlElement.fromDomElement(rpcReply).getOnlyChildElement();
294             assertEquals("result", resultElement.getName());
295
296             final String namespace = resultElement.getNamespaceAttribute();
297             assertEquals(expectedNamespace, namespace);
298         }
299     }
300
301     @Test
302 //    @Ignore
303     public void testStartExi() throws Exception {
304         try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
305
306
307             Document rpcReply = netconfClient.sendMessage(this.startExi)
308                     .getDocument();
309             assertIsOK(rpcReply);
310
311             ExiParameters exiParams = new ExiParameters();
312             exiParams.setParametersFromXmlElement(XmlElement.fromDomDocument(this.startExi.getDocument()));
313
314             netconfClient.getClientSession().addExiDecoder(ExiDecoderHandler.HANDLER_NAME, new ExiDecoderHandler(exiParams));
315             netconfClient.getClientSession().addExiEncoder(ExiEncoderHandler.HANDLER_NAME, new ExiEncoderHandler(exiParams));
316
317             rpcReply = netconfClient.sendMessage(this.editConfig)
318                     .getDocument();
319             assertIsOK(rpcReply);
320
321             rpcReply = netconfClient.sendMessage(this.stopExi)
322                     .getDocument();
323             assertIsOK(rpcReply);
324
325         }
326     }
327
328     @Test
329     public void testCloseSession() throws Exception {
330         try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
331
332             // edit config
333             Document rpcReply = netconfClient.sendMessage(this.editConfig)
334                     .getDocument();
335             assertIsOK(rpcReply);
336
337             rpcReply = netconfClient.sendMessage(this.closeSession)
338                     .getDocument();
339
340             assertIsOK(rpcReply);
341         }
342     }
343
344     @Test
345     public void testEditConfig() throws Exception {
346         try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
347             // send edit_config.xml
348             final Document rpcReply = netconfClient.sendMessage(this.editConfig).getDocument();
349             assertIsOK(rpcReply);
350         }
351     }
352
353     @Test
354     public void testValidate() throws Exception {
355         try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
356             // begin transaction
357             Document rpcReply = netconfClient.sendMessage(getConfigCandidate).getDocument();
358             assertEquals("data", XmlElement.fromDomDocument(rpcReply).getOnlyChildElement().getName());
359
360             // operations empty
361             rpcReply = netconfClient.sendMessage(XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/validate.xml"))
362                     .getDocument();
363             assertIsOK(rpcReply);
364         }
365     }
366
367     private void assertIsOK(final Document rpcReply) {
368         assertEquals("rpc-reply", rpcReply.getDocumentElement().getLocalName());
369         assertEquals("ok", XmlElement.fromDomDocument(rpcReply).getOnlyChildElement().getName());
370     }
371
372     @Ignore
373     @Test
374     // TODO can only send NetconfMessage - it must be valid xml
375     public void testClientHelloWithAuth() throws Exception {
376         final String fileName = "netconfMessages/client_hello_with_auth.xml";
377         // final InputStream resourceAsStream =
378         // AbstractListenerTest.class.getResourceAsStream(fileName);
379         // assertNotNull(resourceAsStream);
380         try (NetconfClient netconfClient = new NetconfClient("test", tcpAddress, 5000, clientDispatcher)) {
381             // IOUtils.copy(resourceAsStream, netconfClient.getStream());
382             // netconfClient.getOutputStream().write(NetconfMessageFactory.endOfMessage);
383             // server should not write anything back
384             // assertEquals(null, netconfClient.readMessage());
385             assertGetConfigWorks(netconfClient);
386         }
387     }
388
389     private Document assertGetConfigWorks(final NetconfClient netconfClient) throws InterruptedException {
390         return assertGetConfigWorks(netconfClient, this.getConfig);
391     }
392
393     private Document assertGetConfigWorks(final NetconfClient netconfClient, final NetconfMessage getConfigMessage)
394             throws InterruptedException {
395         final NetconfMessage rpcReply = netconfClient.sendMessage(getConfigMessage);
396         assertNotNull(rpcReply);
397         assertEquals("data", XmlElement.fromDomDocument(rpcReply.getDocument()).getOnlyChildElement().getName());
398         return rpcReply.getDocument();
399     }
400
401     @Test
402     public void testGetConfig() throws Exception {
403         try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
404             assertGetConfigWorks(netconfClient);
405         }
406     }
407
408     @Test
409     public void createYangTestBasedOnYuma() throws Exception {
410         try (NetconfClient netconfClient = createSession(tcpAddress, "1")) {
411             Document rpcReply = netconfClient.sendMessage(
412                     XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/editConfig_merge_yang-test.xml"))
413                     .getDocument();
414             assertEquals("rpc-reply", rpcReply.getDocumentElement().getTagName());
415             assertIsOK(rpcReply);
416             assertGetConfigWorks(netconfClient, this.getConfigCandidate);
417             rpcReply = netconfClient.sendMessage(XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/commit.xml"))
418                     .getDocument();
419             assertIsOK(rpcReply);
420
421             final ObjectName on = new ObjectName(
422                     "org.opendaylight.controller:instanceName=impl-dep-instance,type=Module,moduleFactoryName=impl-dep");
423             Set<ObjectName> cfgBeans = configRegistryClient.lookupConfigBeans();
424             assertEquals(cfgBeans, Sets.newHashSet(on));
425         }
426     }
427
428     private NetconfClient createSession(final InetSocketAddress address, final String expected) throws Exception {
429         final NetconfClient netconfClient = new NetconfClient("test " + address.toString(), address, 5000, clientDispatcher);
430         assertEquals(expected, Long.toString(netconfClient.getSessionId()));
431         return netconfClient;
432     }
433
434     private void startSSHServer() throws Exception{
435         logger.info("Creating SSH server");
436         Thread sshServerThread = new Thread(NetconfSSHServer.start(10830,tcpAddress));
437         sshServerThread.setDaemon(true);
438         sshServerThread.start();
439         logger.info("SSH server on");
440     }
441
442     @Test
443     public void sshTest() throws Exception {
444         startSSHServer();
445         logger.info("creating connection");
446         Connection conn = new Connection(sshAddress.getHostName(),sshAddress.getPort());
447         Assert.assertNotNull(conn);
448         logger.info("connection created");
449         conn.connect();
450         boolean isAuthenticated = conn.authenticateWithPassword(USERNAME,PASSWORD);
451         assertTrue(isAuthenticated);
452         logger.info("user authenticated");
453         final Session sess = conn.openSession();
454         sess.startSubSystem("netconf");
455         logger.info("user authenticated");
456         sess.getStdin().write(XmlUtil.toString(this.getConfig.getDocument()).getBytes());
457
458         new Thread(){
459            public void run(){
460                while (true){
461                  byte[] bytes = new byte[1024];
462                    int c = 0;
463                    try {
464                        c = sess.getStdout().read(bytes);
465                    } catch (IOException e) {
466                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
467                    }
468                    logger.info("got data:"+bytes);
469                  if (c == 0) break;
470                }
471            }
472         }.join();
473     }
474
475
476 }