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.confignetconfconnector;
11 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertThat;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
16 import static org.mockito.Matchers.any;
17 import static org.mockito.Matchers.anyString;
18 import static org.mockito.Mockito.doNothing;
19 import static org.mockito.Mockito.doReturn;
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.verifyNoMoreInteractions;
23 import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertContainsElement;
24 import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertContainsElementWithText;
25 import static org.opendaylight.controller.netconf.util.xml.XmlUtil.readXmlToElement;
27 import com.google.common.base.Optional;
28 import com.google.common.base.Preconditions;
29 import com.google.common.collect.ImmutableMap;
30 import com.google.common.collect.Lists;
31 import com.google.common.collect.Maps;
32 import com.google.common.collect.Sets;
33 import java.io.FileNotFoundException;
34 import java.io.IOException;
35 import java.io.InputStream;
36 import java.math.BigInteger;
37 import java.net.URISyntaxException;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.Collection;
41 import java.util.Collections;
42 import java.util.HashSet;
43 import java.util.List;
45 import java.util.Map.Entry;
47 import javax.management.InstanceAlreadyExistsException;
48 import javax.management.InstanceNotFoundException;
49 import javax.management.ObjectName;
50 import javax.xml.parsers.ParserConfigurationException;
51 import org.custommonkey.xmlunit.AbstractNodeTester;
52 import org.custommonkey.xmlunit.NodeTest;
53 import org.custommonkey.xmlunit.NodeTestException;
54 import org.custommonkey.xmlunit.NodeTester;
55 import org.custommonkey.xmlunit.XMLAssert;
56 import org.custommonkey.xmlunit.XMLUnit;
57 import org.hamcrest.CoreMatchers;
58 import org.junit.Before;
59 import org.junit.Ignore;
60 import org.junit.Test;
61 import org.mockito.Mock;
62 import org.mockito.MockitoAnnotations;
63 import org.opendaylight.controller.config.api.ConflictingVersionException;
64 import org.opendaylight.controller.config.api.ValidationException;
65 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
66 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
67 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
68 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
69 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
70 import org.opendaylight.controller.config.yang.test.impl.ComplexDtoBInner;
71 import org.opendaylight.controller.config.yang.test.impl.ComplexList;
72 import org.opendaylight.controller.config.yang.test.impl.Deep;
73 import org.opendaylight.controller.config.yang.test.impl.DepTestImplModuleFactory;
74 import org.opendaylight.controller.config.yang.test.impl.DtoAInner;
75 import org.opendaylight.controller.config.yang.test.impl.DtoAInnerInner;
76 import org.opendaylight.controller.config.yang.test.impl.DtoC;
77 import org.opendaylight.controller.config.yang.test.impl.DtoD;
78 import org.opendaylight.controller.config.yang.test.impl.IdentityTestModuleFactory;
79 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleFactory;
80 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleMXBean;
81 import org.opendaylight.controller.config.yang.test.impl.Peers;
82 import org.opendaylight.controller.config.yang.test.impl.TestImplModuleFactory;
83 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
84 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
85 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
86 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Commit;
87 import org.opendaylight.controller.netconf.confignetconfconnector.operations.DiscardChanges;
88 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Lock;
89 import org.opendaylight.controller.netconf.confignetconfconnector.operations.UnLock;
90 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
91 import org.opendaylight.controller.netconf.confignetconfconnector.operations.get.Get;
92 import org.opendaylight.controller.netconf.confignetconfconnector.operations.getconfig.GetConfig;
93 import org.opendaylight.controller.netconf.confignetconfconnector.operations.runtimerpc.RuntimeRpc;
94 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreContext;
95 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreService;
96 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
97 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCloseSession;
98 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouter;
99 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceSnapshotImpl;
100 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
101 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
102 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
103 import org.opendaylight.controller.netconf.util.messages.NetconfMessageUtil;
104 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
105 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
106 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity1;
107 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity2;
108 import org.opendaylight.yangtools.yang.data.impl.codec.CodecRegistry;
109 import org.opendaylight.yangtools.yang.data.impl.codec.IdentityCodec;
110 import org.opendaylight.yangtools.yang.model.api.Module;
111 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
112 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
113 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
114 import org.osgi.framework.Filter;
115 import org.osgi.framework.ServiceListener;
116 import org.osgi.framework.ServiceReference;
117 import org.slf4j.Logger;
118 import org.slf4j.LoggerFactory;
119 import org.w3c.dom.Document;
120 import org.w3c.dom.Element;
121 import org.w3c.dom.Node;
122 import org.w3c.dom.NodeList;
123 import org.w3c.dom.Text;
124 import org.w3c.dom.traversal.DocumentTraversal;
125 import org.xml.sax.SAXException;
128 public class NetconfMappingTest extends AbstractConfigTest {
129 private static final Logger LOG = LoggerFactory.getLogger(NetconfMappingTest.class);
131 private static final String INSTANCE_NAME = "instance-from-code";
132 private static final String NETCONF_SESSION_ID = "foo";
133 private static final String TEST_NAMESPACE= "urn:opendaylight:params:xml:ns:yang:controller:test:impl";
134 private NetconfTestImplModuleFactory factory;
135 private DepTestImplModuleFactory factory2;
136 private IdentityTestModuleFactory factory3;
137 private TestImplModuleFactory factory4;
140 YangStoreContext yangStoreSnapshot;
142 NetconfOperationRouter netconfOperationRouter;
144 NetconfOperationServiceSnapshotImpl netconfOperationServiceSnapshot;
146 private TransactionProvider transactionProvider;
149 public void setUp() throws Exception {
150 MockitoAnnotations.initMocks(this);
153 final Filter filter = mock(Filter.class);
154 doReturn(filter).when(mockedContext).createFilter(anyString());
155 doNothing().when(mockedContext).addServiceListener(any(ServiceListener.class), anyString());
156 doReturn(new ServiceReference<?>[]{}).when(mockedContext).getServiceReferences(anyString(), anyString());
158 doReturn(getMbes()).when(this.yangStoreSnapshot).getModuleMXBeanEntryMap();
159 doReturn(getModules()).when(this.yangStoreSnapshot).getModules();
160 doNothing().when(netconfOperationServiceSnapshot).close();
162 this.factory = new NetconfTestImplModuleFactory();
163 this.factory2 = new DepTestImplModuleFactory();
164 this.factory3 = new IdentityTestModuleFactory();
165 factory4 = new TestImplModuleFactory();
168 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, this.factory, this.factory2,
169 this.factory3, factory4));
171 transactionProvider = new TransactionProvider(this.configRegistryClient, NETCONF_SESSION_ID);
174 private ObjectName createModule(final String instanceName) throws InstanceAlreadyExistsException, InstanceNotFoundException, URISyntaxException, ValidationException, ConflictingVersionException {
175 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
177 final ObjectName on = transaction.createModule(this.factory.getImplementationName(), instanceName);
178 final NetconfTestImplModuleMXBean mxBean = transaction.newMXBeanProxy(on, NetconfTestImplModuleMXBean.class);
179 setModule(mxBean, transaction, instanceName + "_dep");
182 for (Class<? extends AbstractServiceInterface> sInterface : factory.getImplementedServiceIntefaces()) {
183 ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
184 transaction.saveServiceReference(
185 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + instanceName + "_" + i++,
189 transaction.commit();
194 public void testIdentityRefs() throws Exception {
195 edit("netconfMessages/editConfig_identities.xml");
202 protected CodecRegistry getCodecRegistry() {
203 IdentityCodec<?> idCodec = mock(IdentityCodec.class);
204 doReturn(TestIdentity1.class).when(idCodec).deserialize(TestIdentity1.QNAME);
205 doReturn(TestIdentity2.class).when(idCodec).deserialize(TestIdentity2.QNAME);
207 CodecRegistry codecReg = super.getCodecRegistry();
208 doReturn(idCodec).when(codecReg).getIdentityCodec();
213 public void testServicePersistance() throws Exception {
214 createModule(INSTANCE_NAME);
216 edit("netconfMessages/editConfig.xml");
217 Document config = getConfigCandidate();
218 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
219 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
220 "ref_from_code_to_instance-from-code_1"));
223 edit("netconfMessages/editConfig_addServiceName.xml");
224 config = getConfigCandidate();
225 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
226 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
227 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
229 edit("netconfMessages/editConfig_addServiceNameOnTest.xml");
230 config = getConfigCandidate();
231 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
232 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
233 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
236 config = getConfigRunning();
237 assertCorrectRefNamesForDependencies(config);
238 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
239 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
240 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
242 edit("netconfMessages/editConfig_replace_default.xml");
243 config = getConfigCandidate();
244 assertCorrectServiceNames(config, Collections.<String>emptySet());
246 edit("netconfMessages/editConfig_remove.xml");
247 config = getConfigCandidate();
248 assertCorrectServiceNames(config, Collections.<String>emptySet());
251 config = getConfigCandidate();
252 assertCorrectServiceNames(config, Collections.<String>emptySet());
257 public void testUnLock() throws Exception {
258 assertTrue(NetconfMessageUtil.isOKMessage(lockCandidate()));
259 assertTrue(NetconfMessageUtil.isOKMessage(unlockCandidate()));
262 private void assertCorrectRefNamesForDependencies(Document config) throws NodeTestException {
263 NodeList modulesList = config.getElementsByTagName("modules");
264 assertEquals(1, modulesList.getLength());
266 NodeTest nt = new NodeTest((DocumentTraversal) config, modulesList.item(0));
267 NodeTester tester = new AbstractNodeTester() {
268 private int defaultRefNameCount = 0;
269 private int userRefNameCount = 0;
272 public void testText(Text text) throws NodeTestException {
273 if(text.getData().equals("ref_dep2")) {
274 defaultRefNameCount++;
275 } else if(text.getData().equals("ref_dep_user_two")) {
281 public void noMoreNodes(NodeTest forTest) throws NodeTestException {
282 assertEquals(0, defaultRefNameCount);
283 assertEquals(2, userRefNameCount);
286 nt.performTest(tester, Node.TEXT_NODE);
289 private void assertCorrectServiceNames(Document configCandidate, Set<String> refNames) throws NodeTestException {
290 final Set<String> refNames2 = new HashSet<>(refNames);
291 NodeList servicesNodes = configCandidate.getElementsByTagName("services");
292 assertEquals(1, servicesNodes.getLength());
294 NodeTest nt = new NodeTest((DocumentTraversal) configCandidate, servicesNodes.item(0));
295 NodeTester tester = new AbstractNodeTester() {
298 public void testElement(Element element) throws NodeTestException {
299 if(element.getNodeName() != null) {
300 if(element.getNodeName().equals("name")) {
301 String elmText = element.getTextContent();
302 if(refNames2.contains(elmText)) {
303 refNames2.remove(elmText);
305 throw new NodeTestException("Unexpected services defined: " + elmText);
312 public void noMoreNodes(NodeTest forTest) throws NodeTestException {
313 assertEquals(Collections.<String>emptySet(), refNames2);
314 assertTrue(refNames2.toString(), refNames2.isEmpty());
317 nt.performTest(tester, Node.ELEMENT_NODE);
321 public void testConfigNetconfUnionTypes() throws Exception {
323 createModule(INSTANCE_NAME);
325 edit("netconfMessages/editConfig.xml");
327 Document response = getConfigRunning();
328 Element ipElement = readXmlToElement("<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">0:0:0:0:0:0:0:1</ip>");
329 assertContainsElement(response, readXmlToElement("<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">0:0:0:0:0:0:0:1</ip>"));
331 assertContainsElement(response, readXmlToElement("<union-test-attr xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">456</union-test-attr>"));
334 edit("netconfMessages/editConfig_setUnions.xml");
336 response = getConfigRunning();
337 assertContainsElement(response, readXmlToElement("<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">127.1.2.3</ip>"));
338 assertContainsElement(response, readXmlToElement("<union-test-attr xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">randomStringForUnion</union-test-attr>"));
343 public void testConfigNetconf() throws Exception {
345 createModule(INSTANCE_NAME);
347 edit("netconfMessages/editConfig.xml");
348 Document configCandidate = getConfigCandidate();
349 checkBinaryLeafEdited(configCandidate);
352 // default-operation:none, should not affect binary leaf
353 edit("netconfMessages/editConfig_none.xml");
354 checkBinaryLeafEdited(getConfigCandidate());
358 Document response = getConfigRunning();
360 checkBinaryLeafEdited(response);
361 checkTypeConfigAttribute(response);
362 checkTypedefs(response);
363 checkTestingDeps(response);
365 checkBigDecimal(response);
367 edit("netconfMessages/editConfig_remove.xml");
370 assertXMLEqual(getConfigCandidate(), getConfigRunning());
372 final Document expectedResult = XmlFileLoader.xmlFileToDocument("netconfMessages/editConfig_expectedResult.xml");
373 XMLUnit.setIgnoreWhitespace(true);
374 assertXMLEqual(expectedResult, getConfigRunning());
375 assertXMLEqual(expectedResult, getConfigCandidate());
377 edit("netconfMessages/editConfig_none.xml");
379 verify(netconfOperationServiceSnapshot).close();
380 verifyNoMoreInteractions(netconfOperationRouter);
381 verifyNoMoreInteractions(netconfOperationServiceSnapshot);
384 private void checkBigDecimal(Document response) throws NodeTestException, SAXException, IOException {
385 assertContainsElement(response, readXmlToElement("<sleep-factor xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.58</sleep-factor>"));
387 assertContainsElement(response, readXmlToElement("<sleep-factor xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.00</sleep-factor>"));
391 private void closeSession() throws NetconfDocumentedException, ParserConfigurationException, SAXException,
393 DefaultCloseSession closeOp = new DefaultCloseSession(NETCONF_SESSION_ID, netconfOperationServiceSnapshot);
394 executeOp(closeOp, "netconfMessages/closeSession.xml");
397 private void edit(String resource) throws ParserConfigurationException, SAXException, IOException,
398 NetconfDocumentedException {
399 EditConfig editOp = new EditConfig(yangStoreSnapshot, transactionProvider, configRegistryClient,
401 executeOp(editOp, resource);
404 private void commit() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
405 Commit commitOp = new Commit(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
406 executeOp(commitOp, "netconfMessages/commit.xml");
409 private Document lockCandidate() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
410 Lock commitOp = new Lock(NETCONF_SESSION_ID);
411 return executeOp(commitOp, "netconfMessages/lock.xml");
414 private Document unlockCandidate() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
415 UnLock commitOp = new UnLock(NETCONF_SESSION_ID);
416 return executeOp(commitOp, "netconfMessages/unlock.xml");
419 private Document getConfigCandidate() throws ParserConfigurationException, SAXException, IOException,
420 NetconfDocumentedException {
421 GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
422 configRegistryClient, NETCONF_SESSION_ID);
423 return executeOp(getConfigOp, "netconfMessages/getConfig_candidate.xml");
426 private Document getConfigRunning() throws ParserConfigurationException, SAXException, IOException,
427 NetconfDocumentedException {
428 GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
429 configRegistryClient, NETCONF_SESSION_ID);
430 return executeOp(getConfigOp, "netconfMessages/getConfig.xml");
433 @Ignore("second edit message corrupted")
434 @Test(expected = NetconfDocumentedException.class)
435 public void testConfigNetconfReplaceDefaultEx() throws Exception {
437 createModule(INSTANCE_NAME);
439 edit("netconfMessages/editConfig.xml");
440 edit("netconfMessages/editConfig_replace_default_ex.xml");
444 public void testConfigNetconfReplaceDefault() throws Exception {
446 createModule(INSTANCE_NAME);
448 edit("netconfMessages/editConfig.xml");
450 Document response = getConfigRunning();
451 final int allInstances = response.getElementsByTagName("module").getLength();
453 edit("netconfMessages/editConfig_replace_default.xml");
456 response = getConfigRunning();
458 final int afterReplace = response.getElementsByTagName("module").getLength();
459 assertEquals(4, allInstances);
460 assertEquals(2, afterReplace);
464 public void testSameAttrDifferentNamespaces() throws Exception {
466 edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespaces.xml");
468 } catch (NetconfDocumentedException e) {
469 String message = e.getMessage();
470 assertContainsString(message, "Element simpleInt present multiple times with different namespaces");
471 assertContainsString(message, TEST_NAMESPACE);
472 assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
477 public void testDifferentNamespaceInTO() throws Exception {
479 edit("netconfMessages/namespaces/editConfig_differentNamespaceTO.xml");
481 } catch (NetconfDocumentedException e) {
482 String message = e.getMessage();
483 assertContainsString(message, "Unrecognised elements");
484 assertContainsString(message, "simple-int2");
485 assertContainsString(message, "dto_d");
490 public void testSameAttrDifferentNamespacesList() throws Exception {
492 edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespacesList.xml");
494 } catch (NetconfDocumentedException e) {
495 String message = e.getMessage();
496 assertContainsString(message, "Element allow-user present multiple times with different namespaces");
497 assertContainsString(message, TEST_NAMESPACE);
498 assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
503 public void testTypeNameConfigAttributeMatching() throws Exception {
504 edit("netconfMessages/editConfig.xml");
506 edit("netconfMessages/namespaces/editConfig_typeNameConfigAttributeMatching.xml");
509 Document response = getConfigRunning();
510 checkTypeConfigAttribute(response);
513 // TODO add <modules operation="replace"> functionality
514 @Test(expected = NetconfDocumentedException.class)
515 public void testConfigNetconfReplaceModuleEx() throws Exception {
517 createModule(INSTANCE_NAME);
519 edit("netconfMessages/editConfig.xml");
520 edit("netconfMessages/editConfig_replace_module_ex.xml");
524 public void testUnrecognisedConfigElements() throws Exception {
526 String format = "netconfMessages/unrecognised/editConfig_unrecognised%d.xml";
527 final int TESTS_COUNT = 8;
529 for (int i = 0; i < TESTS_COUNT; i++) {
530 String file = String.format(format, i + 1);
531 LOG.info("Reading {}", file);
534 } catch (NetconfDocumentedException e) {
535 assertContainsString(e.getMessage(), "Unrecognised elements");
536 assertContainsString(e.getMessage(), "unknownAttribute");
539 fail("Unrecognised test should throw exception " + file);
546 public void testConfigNetconfReplaceModule() throws Exception {
548 createModule(INSTANCE_NAME);
550 edit("netconfMessages/editConfig.xml");
552 Document response = getConfigRunning();
553 final int allInstances = response.getElementsByTagName("instance").getLength();
555 edit("netconfMessages/editConfig_replace_module.xml");
558 response = getConfigRunning();
559 final int afterReplace = response.getElementsByTagName("instance").getLength();
561 assertEquals(4 + 4 /* Instances from services */, allInstances);
562 assertEquals(3 + 3, afterReplace);
565 @Test(expected = NetconfDocumentedException.class)
566 public void testEx() throws Exception {
572 public void testEx2() throws Exception {
573 assertContainsElement(discard(), readXmlToElement("<ok xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
576 private Document discard() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
577 DiscardChanges discardOp = new DiscardChanges(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
578 return executeOp(discardOp, "netconfMessages/discardChanges.xml");
581 private void checkBinaryLeafEdited(final Document response) throws NodeTestException, SAXException, IOException {
582 assertContainsElement(response, readXmlToElement("<binaryLeaf xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">YmluYXJ5</binaryLeaf>"));
583 assertContainsElement(response, readXmlToElement("<binaryLeaf xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ZGVmYXVsdEJpbg==</binaryLeaf>"));
586 private void checkTypedefs(final Document response) throws NodeTestException, SAXException, IOException {
588 assertContainsElement(response, readXmlToElement("<extended xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">10</extended>"));
590 assertContainsElement(response, readXmlToElement("<extended xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">1</extended>"));
592 assertContainsElement(response, readXmlToElement("<extended-twice xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">20</extended-twice>"));
594 assertContainsElement(response, readXmlToElement("<extended-twice xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2</extended-twice>"));
596 assertContainsElement(response, readXmlToElement("<extended-enum xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">TWO</extended-enum>"));
598 assertContainsElement(response, readXmlToElement("<extended-enum xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ONE</extended-enum>"));
601 private void assertContainsString(String string, String substring) {
602 assertThat(string, CoreMatchers.containsString(substring));
605 private void checkEnum(final Document response) throws Exception {
607 String expectedEnumContent = "TWO";
609 XMLAssert.assertXpathEvaluatesTo(expectedEnumContent,
610 getXpathForNetconfImplSubnode(INSTANCE_NAME,"extended-enum"),
614 private void checkTestingDeps(Document response) {
615 int testingDepsSize = response.getElementsByTagName("testing-deps").getLength();
616 assertEquals(2, testingDepsSize);
619 private String getXpathForNetconfImplSubnode(String instanceName, String subnode) {
620 return "/urn:ietf:params:xml:ns:netconf:base:1.0:rpc-reply" +
621 "/urn:ietf:params:xml:ns:netconf:base:1.0:data" +
622 "/urn:opendaylight:params:xml:ns:yang:controller:config:modules" +
623 "/module[name='"+instanceName+"']" +
624 "/urn:opendaylight:params:xml:ns:yang:controller:test:impl:impl-netconf" +
625 "/urn:opendaylight:params:xml:ns:yang:controller:test:impl:"+subnode;
628 private void checkTypeConfigAttribute(Document response) throws Exception {
630 Map<String,String> namesToTypeValues = ImmutableMap.of("instance-from-code", "configAttributeType",
631 "test2", "default-string");
632 for (Entry<String, String> nameToExpectedValue : namesToTypeValues.entrySet()) {
633 XMLAssert.assertXpathEvaluatesTo(nameToExpectedValue.getValue(),
634 getXpathForNetconfImplSubnode(nameToExpectedValue.getKey(),"type"),
639 private Map<String, Map<String, ModuleMXBeanEntry>> getMbes() throws Exception {
640 final List<InputStream> yangDependencies = getYangs();
642 final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries = Maps.newHashMap();
644 YangParserImpl yangParser = new YangParserImpl();
645 final SchemaContext schemaContext = yangParser.resolveSchemaContext(new HashSet<>(yangParser.parseYangModelsFromStreamsMapped(yangDependencies).values()));
646 YangStoreService yangStoreService = new YangStoreService(new SchemaContextProvider() {
648 public SchemaContext getSchemaContext() {
649 return schemaContext ;
652 mBeanEntries.putAll(yangStoreService.getModuleMXBeanEntryMap());
657 private Set<org.opendaylight.yangtools.yang.model.api.Module> getModules() throws Exception {
658 SchemaContext resolveSchemaContext = getSchemaContext();
659 return resolveSchemaContext.getModules();
662 private SchemaContext getSchemaContext() throws Exception {
663 final List<InputStream> yangDependencies = getYangs();
664 YangParserImpl parser = new YangParserImpl();
666 Set<Module> allYangModules = parser.parseYangModelsFromStreams(yangDependencies);
668 return parser.resolveSchemaContext(Sets
669 .newHashSet(allYangModules));
673 public void testConfigNetconfRuntime() throws Exception {
675 createModule(INSTANCE_NAME);
677 edit("netconfMessages/editConfig.xml");
678 checkBinaryLeafEdited(getConfigCandidate());
682 Document response = get();
684 assertEquals(2/*With runtime beans*/ + 2 /*Without runtime beans*/, getElementsSize(response, "module"));
686 assertEquals(2, getElementsSize(response, "asdf"));
687 // data from running config
688 assertEquals(2, getElementsSize(response, "simple-short"));
690 assertEquals(8, getElementsSize(response, "inner-running-data"));
691 assertEquals(8, getElementsSize(response, "deep2"));
692 assertEquals(8 * 4, getElementsSize(response, "inner-inner-running-data"));
693 assertEquals(8 * 4, getElementsSize(response, "deep3"));
694 assertEquals(8 * 4 * 2, getElementsSize(response, "list-of-strings"));
695 assertEquals(8, getElementsSize(response, "inner-running-data-additional", "urn:opendaylight:params:xml:ns:yang:controller:test:impl"));
696 assertEquals(8, getElementsSize(response, "deep4"));
699 RuntimeRpc netconf = new RuntimeRpc(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID);
701 response = executeOp(netconf, "netconfMessages/rpc.xml");
702 assertContainsElementWithText(response, "testarg1");
704 response = executeOp(netconf, "netconfMessages/rpcInner.xml");
705 Document expectedReplyOk = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
706 XMLUnit.setIgnoreWhitespace(true);
707 XMLAssert.assertXMLEqual(expectedReplyOk, response);
709 response = executeOp(netconf, "netconfMessages/rpcInnerInner.xml");
710 assertContainsElementWithText(response, "true");
712 response = executeOp(netconf, "netconfMessages/rpcInnerInner_complex_output.xml");
713 assertContainsElementWithText(response, "1");
714 assertContainsElementWithText(response, "2");
717 private Document get() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
718 Get getOp = new Get(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID);
719 return executeOp(getOp, "netconfMessages/get.xml");
722 private int getElementsSize(Document response, String elementName) {
723 return response.getElementsByTagName(elementName).getLength();
726 private int getElementsSize(Document response, String elementName, String namespace) {
727 return response.getElementsByTagNameNS(namespace, elementName).getLength();
730 private Document executeOp(final NetconfOperation op, final String filename) throws ParserConfigurationException,
731 SAXException, IOException, NetconfDocumentedException {
733 final Document request = XmlFileLoader.xmlFileToDocument(filename);
735 LOG.debug("Executing netconf operation\n{}", XmlUtil.toString(request));
736 HandlingPriority priority = op.canHandle(request);
738 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
740 final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
741 LOG.debug("Got response\n{}", XmlUtil.toString(response));
745 private List<InputStream> getYangs() throws FileNotFoundException {
746 List<String> paths = Arrays.asList("/META-INF/yang/config.yang", "/META-INF/yang/rpc-context.yang",
747 "/META-INF/yang/config-test.yang", "/META-INF/yang/config-test-impl.yang", "/META-INF/yang/test-types.yang",
748 "/META-INF/yang/test-groups.yang", "/META-INF/yang/ietf-inet-types.yang");
749 final Collection<InputStream> yangDependencies = new ArrayList<>();
750 for (String path : paths) {
751 final InputStream is = Preconditions
752 .checkNotNull(getClass().getResourceAsStream(path), path + " not found");
753 yangDependencies.add(is);
755 return Lists.newArrayList(yangDependencies);
758 private void setModule(final NetconfTestImplModuleMXBean mxBean, final ConfigTransactionJMXClient transaction, String depName)
759 throws InstanceAlreadyExistsException, InstanceNotFoundException {
760 mxBean.setSimpleInt((long) 44);
761 mxBean.setBinaryLeaf(new byte[] { 8, 7, 9 });
762 final DtoD dtob = getDtoD();
763 mxBean.setDtoD(dtob);
765 final DtoC dtoa = getDtoC();
766 mxBean.setDtoC(dtoa);
767 mxBean.setSimpleBoolean(false);
769 final Peers p1 = new Peers();
772 p1.setSimpleInt3(456);
773 final Peers p2 = new Peers();
775 p2.setPort("port23");
776 p2.setSimpleInt3(456);
777 mxBean.setPeers(Lists.<Peers> newArrayList(p1, p2));
779 mxBean.setSimpleLong(454545L);
780 mxBean.setSimpleLong2(44L);
781 mxBean.setSimpleBigInteger(BigInteger.valueOf(999L));
782 mxBean.setSimpleByte(new Byte((byte) 4));
783 mxBean.setSimpleShort(new Short((short) 4));
784 mxBean.setSimpleTest(545);
786 mxBean.setComplexList(Lists.<ComplexList> newArrayList());
787 mxBean.setSimpleList(Lists.<Integer> newArrayList());
789 final ObjectName testingDepOn = transaction.createModule(this.factory2.getImplementationName(), depName);
791 for (Class<? extends AbstractServiceInterface> sInterface : factory2.getImplementedServiceIntefaces()) {
792 ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
793 transaction.saveServiceReference(
794 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + depName + "_" + i++,
798 mxBean.setTestingDep(testingDepOn);
801 private static DtoD getDtoD() {
802 final DtoD dtob = new DtoD();
803 dtob.setSimpleInt1((long) 444);
804 dtob.setSimpleInt2((long) 4444);
805 dtob.setSimpleInt3(454);
806 final ComplexDtoBInner dtobInner = new ComplexDtoBInner();
807 final Deep deep = new Deep();
808 deep.setSimpleInt3(4);
809 dtobInner.setDeep(deep);
810 dtobInner.setSimpleInt3(44);
811 dtobInner.setSimpleList(Lists.newArrayList(4));
812 dtob.setComplexDtoBInner(Lists.newArrayList(dtobInner));
813 dtob.setSimpleList(Lists.newArrayList(4));
817 private static DtoC getDtoC() {
818 final DtoC dtoa = new DtoC();
819 // dtoa.setSimpleArg((long) 55);
820 final DtoAInner dtoAInner = new DtoAInner();
821 final DtoAInnerInner dtoAInnerInner = new DtoAInnerInner();
822 dtoAInnerInner.setSimpleArg(456L);
823 dtoAInner.setDtoAInnerInner(dtoAInnerInner);
824 dtoAInner.setSimpleArg(44L);
825 dtoa.setDtoAInner(dtoAInner);