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 com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Lists;
14 import com.google.common.collect.Maps;
15 import com.google.common.collect.Sets;
16 import org.custommonkey.xmlunit.AbstractNodeTester;
17 import org.custommonkey.xmlunit.NodeTest;
18 import org.custommonkey.xmlunit.NodeTestException;
19 import org.custommonkey.xmlunit.NodeTester;
20 import org.custommonkey.xmlunit.XMLAssert;
21 import org.custommonkey.xmlunit.XMLUnit;
22 import org.junit.Before;
23 import org.junit.Ignore;
24 import org.junit.Test;
25 import org.junit.matchers.JUnitMatchers;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.opendaylight.controller.config.api.ConflictingVersionException;
29 import org.opendaylight.controller.config.api.ValidationException;
30 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
31 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
32 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
33 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
34 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
35 import org.opendaylight.controller.config.yang.test.impl.ComplexDtoBInner;
36 import org.opendaylight.controller.config.yang.test.impl.ComplexList;
37 import org.opendaylight.controller.config.yang.test.impl.Deep;
38 import org.opendaylight.controller.config.yang.test.impl.DepTestImplModuleFactory;
39 import org.opendaylight.controller.config.yang.test.impl.DtoAInner;
40 import org.opendaylight.controller.config.yang.test.impl.DtoAInnerInner;
41 import org.opendaylight.controller.config.yang.test.impl.DtoC;
42 import org.opendaylight.controller.config.yang.test.impl.DtoD;
43 import org.opendaylight.controller.config.yang.test.impl.IdentityTestModuleFactory;
44 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleFactory;
45 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleMXBean;
46 import org.opendaylight.controller.config.yang.test.impl.Peers;
47 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
48 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
49 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Commit;
50 import org.opendaylight.controller.netconf.confignetconfconnector.operations.DiscardChanges;
51 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
52 import org.opendaylight.controller.netconf.confignetconfconnector.operations.get.Get;
53 import org.opendaylight.controller.netconf.confignetconfconnector.operations.getconfig.GetConfig;
54 import org.opendaylight.controller.netconf.confignetconfconnector.operations.runtimerpc.RuntimeRpc;
55 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreServiceImpl;
56 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreSnapshot;
57 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
58 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCloseSession;
59 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouter;
60 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceSnapshotImpl;
61 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
62 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
63 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
64 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
65 import org.opendaylight.controller.netconf.util.xml.XmlElement;
66 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
67 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity1;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity2;
70 import org.opendaylight.yangtools.yang.data.impl.codec.CodecRegistry;
71 import org.opendaylight.yangtools.yang.data.impl.codec.IdentityCodec;
72 import org.opendaylight.yangtools.yang.model.api.Module;
73 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
74 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
75 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
76 import org.slf4j.Logger;
77 import org.slf4j.LoggerFactory;
78 import org.w3c.dom.Document;
79 import org.w3c.dom.Element;
80 import org.w3c.dom.Node;
81 import org.w3c.dom.NodeList;
82 import org.w3c.dom.Text;
83 import org.w3c.dom.traversal.DocumentTraversal;
84 import org.xml.sax.SAXException;
86 import javax.management.InstanceAlreadyExistsException;
87 import javax.management.InstanceNotFoundException;
88 import javax.management.ObjectName;
89 import javax.xml.parsers.ParserConfigurationException;
90 import java.io.FileNotFoundException;
91 import java.io.IOException;
92 import java.io.InputStream;
93 import java.math.BigInteger;
94 import java.net.URISyntaxException;
95 import java.util.ArrayList;
96 import java.util.Arrays;
97 import java.util.Collection;
98 import java.util.Collections;
99 import java.util.HashSet;
100 import java.util.List;
101 import java.util.Map;
102 import java.util.Set;
104 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
105 import static org.junit.Assert.assertEquals;
106 import static org.junit.Assert.assertThat;
107 import static org.junit.Assert.assertTrue;
108 import static org.junit.Assert.fail;
109 import static org.mockito.Mockito.doNothing;
110 import static org.mockito.Mockito.doReturn;
111 import static org.mockito.Mockito.mock;
112 import static org.mockito.Mockito.verify;
113 import static org.mockito.Mockito.verifyNoMoreInteractions;
114 import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertContainsElement;
115 import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertContainsElementWithText;
116 import static org.opendaylight.controller.netconf.util.xml.XmlUtil.readXmlToElement;
119 public class NetconfMappingTest extends AbstractConfigTest {
120 private static final Logger logger = LoggerFactory.getLogger(NetconfMappingTest.class);
122 private static final String INSTANCE_NAME = "instance-from-code";
123 private static final String NETCONF_SESSION_ID = "foo";
124 private static final String TEST_NAMESPACE= "urn:opendaylight:params:xml:ns:yang:controller:test:impl";
125 private NetconfTestImplModuleFactory factory;
126 private DepTestImplModuleFactory factory2;
127 private IdentityTestModuleFactory factory3;
130 YangStoreSnapshot yangStoreSnapshot;
132 NetconfOperationRouter netconfOperationRouter;
134 NetconfOperationServiceSnapshotImpl netconfOperationServiceSnapshot;
136 private TransactionProvider transactionProvider;
139 public void setUp() throws Exception {
140 MockitoAnnotations.initMocks(this);
141 doReturn(getMbes()).when(this.yangStoreSnapshot).getModuleMXBeanEntryMap();
142 doReturn(getModules()).when(this.yangStoreSnapshot).getModules();
143 doNothing().when(netconfOperationServiceSnapshot).close();
145 this.factory = new NetconfTestImplModuleFactory();
146 this.factory2 = new DepTestImplModuleFactory();
147 this.factory3 = new IdentityTestModuleFactory();
148 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, this.factory, this.factory2,
151 transactionProvider = new TransactionProvider(this.configRegistryClient, NETCONF_SESSION_ID);
154 private ObjectName createModule(final String instanceName) throws InstanceAlreadyExistsException, InstanceNotFoundException, URISyntaxException, ValidationException, ConflictingVersionException {
155 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
157 final ObjectName on = transaction.createModule(this.factory.getImplementationName(), instanceName);
158 final NetconfTestImplModuleMXBean mxBean = transaction.newMXBeanProxy(on, NetconfTestImplModuleMXBean.class);
159 setModule(mxBean, transaction, instanceName + "_dep");
162 for (Class<? extends AbstractServiceInterface> sInterface : factory.getImplementedServiceIntefaces()) {
163 ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
164 transaction.saveServiceReference(
165 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + instanceName + "_" + i++,
169 transaction.commit();
174 public void testIdentityRefs() throws Exception {
175 edit("netconfMessages/editConfig_identities.xml");
182 protected CodecRegistry getCodecRegistry() {
183 IdentityCodec<?> idCodec = mock(IdentityCodec.class);
184 doReturn(TestIdentity1.class).when(idCodec).deserialize(TestIdentity1.QNAME);
185 doReturn(TestIdentity2.class).when(idCodec).deserialize(TestIdentity2.QNAME);
187 CodecRegistry codecReg = super.getCodecRegistry();
188 doReturn(idCodec).when(codecReg).getIdentityCodec();
193 public void testServicePersistance() throws Exception {
194 createModule(INSTANCE_NAME);
196 edit("netconfMessages/editConfig.xml");
197 Document config = getConfigCandidate();
198 assertCorrectServiceNames(config, Sets.newHashSet("ref_test2", "user_to_instance_from_code", "ref_dep_user",
199 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
200 "ref_from_code_to_instance-from-code_1"));
203 edit("netconfMessages/editConfig_addServiceName.xml");
204 config = getConfigCandidate();
205 assertCorrectServiceNames(config, Sets.newHashSet("ref_test2", "user_to_instance_from_code", "ref_dep_user",
206 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
207 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
209 edit("netconfMessages/editConfig_addServiceNameOnTest.xml");
210 config = getConfigCandidate();
211 assertCorrectServiceNames(config, Sets.newHashSet("ref_test2", "user_to_instance_from_code", "ref_dep_user",
212 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
213 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
216 config = getConfigRunning();
217 assertCorrectRefNamesForDependencies(config);
218 assertCorrectServiceNames(config, Sets.newHashSet("ref_test2", "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", "ref_dep_user_another"));
222 edit("netconfMessages/editConfig_replace_default.xml");
223 config = getConfigCandidate();
224 assertCorrectServiceNames(config, Sets.newHashSet("ref_dep", "ref_dep2"));
226 edit("netconfMessages/editConfig_remove.xml");
227 config = getConfigCandidate();
228 assertCorrectServiceNames(config, Collections.<String>emptySet());
231 config = getConfigCandidate();
232 assertCorrectServiceNames(config, Collections.<String>emptySet());
236 private void assertCorrectRefNamesForDependencies(Document config) throws NodeTestException {
237 NodeList modulesList = config.getElementsByTagName("modules");
238 assertEquals(1, modulesList.getLength());
240 NodeTest nt = new NodeTest((DocumentTraversal) config, modulesList.item(0));
241 NodeTester tester = new AbstractNodeTester() {
242 private int defaultRefNameCount = 0;
243 private int userRefNameCount = 0;
246 public void testText(Text text) throws NodeTestException {
247 if(text.getData().equals("ref_dep2")) {
248 defaultRefNameCount++;
249 } else if(text.getData().equals("ref_dep_user_two")) {
255 public void noMoreNodes(NodeTest forTest) throws NodeTestException {
256 assertEquals(0, defaultRefNameCount);
257 assertEquals(2, userRefNameCount);
260 nt.performTest(tester, Node.TEXT_NODE);
263 private void assertCorrectServiceNames(Document configCandidate, final Set<String> refNames) throws NodeTestException {
265 NodeList servicesNodes = configCandidate.getElementsByTagName("services");
266 assertEquals(1, servicesNodes.getLength());
268 NodeTest nt = new NodeTest((DocumentTraversal) configCandidate, servicesNodes.item(0));
269 NodeTester tester = new AbstractNodeTester() {
272 public void testElement(Element element) throws NodeTestException {
273 if(element.getNodeName() != null) {
274 if(element.getNodeName().equals("name")) {
275 String elmText = element.getTextContent();
276 if(refNames.contains(elmText)) {
277 refNames.remove(elmText);
280 throw new NodeTestException("Unexpected services defined: " + elmText);
287 public void noMoreNodes(NodeTest forTest) throws NodeTestException {
288 assertTrue(refNames.isEmpty());
291 nt.performTest(tester, Node.ELEMENT_NODE);
295 public void testConfigNetconfUnionTypes() throws Exception {
297 createModule(INSTANCE_NAME);
299 edit("netconfMessages/editConfig.xml");
301 Document response = getConfigRunning();
302 Element ipElement = readXmlToElement("<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">0:0:0:0:0:0:0:1</ip>");
303 assertContainsElement(response, readXmlToElement("<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">0:0:0:0:0:0:0:1</ip>"));
305 assertContainsElement(response, readXmlToElement("<union-test-attr xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">456</union-test-attr>"));
308 edit("netconfMessages/editConfig_setUnions.xml");
310 response = getConfigRunning();
311 assertContainsElement(response, readXmlToElement("<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">127.1.2.3</ip>"));
312 assertContainsElement(response, readXmlToElement("<union-test-attr xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">randomStringForUnion</union-test-attr>"));
317 public void testConfigNetconf() throws Exception {
319 createModule(INSTANCE_NAME);
321 edit("netconfMessages/editConfig.xml");
322 Document configCandidate = getConfigCandidate();
323 checkBinaryLeafEdited(configCandidate);
326 // default-operation:none, should not affect binary leaf
327 edit("netconfMessages/editConfig_none.xml");
328 checkBinaryLeafEdited(getConfigCandidate());
332 Document response = getConfigRunning();
334 checkBinaryLeafEdited(response);
335 checkTypeConfigAttribute(response);
336 checkTypedefs(response);
337 checkTestingDeps(response);
339 checkBigDecimal(response);
341 edit("netconfMessages/editConfig_remove.xml");
344 assertXMLEqual(getConfigCandidate(), getConfigRunning());
346 final Document expectedResult = XmlFileLoader.xmlFileToDocument("netconfMessages/editConfig_expectedResult.xml");
347 XMLUnit.setIgnoreWhitespace(true);
348 assertXMLEqual(expectedResult, getConfigRunning());
349 assertXMLEqual(expectedResult, getConfigCandidate());
351 edit("netconfMessages/editConfig_none.xml");
353 verify(netconfOperationServiceSnapshot).close();
354 verifyNoMoreInteractions(netconfOperationRouter);
355 verifyNoMoreInteractions(netconfOperationServiceSnapshot);
358 private void checkBigDecimal(Document response) throws NodeTestException, SAXException, IOException {
359 assertContainsElement(response, readXmlToElement("<sleep-factor xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.58</sleep-factor>"));
361 assertContainsElement(response, readXmlToElement("<sleep-factor xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.00</sleep-factor>"));
365 private void closeSession() throws NetconfDocumentedException, ParserConfigurationException, SAXException,
367 DefaultCloseSession closeOp = new DefaultCloseSession(NETCONF_SESSION_ID, netconfOperationServiceSnapshot);
368 executeOp(closeOp, "netconfMessages/closeSession.xml");
371 private void edit(String resource) throws ParserConfigurationException, SAXException, IOException,
372 NetconfDocumentedException {
373 EditConfig editOp = new EditConfig(yangStoreSnapshot, transactionProvider, configRegistryClient,
375 executeOp(editOp, resource);
378 private void commit() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
379 Commit commitOp = new Commit(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
380 executeOp(commitOp, "netconfMessages/commit.xml");
383 private Document getConfigCandidate() throws ParserConfigurationException, SAXException, IOException,
384 NetconfDocumentedException {
385 GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
386 configRegistryClient, NETCONF_SESSION_ID);
387 return executeOp(getConfigOp, "netconfMessages/getConfig_candidate.xml");
390 private Document getConfigRunning() throws ParserConfigurationException, SAXException, IOException,
391 NetconfDocumentedException {
392 GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
393 configRegistryClient, NETCONF_SESSION_ID);
394 return executeOp(getConfigOp, "netconfMessages/getConfig.xml");
397 @Ignore("second edit message corrupted")
398 @Test(expected = NetconfDocumentedException.class)
399 public void testConfigNetconfReplaceDefaultEx() throws Exception {
401 createModule(INSTANCE_NAME);
403 edit("netconfMessages/editConfig.xml");
404 edit("netconfMessages/editConfig_replace_default_ex.xml");
408 public void testConfigNetconfReplaceDefault() throws Exception {
410 createModule(INSTANCE_NAME);
412 edit("netconfMessages/editConfig.xml");
414 Document response = getConfigRunning();
415 final int allInstances = response.getElementsByTagName("module").getLength();
417 edit("netconfMessages/editConfig_replace_default.xml");
420 response = getConfigRunning();
422 final int afterReplace = response.getElementsByTagName("module").getLength();
423 assertEquals(4, allInstances);
424 assertEquals(2, afterReplace);
427 @Test(expected = NetconfDocumentedException.class)
428 public void testSameAttrDifferentNamespaces() throws Exception {
430 edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespaces.xml");
431 } catch (NetconfDocumentedException e) {
432 String message = e.getMessage();
433 assertContainsString(message, "Element simple-long-2 present multiple times with different namespaces");
434 assertContainsString(message, TEST_NAMESPACE);
435 assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
440 @Test(expected = NetconfDocumentedException.class)
441 public void testDifferentNamespaceInTO() throws Exception {
443 edit("netconfMessages/namespaces/editConfig_differentNamespaceTO.xml");
444 } catch (NetconfDocumentedException e) {
445 String message = e.getMessage();
446 assertContainsString(message, "Unrecognised elements");
447 assertContainsString(message, "simple-int2");
448 assertContainsString(message, "dto_d");
453 @Test(expected = NetconfDocumentedException.class)
454 public void testSameAttrDifferentNamespacesList() throws Exception {
456 edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespacesList.xml");
457 } catch (NetconfDocumentedException e) {
458 String message = e.getMessage();
459 assertContainsString(message, "Element binaryLeaf present multiple times with different namespaces");
460 assertContainsString(message, TEST_NAMESPACE);
461 assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
467 public void testTypeNameConfigAttributeMatching() throws Exception {
468 edit("netconfMessages/editConfig.xml");
470 edit("netconfMessages/namespaces/editConfig_typeNameConfigAttributeMatching.xml");
473 Document response = getConfigRunning();
474 checkTypeConfigAttribute(response);
477 // TODO add <modules operation="replace"> functionality
478 @Test(expected = NetconfDocumentedException.class)
479 public void testConfigNetconfReplaceModuleEx() throws Exception {
481 createModule(INSTANCE_NAME);
483 edit("netconfMessages/editConfig.xml");
484 edit("netconfMessages/editConfig_replace_module_ex.xml");
488 public void testUnrecognisedConfigElements() throws Exception {
490 String format = "netconfMessages/unrecognised/editConfig_unrecognised%d.xml";
491 final int TESTS_COUNT = 8;
493 for (int i = 0; i < TESTS_COUNT; i++) {
494 String file = String.format(format, i + 1);
497 } catch (NetconfDocumentedException e) {
498 assertContainsString(e.getMessage(), "Unrecognised elements");
499 assertContainsString(e.getMessage(), "unknownAttribute");
502 fail("Unrecognised test should throw exception " + file);
509 public void testConfigNetconfReplaceModule() throws Exception {
511 createModule(INSTANCE_NAME);
513 edit("netconfMessages/editConfig.xml");
515 Document response = getConfigRunning();
516 final int allInstances = response.getElementsByTagName("instance").getLength();
518 edit("netconfMessages/editConfig_replace_module.xml");
521 response = getConfigRunning();
522 final int afterReplace = response.getElementsByTagName("instance").getLength();
524 assertEquals(4 + 4 /* Instances from services */, allInstances);
525 assertEquals(3 + 3, afterReplace);
528 @Test(expected = NetconfDocumentedException.class)
529 public void testEx() throws Exception {
534 @Test(expected = NetconfDocumentedException.class)
535 public void testEx2() throws Exception {
539 private void discard() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
540 DiscardChanges discardOp = new DiscardChanges(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
541 executeOp(discardOp, "netconfMessages/discardChanges.xml");
544 private void checkBinaryLeafEdited(final Document response) throws NodeTestException, SAXException, IOException {
545 assertContainsElement(response, readXmlToElement("<binaryLeaf xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">YmluYXJ5</binaryLeaf>"));
546 assertContainsElement(response, readXmlToElement("<binaryLeaf xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ZGVmYXVsdEJpbg==</binaryLeaf>"));
549 private void checkTypedefs(final Document response) throws NodeTestException, SAXException, IOException {
551 assertContainsElement(response, readXmlToElement("<extended xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">10</extended>"));
553 assertContainsElement(response, readXmlToElement("<extended xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">1</extended>"));
555 assertContainsElement(response, readXmlToElement("<extended-twice xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">20</extended-twice>"));
557 assertContainsElement(response, readXmlToElement("<extended-twice xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2</extended-twice>"));
559 assertContainsElement(response, readXmlToElement("<extended-enum xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">TWO</extended-enum>"));
561 assertContainsElement(response, readXmlToElement("<extended-enum xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ONE</extended-enum>"));
564 private void assertContainsString(String string, String substring) {
565 assertThat(string, JUnitMatchers.containsString(substring));
568 private void checkEnum(final Document response) throws NetconfDocumentedException {
569 XmlElement modulesElement = XmlElement.fromDomElement(response.getDocumentElement()).getOnlyChildElement("data")
570 .getOnlyChildElement("modules");
572 String enumName = "extended-enum";
573 String enumContent = "TWO";
575 for (XmlElement moduleElement : modulesElement.getChildElements("module")) {
576 String name = moduleElement.getOnlyChildElement("prefix:name").getTextContent();
577 if(name.equals(INSTANCE_NAME)) {
578 XmlElement enumAttr = moduleElement.getOnlyChildElement(enumName);
579 assertEquals(enumContent, enumAttr.getTextContent());
584 fail("Enum attribute " + enumName + ":" + enumContent + " not present in " + XmlUtil.toString(response));
587 private void checkTestingDeps(Document response) {
588 int testingDepsSize = response.getElementsByTagName("testing-deps").getLength();
589 assertEquals(2, testingDepsSize);
592 private void checkTypeConfigAttribute(Document response) throws NetconfDocumentedException {
594 XmlElement modulesElement = XmlElement.fromDomElement(response.getDocumentElement()).getOnlyChildElement("data")
595 .getOnlyChildElement("modules");
597 List<String> expectedValues = Lists.newArrayList("default-string", "configAttributeType");
598 Set<String> configAttributeType = Sets.newHashSet();
600 for (XmlElement moduleElement : modulesElement.getChildElements("module")) {
601 for (XmlElement type : moduleElement.getChildElements("type")) {
602 if (type.getNamespace() != null) {
603 configAttributeType.add(type.getTextContent());
608 for (String expectedValue : expectedValues) {
609 assertTrue(configAttributeType.contains(expectedValue));
613 private Map<String, Map<String, ModuleMXBeanEntry>> getMbes() throws Exception {
614 final List<InputStream> yangDependencies = getYangs();
616 final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries = Maps.newHashMap();
618 YangParserImpl yangParser = new YangParserImpl();
619 final SchemaContext schemaContext = yangParser.resolveSchemaContext(new HashSet<>(yangParser.parseYangModelsFromStreamsMapped(yangDependencies).values()));
620 YangStoreServiceImpl yangStoreService = new YangStoreServiceImpl(new SchemaContextProvider() {
622 public SchemaContext getSchemaContext() {
623 return schemaContext ;
626 mBeanEntries.putAll(yangStoreService.getYangStoreSnapshot().getModuleMXBeanEntryMap());
631 private Set<org.opendaylight.yangtools.yang.model.api.Module> getModules() throws Exception {
632 SchemaContext resolveSchemaContext = getSchemaContext();
633 return resolveSchemaContext.getModules();
636 private SchemaContext getSchemaContext() throws Exception {
637 final List<InputStream> yangDependencies = getYangs();
638 YangParserImpl parser = new YangParserImpl();
640 Set<Module> allYangModules = parser.parseYangModelsFromStreams(yangDependencies);
642 return parser.resolveSchemaContext(Sets
643 .newHashSet(allYangModules));
647 public void testConfigNetconfRuntime() throws Exception {
649 createModule(INSTANCE_NAME);
651 edit("netconfMessages/editConfig.xml");
652 checkBinaryLeafEdited(getConfigCandidate());
656 Document response = get();
658 assertEquals(2/*With runtime beans*/ + 2 /*Without runtime beans*/, getElementsSize(response, "module"));
660 assertEquals(2, getElementsSize(response, "asdf"));
661 // data from running config
662 assertEquals(2, getElementsSize(response, "simple-short"));
664 assertEquals(8, getElementsSize(response, "inner-running-data"));
665 assertEquals(8, getElementsSize(response, "deep2"));
666 assertEquals(8 * 4, getElementsSize(response, "inner-inner-running-data"));
667 assertEquals(8 * 4, getElementsSize(response, "deep3"));
668 assertEquals(8 * 4 * 2, getElementsSize(response, "list-of-strings"));
669 assertEquals(8, getElementsSize(response, "inner-running-data-additional"));
670 assertEquals(8, getElementsSize(response, "deep4"));
673 RuntimeRpc netconf = new RuntimeRpc(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID);
675 response = executeOp(netconf, "netconfMessages/rpc.xml");
676 assertContainsElementWithText(response, "testarg1");
678 response = executeOp(netconf, "netconfMessages/rpcInner.xml");
679 Document expectedReplyOk = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
680 XMLUnit.setIgnoreWhitespace(true);
681 XMLAssert.assertXMLEqual(expectedReplyOk, response);
683 response = executeOp(netconf, "netconfMessages/rpcInnerInner.xml");
684 assertContainsElementWithText(response, "true");
686 response = executeOp(netconf, "netconfMessages/rpcInnerInner_complex_output.xml");
687 assertContainsElementWithText(response, "1");
688 assertContainsElementWithText(response, "2");
691 private Document get() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
692 Get getOp = new Get(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID, transactionProvider);
693 return executeOp(getOp, "netconfMessages/get.xml");
696 private int getElementsSize(Document response, String elementName) {
697 return response.getElementsByTagName(elementName).getLength();
700 private Document executeOp(final NetconfOperation op, final String filename) throws ParserConfigurationException,
701 SAXException, IOException, NetconfDocumentedException {
703 final Document request = XmlFileLoader.xmlFileToDocument(filename);
705 logger.debug("Executing netconf operation\n{}", XmlUtil.toString(request));
706 HandlingPriority priority = op.canHandle(request);
708 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
710 final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
711 logger.debug("Got response\n{}", XmlUtil.toString(response));
715 private List<InputStream> getYangs() throws FileNotFoundException {
716 List<String> paths = Arrays.asList("/META-INF/yang/config.yang", "/META-INF/yang/rpc-context.yang",
717 "/META-INF/yang/config-test.yang", "/META-INF/yang/config-test-impl.yang", "/META-INF/yang/test-types.yang",
718 "/META-INF/yang/ietf-inet-types.yang");
719 final Collection<InputStream> yangDependencies = new ArrayList<>();
720 for (String path : paths) {
721 final InputStream is = Preconditions
722 .checkNotNull(getClass().getResourceAsStream(path), path + " not found");
723 yangDependencies.add(is);
725 return Lists.newArrayList(yangDependencies);
728 private void setModule(final NetconfTestImplModuleMXBean mxBean, final ConfigTransactionJMXClient transaction, String depName)
729 throws InstanceAlreadyExistsException, InstanceNotFoundException {
730 mxBean.setSimpleInt((long) 44);
731 mxBean.setBinaryLeaf(new byte[] { 8, 7, 9 });
732 final DtoD dtob = getDtoD();
733 mxBean.setDtoD(dtob);
735 final DtoC dtoa = getDtoC();
736 mxBean.setDtoC(dtoa);
737 mxBean.setSimpleBoolean(false);
739 final Peers p1 = new Peers();
742 p1.setSimpleInt3(456);
743 final Peers p2 = new Peers();
745 p2.setPort("port23");
746 p2.setSimpleInt3(456);
747 mxBean.setPeers(Lists.<Peers> newArrayList(p1, p2));
749 mxBean.setSimpleLong(454545L);
750 mxBean.setSimpleLong2(44L);
751 mxBean.setSimpleBigInteger(BigInteger.valueOf(999L));
752 mxBean.setSimpleByte(new Byte((byte) 4));
753 mxBean.setSimpleShort(new Short((short) 4));
754 mxBean.setSimpleTest(545);
756 mxBean.setComplexList(Lists.<ComplexList> newArrayList());
757 mxBean.setSimpleList(Lists.<Integer> newArrayList());
759 final ObjectName testingDepOn = transaction.createModule(this.factory2.getImplementationName(), depName);
761 for (Class<? extends AbstractServiceInterface> sInterface : factory2.getImplementedServiceIntefaces()) {
762 ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
763 transaction.saveServiceReference(
764 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + depName + "_" + i++,
768 mxBean.setTestingDep(testingDepOn);
771 private static DtoD getDtoD() {
772 final DtoD dtob = new DtoD();
773 dtob.setSimpleInt1((long) 444);
774 dtob.setSimpleInt2((long) 4444);
775 dtob.setSimpleInt3(454);
776 final ComplexDtoBInner dtobInner = new ComplexDtoBInner();
777 final Deep deep = new Deep();
778 deep.setSimpleInt3(4);
779 dtobInner.setDeep(deep);
780 dtobInner.setSimpleInt3(44);
781 dtobInner.setSimpleList(Lists.newArrayList(4));
782 dtob.setComplexDtoBInner(Lists.newArrayList(dtobInner));
783 dtob.setSimpleList(Lists.newArrayList(4));
787 private static DtoC getDtoC() {
788 final DtoC dtoa = new DtoC();
789 // dtoa.setSimpleArg((long) 55);
790 final DtoAInner dtoAInner = new DtoAInner();
791 final DtoAInnerInner dtoAInnerInner = new DtoAInnerInner();
792 dtoAInnerInner.setSimpleArg(456L);
793 dtoAInner.setDtoAInnerInner(dtoAInnerInner);
794 dtoAInner.setSimpleArg(44L);
795 dtoa.setDtoAInner(dtoAInner);