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