2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.netconf.it;
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;
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;
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;
56 public class NetconfITTest extends AbstractNetconfConfigTest {
58 public static final int PORT = 12023;
59 public static final InetSocketAddress TCP_ADDRESS = new InetSocketAddress(LOOPBACK_ADDRESS, PORT);
61 private NetconfMessage getConfigCandidate, editConfig, closeSession;
62 private NetconfClientDispatcher clientDispatcher;
65 public void setUp() throws Exception {
67 clientDispatcher = getClientDispatcher();
71 protected InetSocketAddress getTcpServerAddress() {
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");
82 public void testNetconfClientDemonstration() throws Exception {
83 try (TestingNetconfClient netconfClient = new TestingNetconfClient("client", clientDispatcher, getClientConfiguration(TCP_ADDRESS, 4000))) {
85 final Set<String> capabilitiesFromNetconfServer = netconfClient.getCapabilities();
86 final long sessionId = netconfClient.getSessionId();
88 // NetconfMessage can be created :
89 // new NetconfMessage(XmlUtil.readXmlToDocument("<xml/>"));
91 final NetconfMessage response = netconfClient.sendMessage(getGetConfig());
92 response.getDocument();
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());
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();
115 assertSameAttributes(expectedAttributes, returnedAttributes);
119 private void assertSameAttributes(final NamedNodeMap expectedAttributes, final NamedNodeMap returnedAttributes) {
120 assertNotNull("Expecting 4 attributes", returnedAttributes);
121 assertEquals(expectedAttributes.getLength(), returnedAttributes.getLength());
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());
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();
141 assertSameAttributes(expectedAttributes, returnedAttributes);
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);
155 transaction.commit();
157 try (TestingNetconfClient netconfClient = createSession(TCP_ADDRESS, "1")) {
158 final String expectedNamespace = "urn:opendaylight:params:xml:ns:yang:controller:test:impl";
160 final String rpc = "<rpc message-id=\"5\" xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\">"
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));
169 final Element rpcReply = message.getDocument().getDocumentElement();
170 final XmlElement resultElement = XmlElement.fromDomElement(rpcReply).getOnlyChildElement();
171 assertEquals("result", resultElement.getName());
173 final String namespace = resultElement.getNamespaceAttribute();
174 assertEquals(expectedNamespace, namespace);
179 public void testCloseSession() throws Exception {
180 try (TestingNetconfClient netconfClient = createSession(TCP_ADDRESS, "1")) {
183 Document rpcReply = netconfClient.sendMessage(this.editConfig)
185 assertIsOK(rpcReply);
187 rpcReply = netconfClient.sendMessage(this.closeSession)
190 assertIsOK(rpcReply);
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);
204 public void testValidate() throws Exception {
205 try (TestingNetconfClient netconfClient = createSession(TCP_ADDRESS, "1")) {
207 Document rpcReply = netconfClient.sendMessage(getConfigCandidate).getDocument();
208 assertEquals("data", XmlElement.fromDomDocument(rpcReply).getOnlyChildElement().getName());
211 rpcReply = netconfClient.sendMessage(XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/validate.xml"))
213 assertIsOK(rpcReply);
217 private void assertIsOK(final Document rpcReply) throws NetconfDocumentedException {
218 assertEquals("rpc-reply", rpcReply.getDocumentElement().getLocalName());
219 assertEquals("ok", XmlElement.fromDomDocument(rpcReply).getOnlyChildElement().getName());
222 private Document assertGetConfigWorks(final TestingNetconfClient netconfClient) throws InterruptedException, ExecutionException, TimeoutException, NetconfDocumentedException {
223 return assertGetConfigWorks(netconfClient, getGetConfig());
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();
235 public void testGetConfig() throws Exception {
236 try (TestingNetconfClient netconfClient = createSession(TCP_ADDRESS, "1")) {
237 assertGetConfigWorks(netconfClient);
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"))
247 assertEquals("rpc-reply", rpcReply.getDocumentElement().getTagName());
248 assertIsOK(rpcReply);
249 assertGetConfigWorks(netconfClient, this.getConfigCandidate);
250 rpcReply = netconfClient.sendMessage(XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/commit.xml"))
252 assertIsOK(rpcReply);
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));
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;
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");
272 try (TestingNetconfClient netconfClient = createSession(TCP_ADDRESS, "1")) {
273 assertIsOK(netconfClient.sendMessage(editId).getDocument());
274 assertIsOK(netconfClient.sendMessage(commit).getDocument());
276 final NetconfMessage response = netconfClient.sendMessage(getGetConfig());
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>"));
283 } catch (final Exception e) {
284 fail(Throwables.getStackTraceAsString(e));
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);
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"))
303 assertIsOK(rpcReply);
304 commit(netconfClient);
306 // verify that parent.getTestingDeps == d1,d2
307 final MultipleDependenciesModuleMXBean parentProxy = configRegistryClient.newMXBeanProxy(
308 configRegistryClient.lookupConfigBean(MultipleDependenciesModuleFactory.NAME, "parent"),
309 MultipleDependenciesModuleMXBean.class);
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);
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);
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))) {
327 final Document rpcReply = netconfClient.sendMessage(
328 XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/editConfig_merge_multiple-deps2.xml"))
330 assertIsOK(rpcReply);
331 commit(netconfClient);
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);
341 public Set<String> getServiceReferences(final List<ObjectName> testingDeps) {
342 return new HashSet<>(Lists.transform(testingDeps, new Function<ObjectName, String>() {
344 public String apply(final ObjectName input) {
345 return ObjectNameUtil.getReferenceName(input);
350 public void commit(final TestingNetconfClient netconfClient) throws Exception {
351 final Document rpcReply;
352 rpcReply = netconfClient.sendMessage(XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/commit.xml"))
354 assertIsOK(rpcReply);