Merge "BUG 2854 : Do not add empty read write transactions to the replicable journal"
[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.hamcrest.CoreMatchers.containsString;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertThat;
15 import static org.junit.Assert.fail;
16 import static org.mockito.Mockito.doReturn;
17
18 import com.google.common.base.Function;
19 import com.google.common.base.Throwables;
20 import com.google.common.collect.Lists;
21 import com.google.common.collect.Sets;
22 import java.io.IOException;
23 import java.net.InetSocketAddress;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Set;
27 import java.util.concurrent.ExecutionException;
28 import java.util.concurrent.TimeoutException;
29 import javax.management.ObjectName;
30 import javax.xml.parsers.ParserConfigurationException;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.opendaylight.controller.config.api.jmx.ObjectNameUtil;
34 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
35 import org.opendaylight.controller.config.yang.test.impl.DepTestImplModuleFactory;
36 import org.opendaylight.controller.config.yang.test.impl.MultipleDependenciesModuleFactory;
37 import org.opendaylight.controller.config.yang.test.impl.MultipleDependenciesModuleMXBean;
38 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleFactory;
39 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleMXBean;
40 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
41 import org.opendaylight.controller.netconf.api.NetconfMessage;
42 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
43 import org.opendaylight.controller.netconf.client.TestingNetconfClient;
44 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
45 import org.opendaylight.controller.netconf.util.xml.XmlElement;
46 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity1;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity2;
49 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
50 import org.w3c.dom.Document;
51 import org.w3c.dom.Element;
52 import org.w3c.dom.NamedNodeMap;
53 import org.w3c.dom.Node;
54 import org.xml.sax.SAXException;
55
56 public class NetconfITTest extends AbstractNetconfConfigTest {
57
58     public static final int PORT = 12023;
59     public static final InetSocketAddress TCP_ADDRESS = new InetSocketAddress(LOOPBACK_ADDRESS, PORT);
60
61     private NetconfMessage getConfigCandidate, editConfig, closeSession;
62     private NetconfClientDispatcher clientDispatcher;
63
64     @Before
65     public void setUp() throws Exception {
66         loadMessages();
67         clientDispatcher = getClientDispatcher();
68     }
69
70     @Override
71     protected InetSocketAddress getTcpServerAddress() {
72         return TCP_ADDRESS;
73     }
74
75     private void loadMessages() throws IOException, SAXException, ParserConfigurationException {
76         this.editConfig = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/edit_config.xml");
77         this.getConfigCandidate = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig_candidate.xml");
78         this.closeSession = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/closeSession.xml");
79     }
80
81     @Test
82     public void testNetconfClientDemonstration() throws Exception {
83         try (TestingNetconfClient netconfClient = new TestingNetconfClient("client", clientDispatcher, getClientConfiguration(TCP_ADDRESS, 4000))) {
84
85             final Set<String> capabilitiesFromNetconfServer = netconfClient.getCapabilities();
86             final long sessionId = netconfClient.getSessionId();
87
88             // NetconfMessage can be created :
89             // new NetconfMessage(XmlUtil.readXmlToDocument("<xml/>"));
90
91             final NetconfMessage response = netconfClient.sendMessage(getGetConfig());
92             response.getDocument();
93         }
94     }
95
96     @Test
97     public void testTwoSessions() throws Exception {
98         try (TestingNetconfClient netconfClient = new TestingNetconfClient("1", clientDispatcher, getClientConfiguration(TCP_ADDRESS, 10000)))  {
99             try (TestingNetconfClient netconfClient2 = new TestingNetconfClient("2", clientDispatcher, getClientConfiguration(TCP_ADDRESS, 10000))) {
100                 assertNotNull(netconfClient2.getCapabilities());
101             }
102         }
103     }
104
105     @Test
106     public void rpcReplyContainsAllAttributesTest() throws Exception {
107         try (TestingNetconfClient netconfClient = createSession(TCP_ADDRESS, "1")) {
108             final String rpc = "<rpc message-id=\"5\" a=\"a\" b=\"44\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><get/>" + "</rpc>";
109             final Document doc = XmlUtil.readXmlToDocument(rpc);
110             final NetconfMessage message = netconfClient.sendMessage(new NetconfMessage(doc));
111             assertNotNull(message);
112             final NamedNodeMap expectedAttributes = doc.getDocumentElement().getAttributes();
113             final NamedNodeMap returnedAttributes = message.getDocument().getDocumentElement().getAttributes();
114
115             assertSameAttributes(expectedAttributes, returnedAttributes);
116         }
117     }
118
119     private void assertSameAttributes(final NamedNodeMap expectedAttributes, final NamedNodeMap returnedAttributes) {
120         assertNotNull("Expecting 4 attributes", returnedAttributes);
121         assertEquals(expectedAttributes.getLength(), returnedAttributes.getLength());
122
123         for (int i = 0; i < expectedAttributes.getLength(); i++) {
124             final Node expAttr = expectedAttributes.item(i);
125             final Node attr = returnedAttributes.item(i);
126             assertEquals(expAttr.getNodeName(), attr.getNodeName());
127             assertEquals(expAttr.getNamespaceURI(), attr.getNamespaceURI());
128             assertEquals(expAttr.getTextContent(), attr.getTextContent());
129         }
130     }
131
132     @Test
133     public void rpcReplyErrorContainsAllAttributesTest() throws Exception {
134         try (TestingNetconfClient netconfClient = createSession(TCP_ADDRESS, "1")) {
135             final String rpc = "<rpc message-id=\"1\" a=\"adada\" b=\"4\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><commit/>" + "</rpc>";
136             final Document doc = XmlUtil.readXmlToDocument(rpc);
137             final NetconfMessage message = netconfClient.sendMessage(new NetconfMessage(doc));
138             final NamedNodeMap expectedAttributes = doc.getDocumentElement().getAttributes();
139             final NamedNodeMap returnedAttributes = message.getDocument().getDocumentElement().getAttributes();
140
141             assertSameAttributes(expectedAttributes, returnedAttributes);
142         }
143     }
144
145     @Test
146     public void rpcOutputContainsCorrectNamespace() throws Exception {
147         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
148         final ObjectName dep = transaction.createModule(DepTestImplModuleFactory.NAME, "instanceD");
149         final ObjectName impl = transaction.createModule(NetconfTestImplModuleFactory.NAME, "instance");
150         final NetconfTestImplModuleMXBean proxy = configRegistryClient
151                 .newMXBeanProxy(impl, NetconfTestImplModuleMXBean.class);
152         proxy.setTestingDep(dep);
153         proxy.setSimpleShort((short) 0);
154
155         transaction.commit();
156
157         try (TestingNetconfClient netconfClient = createSession(TCP_ADDRESS, "1")) {
158             final String expectedNamespace = "urn:opendaylight:params:xml:ns:yang:controller:test:impl";
159
160             final String rpc = "<rpc message-id=\"5\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
161                     + "<no-arg xmlns=\""
162                     + expectedNamespace
163                     + "\">    "
164                     + "<context-instance>/modules/module[type='impl-netconf'][name='instance']</context-instance>"
165                     + "<arg1>argument1</arg1>" + "</no-arg>" + "</rpc>";
166             final Document doc = XmlUtil.readXmlToDocument(rpc);
167             final NetconfMessage message = netconfClient.sendMessage(new NetconfMessage(doc));
168
169             final Element rpcReply = message.getDocument().getDocumentElement();
170             final XmlElement resultElement = XmlElement.fromDomElement(rpcReply).getOnlyChildElement();
171             assertEquals("result", resultElement.getName());
172
173             final String namespace = resultElement.getNamespaceAttribute();
174             assertEquals(expectedNamespace, namespace);
175         }
176     }
177
178     @Test
179     public void testCloseSession() throws Exception {
180         try (TestingNetconfClient netconfClient = createSession(TCP_ADDRESS, "1")) {
181
182             // edit config
183             Document rpcReply = netconfClient.sendMessage(this.editConfig)
184                     .getDocument();
185             assertIsOK(rpcReply);
186
187             rpcReply = netconfClient.sendMessage(this.closeSession)
188                     .getDocument();
189
190             assertIsOK(rpcReply);
191         }
192     }
193
194     @Test
195     public void testEditConfig() throws Exception {
196         try (TestingNetconfClient netconfClient = createSession(TCP_ADDRESS, "1")) {
197             // send edit_config.xml
198             final Document rpcReply = netconfClient.sendMessage(this.editConfig).getDocument();
199             assertIsOK(rpcReply);
200         }
201     }
202
203     @Test
204     public void testValidate() throws Exception {
205         try (TestingNetconfClient netconfClient = createSession(TCP_ADDRESS, "1")) {
206             // begin transaction
207             Document rpcReply = netconfClient.sendMessage(getConfigCandidate).getDocument();
208             assertEquals("data", XmlElement.fromDomDocument(rpcReply).getOnlyChildElement().getName());
209
210             // operations empty
211             rpcReply = netconfClient.sendMessage(XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/validate.xml"))
212                     .getDocument();
213             assertIsOK(rpcReply);
214         }
215     }
216
217     private void assertIsOK(final Document rpcReply) throws NetconfDocumentedException {
218         assertEquals("rpc-reply", rpcReply.getDocumentElement().getLocalName());
219         assertEquals("ok", XmlElement.fromDomDocument(rpcReply).getOnlyChildElement().getName());
220     }
221
222     private Document assertGetConfigWorks(final TestingNetconfClient netconfClient) throws InterruptedException, ExecutionException, TimeoutException, NetconfDocumentedException {
223         return assertGetConfigWorks(netconfClient, getGetConfig());
224     }
225
226     private Document assertGetConfigWorks(final TestingNetconfClient netconfClient, final NetconfMessage getConfigMessage)
227             throws InterruptedException, ExecutionException, TimeoutException, NetconfDocumentedException {
228         final NetconfMessage rpcReply = netconfClient.sendMessage(getConfigMessage);
229         assertNotNull(rpcReply);
230         assertEquals("data", XmlElement.fromDomDocument(rpcReply.getDocument()).getOnlyChildElement().getName());
231         return rpcReply.getDocument();
232     }
233
234     @Test
235     public void testGetConfig() throws Exception {
236         try (TestingNetconfClient netconfClient = createSession(TCP_ADDRESS, "1")) {
237             assertGetConfigWorks(netconfClient);
238         }
239     }
240
241     @Test
242     public void createYangTestBasedOnYuma() throws Exception {
243         try (TestingNetconfClient netconfClient = createSession(TCP_ADDRESS, "1")) {
244             Document rpcReply = netconfClient.sendMessage(
245                     XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/editConfig_merge_yang-test.xml"))
246                     .getDocument();
247             assertEquals("rpc-reply", rpcReply.getDocumentElement().getTagName());
248             assertIsOK(rpcReply);
249             assertGetConfigWorks(netconfClient, this.getConfigCandidate);
250             rpcReply = netconfClient.sendMessage(XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/commit.xml"))
251                     .getDocument();
252             assertIsOK(rpcReply);
253
254             final ObjectName on = new ObjectName(
255                     "org.opendaylight.controller:instanceName=impl-dep-instance,type=Module,moduleFactoryName=impl-dep");
256             final Set<ObjectName> cfgBeans = configRegistryClient.lookupConfigBeans();
257             assertEquals(cfgBeans, Sets.newHashSet(on));
258         }
259     }
260
261     private TestingNetconfClient createSession(final InetSocketAddress address, final String expected) throws Exception {
262         final TestingNetconfClient netconfClient = new TestingNetconfClient("test " + address.toString(), clientDispatcher, getClientConfiguration(address, 5000));
263         assertEquals(expected, Long.toString(netconfClient.getSessionId()));
264         return netconfClient;
265     }
266
267     @Test
268     public void testIdRef() throws Exception {
269         final NetconfMessage editId = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/editConfig_identities.xml");
270         final NetconfMessage commit = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/commit.xml");
271
272         try (TestingNetconfClient netconfClient = createSession(TCP_ADDRESS, "1")) {
273             assertIsOK(netconfClient.sendMessage(editId).getDocument());
274             assertIsOK(netconfClient.sendMessage(commit).getDocument());
275
276             final NetconfMessage response = netconfClient.sendMessage(getGetConfig());
277
278             assertThat(XmlUtil.toString(response.getDocument()), containsString("<afi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity1</afi>"));
279             assertThat(XmlUtil.toString(response.getDocument()), containsString("<afi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity2</afi>"));
280             assertThat(XmlUtil.toString(response.getDocument()), containsString("<safi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity2</safi>"));
281             assertThat(XmlUtil.toString(response.getDocument()), containsString("<safi xmlns:prefix=\"urn:opendaylight:params:xml:ns:yang:controller:config:test:types\">prefix:test-identity1</safi>"));
282
283         } catch (final Exception e) {
284             fail(Throwables.getStackTraceAsString(e));
285         }
286     }
287
288     @Override
289     protected BindingRuntimeContext getBindingRuntimeContext() {
290         final BindingRuntimeContext ret = super.getBindingRuntimeContext();
291         doReturn(TestIdentity1.class).when(ret).getIdentityClass(TestIdentity1.QNAME);
292         doReturn(TestIdentity2.class).when(ret).getIdentityClass(TestIdentity2.QNAME);
293         return ret;
294     }
295
296     @Test
297     public void testMultipleDependencies() throws Exception {
298         // push first xml, should add parent and d1,d2 dependencies
299         try (TestingNetconfClient netconfClient = createSession(TCP_ADDRESS, "1")) {
300             final Document rpcReply = netconfClient.sendMessage(
301                     XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/editConfig_merge_multiple-deps1.xml"))
302                     .getDocument();
303             assertIsOK(rpcReply);
304             commit(netconfClient);
305         }
306         // verify that parent.getTestingDeps == d1,d2
307         final MultipleDependenciesModuleMXBean parentProxy = configRegistryClient.newMXBeanProxy(
308                 configRegistryClient.lookupConfigBean(MultipleDependenciesModuleFactory.NAME, "parent"),
309                 MultipleDependenciesModuleMXBean.class);
310         {
311             final List<ObjectName> testingDeps = parentProxy.getTestingDeps();
312             assertEquals(2, testingDeps.size());
313             final Set<String> actualRefs = getServiceReferences(testingDeps);
314             assertEquals(Sets.newHashSet("ref_d1", "ref_d2"), actualRefs);
315         }
316
317         // push second xml, should add d3 to parent's dependencies
318         mergeD3(parentProxy);
319         // push second xml again, to test that d3 is not added again
320         mergeD3(parentProxy);
321     }
322
323     public void mergeD3(final MultipleDependenciesModuleMXBean parentProxy) throws Exception {
324         try (TestingNetconfClient netconfClient = new TestingNetconfClient(
325                 "test " + TCP_ADDRESS.toString(), clientDispatcher, getClientConfiguration(TCP_ADDRESS, 5000))) {
326
327             final Document rpcReply = netconfClient.sendMessage(
328                     XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/editConfig_merge_multiple-deps2.xml"))
329                     .getDocument();
330             assertIsOK(rpcReply);
331             commit(netconfClient);
332         }
333         {
334             final List<ObjectName> testingDeps = parentProxy.getTestingDeps();
335             assertEquals(3, testingDeps.size());
336             final Set<String> actualRefs = getServiceReferences(testingDeps);
337             assertEquals(Sets.newHashSet("ref_d1", "ref_d2", "ref_d3"), actualRefs);
338         }
339     }
340
341     public Set<String> getServiceReferences(final List<ObjectName> testingDeps) {
342         return new HashSet<>(Lists.transform(testingDeps, new Function<ObjectName, String>() {
343             @Override
344             public String apply(final ObjectName input) {
345                 return ObjectNameUtil.getReferenceName(input);
346             }
347         }));
348     }
349
350     public void commit(final TestingNetconfClient netconfClient) throws Exception {
351         final Document rpcReply;
352         rpcReply = netconfClient.sendMessage(XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/commit.xml"))
353                 .getDocument();
354         assertIsOK(rpcReply);
355     }
356 }