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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.