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.Mockito.doNothing;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.verify;
20 import static org.mockito.Mockito.verifyNoMoreInteractions;
21 import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertContainsElement;
22 import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertContainsElementWithText;
23 import static org.opendaylight.controller.netconf.util.xml.XmlUtil.readXmlToElement;
25 import com.google.common.base.Optional;
26 import com.google.common.base.Preconditions;
27 import com.google.common.collect.Lists;
28 import com.google.common.collect.Maps;
29 import com.google.common.collect.Sets;
30 import java.io.FileNotFoundException;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.math.BigInteger;
34 import java.net.URISyntaxException;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.Collection;
38 import java.util.Collections;
39 import java.util.HashSet;
40 import java.util.List;
43 import javax.management.InstanceAlreadyExistsException;
44 import javax.management.InstanceNotFoundException;
45 import javax.management.ObjectName;
46 import javax.xml.parsers.ParserConfigurationException;
47 import org.custommonkey.xmlunit.AbstractNodeTester;
48 import org.custommonkey.xmlunit.NodeTest;
49 import org.custommonkey.xmlunit.NodeTestException;
50 import org.custommonkey.xmlunit.NodeTester;
51 import org.custommonkey.xmlunit.XMLAssert;
52 import org.custommonkey.xmlunit.XMLUnit;
53 import org.junit.Before;
54 import org.junit.Ignore;
55 import org.junit.Test;
56 import org.junit.matchers.JUnitMatchers;
57 import org.mockito.Mock;
58 import org.mockito.MockitoAnnotations;
59 import org.opendaylight.controller.config.api.ConflictingVersionException;
60 import org.opendaylight.controller.config.api.ValidationException;
61 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
62 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
63 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
64 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
65 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
66 import org.opendaylight.controller.config.yang.test.impl.ComplexDtoBInner;
67 import org.opendaylight.controller.config.yang.test.impl.ComplexList;
68 import org.opendaylight.controller.config.yang.test.impl.Deep;
69 import org.opendaylight.controller.config.yang.test.impl.DepTestImplModuleFactory;
70 import org.opendaylight.controller.config.yang.test.impl.DtoAInner;
71 import org.opendaylight.controller.config.yang.test.impl.DtoAInnerInner;
72 import org.opendaylight.controller.config.yang.test.impl.DtoC;
73 import org.opendaylight.controller.config.yang.test.impl.DtoD;
74 import org.opendaylight.controller.config.yang.test.impl.IdentityTestModuleFactory;
75 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleFactory;
76 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleMXBean;
77 import org.opendaylight.controller.config.yang.test.impl.Peers;
78 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
79 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
80 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Commit;
81 import org.opendaylight.controller.netconf.confignetconfconnector.operations.DiscardChanges;
82 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
83 import org.opendaylight.controller.netconf.confignetconfconnector.operations.get.Get;
84 import org.opendaylight.controller.netconf.confignetconfconnector.operations.getconfig.GetConfig;
85 import org.opendaylight.controller.netconf.confignetconfconnector.operations.runtimerpc.RuntimeRpc;
86 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreServiceImpl;
87 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreSnapshot;
88 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
89 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCloseSession;
90 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouter;
91 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceSnapshotImpl;
92 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
93 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
94 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
95 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
96 import org.opendaylight.controller.netconf.util.xml.XmlElement;
97 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
98 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity1;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity2;
101 import org.opendaylight.yangtools.yang.data.impl.codec.CodecRegistry;
102 import org.opendaylight.yangtools.yang.data.impl.codec.IdentityCodec;
103 import org.opendaylight.yangtools.yang.model.api.Module;
104 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
105 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
106 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
107 import org.slf4j.Logger;
108 import org.slf4j.LoggerFactory;
109 import org.w3c.dom.Document;
110 import org.w3c.dom.Element;
111 import org.w3c.dom.Node;
112 import org.w3c.dom.NodeList;
113 import org.w3c.dom.Text;
114 import org.w3c.dom.traversal.DocumentTraversal;
115 import org.xml.sax.SAXException;
118 public class NetconfMappingTest extends AbstractConfigTest {
119 private static final Logger logger = LoggerFactory.getLogger(NetconfMappingTest.class);
121 private static final String INSTANCE_NAME = "instance-from-code";
122 private static final String NETCONF_SESSION_ID = "foo";
123 private static final String TEST_NAMESPACE= "urn:opendaylight:params:xml:ns:yang:controller:test:impl";
124 private NetconfTestImplModuleFactory factory;
125 private DepTestImplModuleFactory factory2;
126 private IdentityTestModuleFactory factory3;
129 YangStoreSnapshot yangStoreSnapshot;
131 NetconfOperationRouter netconfOperationRouter;
133 NetconfOperationServiceSnapshotImpl netconfOperationServiceSnapshot;
135 private TransactionProvider transactionProvider;
138 public void setUp() throws Exception {
139 MockitoAnnotations.initMocks(this);
140 doReturn(getMbes()).when(this.yangStoreSnapshot).getModuleMXBeanEntryMap();
141 doReturn(getModules()).when(this.yangStoreSnapshot).getModules();
142 doNothing().when(netconfOperationServiceSnapshot).close();
144 this.factory = new NetconfTestImplModuleFactory();
145 this.factory2 = new DepTestImplModuleFactory();
146 this.factory3 = new IdentityTestModuleFactory();
147 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, this.factory, this.factory2,
150 transactionProvider = new TransactionProvider(this.configRegistryClient, NETCONF_SESSION_ID);
153 private ObjectName createModule(final String instanceName) throws InstanceAlreadyExistsException, InstanceNotFoundException, URISyntaxException, ValidationException, ConflictingVersionException {
154 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
156 final ObjectName on = transaction.createModule(this.factory.getImplementationName(), instanceName);
157 final NetconfTestImplModuleMXBean mxBean = transaction.newMXBeanProxy(on, NetconfTestImplModuleMXBean.class);
158 setModule(mxBean, transaction, instanceName + "_dep");
161 for (Class<? extends AbstractServiceInterface> sInterface : factory.getImplementedServiceIntefaces()) {
162 ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
163 transaction.saveServiceReference(
164 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + instanceName + "_" + i++,
168 transaction.commit();
173 public void testIdentityRefs() throws Exception {
174 edit("netconfMessages/editConfig_identities.xml");
181 protected CodecRegistry getCodecRegistry() {
182 IdentityCodec<?> idCodec = mock(IdentityCodec.class);
183 doReturn(TestIdentity1.class).when(idCodec).deserialize(TestIdentity1.QNAME);
184 doReturn(TestIdentity2.class).when(idCodec).deserialize(TestIdentity2.QNAME);
186 CodecRegistry codecReg = super.getCodecRegistry();
187 doReturn(idCodec).when(codecReg).getIdentityCodec();
192 public void testServicePersistance() throws Exception {
193 createModule(INSTANCE_NAME);
195 edit("netconfMessages/editConfig.xml");
196 Document config = getConfigCandidate();
197 assertCorrectServiceNames(config, Sets.newHashSet("ref_test2", "user_to_instance_from_code", "ref_dep_user",
198 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
199 "ref_from_code_to_instance-from-code_1"));
202 edit("netconfMessages/editConfig_addServiceName.xml");
203 config = getConfigCandidate();
204 assertCorrectServiceNames(config, Sets.newHashSet("ref_test2", "user_to_instance_from_code", "ref_dep_user",
205 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
206 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
208 edit("netconfMessages/editConfig_addServiceNameOnTest.xml");
209 config = getConfigCandidate();
210 assertCorrectServiceNames(config, Sets.newHashSet("ref_test2", "user_to_instance_from_code", "ref_dep_user",
211 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
212 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
215 config = getConfigRunning();
216 assertCorrectRefNamesForDependencies(config);
217 assertCorrectServiceNames(config, Sets.newHashSet("ref_test2", "user_to_instance_from_code", "ref_dep_user",
218 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
219 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
221 edit("netconfMessages/editConfig_replace_default.xml");
222 config = getConfigCandidate();
223 assertCorrectServiceNames(config, Sets.newHashSet("ref_dep", "ref_dep2"));
225 edit("netconfMessages/editConfig_remove.xml");
226 config = getConfigCandidate();
227 assertCorrectServiceNames(config, Collections.<String>emptySet());
230 config = getConfigCandidate();
231 assertCorrectServiceNames(config, Collections.<String>emptySet());
235 private void assertCorrectRefNamesForDependencies(Document config) throws NodeTestException {
236 NodeList modulesList = config.getElementsByTagName("modules");
237 assertEquals(1, modulesList.getLength());
239 NodeTest nt = new NodeTest((DocumentTraversal) config, modulesList.item(0));
240 NodeTester tester = new AbstractNodeTester() {
241 private int defaultRefNameCount = 0;
242 private int userRefNameCount = 0;
245 public void testText(Text text) throws NodeTestException {
246 if(text.getData().equals("ref_dep2")) {
247 defaultRefNameCount++;
248 } else if(text.getData().equals("ref_dep_user_two")) {
254 public void noMoreNodes(NodeTest forTest) throws NodeTestException {
255 assertEquals(0, defaultRefNameCount);
256 assertEquals(2, userRefNameCount);
259 nt.performTest(tester, Node.TEXT_NODE);
262 private void assertCorrectServiceNames(Document configCandidate, final Set<String> refNames) throws NodeTestException {
264 NodeList servicesNodes = configCandidate.getElementsByTagName("services");
265 assertEquals(1, servicesNodes.getLength());
267 NodeTest nt = new NodeTest((DocumentTraversal) configCandidate, servicesNodes.item(0));
268 NodeTester tester = new AbstractNodeTester() {
271 public void testElement(Element element) throws NodeTestException {
272 if(element.getNodeName() != null) {
273 if(element.getNodeName().equals("name")) {
274 String elmText = element.getTextContent();
275 if(refNames.contains(elmText)) {
276 refNames.remove(elmText);
279 throw new NodeTestException("Unexpected services defined: " + elmText);
286 public void noMoreNodes(NodeTest forTest) throws NodeTestException {
287 assertTrue(refNames.isEmpty());
290 nt.performTest(tester, Node.ELEMENT_NODE);
294 public void testConfigNetconfUnionTypes() throws Exception {
296 createModule(INSTANCE_NAME);
298 edit("netconfMessages/editConfig.xml");
300 Document response = getConfigRunning();
301 Element ipElement = readXmlToElement("<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">0:0:0:0:0:0:0:1</ip>");
302 assertContainsElement(response, readXmlToElement("<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">0:0:0:0:0:0:0:1</ip>"));
304 assertContainsElement(response, readXmlToElement("<union-test-attr xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">456</union-test-attr>"));
307 edit("netconfMessages/editConfig_setUnions.xml");
309 response = getConfigRunning();
310 assertContainsElement(response, readXmlToElement("<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">127.1.2.3</ip>"));
311 assertContainsElement(response, readXmlToElement("<union-test-attr xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">randomStringForUnion</union-test-attr>"));
316 public void testConfigNetconf() throws Exception {
318 createModule(INSTANCE_NAME);
320 edit("netconfMessages/editConfig.xml");
321 Document configCandidate = getConfigCandidate();
322 checkBinaryLeafEdited(configCandidate);
325 // default-operation:none, should not affect binary leaf
326 edit("netconfMessages/editConfig_none.xml");
327 checkBinaryLeafEdited(getConfigCandidate());
331 Document response = getConfigRunning();
333 checkBinaryLeafEdited(response);
334 checkTypeConfigAttribute(response);
335 checkTypedefs(response);
336 checkTestingDeps(response);
338 checkBigDecimal(response);
340 edit("netconfMessages/editConfig_remove.xml");
343 assertXMLEqual(getConfigCandidate(), getConfigRunning());
345 final Document expectedResult = XmlFileLoader.xmlFileToDocument("netconfMessages/editConfig_expectedResult.xml");
346 XMLUnit.setIgnoreWhitespace(true);
347 assertXMLEqual(expectedResult, getConfigRunning());
348 assertXMLEqual(expectedResult, getConfigCandidate());
350 edit("netconfMessages/editConfig_none.xml");
352 verify(netconfOperationServiceSnapshot).close();
353 verifyNoMoreInteractions(netconfOperationRouter);
354 verifyNoMoreInteractions(netconfOperationServiceSnapshot);
357 private void checkBigDecimal(Document response) throws NodeTestException, SAXException, IOException {
358 assertContainsElement(response, readXmlToElement("<sleep-factor xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.58</sleep-factor>"));
360 assertContainsElement(response, readXmlToElement("<sleep-factor xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.00</sleep-factor>"));
364 private void closeSession() throws NetconfDocumentedException, ParserConfigurationException, SAXException,
366 DefaultCloseSession closeOp = new DefaultCloseSession(NETCONF_SESSION_ID, netconfOperationServiceSnapshot);
367 executeOp(closeOp, "netconfMessages/closeSession.xml");
370 private void edit(String resource) throws ParserConfigurationException, SAXException, IOException,
371 NetconfDocumentedException {
372 EditConfig editOp = new EditConfig(yangStoreSnapshot, transactionProvider, configRegistryClient,
374 executeOp(editOp, resource);
377 private void commit() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
378 Commit commitOp = new Commit(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
379 executeOp(commitOp, "netconfMessages/commit.xml");
382 private Document getConfigCandidate() throws ParserConfigurationException, SAXException, IOException,
383 NetconfDocumentedException {
384 GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
385 configRegistryClient, NETCONF_SESSION_ID);
386 return executeOp(getConfigOp, "netconfMessages/getConfig_candidate.xml");
389 private Document getConfigRunning() throws ParserConfigurationException, SAXException, IOException,
390 NetconfDocumentedException {
391 GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
392 configRegistryClient, NETCONF_SESSION_ID);
393 return executeOp(getConfigOp, "netconfMessages/getConfig.xml");
396 @Ignore("second edit message corrupted")
397 @Test(expected = NetconfDocumentedException.class)
398 public void testConfigNetconfReplaceDefaultEx() throws Exception {
400 createModule(INSTANCE_NAME);
402 edit("netconfMessages/editConfig.xml");
403 edit("netconfMessages/editConfig_replace_default_ex.xml");
407 public void testConfigNetconfReplaceDefault() throws Exception {
409 createModule(INSTANCE_NAME);
411 edit("netconfMessages/editConfig.xml");
413 Document response = getConfigRunning();
414 final int allInstances = response.getElementsByTagName("module").getLength();
416 edit("netconfMessages/editConfig_replace_default.xml");
419 response = getConfigRunning();
421 final int afterReplace = response.getElementsByTagName("module").getLength();
422 assertEquals(4, allInstances);
423 assertEquals(2, afterReplace);
426 @Test(expected = NetconfDocumentedException.class)
427 public void testSameAttrDifferentNamespaces() throws Exception {
429 edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespaces.xml");
430 } catch (NetconfDocumentedException e) {
431 String message = e.getMessage();
432 assertContainsString(message, "Element simple-long-2 present multiple times with different namespaces");
433 assertContainsString(message, TEST_NAMESPACE);
434 assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
439 @Test(expected = NetconfDocumentedException.class)
440 public void testDifferentNamespaceInTO() throws Exception {
442 edit("netconfMessages/namespaces/editConfig_differentNamespaceTO.xml");
443 } catch (NetconfDocumentedException e) {
444 String message = e.getMessage();
445 assertContainsString(message, "Unrecognised elements");
446 assertContainsString(message, "simple-int2");
447 assertContainsString(message, "dto_d");
452 @Test(expected = NetconfDocumentedException.class)
453 public void testSameAttrDifferentNamespacesList() throws Exception {
455 edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespacesList.xml");
456 } catch (NetconfDocumentedException e) {
457 String message = e.getMessage();
458 assertContainsString(message, "Element binaryLeaf present multiple times with different namespaces");
459 assertContainsString(message, TEST_NAMESPACE);
460 assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
466 public void testTypeNameConfigAttributeMatching() throws Exception {
467 edit("netconfMessages/editConfig.xml");
469 edit("netconfMessages/namespaces/editConfig_typeNameConfigAttributeMatching.xml");
472 Document response = getConfigRunning();
473 checkTypeConfigAttribute(response);
476 // TODO add <modules operation="replace"> functionality
477 @Test(expected = NetconfDocumentedException.class)
478 public void testConfigNetconfReplaceModuleEx() throws Exception {
480 createModule(INSTANCE_NAME);
482 edit("netconfMessages/editConfig.xml");
483 edit("netconfMessages/editConfig_replace_module_ex.xml");
487 public void testUnrecognisedConfigElements() throws Exception {
489 String format = "netconfMessages/unrecognised/editConfig_unrecognised%d.xml";
490 final int TESTS_COUNT = 8;
492 for (int i = 0; i < TESTS_COUNT; i++) {
493 String file = String.format(format, i + 1);
496 } catch (NetconfDocumentedException e) {
497 assertContainsString(e.getMessage(), "Unrecognised elements");
498 assertContainsString(e.getMessage(), "unknownAttribute");
501 fail("Unrecognised test should throw exception " + file);
508 public void testConfigNetconfReplaceModule() throws Exception {
510 createModule(INSTANCE_NAME);
512 edit("netconfMessages/editConfig.xml");
514 Document response = getConfigRunning();
515 final int allInstances = response.getElementsByTagName("instance").getLength();
517 edit("netconfMessages/editConfig_replace_module.xml");
520 response = getConfigRunning();
521 final int afterReplace = response.getElementsByTagName("instance").getLength();
523 assertEquals(4 + 4 /* Instances from services */, allInstances);
524 assertEquals(3 + 3, afterReplace);
527 @Test(expected = NetconfDocumentedException.class)
528 public void testEx() throws Exception {
533 @Test(expected = NetconfDocumentedException.class)
534 public void testEx2() throws Exception {
538 private void discard() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
539 DiscardChanges discardOp = new DiscardChanges(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
540 executeOp(discardOp, "netconfMessages/discardChanges.xml");
543 private void checkBinaryLeafEdited(final Document response) throws NodeTestException, SAXException, IOException {
544 assertContainsElement(response, readXmlToElement("<binaryLeaf xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">YmluYXJ5</binaryLeaf>"));
545 assertContainsElement(response, readXmlToElement("<binaryLeaf xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ZGVmYXVsdEJpbg==</binaryLeaf>"));
548 private void checkTypedefs(final Document response) throws NodeTestException, SAXException, IOException {
550 assertContainsElement(response, readXmlToElement("<extended xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">10</extended>"));
552 assertContainsElement(response, readXmlToElement("<extended xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">1</extended>"));
554 assertContainsElement(response, readXmlToElement("<extended-twice xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">20</extended-twice>"));
556 assertContainsElement(response, readXmlToElement("<extended-twice xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2</extended-twice>"));
558 assertContainsElement(response, readXmlToElement("<extended-enum xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">TWO</extended-enum>"));
560 assertContainsElement(response, readXmlToElement("<extended-enum xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ONE</extended-enum>"));
563 private void assertContainsString(String string, String substring) {
564 assertThat(string, JUnitMatchers.containsString(substring));
567 private void checkEnum(final Document response) throws NetconfDocumentedException {
568 XmlElement modulesElement = XmlElement.fromDomElement(response.getDocumentElement()).getOnlyChildElement("data")
569 .getOnlyChildElement("modules");
571 String enumName = "extended-enum";
572 String enumContent = "TWO";
574 for (XmlElement moduleElement : modulesElement.getChildElements("module")) {
575 String name = moduleElement.getOnlyChildElement("name").getTextContent();
576 if(name.equals(INSTANCE_NAME)) {
577 XmlElement enumAttr = moduleElement.getOnlyChildElement(enumName);
578 assertEquals(enumContent, enumAttr.getTextContent());
583 fail("Enum attribute " + enumName + ":" + enumContent + " not present in " + XmlUtil.toString(response));
586 private void checkTestingDeps(Document response) {
587 int testingDepsSize = response.getElementsByTagName("testing-deps").getLength();
588 assertEquals(2, testingDepsSize);
591 private void checkTypeConfigAttribute(Document response) throws NetconfDocumentedException {
593 XmlElement modulesElement = XmlElement.fromDomElement(response.getDocumentElement()).getOnlyChildElement("data")
594 .getOnlyChildElement("modules");
596 List<String> expectedValues = Lists.newArrayList("default-string", "configAttributeType");
597 Set<String> configAttributeType = Sets.newHashSet();
599 for (XmlElement moduleElement : modulesElement.getChildElements("module")) {
600 for (XmlElement type : moduleElement.getChildElements("type")) {
601 if (type.getNamespaceOptionally().isPresent()) {
602 configAttributeType.add(type.getTextContent());
607 for (String expectedValue : expectedValues) {
608 assertTrue(configAttributeType.contains(expectedValue));
612 private Map<String, Map<String, ModuleMXBeanEntry>> getMbes() throws Exception {
613 final List<InputStream> yangDependencies = getYangs();
615 final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries = Maps.newHashMap();
617 YangParserImpl yangParser = new YangParserImpl();
618 final SchemaContext schemaContext = yangParser.resolveSchemaContext(new HashSet<>(yangParser.parseYangModelsFromStreamsMapped(yangDependencies).values()));
619 YangStoreServiceImpl yangStoreService = new YangStoreServiceImpl(new SchemaContextProvider() {
621 public SchemaContext getSchemaContext() {
622 return schemaContext ;
625 mBeanEntries.putAll(yangStoreService.getYangStoreSnapshot().getModuleMXBeanEntryMap());
630 private Set<org.opendaylight.yangtools.yang.model.api.Module> getModules() throws Exception {
631 SchemaContext resolveSchemaContext = getSchemaContext();
632 return resolveSchemaContext.getModules();
635 private SchemaContext getSchemaContext() throws Exception {
636 final List<InputStream> yangDependencies = getYangs();
637 YangParserImpl parser = new YangParserImpl();
639 Set<Module> allYangModules = parser.parseYangModelsFromStreams(yangDependencies);
641 return parser.resolveSchemaContext(Sets
642 .newHashSet(allYangModules));
646 public void testConfigNetconfRuntime() throws Exception {
648 createModule(INSTANCE_NAME);
650 edit("netconfMessages/editConfig.xml");
651 checkBinaryLeafEdited(getConfigCandidate());
655 Document response = get();
657 assertEquals(2/*With runtime beans*/ + 2 /*Without runtime beans*/, getElementsSize(response, "module"));
659 assertEquals(2, getElementsSize(response, "asdf"));
660 // data from running config
661 assertEquals(2, getElementsSize(response, "simple-short"));
663 assertEquals(8, getElementsSize(response, "inner-running-data"));
664 assertEquals(8, getElementsSize(response, "deep2"));
665 assertEquals(8 * 4, getElementsSize(response, "inner-inner-running-data"));
666 assertEquals(8 * 4, getElementsSize(response, "deep3"));
667 assertEquals(8 * 4 * 2, getElementsSize(response, "list-of-strings"));
668 assertEquals(8, getElementsSize(response, "inner-running-data-additional"));
669 assertEquals(8, getElementsSize(response, "deep4"));
672 RuntimeRpc netconf = new RuntimeRpc(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID);
674 response = executeOp(netconf, "netconfMessages/rpc.xml");
675 assertContainsElementWithText(response, "testarg1");
677 response = executeOp(netconf, "netconfMessages/rpcInner.xml");
678 Document expectedReplyOk = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
679 XMLUnit.setIgnoreWhitespace(true);
680 XMLAssert.assertXMLEqual(expectedReplyOk, response);
682 response = executeOp(netconf, "netconfMessages/rpcInnerInner.xml");
683 assertContainsElementWithText(response, "true");
685 response = executeOp(netconf, "netconfMessages/rpcInnerInner_complex_output.xml");
686 assertContainsElementWithText(response, "1");
687 assertContainsElementWithText(response, "2");
690 private Document get() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
691 Get getOp = new Get(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID, transactionProvider);
692 return executeOp(getOp, "netconfMessages/get.xml");
695 private int getElementsSize(Document response, String elementName) {
696 return response.getElementsByTagName(elementName).getLength();
699 private Document executeOp(final NetconfOperation op, final String filename) throws ParserConfigurationException,
700 SAXException, IOException, NetconfDocumentedException {
702 final Document request = XmlFileLoader.xmlFileToDocument(filename);
704 logger.debug("Executing netconf operation\n{}", XmlUtil.toString(request));
705 HandlingPriority priority = op.canHandle(request);
707 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
709 final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
710 logger.debug("Got response\n{}", XmlUtil.toString(response));
714 private List<InputStream> getYangs() throws FileNotFoundException {
715 List<String> paths = Arrays.asList("/META-INF/yang/config.yang", "/META-INF/yang/rpc-context.yang",
716 "/META-INF/yang/config-test.yang", "/META-INF/yang/config-test-impl.yang", "/META-INF/yang/test-types.yang",
717 "/META-INF/yang/ietf-inet-types.yang");
718 final Collection<InputStream> yangDependencies = new ArrayList<>();
719 for (String path : paths) {
720 final InputStream is = Preconditions
721 .checkNotNull(getClass().getResourceAsStream(path), path + " not found");
722 yangDependencies.add(is);
724 return Lists.newArrayList(yangDependencies);
727 private void setModule(final NetconfTestImplModuleMXBean mxBean, final ConfigTransactionJMXClient transaction, String depName)
728 throws InstanceAlreadyExistsException, InstanceNotFoundException {
729 mxBean.setSimpleInt((long) 44);
730 mxBean.setBinaryLeaf(new byte[] { 8, 7, 9 });
731 final DtoD dtob = getDtoD();
732 mxBean.setDtoD(dtob);
734 final DtoC dtoa = getDtoC();
735 mxBean.setDtoC(dtoa);
736 mxBean.setSimpleBoolean(false);
738 final Peers p1 = new Peers();
741 p1.setSimpleInt3(456);
742 final Peers p2 = new Peers();
744 p2.setPort("port23");
745 p2.setSimpleInt3(456);
746 mxBean.setPeers(Lists.<Peers> newArrayList(p1, p2));
748 mxBean.setSimpleLong(454545L);
749 mxBean.setSimpleLong2(44L);
750 mxBean.setSimpleBigInteger(BigInteger.valueOf(999L));
751 mxBean.setSimpleByte(new Byte((byte) 4));
752 mxBean.setSimpleShort(new Short((short) 4));
753 mxBean.setSimpleTest(545);
755 mxBean.setComplexList(Lists.<ComplexList> newArrayList());
756 mxBean.setSimpleList(Lists.<Integer> newArrayList());
758 final ObjectName testingDepOn = transaction.createModule(this.factory2.getImplementationName(), depName);
760 for (Class<? extends AbstractServiceInterface> sInterface : factory2.getImplementedServiceIntefaces()) {
761 ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
762 transaction.saveServiceReference(
763 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + depName + "_" + i++,
767 mxBean.setTestingDep(testingDepOn);
770 private static DtoD getDtoD() {
771 final DtoD dtob = new DtoD();
772 dtob.setSimpleInt1((long) 444);
773 dtob.setSimpleInt2((long) 4444);
774 dtob.setSimpleInt3(454);
775 final ComplexDtoBInner dtobInner = new ComplexDtoBInner();
776 final Deep deep = new Deep();
777 deep.setSimpleInt3(4);
778 dtobInner.setDeep(deep);
779 dtobInner.setSimpleInt3(44);
780 dtobInner.setSimpleList(Lists.newArrayList(4));
781 dtob.setComplexDtoBInner(Lists.newArrayList(dtobInner));
782 dtob.setSimpleList(Lists.newArrayList(4));
786 private static DtoC getDtoC() {
787 final DtoC dtoa = new DtoC();
788 // dtoa.setSimpleArg((long) 55);
789 final DtoAInner dtoAInner = new DtoAInner();
790 final DtoAInnerInner dtoAInnerInner = new DtoAInnerInner();
791 dtoAInnerInner.setSimpleArg(456L);
792 dtoAInner.setDtoAInnerInner(dtoAInnerInner);
793 dtoAInner.setSimpleArg(44L);
794 dtoa.setDtoAInner(dtoAInner);