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