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