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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.