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