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