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