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.netconf.confignetconfconnector;
11 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
12 import static org.hamcrest.CoreMatchers.containsString;
13 import static org.hamcrest.CoreMatchers.not;
14 import static org.junit.Assert.assertEquals;
15 import static org.junit.Assert.assertThat;
16 import static org.junit.Assert.assertTrue;
17 import static org.junit.Assert.fail;
18 import static org.mockito.Matchers.any;
19 import static org.mockito.Matchers.anyString;
20 import static org.mockito.Mockito.doNothing;
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.doThrow;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.verifyNoMoreInteractions;
26 import static org.opendaylight.controller.config.util.xml.XmlUtil.readXmlToElement;
27 import static org.opendaylight.netconf.util.test.XmlUnitUtil.assertContainsElement;
28 import static org.opendaylight.netconf.util.test.XmlUnitUtil.assertContainsElementWithText;
30 import com.google.common.base.Optional;
31 import com.google.common.base.Preconditions;
32 import com.google.common.collect.BiMap;
33 import com.google.common.collect.HashBiMap;
34 import com.google.common.collect.ImmutableMap;
35 import com.google.common.collect.Lists;
36 import com.google.common.collect.Sets;
37 import io.netty.channel.Channel;
38 import java.io.IOException;
39 import java.math.BigInteger;
40 import java.net.URISyntaxException;
41 import java.util.Collections;
42 import java.util.HashMap;
43 import java.util.HashSet;
45 import java.util.Map.Entry;
47 import java.util.regex.Matcher;
48 import java.util.regex.Pattern;
49 import javax.management.InstanceAlreadyExistsException;
50 import javax.management.InstanceNotFoundException;
51 import javax.management.ObjectName;
52 import javax.xml.parsers.ParserConfigurationException;
53 import org.custommonkey.xmlunit.AbstractNodeTester;
54 import org.custommonkey.xmlunit.NodeTest;
55 import org.custommonkey.xmlunit.NodeTestException;
56 import org.custommonkey.xmlunit.NodeTester;
57 import org.custommonkey.xmlunit.XMLAssert;
58 import org.custommonkey.xmlunit.XMLUnit;
59 import org.junit.Before;
60 import org.junit.Ignore;
61 import org.junit.Test;
62 import org.mockito.Mock;
63 import org.mockito.MockitoAnnotations;
64 import org.opendaylight.controller.config.api.ConflictingVersionException;
65 import org.opendaylight.controller.config.api.ValidationException;
66 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
67 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
68 import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacade;
69 import org.opendaylight.controller.config.facade.xml.osgi.EnumResolver;
70 import org.opendaylight.controller.config.facade.xml.osgi.YangStoreService;
71 import org.opendaylight.controller.config.facade.xml.transactions.TransactionProvider;
72 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
73 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
74 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
75 import org.opendaylight.controller.config.util.xml.DocumentedException;
76 import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
77 import org.opendaylight.controller.config.util.xml.XmlUtil;
78 import org.opendaylight.controller.config.yang.test.impl.ComplexDtoBInner;
79 import org.opendaylight.controller.config.yang.test.impl.ComplexList;
80 import org.opendaylight.controller.config.yang.test.impl.Deep;
81 import org.opendaylight.controller.config.yang.test.impl.DepTestImplModuleFactory;
82 import org.opendaylight.controller.config.yang.test.impl.DtoAInner;
83 import org.opendaylight.controller.config.yang.test.impl.DtoAInnerInner;
84 import org.opendaylight.controller.config.yang.test.impl.DtoC;
85 import org.opendaylight.controller.config.yang.test.impl.DtoD;
86 import org.opendaylight.controller.config.yang.test.impl.IdentityTestModuleFactory;
87 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleFactory;
88 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleMXBean;
89 import org.opendaylight.controller.config.yang.test.impl.Peers;
90 import org.opendaylight.controller.config.yang.test.impl.TestImplModuleFactory;
91 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
92 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
93 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
94 import org.opendaylight.netconf.confignetconfconnector.operations.Commit;
95 import org.opendaylight.netconf.confignetconfconnector.operations.DiscardChanges;
96 import org.opendaylight.netconf.confignetconfconnector.operations.Lock;
97 import org.opendaylight.netconf.confignetconfconnector.operations.UnLock;
98 import org.opendaylight.netconf.confignetconfconnector.operations.editconfig.EditConfig;
99 import org.opendaylight.netconf.confignetconfconnector.operations.get.Get;
100 import org.opendaylight.netconf.confignetconfconnector.operations.getconfig.GetConfig;
101 import org.opendaylight.netconf.confignetconfconnector.operations.runtimerpc.RuntimeRpc;
102 import org.opendaylight.netconf.impl.NetconfServerSession;
103 import org.opendaylight.netconf.impl.NetconfServerSessionListener;
104 import org.opendaylight.netconf.impl.mapping.operations.DefaultCloseSession;
105 import org.opendaylight.netconf.impl.osgi.AggregatedNetconfOperationServiceFactory;
106 import org.opendaylight.netconf.impl.osgi.NetconfOperationRouter;
107 import org.opendaylight.netconf.mapping.api.HandlingPriority;
108 import org.opendaylight.netconf.mapping.api.NetconfOperation;
109 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
110 import org.opendaylight.netconf.util.messages.NetconfMessageUtil;
111 import org.opendaylight.netconf.util.test.XmlFileLoader;
112 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity1;
113 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity2;
114 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
115 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
116 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
117 import org.osgi.framework.Filter;
118 import org.osgi.framework.ServiceListener;
119 import org.osgi.framework.ServiceReference;
120 import org.slf4j.Logger;
121 import org.slf4j.LoggerFactory;
122 import org.w3c.dom.Document;
123 import org.w3c.dom.Element;
124 import org.w3c.dom.Node;
125 import org.w3c.dom.NodeList;
126 import org.w3c.dom.Text;
127 import org.w3c.dom.traversal.DocumentTraversal;
128 import org.xml.sax.SAXException;
131 public class NetconfMappingTest extends AbstractConfigTest {
132 private static final Logger LOG = LoggerFactory.getLogger(NetconfMappingTest.class);
134 private static final String INSTANCE_NAME = "instance-from-code";
135 private static final String NETCONF_SESSION_ID = "foo";
136 private static final String TEST_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:test:impl";
137 private NetconfTestImplModuleFactory factory;
138 private DepTestImplModuleFactory factory2;
139 private IdentityTestModuleFactory factory3;
140 private TestImplModuleFactory factory4;
143 YangStoreService yangStoreSnapshot;
145 NetconfOperationRouter netconfOperationRouter;
147 AggregatedNetconfOperationServiceFactory netconfOperationServiceSnapshot;
149 private AutoCloseable sessionCloseable;
151 private TransactionProvider transactionProvider;
153 private ConfigSubsystemFacade configSubsystemFacade;
156 public void setUp() throws Exception {
157 MockitoAnnotations.initMocks(this);
160 final Filter filter = mock(Filter.class);
161 doReturn(filter).when(mockedContext).createFilter(anyString());
162 doNothing().when(mockedContext).addServiceListener(any(ServiceListener.class), anyString());
163 doReturn(new ServiceReference<?>[]{}).when(mockedContext).getServiceReferences(anyString(), anyString());
165 doReturn(yangStoreSnapshot).when(yangStoreSnapshot).getCurrentSnapshot();
166 doReturn(getMbes()).when(this.yangStoreSnapshot).getModuleMXBeanEntryMap();
167 doReturn(getModules()).when(this.yangStoreSnapshot).getModules();
168 doReturn(new EnumResolver() {
170 public String fromYang(final String enumType, final String enumYangValue) {
171 return Preconditions.checkNotNull(getEnumMapping().get(enumYangValue),
172 "Unable to resolve enum value %s, for enum %s with mappings %s",
173 enumYangValue, enumType, getEnumMapping());
177 public String toYang(final String enumType, final String enumYangValue) {
178 return Preconditions.checkNotNull(getEnumMapping().inverse().get(enumYangValue),
179 "Unable to resolve enum value %s, for enum %s with mappings %s",
180 enumYangValue, enumType, getEnumMapping().inverse());
182 }).when(this.yangStoreSnapshot).getEnumResolver();
184 this.factory = new NetconfTestImplModuleFactory();
185 this.factory2 = new DepTestImplModuleFactory();
186 this.factory3 = new IdentityTestModuleFactory();
187 factory4 = new TestImplModuleFactory();
188 doNothing().when(sessionCloseable).close();
190 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, this.factory,
191 this.factory2, this.factory3, factory4));
193 transactionProvider = new TransactionProvider(this.configRegistryClient, NETCONF_SESSION_ID);
195 configSubsystemFacade = new ConfigSubsystemFacade(configRegistryClient, configRegistryClient, yangStoreSnapshot,
199 private ObjectName createModule(final String instanceName) throws InstanceAlreadyExistsException,
200 InstanceNotFoundException, URISyntaxException, ValidationException, ConflictingVersionException {
201 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
203 final ObjectName on = transaction.createModule(this.factory.getImplementationName(), instanceName);
204 final NetconfTestImplModuleMXBean mxBean = transaction.newMXBeanProxy(on, NetconfTestImplModuleMXBean.class);
205 setModule(mxBean, transaction, instanceName + "_dep");
208 for (final Class<? extends AbstractServiceInterface> serviceInterface :
209 factory.getImplementedServiceIntefaces()) {
210 final ServiceInterfaceAnnotation annotation =
211 serviceInterface.getAnnotation(ServiceInterfaceAnnotation.class);
212 transaction.saveServiceReference(
213 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()),
214 "ref_from_code_to_" + instanceName + "_" + index++, on);
217 transaction.commit();
222 public void testIdentityRefs() throws Exception {
223 edit("netconfMessages/editConfig_identities.xml");
226 Document configRunning = getConfigRunning();
227 String asString = XmlUtil.toString(configRunning);
228 assertThat(asString, containsString("test-identity2"));
229 assertThat(asString, containsString("test-identity1"));
230 assertEquals(2, countSubstringOccurence(asString, "</identities>"));
232 edit("netconfMessages/editConfig_identities_inner_replace.xml");
234 configRunning = getConfigRunning();
235 asString = XmlUtil.toString(configRunning);
236 // test-identity1 was removed by replace
237 assertThat(asString, not(containsString("test-identity2")));
238 // now only 1 identities entry is present
239 assertEquals(1, countSubstringOccurence(asString, "</identities>"));
242 private static int countSubstringOccurence(final String string, final String substring) {
243 final Matcher matches = Pattern.compile(substring).matcher(string);
245 while (matches.find()) {
252 protected BindingRuntimeContext getBindingRuntimeContext() {
253 final BindingRuntimeContext ret = super.getBindingRuntimeContext();
254 doReturn(TestIdentity1.class).when(ret).getIdentityClass(TestIdentity1.QNAME);
255 doReturn(TestIdentity2.class).when(ret).getIdentityClass(TestIdentity2.QNAME);
256 doReturn(getSchemaContext()).when(ret).getSchemaContext();
261 public void testServicePersistance() throws Exception {
262 createModule(INSTANCE_NAME);
264 edit("netconfMessages/editConfig.xml");
265 Document config = getConfigCandidate();
266 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
267 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
268 "ref_from_code_to_instance-from-code_1"));
271 edit("netconfMessages/editConfig_addServiceName.xml");
272 config = getConfigCandidate();
273 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
274 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
275 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
277 edit("netconfMessages/editConfig_addServiceNameOnTest.xml");
278 config = getConfigCandidate();
279 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
280 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
281 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
284 config = getConfigRunning();
285 assertCorrectRefNamesForDependencies(config);
286 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
287 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
288 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
290 edit("netconfMessages/editConfig_removeServiceNameOnTest.xml");
291 config = getConfigCandidate();
292 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
293 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
294 "ref_from_code_to_instance-from-code_1"));
297 edit("netconfMessages/editConfig_removeServiceNameOnTest.xml");
298 fail("Should've failed, non-existing service instance");
299 } catch (final DocumentedException e) {
300 assertEquals(e.getErrorSeverity(), DocumentedException.ErrorSeverity.ERROR);
301 assertEquals(e.getErrorTag(), DocumentedException.ErrorTag.OPERATION_FAILED);
302 assertEquals(e.getErrorType(), DocumentedException.ErrorType.APPLICATION);
305 edit("netconfMessages/editConfig_replace_default.xml");
306 config = getConfigCandidate();
307 assertCorrectServiceNames(config, Collections.<String>emptySet());
309 edit("netconfMessages/editConfig_remove.xml");
310 config = getConfigCandidate();
311 assertCorrectServiceNames(config, Collections.<String>emptySet());
314 config = getConfigCandidate();
315 assertCorrectServiceNames(config, Collections.<String>emptySet());
320 public void testUnLock() throws Exception {
321 assertTrue(NetconfMessageUtil.isOKMessage(lockCandidate()));
322 assertTrue(NetconfMessageUtil.isOKMessage(unlockCandidate()));
325 private static void assertCorrectRefNamesForDependencies(final Document config) throws NodeTestException {
326 final NodeList modulesList = config.getElementsByTagName("modules");
327 assertEquals(1, modulesList.getLength());
329 final NodeTest nt = new NodeTest((DocumentTraversal) config, modulesList.item(0));
330 final NodeTester tester = new AbstractNodeTester() {
331 private int defaultRefNameCount = 0;
332 private int userRefNameCount = 0;
335 public void testText(final Text text) throws NodeTestException {
336 if (text.getData().equals("ref_dep2")) {
337 defaultRefNameCount++;
338 } else if (text.getData().equals("ref_dep_user_two")) {
344 public void noMoreNodes(final NodeTest forTest) throws NodeTestException {
345 assertEquals(0, defaultRefNameCount);
346 assertEquals(2, userRefNameCount);
349 nt.performTest(tester, Node.TEXT_NODE);
352 private static void assertCorrectServiceNames(final Document configCandidate,
353 final Set<String> refNames) throws NodeTestException {
354 final Set<String> refNames2 = new HashSet<>(refNames);
355 final NodeList servicesNodes = configCandidate.getElementsByTagName("services");
356 assertEquals(1, servicesNodes.getLength());
358 final NodeTest nt = new NodeTest((DocumentTraversal) configCandidate, servicesNodes.item(0));
359 final NodeTester tester = new AbstractNodeTester() {
362 public void testElement(final Element element) throws NodeTestException {
363 if (element.getNodeName() != null) {
364 if (element.getNodeName().equals("name")) {
365 final String elmText = element.getTextContent();
366 if (refNames2.contains(elmText)) {
367 refNames2.remove(elmText);
369 throw new NodeTestException("Unexpected services defined: " + elmText);
376 public void noMoreNodes(final NodeTest forTest) throws NodeTestException {
377 assertEquals(Collections.<String>emptySet(), refNames2);
378 assertTrue(refNames2.toString(), refNames2.isEmpty());
381 nt.performTest(tester, Node.ELEMENT_NODE);
385 public void testConfigNetconfUnionTypes() throws Exception {
387 createModule(INSTANCE_NAME);
389 edit("netconfMessages/editConfig.xml");
391 Document response = getConfigRunning();
392 final Element ipElement = readXmlToElement(
393 "<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">0:0:0:0:0:0:0:1</ip>");
394 assertContainsElement(response, readXmlToElement(
395 "<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">0:0:0:0:0:0:0:1</ip>"));
397 assertContainsElement(response, readXmlToElement("<union-test-attr xmlns="
398 + "\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">456</union-test-attr>"));
401 edit("netconfMessages/editConfig_setUnions.xml");
403 response = getConfigRunning();
404 assertContainsElement(response, readXmlToElement(
405 "<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">127.1.2.3</ip>"));
406 assertContainsElement(response, readXmlToElement("<union-test-attr xmlns="
407 + "\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">"
408 + "randomStringForUnion</union-test-attr>"));
413 public void testConfigNetconf() throws Exception {
415 createModule(INSTANCE_NAME);
417 edit("netconfMessages/editConfig.xml");
418 final Document configCandidate = getConfigCandidate();
419 checkBinaryLeafEdited(configCandidate);
422 // default-operation:none, should not affect binary leaf
423 edit("netconfMessages/editConfig_none.xml");
424 checkBinaryLeafEdited(getConfigCandidate());
428 final Document response = getConfigRunning();
430 checkBinaryLeafEdited(response);
431 checkTypeConfigAttribute(response);
432 checkTypedefs(response);
433 checkTestingDeps(response);
435 checkBigDecimal(response);
437 edit("netconfMessages/editConfig_remove.xml");
440 assertXMLEqual(getConfigCandidate(), getConfigRunning());
442 final Document expectedResult =
443 XmlFileLoader.xmlFileToDocument("netconfMessages/editConfig_expectedResult.xml");
444 XMLUnit.setIgnoreWhitespace(true);
445 assertXMLEqual(expectedResult, getConfigRunning());
446 assertXMLEqual(expectedResult, getConfigCandidate());
448 edit("netconfMessages/editConfig_none.xml");
450 verify(sessionCloseable).close();
451 verifyNoMoreInteractions(netconfOperationRouter);
452 verifyNoMoreInteractions(netconfOperationServiceSnapshot);
455 private static void checkBigDecimal(final Document response) throws NodeTestException, SAXException, IOException {
456 assertContainsElement(response, readXmlToElement("<sleep-factor xmlns="
457 + "\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.58</sleep-factor>"));
459 assertContainsElement(response, readXmlToElement("<sleep-factor xmlns="
460 + "\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.00</sleep-factor>"));
463 private void closeSession() throws ParserConfigurationException, SAXException,
464 IOException, DocumentedException {
465 final Channel channel = mock(Channel.class);
466 doReturn("channel").when(channel).toString();
467 final NetconfServerSessionListener listener = mock(NetconfServerSessionListener.class);
468 final NetconfServerSession session =
469 new NetconfServerSession(listener, channel, 1L,
470 NetconfHelloMessageAdditionalHeader.fromString("[netconf;10.12.0.102:48528;ssh;;;;;;]"));
471 final DefaultCloseSession closeOp = new DefaultCloseSession(NETCONF_SESSION_ID, sessionCloseable);
472 closeOp.setNetconfSession(session);
473 executeOp(closeOp, "netconfMessages/closeSession.xml");
476 private void edit(final String resource) throws ParserConfigurationException, SAXException, IOException,
477 DocumentedException {
478 final EditConfig editOp = new EditConfig(configSubsystemFacade, NETCONF_SESSION_ID);
479 executeOp(editOp, resource);
482 private void commit() throws ParserConfigurationException, SAXException, IOException, DocumentedException {
483 final Commit commitOp = new Commit(configSubsystemFacade, NETCONF_SESSION_ID);
484 executeOp(commitOp, "netconfMessages/commit.xml");
487 private static Document lockCandidate() throws ParserConfigurationException, SAXException, IOException,
488 DocumentedException {
489 final Lock commitOp = new Lock(NETCONF_SESSION_ID);
490 return executeOp(commitOp, "netconfMessages/lock.xml");
493 private static Document unlockCandidate() throws ParserConfigurationException, SAXException, IOException,
494 DocumentedException {
495 final UnLock commitOp = new UnLock(NETCONF_SESSION_ID);
496 return executeOp(commitOp, "netconfMessages/unlock.xml");
499 private Document getConfigCandidate() throws ParserConfigurationException, SAXException, IOException,
500 DocumentedException {
501 final GetConfig getConfigOp = new GetConfig(configSubsystemFacade, Optional.<String>absent(),
503 return executeOp(getConfigOp, "netconfMessages/getConfig_candidate.xml");
506 private Document getConfigRunning() throws ParserConfigurationException, SAXException, IOException,
507 DocumentedException {
508 final GetConfig getConfigOp = new GetConfig(configSubsystemFacade, Optional.<String>absent(),
510 return executeOp(getConfigOp, "netconfMessages/getConfig.xml");
513 @Ignore("second edit message corrupted")
514 @Test(expected = DocumentedException.class)
515 public void testConfigNetconfReplaceDefaultEx() throws Exception {
517 createModule(INSTANCE_NAME);
519 edit("netconfMessages/editConfig.xml");
520 edit("netconfMessages/editConfig_replace_default_ex.xml");
524 public void testConfigNetconfReplaceDefault() throws Exception {
526 createModule(INSTANCE_NAME);
528 edit("netconfMessages/editConfig.xml");
530 Document response = getConfigRunning();
531 final int allInstances = response.getElementsByTagName("module").getLength();
533 edit("netconfMessages/editConfig_replace_default.xml");
536 response = getConfigRunning();
538 final int afterReplace = response.getElementsByTagName("module").getLength();
539 assertEquals(4, allInstances);
540 assertEquals(2, afterReplace);
544 public void testSameAttrDifferentNamespaces() throws Exception {
546 edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespaces.xml");
548 } catch (final DocumentedException e) {
549 final String message = e.getMessage();
550 assertContainsString(message, "Element simpleInt present multiple times with different namespaces");
551 assertContainsString(message, TEST_NAMESPACE);
552 assertContainsString(message, XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
557 public void testDifferentNamespaceInTO() throws Exception {
559 edit("netconfMessages/namespaces/editConfig_differentNamespaceTO.xml");
561 } catch (final DocumentedException e) {
562 final String message = e.getMessage();
563 assertContainsString(message, "Unrecognised elements");
564 assertContainsString(message, "simple-int2");
565 assertContainsString(message, "dto_d");
570 public void testSameAttrDifferentNamespacesList() throws Exception {
572 edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespacesList.xml");
574 } catch (final DocumentedException e) {
575 final String message = e.getMessage();
576 assertContainsString(message, "Element allow-user present multiple times with different namespaces");
577 assertContainsString(message, TEST_NAMESPACE);
578 assertContainsString(message, XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
583 public void testTypeNameConfigAttributeMatching() throws Exception {
584 edit("netconfMessages/editConfig.xml");
586 edit("netconfMessages/namespaces/editConfig_typeNameConfigAttributeMatching.xml");
589 final Document response = getConfigRunning();
590 checkTypeConfigAttribute(response);
593 // TODO add <modules operation="replace"> functionality
594 @Test(expected = DocumentedException.class)
595 public void testConfigNetconfReplaceModuleEx() throws Exception {
597 createModule(INSTANCE_NAME);
599 edit("netconfMessages/editConfig.xml");
600 edit("netconfMessages/editConfig_replace_module_ex.xml");
604 public void testUnrecognisedConfigElements() throws Exception {
606 final String format = "netconfMessages/unrecognised/editConfig_unrecognised%d.xml";
607 final int testsCount = 8;
609 for (int i = 0; i < testsCount; i++) {
610 final String file = String.format(format, i + 1);
611 LOG.info("Reading {}", file);
614 } catch (final DocumentedException e) {
615 assertContainsString(e.getMessage(), "Unrecognised elements");
616 assertContainsString(e.getMessage(), "unknownAttribute");
619 fail("Unrecognised test should throw exception " + file);
626 public void testConfigNetconfReplaceModule() throws Exception {
628 createModule(INSTANCE_NAME);
630 edit("netconfMessages/editConfig.xml");
632 Document response = getConfigRunning();
633 final int allInstances = response.getElementsByTagName("instance").getLength();
635 edit("netconfMessages/editConfig_replace_module.xml");
638 response = getConfigRunning();
639 final int afterReplace = response.getElementsByTagName("instance").getLength();
641 assertEquals(4 + 4 /* Instances from services */, allInstances);
642 assertEquals(3 + 3, afterReplace);
646 public void testEx2() throws Exception {
647 //check abort before tx creation
648 assertContainsElement(discard(), readXmlToElement("<ok xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
650 //check abort after tx creation
651 edit("netconfMessages/editConfig.xml");
652 assertContainsElement(discard(), readXmlToElement("<ok xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
656 public void testFailedDiscardChangesAbort() throws Exception {
657 final ConfigSubsystemFacade facade = mock(ConfigSubsystemFacade.class);
658 doThrow(new RuntimeException("Mocked runtime exception, Abort has to fail")).when(facade).abortConfiguration();
660 final DiscardChanges discardOp = new DiscardChanges(facade, NETCONF_SESSION_ID);
663 executeOp(discardOp, "netconfMessages/discardChanges.xml");
664 fail("Should've failed, abort on mocked is supposed to throw RuntimeException");
665 } catch (final DocumentedException e) {
666 assertTrue(e.getErrorTag() == DocumentedException.ErrorTag.OPERATION_FAILED);
667 assertTrue(e.getErrorSeverity() == DocumentedException.ErrorSeverity.ERROR);
668 assertTrue(e.getErrorType() == DocumentedException.ErrorType.APPLICATION);
672 private Document discard() throws ParserConfigurationException, SAXException, IOException, DocumentedException {
673 final DiscardChanges discardOp = new DiscardChanges(configSubsystemFacade, NETCONF_SESSION_ID);
674 return executeOp(discardOp, "netconfMessages/discardChanges.xml");
677 private static void checkBinaryLeafEdited(final Document response)
678 throws NodeTestException, SAXException, IOException {
679 assertContainsElement(response, readXmlToElement("<binaryLeaf xmlns="
680 + "\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">YmluYXJ5</binaryLeaf>"));
681 assertContainsElement(response, readXmlToElement("<binaryLeaf xmlns="
682 + "\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ZGVmYXVsdEJpbg==</binaryLeaf>"));
685 private static void checkTypedefs(final Document response) throws NodeTestException, SAXException, IOException {
687 assertContainsElement(response, readXmlToElement(
688 "<extended xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">10</extended>"));
690 assertContainsElement(response, readXmlToElement(
691 "<extended xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">1</extended>"));
693 assertContainsElement(response, readXmlToElement("<extended-twice xmlns="
694 + "\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">20</extended-twice>"));
696 assertContainsElement(response, readXmlToElement("<extended-twice xmlns="
697 + "\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2</extended-twice>"));
699 assertContainsElement(response, readXmlToElement("<extended-enum xmlns="
700 + "\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">two</extended-enum>"));
702 assertContainsElement(response, readXmlToElement("<extended-enum xmlns="
703 + "\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">one</extended-enum>"));
706 private static void assertContainsString(final String string, final String substring) {
707 assertThat(string, containsString(substring));
710 private static void checkEnum(final Document response) throws Exception {
712 final String expectedEnumContent = "two";
714 XMLAssert.assertXpathEvaluatesTo(expectedEnumContent,
715 getXpathForNetconfImplSubnode(INSTANCE_NAME, "extended-enum"),
719 private static void checkTestingDeps(final Document response) {
720 final int testingDepsSize = response.getElementsByTagName("testing-deps").getLength();
721 assertEquals(2, testingDepsSize);
724 private static String getXpathForNetconfImplSubnode(final String instanceName, final String subnode) {
725 return "/urn:ietf:params:xml:ns:netconf:base:1.0:rpc-reply"
726 + "/urn:ietf:params:xml:ns:netconf:base:1.0:data"
727 + "/urn:opendaylight:params:xml:ns:yang:controller:config:modules"
728 + "/urn:opendaylight:params:xml:ns:yang:controller:config:module"
729 + "[urn:opendaylight:params:xml:ns:yang:controller:config:name='" + instanceName + "']"
730 + "/urn:opendaylight:params:xml:ns:yang:controller:test:impl:impl-netconf"
731 + "/urn:opendaylight:params:xml:ns:yang:controller:test:impl:" + subnode;
734 private static void checkTypeConfigAttribute(final Document response) throws Exception {
736 final Map<String, String> namesToTypeValues = ImmutableMap.of("instance-from-code", "configAttributeType",
737 "test2", "default-string");
738 for (final Entry<String, String> nameToExpectedValue : namesToTypeValues.entrySet()) {
739 XMLAssert.assertXpathEvaluatesTo(nameToExpectedValue.getValue(),
740 getXpathForNetconfImplSubnode(nameToExpectedValue.getKey(), "type"),
745 private static Map<String, Map<String, ModuleMXBeanEntry>> getMbes() {
747 final SchemaContext schemaContext = getSchemaContext();
748 final YangStoreService yangStoreService = new YangStoreService(() -> schemaContext,
749 mock(SchemaSourceProvider.class));
750 final BindingRuntimeContext bindingRuntimeContext = mock(BindingRuntimeContext.class);
751 doReturn(schemaContext).when(bindingRuntimeContext).getSchemaContext();
752 doReturn(getEnumMapping()).when(bindingRuntimeContext).getEnumMapping(any(Class.class));
753 yangStoreService.refresh(bindingRuntimeContext);
754 final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries = new HashMap<>();
755 mBeanEntries.putAll(yangStoreService.getModuleMXBeanEntryMap());
760 private static BiMap<String, String> getEnumMapping() {
761 final HashBiMap<String, String> enumBiMap = HashBiMap.create();
762 // Enum constants mapping from yang -> Java and back
763 enumBiMap.put("one", "One");
764 enumBiMap.put("two", "Two");
765 enumBiMap.put("version1", "Version1");
766 enumBiMap.put("version2", "Version2");
770 private static Set<org.opendaylight.yangtools.yang.model.api.Module> getModules() {
771 return getSchemaContext().getModules();
775 public void testConfigNetconfRuntime() throws Exception {
777 createModule(INSTANCE_NAME);
779 edit("netconfMessages/editConfig.xml");
780 checkBinaryLeafEdited(getConfigCandidate());
784 Document response = get();
786 assertEquals(2/*With runtime beans*/ + 2 /*Without runtime beans*/, getElementsSize(response, "module"));
788 assertEquals(2, getElementsSize(response, "asdf"));
789 // data from running config
790 assertEquals(2, getElementsSize(response, "simple-short"));
792 assertEquals(8, getElementsSize(response, "inner-running-data"));
793 assertEquals(8, getElementsSize(response, "deep2"));
794 assertEquals(8 * 4, getElementsSize(response, "inner-inner-running-data"));
795 assertEquals(8 * 4, getElementsSize(response, "deep3"));
796 assertEquals(8 * 4 * 2, getElementsSize(response, "list-of-strings"));
797 assertEquals(8, getElementsSize(response, "inner-running-data-additional",
798 "urn:opendaylight:params:xml:ns:yang:controller:test:impl"));
799 assertEquals(8, getElementsSize(response, "deep4"));
802 final RuntimeRpc netconf = new RuntimeRpc(configSubsystemFacade, NETCONF_SESSION_ID);
804 response = executeOp(netconf, "netconfMessages/rpc.xml");
805 assertContainsElementWithText(response, "testarg1");
807 response = executeOp(netconf, "netconfMessages/rpcInner.xml");
808 final Document expectedReplyOk = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
809 XMLUnit.setIgnoreWhitespace(true);
810 XMLAssert.assertXMLEqual(expectedReplyOk, response);
812 response = executeOp(netconf, "netconfMessages/rpcInnerInner.xml");
813 assertContainsElementWithText(response, "true");
815 response = executeOp(netconf, "netconfMessages/rpcInnerInner_complex_output.xml");
816 assertContainsElementWithText(response, "1");
817 assertContainsElementWithText(response, "2");
820 private Document get() throws ParserConfigurationException, SAXException, IOException, DocumentedException {
821 final Get getOp = new Get(configSubsystemFacade, NETCONF_SESSION_ID);
822 return executeOp(getOp, "netconfMessages/get.xml");
825 private static int getElementsSize(final Document response, final String elementName) {
826 return response.getElementsByTagName(elementName).getLength();
829 private static int getElementsSize(final Document response, final String elementName, final String namespace) {
830 return response.getElementsByTagNameNS(namespace, elementName).getLength();
833 private static Document executeOp(final NetconfOperation op,
834 final String filename) throws ParserConfigurationException,
835 SAXException, IOException, DocumentedException {
837 final Document request = XmlFileLoader.xmlFileToDocument(filename);
839 LOG.debug("Executing netconf operation\n{}", XmlUtil.toString(request));
840 final HandlingPriority priority = op.canHandle(request);
842 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
844 final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
845 LOG.debug("Got response\n{}", XmlUtil.toString(response));
849 private static SchemaContext getSchemaContext() {
850 return YangParserTestUtils.parseYangResources(NetconfMappingTest.class,
851 "/META-INF/yang/config@2013-04-05.yang", "/META-INF/yang/rpc-context@2013-06-17.yang",
852 "/META-INF/yang/config-test@2013-06-13.yang", "/META-INF/yang/config-test-impl@2013-04-03.yang",
853 "/META-INF/yang/test-types@2013-11-27.yang", "/META-INF/yang/test-groups@2014-12-08.yang",
854 "/META-INF/yang/ietf-inet-types@2013-07-15.yang");
857 private void setModule(final NetconfTestImplModuleMXBean mxBean, final ConfigTransactionJMXClient transaction,
858 final String depName)
859 throws InstanceAlreadyExistsException, InstanceNotFoundException {
860 mxBean.setSimpleInt((long) 44);
861 mxBean.setBinaryLeaf(new byte[]{8, 7, 9});
862 final DtoD dtob = getDtoD();
863 mxBean.setDtoD(dtob);
865 final DtoC dtoa = getDtoC();
866 mxBean.setDtoC(dtoa);
867 mxBean.setSimpleBoolean(false);
869 final Peers p1 = new Peers();
872 p1.setSimpleInt3(456);
873 final Peers p2 = new Peers();
875 p2.setPort("port23");
876 p2.setSimpleInt3(456);
877 mxBean.setPeers(Lists.<Peers>newArrayList(p1, p2));
879 mxBean.setSimpleLong(454545L);
880 mxBean.setSimpleLong2(44L);
881 mxBean.setSimpleBigInteger(BigInteger.valueOf(999L));
882 mxBean.setSimpleByte(new Byte((byte) 4));
883 mxBean.setSimpleShort(new Short((short) 4));
884 mxBean.setSimpleTest(545);
886 mxBean.setComplexList(Lists.<ComplexList>newArrayList());
887 mxBean.setSimpleList(Lists.<Integer>newArrayList());
889 final ObjectName testingDepOn = transaction.createModule(this.factory2.getImplementationName(), depName);
891 for (final Class<? extends AbstractServiceInterface> serviceInterface :
892 factory2.getImplementedServiceIntefaces()) {
893 final ServiceInterfaceAnnotation annotation =
894 serviceInterface.getAnnotation(ServiceInterfaceAnnotation.class);
895 transaction.saveServiceReference(
896 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()),
897 "ref_from_code_to_" + depName + "_" + index++, testingDepOn);
900 mxBean.setTestingDep(testingDepOn);
903 private static DtoD getDtoD() {
904 final DtoD dtob = new DtoD();
905 dtob.setSimpleInt1((long) 444);
906 dtob.setSimpleInt2((long) 4444);
907 dtob.setSimpleInt3(454);
908 final ComplexDtoBInner dtobInner = new ComplexDtoBInner();
909 final Deep deep = new Deep();
910 deep.setSimpleInt3(4);
911 dtobInner.setDeep(deep);
912 dtobInner.setSimpleInt3(44);
913 dtobInner.setSimpleList(Lists.newArrayList(4));
914 dtob.setComplexDtoBInner(Lists.newArrayList(dtobInner));
915 dtob.setSimpleList(Lists.newArrayList(4));
919 private static DtoC getDtoC() {
920 final DtoC dtoa = new DtoC();
921 // dtoa.setSimpleArg((long) 55);
922 final DtoAInner dtoAInner = new DtoAInner();
923 final DtoAInnerInner dtoAInnerInner = new DtoAInnerInner();
924 dtoAInnerInner.setSimpleArg(456L);
925 dtoAInner.setDtoAInnerInner(dtoAInnerInner);
926 dtoAInner.setSimpleArg(44L);
927 dtoa.setDtoAInner(dtoAInner);