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.Maps;
37 import com.google.common.collect.Sets;
38 import io.netty.channel.Channel;
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.math.BigInteger;
42 import java.net.URISyntaxException;
43 import java.util.ArrayList;
44 import java.util.Arrays;
45 import java.util.Collection;
46 import java.util.Collections;
47 import java.util.HashSet;
48 import java.util.List;
50 import java.util.Map.Entry;
52 import java.util.regex.Matcher;
53 import java.util.regex.Pattern;
54 import javax.management.InstanceAlreadyExistsException;
55 import javax.management.InstanceNotFoundException;
56 import javax.management.ObjectName;
57 import javax.xml.parsers.ParserConfigurationException;
58 import org.custommonkey.xmlunit.AbstractNodeTester;
59 import org.custommonkey.xmlunit.NodeTest;
60 import org.custommonkey.xmlunit.NodeTestException;
61 import org.custommonkey.xmlunit.NodeTester;
62 import org.custommonkey.xmlunit.XMLAssert;
63 import org.custommonkey.xmlunit.XMLUnit;
64 import org.junit.Before;
65 import org.junit.Ignore;
66 import org.junit.Test;
67 import org.mockito.Mock;
68 import org.mockito.MockitoAnnotations;
69 import org.opendaylight.controller.config.api.ConflictingVersionException;
70 import org.opendaylight.controller.config.api.ValidationException;
71 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
72 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
73 import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacade;
74 import org.opendaylight.controller.config.facade.xml.osgi.EnumResolver;
75 import org.opendaylight.controller.config.facade.xml.osgi.YangStoreService;
76 import org.opendaylight.controller.config.facade.xml.transactions.TransactionProvider;
77 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
78 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
79 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
80 import org.opendaylight.controller.config.util.xml.DocumentedException;
81 import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
82 import org.opendaylight.controller.config.util.xml.XmlUtil;
83 import org.opendaylight.controller.config.yang.test.impl.ComplexDtoBInner;
84 import org.opendaylight.controller.config.yang.test.impl.ComplexList;
85 import org.opendaylight.controller.config.yang.test.impl.Deep;
86 import org.opendaylight.controller.config.yang.test.impl.DepTestImplModuleFactory;
87 import org.opendaylight.controller.config.yang.test.impl.DtoAInner;
88 import org.opendaylight.controller.config.yang.test.impl.DtoAInnerInner;
89 import org.opendaylight.controller.config.yang.test.impl.DtoC;
90 import org.opendaylight.controller.config.yang.test.impl.DtoD;
91 import org.opendaylight.controller.config.yang.test.impl.IdentityTestModuleFactory;
92 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleFactory;
93 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleMXBean;
94 import org.opendaylight.controller.config.yang.test.impl.Peers;
95 import org.opendaylight.controller.config.yang.test.impl.TestImplModuleFactory;
96 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
97 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
98 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
99 import org.opendaylight.netconf.confignetconfconnector.operations.Commit;
100 import org.opendaylight.netconf.confignetconfconnector.operations.DiscardChanges;
101 import org.opendaylight.netconf.confignetconfconnector.operations.Lock;
102 import org.opendaylight.netconf.confignetconfconnector.operations.UnLock;
103 import org.opendaylight.netconf.confignetconfconnector.operations.editconfig.EditConfig;
104 import org.opendaylight.netconf.confignetconfconnector.operations.get.Get;
105 import org.opendaylight.netconf.confignetconfconnector.operations.getconfig.GetConfig;
106 import org.opendaylight.netconf.confignetconfconnector.operations.runtimerpc.RuntimeRpc;
107 import org.opendaylight.netconf.impl.NetconfServerSession;
108 import org.opendaylight.netconf.impl.NetconfServerSessionListener;
109 import org.opendaylight.netconf.impl.mapping.operations.DefaultCloseSession;
110 import org.opendaylight.netconf.impl.osgi.AggregatedNetconfOperationServiceFactory;
111 import org.opendaylight.netconf.impl.osgi.NetconfOperationRouter;
112 import org.opendaylight.netconf.mapping.api.HandlingPriority;
113 import org.opendaylight.netconf.mapping.api.NetconfOperation;
114 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
115 import org.opendaylight.netconf.util.messages.NetconfMessageUtil;
116 import org.opendaylight.netconf.util.test.XmlFileLoader;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity1;
118 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity2;
119 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
120 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
121 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
122 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
123 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
124 import org.osgi.framework.Filter;
125 import org.osgi.framework.ServiceListener;
126 import org.osgi.framework.ServiceReference;
127 import org.slf4j.Logger;
128 import org.slf4j.LoggerFactory;
129 import org.w3c.dom.Document;
130 import org.w3c.dom.Element;
131 import org.w3c.dom.Node;
132 import org.w3c.dom.NodeList;
133 import org.w3c.dom.Text;
134 import org.w3c.dom.traversal.DocumentTraversal;
135 import org.xml.sax.SAXException;
138 public class NetconfMappingTest extends AbstractConfigTest {
139 private static final Logger LOG = LoggerFactory.getLogger(NetconfMappingTest.class);
141 private static final String INSTANCE_NAME = "instance-from-code";
142 private static final String NETCONF_SESSION_ID = "foo";
143 private static final String TEST_NAMESPACE = "urn:opendaylight:params:xml:ns:yang:controller:test:impl";
144 private NetconfTestImplModuleFactory factory;
145 private DepTestImplModuleFactory factory2;
146 private IdentityTestModuleFactory factory3;
147 private TestImplModuleFactory factory4;
150 YangStoreService yangStoreSnapshot;
152 NetconfOperationRouter netconfOperationRouter;
154 AggregatedNetconfOperationServiceFactory netconfOperationServiceSnapshot;
156 private AutoCloseable sessionCloseable;
158 private TransactionProvider transactionProvider;
160 private ConfigSubsystemFacade configSubsystemFacade;
163 public void setUp() throws Exception {
164 MockitoAnnotations.initMocks(this);
167 final Filter filter = mock(Filter.class);
168 doReturn(filter).when(mockedContext).createFilter(anyString());
169 doNothing().when(mockedContext).addServiceListener(any(ServiceListener.class), anyString());
170 doReturn(new ServiceReference<?>[]{}).when(mockedContext).getServiceReferences(anyString(), anyString());
172 doReturn(yangStoreSnapshot).when(yangStoreSnapshot).getCurrentSnapshot();
173 doReturn(getMbes()).when(this.yangStoreSnapshot).getModuleMXBeanEntryMap();
174 doReturn(getModules()).when(this.yangStoreSnapshot).getModules();
175 doReturn(new EnumResolver() {
177 public String fromYang(final String enumType, final String enumYangValue) {
178 return Preconditions.checkNotNull(getEnumMapping().get(enumYangValue),
179 "Unable to resolve enum value %s, for enum %s with mappings %s",
180 enumYangValue, enumType, getEnumMapping());
184 public String toYang(final String enumType, final String enumYangValue) {
185 return Preconditions.checkNotNull(getEnumMapping().inverse().get(enumYangValue),
186 "Unable to resolve enum value %s, for enum %s with mappings %s",
187 enumYangValue, enumType, getEnumMapping().inverse());
189 }).when(this.yangStoreSnapshot).getEnumResolver();
191 this.factory = new NetconfTestImplModuleFactory();
192 this.factory2 = new DepTestImplModuleFactory();
193 this.factory3 = new IdentityTestModuleFactory();
194 factory4 = new TestImplModuleFactory();
195 doNothing().when(sessionCloseable).close();
197 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, this.factory,
198 this.factory2, this.factory3, factory4));
200 transactionProvider = new TransactionProvider(this.configRegistryClient, NETCONF_SESSION_ID);
202 configSubsystemFacade = new ConfigSubsystemFacade(configRegistryClient, configRegistryClient, yangStoreSnapshot,
206 private ObjectName createModule(final String instanceName) throws InstanceAlreadyExistsException,
207 InstanceNotFoundException, URISyntaxException, ValidationException, ConflictingVersionException {
208 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
210 final ObjectName on = transaction.createModule(this.factory.getImplementationName(), instanceName);
211 final NetconfTestImplModuleMXBean mxBean = transaction.newMXBeanProxy(on, NetconfTestImplModuleMXBean.class);
212 setModule(mxBean, transaction, instanceName + "_dep");
215 for (final Class<? extends AbstractServiceInterface> serviceInterface :
216 factory.getImplementedServiceIntefaces()) {
217 final ServiceInterfaceAnnotation annotation =
218 serviceInterface.getAnnotation(ServiceInterfaceAnnotation.class);
219 transaction.saveServiceReference(
220 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()),
221 "ref_from_code_to_" + instanceName + "_" + index++, on);
224 transaction.commit();
229 public void testIdentityRefs() throws Exception {
230 edit("netconfMessages/editConfig_identities.xml");
233 Document configRunning = getConfigRunning();
234 String asString = XmlUtil.toString(configRunning);
235 assertThat(asString, containsString("test-identity2"));
236 assertThat(asString, containsString("test-identity1"));
237 assertEquals(2, countSubstringOccurence(asString, "</identities>"));
239 edit("netconfMessages/editConfig_identities_inner_replace.xml");
241 configRunning = getConfigRunning();
242 asString = XmlUtil.toString(configRunning);
243 // test-identity1 was removed by replace
244 assertThat(asString, not(containsString("test-identity2")));
245 // now only 1 identities entry is present
246 assertEquals(1, countSubstringOccurence(asString, "</identities>"));
249 private static int countSubstringOccurence(final String string, final String substring) {
250 final Matcher matches = Pattern.compile(substring).matcher(string);
252 while (matches.find()) {
259 protected BindingRuntimeContext getBindingRuntimeContext() {
260 final BindingRuntimeContext ret = super.getBindingRuntimeContext();
261 doReturn(TestIdentity1.class).when(ret).getIdentityClass(TestIdentity1.QNAME);
262 doReturn(TestIdentity2.class).when(ret).getIdentityClass(TestIdentity2.QNAME);
263 final List<InputStream> streams = getYangs();
265 doReturn(YangParserTestUtils.parseYangStreams(streams)).when(ret).getSchemaContext();
266 } catch (final ReactorException e) {
267 throw new RuntimeException("Unable to build schema context from " + streams, e);
273 public void testServicePersistance() throws Exception {
274 createModule(INSTANCE_NAME);
276 edit("netconfMessages/editConfig.xml");
277 Document config = getConfigCandidate();
278 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
279 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
280 "ref_from_code_to_instance-from-code_1"));
283 edit("netconfMessages/editConfig_addServiceName.xml");
284 config = getConfigCandidate();
285 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
286 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
287 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
289 edit("netconfMessages/editConfig_addServiceNameOnTest.xml");
290 config = getConfigCandidate();
291 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
292 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
293 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
296 config = getConfigRunning();
297 assertCorrectRefNamesForDependencies(config);
298 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
299 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
300 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
302 edit("netconfMessages/editConfig_removeServiceNameOnTest.xml");
303 config = getConfigCandidate();
304 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
305 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
306 "ref_from_code_to_instance-from-code_1"));
309 edit("netconfMessages/editConfig_removeServiceNameOnTest.xml");
310 fail("Should've failed, non-existing service instance");
311 } catch (final DocumentedException e) {
312 assertEquals(e.getErrorSeverity(), DocumentedException.ErrorSeverity.ERROR);
313 assertEquals(e.getErrorTag(), DocumentedException.ErrorTag.OPERATION_FAILED);
314 assertEquals(e.getErrorType(), DocumentedException.ErrorType.APPLICATION);
317 edit("netconfMessages/editConfig_replace_default.xml");
318 config = getConfigCandidate();
319 assertCorrectServiceNames(config, Collections.<String>emptySet());
321 edit("netconfMessages/editConfig_remove.xml");
322 config = getConfigCandidate();
323 assertCorrectServiceNames(config, Collections.<String>emptySet());
326 config = getConfigCandidate();
327 assertCorrectServiceNames(config, Collections.<String>emptySet());
332 public void testUnLock() throws Exception {
333 assertTrue(NetconfMessageUtil.isOKMessage(lockCandidate()));
334 assertTrue(NetconfMessageUtil.isOKMessage(unlockCandidate()));
337 private static void assertCorrectRefNamesForDependencies(final Document config) throws NodeTestException {
338 final NodeList modulesList = config.getElementsByTagName("modules");
339 assertEquals(1, modulesList.getLength());
341 final NodeTest nt = new NodeTest((DocumentTraversal) config, modulesList.item(0));
342 final NodeTester tester = new AbstractNodeTester() {
343 private int defaultRefNameCount = 0;
344 private int userRefNameCount = 0;
347 public void testText(final Text text) throws NodeTestException {
348 if (text.getData().equals("ref_dep2")) {
349 defaultRefNameCount++;
350 } else if (text.getData().equals("ref_dep_user_two")) {
356 public void noMoreNodes(final NodeTest forTest) throws NodeTestException {
357 assertEquals(0, defaultRefNameCount);
358 assertEquals(2, userRefNameCount);
361 nt.performTest(tester, Node.TEXT_NODE);
364 private static void assertCorrectServiceNames(final Document configCandidate,
365 final Set<String> refNames) throws NodeTestException {
366 final Set<String> refNames2 = new HashSet<>(refNames);
367 final NodeList servicesNodes = configCandidate.getElementsByTagName("services");
368 assertEquals(1, servicesNodes.getLength());
370 final NodeTest nt = new NodeTest((DocumentTraversal) configCandidate, servicesNodes.item(0));
371 final NodeTester tester = new AbstractNodeTester() {
374 public void testElement(final Element element) throws NodeTestException {
375 if (element.getNodeName() != null) {
376 if (element.getNodeName().equals("name")) {
377 final String elmText = element.getTextContent();
378 if (refNames2.contains(elmText)) {
379 refNames2.remove(elmText);
381 throw new NodeTestException("Unexpected services defined: " + elmText);
388 public void noMoreNodes(final NodeTest forTest) throws NodeTestException {
389 assertEquals(Collections.<String>emptySet(), refNames2);
390 assertTrue(refNames2.toString(), refNames2.isEmpty());
393 nt.performTest(tester, Node.ELEMENT_NODE);
397 public void testConfigNetconfUnionTypes() throws Exception {
399 createModule(INSTANCE_NAME);
401 edit("netconfMessages/editConfig.xml");
403 Document response = getConfigRunning();
404 final Element ipElement = readXmlToElement(
405 "<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">0:0:0:0:0:0:0:1</ip>");
406 assertContainsElement(response, readXmlToElement(
407 "<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">0:0:0:0:0:0:0:1</ip>"));
409 assertContainsElement(response, readXmlToElement("<union-test-attr xmlns="
410 + "\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">456</union-test-attr>"));
413 edit("netconfMessages/editConfig_setUnions.xml");
415 response = getConfigRunning();
416 assertContainsElement(response, readXmlToElement(
417 "<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">127.1.2.3</ip>"));
418 assertContainsElement(response, readXmlToElement("<union-test-attr xmlns="
419 + "\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">"
420 + "randomStringForUnion</union-test-attr>"));
425 public void testConfigNetconf() throws Exception {
427 createModule(INSTANCE_NAME);
429 edit("netconfMessages/editConfig.xml");
430 final Document configCandidate = getConfigCandidate();
431 checkBinaryLeafEdited(configCandidate);
434 // default-operation:none, should not affect binary leaf
435 edit("netconfMessages/editConfig_none.xml");
436 checkBinaryLeafEdited(getConfigCandidate());
440 final Document response = getConfigRunning();
442 checkBinaryLeafEdited(response);
443 checkTypeConfigAttribute(response);
444 checkTypedefs(response);
445 checkTestingDeps(response);
447 checkBigDecimal(response);
449 edit("netconfMessages/editConfig_remove.xml");
452 assertXMLEqual(getConfigCandidate(), getConfigRunning());
454 final Document expectedResult =
455 XmlFileLoader.xmlFileToDocument("netconfMessages/editConfig_expectedResult.xml");
456 XMLUnit.setIgnoreWhitespace(true);
457 assertXMLEqual(expectedResult, getConfigRunning());
458 assertXMLEqual(expectedResult, getConfigCandidate());
460 edit("netconfMessages/editConfig_none.xml");
462 verify(sessionCloseable).close();
463 verifyNoMoreInteractions(netconfOperationRouter);
464 verifyNoMoreInteractions(netconfOperationServiceSnapshot);
467 private static void checkBigDecimal(final Document response) throws NodeTestException, SAXException, IOException {
468 assertContainsElement(response, readXmlToElement("<sleep-factor xmlns="
469 + "\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.58</sleep-factor>"));
471 assertContainsElement(response, readXmlToElement("<sleep-factor xmlns="
472 + "\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.00</sleep-factor>"));
475 private void closeSession() throws ParserConfigurationException, SAXException,
476 IOException, DocumentedException {
477 final Channel channel = mock(Channel.class);
478 doReturn("channel").when(channel).toString();
479 final NetconfServerSessionListener listener = mock(NetconfServerSessionListener.class);
480 final NetconfServerSession session =
481 new NetconfServerSession(listener, channel, 1L,
482 NetconfHelloMessageAdditionalHeader.fromString("[netconf;10.12.0.102:48528;ssh;;;;;;]"));
483 final DefaultCloseSession closeOp = new DefaultCloseSession(NETCONF_SESSION_ID, sessionCloseable);
484 closeOp.setNetconfSession(session);
485 executeOp(closeOp, "netconfMessages/closeSession.xml");
488 private void edit(final String resource) throws ParserConfigurationException, SAXException, IOException,
489 DocumentedException {
490 final EditConfig editOp = new EditConfig(configSubsystemFacade, NETCONF_SESSION_ID);
491 executeOp(editOp, resource);
494 private void commit() throws ParserConfigurationException, SAXException, IOException, DocumentedException {
495 final Commit commitOp = new Commit(configSubsystemFacade, NETCONF_SESSION_ID);
496 executeOp(commitOp, "netconfMessages/commit.xml");
499 private static Document lockCandidate() throws ParserConfigurationException, SAXException, IOException,
500 DocumentedException {
501 final Lock commitOp = new Lock(NETCONF_SESSION_ID);
502 return executeOp(commitOp, "netconfMessages/lock.xml");
505 private static Document unlockCandidate() throws ParserConfigurationException, SAXException, IOException,
506 DocumentedException {
507 final UnLock commitOp = new UnLock(NETCONF_SESSION_ID);
508 return executeOp(commitOp, "netconfMessages/unlock.xml");
511 private Document getConfigCandidate() throws ParserConfigurationException, SAXException, IOException,
512 DocumentedException {
513 final GetConfig getConfigOp = new GetConfig(configSubsystemFacade, Optional.<String>absent(),
515 return executeOp(getConfigOp, "netconfMessages/getConfig_candidate.xml");
518 private Document getConfigRunning() throws ParserConfigurationException, SAXException, IOException,
519 DocumentedException {
520 final GetConfig getConfigOp = new GetConfig(configSubsystemFacade, Optional.<String>absent(),
522 return executeOp(getConfigOp, "netconfMessages/getConfig.xml");
525 @Ignore("second edit message corrupted")
526 @Test(expected = DocumentedException.class)
527 public void testConfigNetconfReplaceDefaultEx() throws Exception {
529 createModule(INSTANCE_NAME);
531 edit("netconfMessages/editConfig.xml");
532 edit("netconfMessages/editConfig_replace_default_ex.xml");
536 public void testConfigNetconfReplaceDefault() throws Exception {
538 createModule(INSTANCE_NAME);
540 edit("netconfMessages/editConfig.xml");
542 Document response = getConfigRunning();
543 final int allInstances = response.getElementsByTagName("module").getLength();
545 edit("netconfMessages/editConfig_replace_default.xml");
548 response = getConfigRunning();
550 final int afterReplace = response.getElementsByTagName("module").getLength();
551 assertEquals(4, allInstances);
552 assertEquals(2, afterReplace);
556 public void testSameAttrDifferentNamespaces() throws Exception {
558 edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespaces.xml");
560 } catch (final DocumentedException e) {
561 final String message = e.getMessage();
562 assertContainsString(message, "Element simpleInt present multiple times with different namespaces");
563 assertContainsString(message, TEST_NAMESPACE);
564 assertContainsString(message, XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
569 public void testDifferentNamespaceInTO() throws Exception {
571 edit("netconfMessages/namespaces/editConfig_differentNamespaceTO.xml");
573 } catch (final DocumentedException e) {
574 final String message = e.getMessage();
575 assertContainsString(message, "Unrecognised elements");
576 assertContainsString(message, "simple-int2");
577 assertContainsString(message, "dto_d");
582 public void testSameAttrDifferentNamespacesList() throws Exception {
584 edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespacesList.xml");
586 } catch (final DocumentedException e) {
587 final String message = e.getMessage();
588 assertContainsString(message, "Element allow-user present multiple times with different namespaces");
589 assertContainsString(message, TEST_NAMESPACE);
590 assertContainsString(message, XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
595 public void testTypeNameConfigAttributeMatching() throws Exception {
596 edit("netconfMessages/editConfig.xml");
598 edit("netconfMessages/namespaces/editConfig_typeNameConfigAttributeMatching.xml");
601 final Document response = getConfigRunning();
602 checkTypeConfigAttribute(response);
605 // TODO add <modules operation="replace"> functionality
606 @Test(expected = DocumentedException.class)
607 public void testConfigNetconfReplaceModuleEx() throws Exception {
609 createModule(INSTANCE_NAME);
611 edit("netconfMessages/editConfig.xml");
612 edit("netconfMessages/editConfig_replace_module_ex.xml");
616 public void testUnrecognisedConfigElements() throws Exception {
618 final String format = "netconfMessages/unrecognised/editConfig_unrecognised%d.xml";
619 final int testsCount = 8;
621 for (int i = 0; i < testsCount; i++) {
622 final String file = String.format(format, i + 1);
623 LOG.info("Reading {}", file);
626 } catch (final DocumentedException e) {
627 assertContainsString(e.getMessage(), "Unrecognised elements");
628 assertContainsString(e.getMessage(), "unknownAttribute");
631 fail("Unrecognised test should throw exception " + file);
638 public void testConfigNetconfReplaceModule() throws Exception {
640 createModule(INSTANCE_NAME);
642 edit("netconfMessages/editConfig.xml");
644 Document response = getConfigRunning();
645 final int allInstances = response.getElementsByTagName("instance").getLength();
647 edit("netconfMessages/editConfig_replace_module.xml");
650 response = getConfigRunning();
651 final int afterReplace = response.getElementsByTagName("instance").getLength();
653 assertEquals(4 + 4 /* Instances from services */, allInstances);
654 assertEquals(3 + 3, afterReplace);
658 public void testEx2() throws Exception {
659 //check abort before tx creation
660 assertContainsElement(discard(), readXmlToElement("<ok xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
662 //check abort after tx creation
663 edit("netconfMessages/editConfig.xml");
664 assertContainsElement(discard(), readXmlToElement("<ok xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
668 public void testFailedDiscardChangesAbort() throws Exception {
669 final ConfigSubsystemFacade facade = mock(ConfigSubsystemFacade.class);
670 doThrow(new RuntimeException("Mocked runtime exception, Abort has to fail")).when(facade).abortConfiguration();
672 final DiscardChanges discardOp = new DiscardChanges(facade, NETCONF_SESSION_ID);
675 executeOp(discardOp, "netconfMessages/discardChanges.xml");
676 fail("Should've failed, abort on mocked is supposed to throw RuntimeException");
677 } catch (final DocumentedException e) {
678 assertTrue(e.getErrorTag() == DocumentedException.ErrorTag.OPERATION_FAILED);
679 assertTrue(e.getErrorSeverity() == DocumentedException.ErrorSeverity.ERROR);
680 assertTrue(e.getErrorType() == DocumentedException.ErrorType.APPLICATION);
684 private Document discard() throws ParserConfigurationException, SAXException, IOException, DocumentedException {
685 final DiscardChanges discardOp = new DiscardChanges(configSubsystemFacade, NETCONF_SESSION_ID);
686 return executeOp(discardOp, "netconfMessages/discardChanges.xml");
689 private static void checkBinaryLeafEdited(final Document response)
690 throws NodeTestException, SAXException, IOException {
691 assertContainsElement(response, readXmlToElement("<binaryLeaf xmlns="
692 + "\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">YmluYXJ5</binaryLeaf>"));
693 assertContainsElement(response, readXmlToElement("<binaryLeaf xmlns="
694 + "\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ZGVmYXVsdEJpbg==</binaryLeaf>"));
697 private static void checkTypedefs(final Document response) throws NodeTestException, SAXException, IOException {
699 assertContainsElement(response, readXmlToElement(
700 "<extended xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">10</extended>"));
702 assertContainsElement(response, readXmlToElement(
703 "<extended xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">1</extended>"));
705 assertContainsElement(response, readXmlToElement("<extended-twice xmlns="
706 + "\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">20</extended-twice>"));
708 assertContainsElement(response, readXmlToElement("<extended-twice xmlns="
709 + "\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2</extended-twice>"));
711 assertContainsElement(response, readXmlToElement("<extended-enum xmlns="
712 + "\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">two</extended-enum>"));
714 assertContainsElement(response, readXmlToElement("<extended-enum xmlns="
715 + "\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">one</extended-enum>"));
718 private static void assertContainsString(final String string, final String substring) {
719 assertThat(string, containsString(substring));
722 private static void checkEnum(final Document response) throws Exception {
724 final String expectedEnumContent = "two";
726 XMLAssert.assertXpathEvaluatesTo(expectedEnumContent,
727 getXpathForNetconfImplSubnode(INSTANCE_NAME, "extended-enum"),
731 private static void checkTestingDeps(final Document response) {
732 final int testingDepsSize = response.getElementsByTagName("testing-deps").getLength();
733 assertEquals(2, testingDepsSize);
736 private static String getXpathForNetconfImplSubnode(final String instanceName, final String subnode) {
737 return "/urn:ietf:params:xml:ns:netconf:base:1.0:rpc-reply"
738 + "/urn:ietf:params:xml:ns:netconf:base:1.0:data"
739 + "/urn:opendaylight:params:xml:ns:yang:controller:config:modules"
740 + "/urn:opendaylight:params:xml:ns:yang:controller:config:module"
741 + "[urn:opendaylight:params:xml:ns:yang:controller:config:name='" + instanceName + "']"
742 + "/urn:opendaylight:params:xml:ns:yang:controller:test:impl:impl-netconf"
743 + "/urn:opendaylight:params:xml:ns:yang:controller:test:impl:" + subnode;
746 private static void checkTypeConfigAttribute(final Document response) throws Exception {
748 final Map<String, String> namesToTypeValues = ImmutableMap.of("instance-from-code", "configAttributeType",
749 "test2", "default-string");
750 for (final Entry<String, String> nameToExpectedValue : namesToTypeValues.entrySet()) {
751 XMLAssert.assertXpathEvaluatesTo(nameToExpectedValue.getValue(),
752 getXpathForNetconfImplSubnode(nameToExpectedValue.getKey(), "type"),
757 private Map<String, Map<String, ModuleMXBeanEntry>> getMbes() throws Exception {
758 final List<InputStream> yangDependencies = getYangs();
760 final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries = Maps.newHashMap();
762 final SchemaContext schemaContext = YangParserTestUtils.parseYangStreams(yangDependencies);
763 final YangStoreService yangStoreService = new YangStoreService(new SchemaContextProvider() {
765 public SchemaContext getSchemaContext() {
766 return schemaContext;
768 }, mock(SchemaSourceProvider.class));
769 final BindingRuntimeContext bindingRuntimeContext = mock(BindingRuntimeContext.class);
770 doReturn(schemaContext).when(bindingRuntimeContext).getSchemaContext();
771 doReturn(getEnumMapping()).when(bindingRuntimeContext).getEnumMapping(any(Class.class));
772 yangStoreService.refresh(bindingRuntimeContext);
773 mBeanEntries.putAll(yangStoreService.getModuleMXBeanEntryMap());
778 private static BiMap<String, String> getEnumMapping() {
779 final HashBiMap<String, String> enumBiMap = HashBiMap.create();
780 // Enum constants mapping from yang -> Java and back
781 enumBiMap.put("one", "One");
782 enumBiMap.put("two", "Two");
783 enumBiMap.put("version1", "Version1");
784 enumBiMap.put("version2", "Version2");
788 private Set<org.opendaylight.yangtools.yang.model.api.Module> getModules() throws Exception {
789 final SchemaContext resolveSchemaContext = YangParserTestUtils.parseYangStreams(getYangs());
790 return resolveSchemaContext.getModules();
794 public void testConfigNetconfRuntime() throws Exception {
796 createModule(INSTANCE_NAME);
798 edit("netconfMessages/editConfig.xml");
799 checkBinaryLeafEdited(getConfigCandidate());
803 Document response = get();
805 assertEquals(2/*With runtime beans*/ + 2 /*Without runtime beans*/, getElementsSize(response, "module"));
807 assertEquals(2, getElementsSize(response, "asdf"));
808 // data from running config
809 assertEquals(2, getElementsSize(response, "simple-short"));
811 assertEquals(8, getElementsSize(response, "inner-running-data"));
812 assertEquals(8, getElementsSize(response, "deep2"));
813 assertEquals(8 * 4, getElementsSize(response, "inner-inner-running-data"));
814 assertEquals(8 * 4, getElementsSize(response, "deep3"));
815 assertEquals(8 * 4 * 2, getElementsSize(response, "list-of-strings"));
816 assertEquals(8, getElementsSize(response, "inner-running-data-additional",
817 "urn:opendaylight:params:xml:ns:yang:controller:test:impl"));
818 assertEquals(8, getElementsSize(response, "deep4"));
821 final RuntimeRpc netconf = new RuntimeRpc(configSubsystemFacade, NETCONF_SESSION_ID);
823 response = executeOp(netconf, "netconfMessages/rpc.xml");
824 assertContainsElementWithText(response, "testarg1");
826 response = executeOp(netconf, "netconfMessages/rpcInner.xml");
827 final Document expectedReplyOk = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
828 XMLUnit.setIgnoreWhitespace(true);
829 XMLAssert.assertXMLEqual(expectedReplyOk, response);
831 response = executeOp(netconf, "netconfMessages/rpcInnerInner.xml");
832 assertContainsElementWithText(response, "true");
834 response = executeOp(netconf, "netconfMessages/rpcInnerInner_complex_output.xml");
835 assertContainsElementWithText(response, "1");
836 assertContainsElementWithText(response, "2");
839 private Document get() throws ParserConfigurationException, SAXException, IOException, DocumentedException {
840 final Get getOp = new Get(configSubsystemFacade, NETCONF_SESSION_ID);
841 return executeOp(getOp, "netconfMessages/get.xml");
844 private static int getElementsSize(final Document response, final String elementName) {
845 return response.getElementsByTagName(elementName).getLength();
848 private static int getElementsSize(final Document response, final String elementName, final String namespace) {
849 return response.getElementsByTagNameNS(namespace, elementName).getLength();
852 private static Document executeOp(final NetconfOperation op,
853 final String filename) throws ParserConfigurationException,
854 SAXException, IOException, DocumentedException {
856 final Document request = XmlFileLoader.xmlFileToDocument(filename);
858 LOG.debug("Executing netconf operation\n{}", XmlUtil.toString(request));
859 final HandlingPriority priority = op.canHandle(request);
861 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
863 final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
864 LOG.debug("Got response\n{}", XmlUtil.toString(response));
868 private List<InputStream> getYangs() {
869 final List<String> paths = Arrays.asList("/META-INF/yang/config@2013-04-05.yang",
870 "/META-INF/yang/rpc-context@2013-06-17.yang", "/META-INF/yang/config-test@2013-06-13.yang",
871 "/META-INF/yang/config-test-impl@2013-04-03.yang", "/META-INF/yang/test-types@2013-11-27.yang",
872 "/META-INF/yang/test-groups@2014-12-08.yang", "/META-INF/yang/ietf-inet-types@2013-07-15.yang");
873 final Collection<InputStream> yangDependencies = new ArrayList<>();
874 for (final String path : paths) {
875 final InputStream is = Preconditions
876 .checkNotNull(getClass().getResourceAsStream(path), path + " not found");
877 yangDependencies.add(is);
879 return Lists.newArrayList(yangDependencies);
882 private void setModule(final NetconfTestImplModuleMXBean mxBean, final ConfigTransactionJMXClient transaction,
883 final String depName)
884 throws InstanceAlreadyExistsException, InstanceNotFoundException {
885 mxBean.setSimpleInt((long) 44);
886 mxBean.setBinaryLeaf(new byte[]{8, 7, 9});
887 final DtoD dtob = getDtoD();
888 mxBean.setDtoD(dtob);
890 final DtoC dtoa = getDtoC();
891 mxBean.setDtoC(dtoa);
892 mxBean.setSimpleBoolean(false);
894 final Peers p1 = new Peers();
897 p1.setSimpleInt3(456);
898 final Peers p2 = new Peers();
900 p2.setPort("port23");
901 p2.setSimpleInt3(456);
902 mxBean.setPeers(Lists.<Peers>newArrayList(p1, p2));
904 mxBean.setSimpleLong(454545L);
905 mxBean.setSimpleLong2(44L);
906 mxBean.setSimpleBigInteger(BigInteger.valueOf(999L));
907 mxBean.setSimpleByte(new Byte((byte) 4));
908 mxBean.setSimpleShort(new Short((short) 4));
909 mxBean.setSimpleTest(545);
911 mxBean.setComplexList(Lists.<ComplexList>newArrayList());
912 mxBean.setSimpleList(Lists.<Integer>newArrayList());
914 final ObjectName testingDepOn = transaction.createModule(this.factory2.getImplementationName(), depName);
916 for (final Class<? extends AbstractServiceInterface> serviceInterface :
917 factory2.getImplementedServiceIntefaces()) {
918 final ServiceInterfaceAnnotation annotation =
919 serviceInterface.getAnnotation(ServiceInterfaceAnnotation.class);
920 transaction.saveServiceReference(
921 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()),
922 "ref_from_code_to_" + depName + "_" + index++, testingDepOn);
925 mxBean.setTestingDep(testingDepOn);
928 private static DtoD getDtoD() {
929 final DtoD dtob = new DtoD();
930 dtob.setSimpleInt1((long) 444);
931 dtob.setSimpleInt2((long) 4444);
932 dtob.setSimpleInt3(454);
933 final ComplexDtoBInner dtobInner = new ComplexDtoBInner();
934 final Deep deep = new Deep();
935 deep.setSimpleInt3(4);
936 dtobInner.setDeep(deep);
937 dtobInner.setSimpleInt3(44);
938 dtobInner.setSimpleList(Lists.newArrayList(4));
939 dtob.setComplexDtoBInner(Lists.newArrayList(dtobInner));
940 dtob.setSimpleList(Lists.newArrayList(4));
944 private static DtoC getDtoC() {
945 final DtoC dtoa = new DtoC();
946 // dtoa.setSimpleArg((long) 55);
947 final DtoAInner dtoAInner = new DtoAInner();
948 final DtoAInnerInner dtoAInnerInner = new DtoAInnerInner();
949 dtoAInnerInner.setSimpleArg(456L);
950 dtoAInner.setDtoAInnerInner(dtoAInnerInner);
951 dtoAInner.setSimpleArg(44L);
952 dtoa.setDtoAInner(dtoAInner);