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.ImmutableMap;
28 import com.google.common.collect.Lists;
29 import com.google.common.collect.Maps;
30 import com.google.common.collect.Sets;
31 import java.io.FileNotFoundException;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.math.BigInteger;
35 import java.net.URISyntaxException;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.Collection;
39 import java.util.Collections;
40 import java.util.HashSet;
41 import java.util.List;
43 import java.util.Map.Entry;
45 import javax.management.InstanceAlreadyExistsException;
46 import javax.management.InstanceNotFoundException;
47 import javax.management.ObjectName;
48 import javax.xml.parsers.ParserConfigurationException;
49 import org.custommonkey.xmlunit.AbstractNodeTester;
50 import org.custommonkey.xmlunit.NodeTest;
51 import org.custommonkey.xmlunit.NodeTestException;
52 import org.custommonkey.xmlunit.NodeTester;
53 import org.custommonkey.xmlunit.XMLAssert;
54 import org.custommonkey.xmlunit.XMLUnit;
55 import org.hamcrest.CoreMatchers;
56 import org.junit.Before;
57 import org.junit.Ignore;
58 import org.junit.Test;
59 import org.mockito.Mock;
60 import org.mockito.MockitoAnnotations;
61 import org.opendaylight.controller.config.api.ConflictingVersionException;
62 import org.opendaylight.controller.config.api.ValidationException;
63 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
64 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
65 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
66 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
67 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
68 import org.opendaylight.controller.config.yang.test.impl.ComplexDtoBInner;
69 import org.opendaylight.controller.config.yang.test.impl.ComplexList;
70 import org.opendaylight.controller.config.yang.test.impl.Deep;
71 import org.opendaylight.controller.config.yang.test.impl.DepTestImplModuleFactory;
72 import org.opendaylight.controller.config.yang.test.impl.DtoAInner;
73 import org.opendaylight.controller.config.yang.test.impl.DtoAInnerInner;
74 import org.opendaylight.controller.config.yang.test.impl.DtoC;
75 import org.opendaylight.controller.config.yang.test.impl.DtoD;
76 import org.opendaylight.controller.config.yang.test.impl.IdentityTestModuleFactory;
77 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleFactory;
78 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleMXBean;
79 import org.opendaylight.controller.config.yang.test.impl.Peers;
80 import org.opendaylight.controller.config.yang.test.impl.TestImplModuleFactory;
81 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
82 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
83 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
84 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Commit;
85 import org.opendaylight.controller.netconf.confignetconfconnector.operations.DiscardChanges;
86 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
87 import org.opendaylight.controller.netconf.confignetconfconnector.operations.get.Get;
88 import org.opendaylight.controller.netconf.confignetconfconnector.operations.getconfig.GetConfig;
89 import org.opendaylight.controller.netconf.confignetconfconnector.operations.runtimerpc.RuntimeRpc;
90 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreServiceImpl;
91 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreSnapshot;
92 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
93 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCloseSession;
94 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouter;
95 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceSnapshotImpl;
96 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
97 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
98 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
99 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
100 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity1;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity2;
103 import org.opendaylight.yangtools.yang.data.impl.codec.CodecRegistry;
104 import org.opendaylight.yangtools.yang.data.impl.codec.IdentityCodec;
105 import org.opendaylight.yangtools.yang.model.api.Module;
106 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
107 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
108 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
109 import org.slf4j.Logger;
110 import org.slf4j.LoggerFactory;
111 import org.w3c.dom.Document;
112 import org.w3c.dom.Element;
113 import org.w3c.dom.Node;
114 import org.w3c.dom.NodeList;
115 import org.w3c.dom.Text;
116 import org.w3c.dom.traversal.DocumentTraversal;
117 import org.xml.sax.SAXException;
120 public class NetconfMappingTest extends AbstractConfigTest {
121 private static final Logger LOG = LoggerFactory.getLogger(NetconfMappingTest.class);
123 private static final String INSTANCE_NAME = "instance-from-code";
124 private static final String NETCONF_SESSION_ID = "foo";
125 private static final String TEST_NAMESPACE= "urn:opendaylight:params:xml:ns:yang:controller:test:impl";
126 private NetconfTestImplModuleFactory factory;
127 private DepTestImplModuleFactory factory2;
128 private IdentityTestModuleFactory factory3;
129 private TestImplModuleFactory factory4;
132 YangStoreSnapshot yangStoreSnapshot;
134 NetconfOperationRouter netconfOperationRouter;
136 NetconfOperationServiceSnapshotImpl netconfOperationServiceSnapshot;
138 private TransactionProvider transactionProvider;
141 public void setUp() throws Exception {
142 MockitoAnnotations.initMocks(this);
143 doReturn(getMbes()).when(this.yangStoreSnapshot).getModuleMXBeanEntryMap();
144 doReturn(getModules()).when(this.yangStoreSnapshot).getModules();
145 doNothing().when(netconfOperationServiceSnapshot).close();
147 this.factory = new NetconfTestImplModuleFactory();
148 this.factory2 = new DepTestImplModuleFactory();
149 this.factory3 = new IdentityTestModuleFactory();
150 factory4 = new TestImplModuleFactory();
151 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, this.factory, this.factory2,
152 this.factory3, factory4));
154 transactionProvider = new TransactionProvider(this.configRegistryClient, NETCONF_SESSION_ID);
157 private ObjectName createModule(final String instanceName) throws InstanceAlreadyExistsException, InstanceNotFoundException, URISyntaxException, ValidationException, ConflictingVersionException {
158 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
160 final ObjectName on = transaction.createModule(this.factory.getImplementationName(), instanceName);
161 final NetconfTestImplModuleMXBean mxBean = transaction.newMXBeanProxy(on, NetconfTestImplModuleMXBean.class);
162 setModule(mxBean, transaction, instanceName + "_dep");
165 for (Class<? extends AbstractServiceInterface> sInterface : factory.getImplementedServiceIntefaces()) {
166 ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
167 transaction.saveServiceReference(
168 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + instanceName + "_" + i++,
172 transaction.commit();
177 public void testIdentityRefs() throws Exception {
178 edit("netconfMessages/editConfig_identities.xml");
185 protected CodecRegistry getCodecRegistry() {
186 IdentityCodec<?> idCodec = mock(IdentityCodec.class);
187 doReturn(TestIdentity1.class).when(idCodec).deserialize(TestIdentity1.QNAME);
188 doReturn(TestIdentity2.class).when(idCodec).deserialize(TestIdentity2.QNAME);
190 CodecRegistry codecReg = super.getCodecRegistry();
191 doReturn(idCodec).when(codecReg).getIdentityCodec();
196 public void testServicePersistance() throws Exception {
197 createModule(INSTANCE_NAME);
199 edit("netconfMessages/editConfig.xml");
200 Document config = getConfigCandidate();
201 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
202 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
203 "ref_from_code_to_instance-from-code_1"));
206 edit("netconfMessages/editConfig_addServiceName.xml");
207 config = getConfigCandidate();
208 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
209 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
210 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
212 edit("netconfMessages/editConfig_addServiceNameOnTest.xml");
213 config = getConfigCandidate();
214 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
215 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
216 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
219 config = getConfigRunning();
220 assertCorrectRefNamesForDependencies(config);
221 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
222 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
223 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
225 edit("netconfMessages/editConfig_replace_default.xml");
226 config = getConfigCandidate();
227 assertCorrectServiceNames(config, Collections.<String>emptySet());
229 edit("netconfMessages/editConfig_remove.xml");
230 config = getConfigCandidate();
231 assertCorrectServiceNames(config, Collections.<String>emptySet());
234 config = getConfigCandidate();
235 assertCorrectServiceNames(config, Collections.<String>emptySet());
239 private void assertCorrectRefNamesForDependencies(Document config) throws NodeTestException {
240 NodeList modulesList = config.getElementsByTagName("modules");
241 assertEquals(1, modulesList.getLength());
243 NodeTest nt = new NodeTest((DocumentTraversal) config, modulesList.item(0));
244 NodeTester tester = new AbstractNodeTester() {
245 private int defaultRefNameCount = 0;
246 private int userRefNameCount = 0;
249 public void testText(Text text) throws NodeTestException {
250 if(text.getData().equals("ref_dep2")) {
251 defaultRefNameCount++;
252 } else if(text.getData().equals("ref_dep_user_two")) {
258 public void noMoreNodes(NodeTest forTest) throws NodeTestException {
259 assertEquals(0, defaultRefNameCount);
260 assertEquals(2, userRefNameCount);
263 nt.performTest(tester, Node.TEXT_NODE);
266 private void assertCorrectServiceNames(Document configCandidate, Set<String> refNames) throws NodeTestException {
267 final Set<String> refNames2 = new HashSet<>(refNames);
268 NodeList servicesNodes = configCandidate.getElementsByTagName("services");
269 assertEquals(1, servicesNodes.getLength());
271 NodeTest nt = new NodeTest((DocumentTraversal) configCandidate, servicesNodes.item(0));
272 NodeTester tester = new AbstractNodeTester() {
275 public void testElement(Element element) throws NodeTestException {
276 if(element.getNodeName() != null) {
277 if(element.getNodeName().equals("name")) {
278 String elmText = element.getTextContent();
279 if(refNames2.contains(elmText)) {
280 refNames2.remove(elmText);
282 throw new NodeTestException("Unexpected services defined: " + elmText);
289 public void noMoreNodes(NodeTest forTest) throws NodeTestException {
290 assertEquals(Collections.<String>emptySet(), refNames2);
291 assertTrue(refNames2.toString(), refNames2.isEmpty());
294 nt.performTest(tester, Node.ELEMENT_NODE);
298 public void testConfigNetconfUnionTypes() throws Exception {
300 createModule(INSTANCE_NAME);
302 edit("netconfMessages/editConfig.xml");
304 Document response = getConfigRunning();
305 Element ipElement = readXmlToElement("<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">0:0:0:0:0:0:0:1</ip>");
306 assertContainsElement(response, readXmlToElement("<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">0:0:0:0:0:0:0:1</ip>"));
308 assertContainsElement(response, readXmlToElement("<union-test-attr xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">456</union-test-attr>"));
311 edit("netconfMessages/editConfig_setUnions.xml");
313 response = getConfigRunning();
314 assertContainsElement(response, readXmlToElement("<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">127.1.2.3</ip>"));
315 assertContainsElement(response, readXmlToElement("<union-test-attr xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">randomStringForUnion</union-test-attr>"));
320 public void testConfigNetconf() throws Exception {
322 createModule(INSTANCE_NAME);
324 edit("netconfMessages/editConfig.xml");
325 Document configCandidate = getConfigCandidate();
326 checkBinaryLeafEdited(configCandidate);
329 // default-operation:none, should not affect binary leaf
330 edit("netconfMessages/editConfig_none.xml");
331 checkBinaryLeafEdited(getConfigCandidate());
335 Document response = getConfigRunning();
337 checkBinaryLeafEdited(response);
338 checkTypeConfigAttribute(response);
339 checkTypedefs(response);
340 checkTestingDeps(response);
342 checkBigDecimal(response);
344 edit("netconfMessages/editConfig_remove.xml");
347 assertXMLEqual(getConfigCandidate(), getConfigRunning());
349 final Document expectedResult = XmlFileLoader.xmlFileToDocument("netconfMessages/editConfig_expectedResult.xml");
350 XMLUnit.setIgnoreWhitespace(true);
351 assertXMLEqual(expectedResult, getConfigRunning());
352 assertXMLEqual(expectedResult, getConfigCandidate());
354 edit("netconfMessages/editConfig_none.xml");
356 verify(netconfOperationServiceSnapshot).close();
357 verifyNoMoreInteractions(netconfOperationRouter);
358 verifyNoMoreInteractions(netconfOperationServiceSnapshot);
361 private void checkBigDecimal(Document response) throws NodeTestException, SAXException, IOException {
362 assertContainsElement(response, readXmlToElement("<sleep-factor xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.58</sleep-factor>"));
364 assertContainsElement(response, readXmlToElement("<sleep-factor xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.00</sleep-factor>"));
368 private void closeSession() throws NetconfDocumentedException, ParserConfigurationException, SAXException,
370 DefaultCloseSession closeOp = new DefaultCloseSession(NETCONF_SESSION_ID, netconfOperationServiceSnapshot);
371 executeOp(closeOp, "netconfMessages/closeSession.xml");
374 private void edit(String resource) throws ParserConfigurationException, SAXException, IOException,
375 NetconfDocumentedException {
376 EditConfig editOp = new EditConfig(yangStoreSnapshot, transactionProvider, configRegistryClient,
378 executeOp(editOp, resource);
381 private void commit() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
382 Commit commitOp = new Commit(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
383 executeOp(commitOp, "netconfMessages/commit.xml");
386 private Document getConfigCandidate() throws ParserConfigurationException, SAXException, IOException,
387 NetconfDocumentedException {
388 GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
389 configRegistryClient, NETCONF_SESSION_ID);
390 return executeOp(getConfigOp, "netconfMessages/getConfig_candidate.xml");
393 private Document getConfigRunning() throws ParserConfigurationException, SAXException, IOException,
394 NetconfDocumentedException {
395 GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
396 configRegistryClient, NETCONF_SESSION_ID);
397 return executeOp(getConfigOp, "netconfMessages/getConfig.xml");
400 @Ignore("second edit message corrupted")
401 @Test(expected = NetconfDocumentedException.class)
402 public void testConfigNetconfReplaceDefaultEx() throws Exception {
404 createModule(INSTANCE_NAME);
406 edit("netconfMessages/editConfig.xml");
407 edit("netconfMessages/editConfig_replace_default_ex.xml");
411 public void testConfigNetconfReplaceDefault() throws Exception {
413 createModule(INSTANCE_NAME);
415 edit("netconfMessages/editConfig.xml");
417 Document response = getConfigRunning();
418 final int allInstances = response.getElementsByTagName("module").getLength();
420 edit("netconfMessages/editConfig_replace_default.xml");
423 response = getConfigRunning();
425 final int afterReplace = response.getElementsByTagName("module").getLength();
426 assertEquals(4, allInstances);
427 assertEquals(2, afterReplace);
431 public void testSameAttrDifferentNamespaces() throws Exception {
433 edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespaces.xml");
435 } catch (NetconfDocumentedException e) {
436 String message = e.getMessage();
437 assertContainsString(message, "Element simpleInt present multiple times with different namespaces");
438 assertContainsString(message, TEST_NAMESPACE);
439 assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
444 public void testDifferentNamespaceInTO() throws Exception {
446 edit("netconfMessages/namespaces/editConfig_differentNamespaceTO.xml");
448 } catch (NetconfDocumentedException e) {
449 String message = e.getMessage();
450 assertContainsString(message, "Unrecognised elements");
451 assertContainsString(message, "simple-int2");
452 assertContainsString(message, "dto_d");
457 public void testSameAttrDifferentNamespacesList() throws Exception {
459 edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespacesList.xml");
461 } catch (NetconfDocumentedException e) {
462 String message = e.getMessage();
463 assertContainsString(message, "Element allow-user present multiple times with different namespaces");
464 assertContainsString(message, TEST_NAMESPACE);
465 assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
470 public void testTypeNameConfigAttributeMatching() throws Exception {
471 edit("netconfMessages/editConfig.xml");
473 edit("netconfMessages/namespaces/editConfig_typeNameConfigAttributeMatching.xml");
476 Document response = getConfigRunning();
477 checkTypeConfigAttribute(response);
480 // TODO add <modules operation="replace"> functionality
481 @Test(expected = NetconfDocumentedException.class)
482 public void testConfigNetconfReplaceModuleEx() throws Exception {
484 createModule(INSTANCE_NAME);
486 edit("netconfMessages/editConfig.xml");
487 edit("netconfMessages/editConfig_replace_module_ex.xml");
491 public void testUnrecognisedConfigElements() throws Exception {
493 String format = "netconfMessages/unrecognised/editConfig_unrecognised%d.xml";
494 final int TESTS_COUNT = 8;
496 for (int i = 0; i < TESTS_COUNT; i++) {
497 String file = String.format(format, i + 1);
498 LOG.info("Reading {}", file);
501 } catch (NetconfDocumentedException e) {
502 assertContainsString(e.getMessage(), "Unrecognised elements");
503 assertContainsString(e.getMessage(), "unknownAttribute");
506 fail("Unrecognised test should throw exception " + file);
513 public void testConfigNetconfReplaceModule() throws Exception {
515 createModule(INSTANCE_NAME);
517 edit("netconfMessages/editConfig.xml");
519 Document response = getConfigRunning();
520 final int allInstances = response.getElementsByTagName("instance").getLength();
522 edit("netconfMessages/editConfig_replace_module.xml");
525 response = getConfigRunning();
526 final int afterReplace = response.getElementsByTagName("instance").getLength();
528 assertEquals(4 + 4 /* Instances from services */, allInstances);
529 assertEquals(3 + 3, afterReplace);
532 @Test(expected = NetconfDocumentedException.class)
533 public void testEx() throws Exception {
538 @Test(expected = NetconfDocumentedException.class)
539 public void testEx2() throws Exception {
543 private void discard() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
544 DiscardChanges discardOp = new DiscardChanges(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
545 executeOp(discardOp, "netconfMessages/discardChanges.xml");
548 private void checkBinaryLeafEdited(final Document response) throws NodeTestException, SAXException, IOException {
549 assertContainsElement(response, readXmlToElement("<binaryLeaf xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">YmluYXJ5</binaryLeaf>"));
550 assertContainsElement(response, readXmlToElement("<binaryLeaf xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ZGVmYXVsdEJpbg==</binaryLeaf>"));
553 private void checkTypedefs(final Document response) throws NodeTestException, SAXException, IOException {
555 assertContainsElement(response, readXmlToElement("<extended xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">10</extended>"));
557 assertContainsElement(response, readXmlToElement("<extended xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">1</extended>"));
559 assertContainsElement(response, readXmlToElement("<extended-twice xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">20</extended-twice>"));
561 assertContainsElement(response, readXmlToElement("<extended-twice xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2</extended-twice>"));
563 assertContainsElement(response, readXmlToElement("<extended-enum xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">TWO</extended-enum>"));
565 assertContainsElement(response, readXmlToElement("<extended-enum xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ONE</extended-enum>"));
568 private void assertContainsString(String string, String substring) {
569 assertThat(string, CoreMatchers.containsString(substring));
572 private void checkEnum(final Document response) throws Exception {
574 String expectedEnumContent = "TWO";
576 XMLAssert.assertXpathEvaluatesTo(expectedEnumContent,
577 getXpathForNetconfImplSubnode(INSTANCE_NAME,"extended-enum"),
581 private void checkTestingDeps(Document response) {
582 int testingDepsSize = response.getElementsByTagName("testing-deps").getLength();
583 assertEquals(2, testingDepsSize);
586 private String getXpathForNetconfImplSubnode(String instanceName, String subnode) {
587 return "/urn:ietf:params:xml:ns:netconf:base:1.0:rpc-reply" +
588 "/urn:ietf:params:xml:ns:netconf:base:1.0:data" +
589 "/urn:opendaylight:params:xml:ns:yang:controller:config:modules" +
590 "/module[name='"+instanceName+"']" +
591 "/urn:opendaylight:params:xml:ns:yang:controller:test:impl:impl-netconf" +
592 "/urn:opendaylight:params:xml:ns:yang:controller:test:impl:"+subnode;
595 private void checkTypeConfigAttribute(Document response) throws Exception {
597 Map<String,String> namesToTypeValues = ImmutableMap.of("instance-from-code", "configAttributeType",
598 "test2", "default-string");
599 for (Entry<String, String> nameToExpectedValue : namesToTypeValues.entrySet()) {
600 XMLAssert.assertXpathEvaluatesTo(nameToExpectedValue.getValue(),
601 getXpathForNetconfImplSubnode(nameToExpectedValue.getKey(),"type"),
606 private Map<String, Map<String, ModuleMXBeanEntry>> getMbes() throws Exception {
607 final List<InputStream> yangDependencies = getYangs();
609 final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries = Maps.newHashMap();
611 YangParserImpl yangParser = new YangParserImpl();
612 final SchemaContext schemaContext = yangParser.resolveSchemaContext(new HashSet<>(yangParser.parseYangModelsFromStreamsMapped(yangDependencies).values()));
613 YangStoreServiceImpl yangStoreService = new YangStoreServiceImpl(new SchemaContextProvider() {
615 public SchemaContext getSchemaContext() {
616 return schemaContext ;
619 mBeanEntries.putAll(yangStoreService.getYangStoreSnapshot().getModuleMXBeanEntryMap());
624 private Set<org.opendaylight.yangtools.yang.model.api.Module> getModules() throws Exception {
625 SchemaContext resolveSchemaContext = getSchemaContext();
626 return resolveSchemaContext.getModules();
629 private SchemaContext getSchemaContext() throws Exception {
630 final List<InputStream> yangDependencies = getYangs();
631 YangParserImpl parser = new YangParserImpl();
633 Set<Module> allYangModules = parser.parseYangModelsFromStreams(yangDependencies);
635 return parser.resolveSchemaContext(Sets
636 .newHashSet(allYangModules));
640 public void testConfigNetconfRuntime() throws Exception {
642 createModule(INSTANCE_NAME);
644 edit("netconfMessages/editConfig.xml");
645 checkBinaryLeafEdited(getConfigCandidate());
649 Document response = get();
651 assertEquals(2/*With runtime beans*/ + 2 /*Without runtime beans*/, getElementsSize(response, "module"));
653 assertEquals(2, getElementsSize(response, "asdf"));
654 // data from running config
655 assertEquals(2, getElementsSize(response, "simple-short"));
657 assertEquals(8, getElementsSize(response, "inner-running-data"));
658 assertEquals(8, getElementsSize(response, "deep2"));
659 assertEquals(8 * 4, getElementsSize(response, "inner-inner-running-data"));
660 assertEquals(8 * 4, getElementsSize(response, "deep3"));
661 assertEquals(8 * 4 * 2, getElementsSize(response, "list-of-strings"));
662 assertEquals(8, getElementsSize(response, "inner-running-data-additional", "urn:opendaylight:params:xml:ns:yang:controller:test:impl"));
663 assertEquals(8, getElementsSize(response, "deep4"));
666 RuntimeRpc netconf = new RuntimeRpc(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID);
668 response = executeOp(netconf, "netconfMessages/rpc.xml");
669 assertContainsElementWithText(response, "testarg1");
671 response = executeOp(netconf, "netconfMessages/rpcInner.xml");
672 Document expectedReplyOk = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
673 XMLUnit.setIgnoreWhitespace(true);
674 XMLAssert.assertXMLEqual(expectedReplyOk, response);
676 response = executeOp(netconf, "netconfMessages/rpcInnerInner.xml");
677 assertContainsElementWithText(response, "true");
679 response = executeOp(netconf, "netconfMessages/rpcInnerInner_complex_output.xml");
680 assertContainsElementWithText(response, "1");
681 assertContainsElementWithText(response, "2");
684 private Document get() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
685 Get getOp = new Get(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID);
686 return executeOp(getOp, "netconfMessages/get.xml");
689 private int getElementsSize(Document response, String elementName) {
690 return response.getElementsByTagName(elementName).getLength();
693 private int getElementsSize(Document response, String elementName, String namespace) {
694 return response.getElementsByTagNameNS(namespace, elementName).getLength();
697 private Document executeOp(final NetconfOperation op, final String filename) throws ParserConfigurationException,
698 SAXException, IOException, NetconfDocumentedException {
700 final Document request = XmlFileLoader.xmlFileToDocument(filename);
702 LOG.debug("Executing netconf operation\n{}", XmlUtil.toString(request));
703 HandlingPriority priority = op.canHandle(request);
705 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
707 final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
708 LOG.debug("Got response\n{}", XmlUtil.toString(response));
712 private List<InputStream> getYangs() throws FileNotFoundException {
713 List<String> paths = Arrays.asList("/META-INF/yang/config.yang", "/META-INF/yang/rpc-context.yang",
714 "/META-INF/yang/config-test.yang", "/META-INF/yang/config-test-impl.yang", "/META-INF/yang/test-types.yang",
715 "/META-INF/yang/ietf-inet-types.yang");
716 final Collection<InputStream> yangDependencies = new ArrayList<>();
717 for (String path : paths) {
718 final InputStream is = Preconditions
719 .checkNotNull(getClass().getResourceAsStream(path), path + " not found");
720 yangDependencies.add(is);
722 return Lists.newArrayList(yangDependencies);
725 private void setModule(final NetconfTestImplModuleMXBean mxBean, final ConfigTransactionJMXClient transaction, String depName)
726 throws InstanceAlreadyExistsException, InstanceNotFoundException {
727 mxBean.setSimpleInt((long) 44);
728 mxBean.setBinaryLeaf(new byte[] { 8, 7, 9 });
729 final DtoD dtob = getDtoD();
730 mxBean.setDtoD(dtob);
732 final DtoC dtoa = getDtoC();
733 mxBean.setDtoC(dtoa);
734 mxBean.setSimpleBoolean(false);
736 final Peers p1 = new Peers();
739 p1.setSimpleInt3(456);
740 final Peers p2 = new Peers();
742 p2.setPort("port23");
743 p2.setSimpleInt3(456);
744 mxBean.setPeers(Lists.<Peers> newArrayList(p1, p2));
746 mxBean.setSimpleLong(454545L);
747 mxBean.setSimpleLong2(44L);
748 mxBean.setSimpleBigInteger(BigInteger.valueOf(999L));
749 mxBean.setSimpleByte(new Byte((byte) 4));
750 mxBean.setSimpleShort(new Short((short) 4));
751 mxBean.setSimpleTest(545);
753 mxBean.setComplexList(Lists.<ComplexList> newArrayList());
754 mxBean.setSimpleList(Lists.<Integer> newArrayList());
756 final ObjectName testingDepOn = transaction.createModule(this.factory2.getImplementationName(), depName);
758 for (Class<? extends AbstractServiceInterface> sInterface : factory2.getImplementedServiceIntefaces()) {
759 ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
760 transaction.saveServiceReference(
761 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + depName + "_" + i++,
765 mxBean.setTestingDep(testingDepOn);
768 private static DtoD getDtoD() {
769 final DtoD dtob = new DtoD();
770 dtob.setSimpleInt1((long) 444);
771 dtob.setSimpleInt2((long) 4444);
772 dtob.setSimpleInt3(454);
773 final ComplexDtoBInner dtobInner = new ComplexDtoBInner();
774 final Deep deep = new Deep();
775 deep.setSimpleInt3(4);
776 dtobInner.setDeep(deep);
777 dtobInner.setSimpleInt3(44);
778 dtobInner.setSimpleList(Lists.newArrayList(4));
779 dtob.setComplexDtoBInner(Lists.newArrayList(dtobInner));
780 dtob.setSimpleList(Lists.newArrayList(4));
784 private static DtoC getDtoC() {
785 final DtoC dtoa = new DtoC();
786 // dtoa.setSimpleArg((long) 55);
787 final DtoAInner dtoAInner = new DtoAInner();
788 final DtoAInnerInner dtoAInnerInner = new DtoAInnerInner();
789 dtoAInnerInner.setSimpleArg(456L);
790 dtoAInner.setDtoAInnerInner(dtoAInnerInner);
791 dtoAInner.setSimpleArg(44L);
792 dtoa.setDtoAInner(dtoAInner);