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