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.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
98 import org.opendaylight.netconf.confignetconfconnector.operations.Commit;
99 import org.opendaylight.netconf.confignetconfconnector.operations.DiscardChanges;
100 import org.opendaylight.netconf.confignetconfconnector.operations.Lock;
101 import org.opendaylight.netconf.confignetconfconnector.operations.UnLock;
102 import org.opendaylight.netconf.confignetconfconnector.operations.editconfig.EditConfig;
103 import org.opendaylight.netconf.confignetconfconnector.operations.get.Get;
104 import org.opendaylight.netconf.confignetconfconnector.operations.getconfig.GetConfig;
105 import org.opendaylight.netconf.confignetconfconnector.operations.runtimerpc.RuntimeRpc;
106 import org.opendaylight.netconf.impl.NetconfServerSession;
107 import org.opendaylight.netconf.impl.NetconfServerSessionListener;
108 import org.opendaylight.netconf.impl.mapping.operations.DefaultCloseSession;
109 import org.opendaylight.netconf.impl.osgi.AggregatedNetconfOperationServiceFactory;
110 import org.opendaylight.netconf.impl.osgi.NetconfOperationRouter;
111 import org.opendaylight.netconf.mapping.api.HandlingPriority;
112 import org.opendaylight.netconf.mapping.api.NetconfOperation;
113 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
114 import org.opendaylight.netconf.util.messages.NetconfMessageUtil;
115 import org.opendaylight.netconf.util.test.XmlFileLoader;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity1;
117 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity2;
118 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
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.parser.stmt.reactor.CrossSourceStatementReactor;
124 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
125 import org.osgi.framework.Filter;
126 import org.osgi.framework.ServiceListener;
127 import org.osgi.framework.ServiceReference;
128 import org.slf4j.Logger;
129 import org.slf4j.LoggerFactory;
130 import org.w3c.dom.Document;
131 import org.w3c.dom.Element;
132 import org.w3c.dom.Node;
133 import org.w3c.dom.NodeList;
134 import org.w3c.dom.Text;
135 import org.w3c.dom.traversal.DocumentTraversal;
136 import org.xml.sax.SAXException;
139 public class NetconfMappingTest extends AbstractConfigTest {
140 private static final Logger LOG = LoggerFactory.getLogger(NetconfMappingTest.class);
142 private static final String INSTANCE_NAME = "instance-from-code";
143 private static final String NETCONF_SESSION_ID = "foo";
144 private static final String TEST_NAMESPACE= "urn:opendaylight:params:xml:ns:yang:controller:test:impl";
145 private NetconfTestImplModuleFactory factory;
146 private DepTestImplModuleFactory factory2;
147 private IdentityTestModuleFactory factory3;
148 private TestImplModuleFactory factory4;
151 YangStoreService yangStoreSnapshot;
153 NetconfOperationRouter netconfOperationRouter;
155 AggregatedNetconfOperationServiceFactory netconfOperationServiceSnapshot;
157 private AutoCloseable sessionCloseable;
159 private TransactionProvider transactionProvider;
161 private ConfigSubsystemFacade configSubsystemFacade;
164 public void setUp() throws Exception {
165 MockitoAnnotations.initMocks(this);
168 final Filter filter = mock(Filter.class);
169 doReturn(filter).when(mockedContext).createFilter(anyString());
170 doNothing().when(mockedContext).addServiceListener(any(ServiceListener.class), anyString());
171 doReturn(new ServiceReference<?>[]{}).when(mockedContext).getServiceReferences(anyString(), anyString());
173 doReturn(yangStoreSnapshot).when(yangStoreSnapshot).getCurrentSnapshot();
174 doReturn(getMbes()).when(this.yangStoreSnapshot).getModuleMXBeanEntryMap();
175 doReturn(getModules()).when(this.yangStoreSnapshot).getModules();
176 doReturn(new EnumResolver() {
178 public String fromYang(final String enumType, final String enumYangValue) {
179 return Preconditions.checkNotNull(getEnumMapping().get(enumYangValue),
180 "Unable to resolve enum value %s, for enum %s with mappings %s", 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", enumYangValue, enumType, getEnumMapping().inverse());
188 }).when(this.yangStoreSnapshot).getEnumResolver();
190 this.factory = new NetconfTestImplModuleFactory();
191 this.factory2 = new DepTestImplModuleFactory();
192 this.factory3 = new IdentityTestModuleFactory();
193 factory4 = new TestImplModuleFactory();
194 doNothing().when(sessionCloseable).close();
196 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, this.factory, this.factory2,
197 this.factory3, factory4));
199 transactionProvider = new TransactionProvider(this.configRegistryClient, NETCONF_SESSION_ID);
201 configSubsystemFacade = new ConfigSubsystemFacade(configRegistryClient, configRegistryClient, yangStoreSnapshot, "mapping-test");
204 private ObjectName createModule(final String instanceName) throws InstanceAlreadyExistsException, InstanceNotFoundException, URISyntaxException, ValidationException, ConflictingVersionException {
205 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
207 final ObjectName on = transaction.createModule(this.factory.getImplementationName(), instanceName);
208 final NetconfTestImplModuleMXBean mxBean = transaction.newMXBeanProxy(on, NetconfTestImplModuleMXBean.class);
209 setModule(mxBean, transaction, instanceName + "_dep");
212 for (Class<? extends AbstractServiceInterface> sInterface : factory.getImplementedServiceIntefaces()) {
213 ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
214 transaction.saveServiceReference(
215 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + instanceName + "_" + i++,
219 transaction.commit();
224 public void testIdentityRefs() throws Exception {
225 edit("netconfMessages/editConfig_identities.xml");
228 Document configRunning = getConfigRunning();
229 String asString = XmlUtil.toString(configRunning);
230 assertThat(asString, containsString("test-identity2"));
231 assertThat(asString, containsString("test-identity1"));
232 assertEquals(2, countSubstringOccurence(asString, "</identities>"));
234 edit("netconfMessages/editConfig_identities_inner_replace.xml");
236 configRunning = getConfigRunning();
237 asString = XmlUtil.toString(configRunning);
238 // test-identity1 was removed by replace
239 assertThat(asString, not(containsString("test-identity2")));
240 // now only 1 identities entry is present
241 assertEquals(1, countSubstringOccurence(asString, "</identities>"));
244 private static int countSubstringOccurence(final String string, final String substring) {
245 final Matcher matches = Pattern.compile(substring).matcher(string);
247 while (matches.find()) {
254 protected BindingRuntimeContext getBindingRuntimeContext() {
255 final BindingRuntimeContext ret = super.getBindingRuntimeContext();
256 doReturn(TestIdentity1.class).when(ret).getIdentityClass(TestIdentity1.QNAME);
257 doReturn(TestIdentity2.class).when(ret).getIdentityClass(TestIdentity2.QNAME);
258 doReturn(parseYangStreams(getYangs())).when(ret).getSchemaContext();
263 public void testServicePersistance() throws Exception {
264 createModule(INSTANCE_NAME);
266 edit("netconfMessages/editConfig.xml");
267 Document config = getConfigCandidate();
268 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
269 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
270 "ref_from_code_to_instance-from-code_1"));
273 edit("netconfMessages/editConfig_addServiceName.xml");
274 config = getConfigCandidate();
275 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
276 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
277 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
279 edit("netconfMessages/editConfig_addServiceNameOnTest.xml");
280 config = getConfigCandidate();
281 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
282 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
283 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
286 config = getConfigRunning();
287 assertCorrectRefNamesForDependencies(config);
288 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
289 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
290 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
292 edit("netconfMessages/editConfig_removeServiceNameOnTest.xml");
293 config = getConfigCandidate();
294 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
295 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
296 "ref_from_code_to_instance-from-code_1"));
299 edit("netconfMessages/editConfig_removeServiceNameOnTest.xml");
300 fail("Should've failed, non-existing service instance");
301 } catch (DocumentedException e) {
302 assertEquals(e.getErrorSeverity(), DocumentedException.ErrorSeverity.error);
303 assertEquals(e.getErrorTag(), DocumentedException.ErrorTag.operation_failed);
304 assertEquals(e.getErrorType(), DocumentedException.ErrorType.application);
307 edit("netconfMessages/editConfig_replace_default.xml");
308 config = getConfigCandidate();
309 assertCorrectServiceNames(config, Collections.<String>emptySet());
311 edit("netconfMessages/editConfig_remove.xml");
312 config = getConfigCandidate();
313 assertCorrectServiceNames(config, Collections.<String>emptySet());
316 config = getConfigCandidate();
317 assertCorrectServiceNames(config, Collections.<String>emptySet());
322 public void testUnLock() throws Exception {
323 assertTrue(NetconfMessageUtil.isOKMessage(lockCandidate()));
324 assertTrue(NetconfMessageUtil.isOKMessage(unlockCandidate()));
327 private static void assertCorrectRefNamesForDependencies(final Document config) throws NodeTestException {
328 NodeList modulesList = config.getElementsByTagName("modules");
329 assertEquals(1, modulesList.getLength());
331 NodeTest nt = new NodeTest((DocumentTraversal) config, modulesList.item(0));
332 NodeTester tester = new AbstractNodeTester() {
333 private int defaultRefNameCount = 0;
334 private int userRefNameCount = 0;
337 public void testText(final Text text) throws NodeTestException {
338 if(text.getData().equals("ref_dep2")) {
339 defaultRefNameCount++;
340 } else if(text.getData().equals("ref_dep_user_two")) {
346 public void noMoreNodes(final NodeTest forTest) throws NodeTestException {
347 assertEquals(0, defaultRefNameCount);
348 assertEquals(2, userRefNameCount);
351 nt.performTest(tester, Node.TEXT_NODE);
354 private static void assertCorrectServiceNames(final Document configCandidate, final Set<String> refNames) throws NodeTestException {
355 final Set<String> refNames2 = new HashSet<>(refNames);
356 NodeList servicesNodes = configCandidate.getElementsByTagName("services");
357 assertEquals(1, servicesNodes.getLength());
359 NodeTest nt = new NodeTest((DocumentTraversal) configCandidate, servicesNodes.item(0));
360 NodeTester tester = new AbstractNodeTester() {
363 public void testElement(final Element element) throws NodeTestException {
364 if(element.getNodeName() != null) {
365 if(element.getNodeName().equals("name")) {
366 String elmText = element.getTextContent();
367 if(refNames2.contains(elmText)) {
368 refNames2.remove(elmText);
370 throw new NodeTestException("Unexpected services defined: " + elmText);
377 public void noMoreNodes(final NodeTest forTest) throws NodeTestException {
378 assertEquals(Collections.<String>emptySet(), refNames2);
379 assertTrue(refNames2.toString(), refNames2.isEmpty());
382 nt.performTest(tester, Node.ELEMENT_NODE);
386 public void testConfigNetconfUnionTypes() throws Exception {
388 createModule(INSTANCE_NAME);
390 edit("netconfMessages/editConfig.xml");
392 Document response = getConfigRunning();
393 Element ipElement = readXmlToElement("<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">0:0:0:0:0:0:0:1</ip>");
394 assertContainsElement(response, readXmlToElement("<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">0:0:0:0:0:0:0:1</ip>"));
396 assertContainsElement(response, readXmlToElement("<union-test-attr xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">456</union-test-attr>"));
399 edit("netconfMessages/editConfig_setUnions.xml");
401 response = getConfigRunning();
402 assertContainsElement(response, readXmlToElement("<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">127.1.2.3</ip>"));
403 assertContainsElement(response, readXmlToElement("<union-test-attr xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">randomStringForUnion</union-test-attr>"));
408 public void testConfigNetconf() throws Exception {
410 createModule(INSTANCE_NAME);
412 edit("netconfMessages/editConfig.xml");
413 Document configCandidate = getConfigCandidate();
414 checkBinaryLeafEdited(configCandidate);
417 // default-operation:none, should not affect binary leaf
418 edit("netconfMessages/editConfig_none.xml");
419 checkBinaryLeafEdited(getConfigCandidate());
423 Document response = getConfigRunning();
425 checkBinaryLeafEdited(response);
426 checkTypeConfigAttribute(response);
427 checkTypedefs(response);
428 checkTestingDeps(response);
430 checkBigDecimal(response);
432 edit("netconfMessages/editConfig_remove.xml");
435 assertXMLEqual(getConfigCandidate(), getConfigRunning());
437 final Document expectedResult = XmlFileLoader.xmlFileToDocument("netconfMessages/editConfig_expectedResult.xml");
438 XMLUnit.setIgnoreWhitespace(true);
439 assertXMLEqual(expectedResult, getConfigRunning());
440 assertXMLEqual(expectedResult, getConfigCandidate());
442 edit("netconfMessages/editConfig_none.xml");
444 verify(sessionCloseable).close();
445 verifyNoMoreInteractions(netconfOperationRouter);
446 verifyNoMoreInteractions(netconfOperationServiceSnapshot);
449 private static void checkBigDecimal(final Document response) throws NodeTestException, SAXException, IOException {
450 assertContainsElement(response, readXmlToElement("<sleep-factor xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.58</sleep-factor>"));
452 assertContainsElement(response, readXmlToElement("<sleep-factor xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.00</sleep-factor>"));
455 private void closeSession() throws ParserConfigurationException, SAXException,
456 IOException, DocumentedException {
457 final Channel channel = mock(Channel.class);
458 doReturn("channel").when(channel).toString();
459 final NetconfServerSessionListener listener = mock(NetconfServerSessionListener.class);
460 final NetconfServerSession session =
461 new NetconfServerSession(listener, channel, 1L,
462 NetconfHelloMessageAdditionalHeader.fromString("[netconf;10.12.0.102:48528;ssh;;;;;;]"));
463 DefaultCloseSession closeOp = new DefaultCloseSession(NETCONF_SESSION_ID, sessionCloseable);
464 closeOp.setNetconfSession(session);
465 executeOp(closeOp, "netconfMessages/closeSession.xml");
468 private void edit(final String resource) throws ParserConfigurationException, SAXException, IOException,
469 DocumentedException {
470 EditConfig editOp = new EditConfig(configSubsystemFacade, NETCONF_SESSION_ID);
471 executeOp(editOp, resource);
474 private void commit() throws ParserConfigurationException, SAXException, IOException, DocumentedException {
475 Commit commitOp = new Commit(configSubsystemFacade, NETCONF_SESSION_ID);
476 executeOp(commitOp, "netconfMessages/commit.xml");
479 private static Document lockCandidate() throws ParserConfigurationException, SAXException, IOException, DocumentedException {
480 Lock commitOp = new Lock(NETCONF_SESSION_ID);
481 return executeOp(commitOp, "netconfMessages/lock.xml");
484 private static Document unlockCandidate() throws ParserConfigurationException, SAXException, IOException, DocumentedException {
485 UnLock commitOp = new UnLock(NETCONF_SESSION_ID);
486 return executeOp(commitOp, "netconfMessages/unlock.xml");
489 private Document getConfigCandidate() throws ParserConfigurationException, SAXException, IOException,
490 DocumentedException {
491 GetConfig getConfigOp = new GetConfig(configSubsystemFacade, Optional.<String> absent(), NETCONF_SESSION_ID);
492 return executeOp(getConfigOp, "netconfMessages/getConfig_candidate.xml");
495 private Document getConfigRunning() throws ParserConfigurationException, SAXException, IOException,
496 DocumentedException {
497 GetConfig getConfigOp = new GetConfig(configSubsystemFacade, Optional.<String> absent(), NETCONF_SESSION_ID);
498 return executeOp(getConfigOp, "netconfMessages/getConfig.xml");
501 @Ignore("second edit message corrupted")
502 @Test(expected = DocumentedException.class)
503 public void testConfigNetconfReplaceDefaultEx() throws Exception {
505 createModule(INSTANCE_NAME);
507 edit("netconfMessages/editConfig.xml");
508 edit("netconfMessages/editConfig_replace_default_ex.xml");
512 public void testConfigNetconfReplaceDefault() throws Exception {
514 createModule(INSTANCE_NAME);
516 edit("netconfMessages/editConfig.xml");
518 Document response = getConfigRunning();
519 final int allInstances = response.getElementsByTagName("module").getLength();
521 edit("netconfMessages/editConfig_replace_default.xml");
524 response = getConfigRunning();
526 final int afterReplace = response.getElementsByTagName("module").getLength();
527 assertEquals(4, allInstances);
528 assertEquals(2, afterReplace);
532 public void testSameAttrDifferentNamespaces() throws Exception {
534 edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespaces.xml");
536 } catch (DocumentedException e) {
537 String message = e.getMessage();
538 assertContainsString(message, "Element simpleInt present multiple times with different namespaces");
539 assertContainsString(message, TEST_NAMESPACE);
540 assertContainsString(message, XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
545 public void testDifferentNamespaceInTO() throws Exception {
547 edit("netconfMessages/namespaces/editConfig_differentNamespaceTO.xml");
549 } catch (DocumentedException e) {
550 String message = e.getMessage();
551 assertContainsString(message, "Unrecognised elements");
552 assertContainsString(message, "simple-int2");
553 assertContainsString(message, "dto_d");
558 public void testSameAttrDifferentNamespacesList() throws Exception {
560 edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespacesList.xml");
562 } catch (DocumentedException e) {
563 String message = e.getMessage();
564 assertContainsString(message, "Element allow-user present multiple times with different namespaces");
565 assertContainsString(message, TEST_NAMESPACE);
566 assertContainsString(message, XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
571 public void testTypeNameConfigAttributeMatching() throws Exception {
572 edit("netconfMessages/editConfig.xml");
574 edit("netconfMessages/namespaces/editConfig_typeNameConfigAttributeMatching.xml");
577 Document response = getConfigRunning();
578 checkTypeConfigAttribute(response);
581 // TODO add <modules operation="replace"> functionality
582 @Test(expected = DocumentedException.class)
583 public void testConfigNetconfReplaceModuleEx() throws Exception {
585 createModule(INSTANCE_NAME);
587 edit("netconfMessages/editConfig.xml");
588 edit("netconfMessages/editConfig_replace_module_ex.xml");
592 public void testUnrecognisedConfigElements() throws Exception {
594 String format = "netconfMessages/unrecognised/editConfig_unrecognised%d.xml";
595 final int TESTS_COUNT = 8;
597 for (int i = 0; i < TESTS_COUNT; i++) {
598 String file = String.format(format, i + 1);
599 LOG.info("Reading {}", file);
602 } catch (DocumentedException e) {
603 assertContainsString(e.getMessage(), "Unrecognised elements");
604 assertContainsString(e.getMessage(), "unknownAttribute");
607 fail("Unrecognised test should throw exception " + file);
614 public void testConfigNetconfReplaceModule() throws Exception {
616 createModule(INSTANCE_NAME);
618 edit("netconfMessages/editConfig.xml");
620 Document response = getConfigRunning();
621 final int allInstances = response.getElementsByTagName("instance").getLength();
623 edit("netconfMessages/editConfig_replace_module.xml");
626 response = getConfigRunning();
627 final int afterReplace = response.getElementsByTagName("instance").getLength();
629 assertEquals(4 + 4 /* Instances from services */, allInstances);
630 assertEquals(3 + 3, afterReplace);
634 public void testEx2() throws Exception {
635 //check abort before tx creation
636 assertContainsElement(discard(), readXmlToElement("<ok xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
638 //check abort after tx creation
639 edit("netconfMessages/editConfig.xml");
640 assertContainsElement(discard(), readXmlToElement("<ok xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
644 public void testFailedDiscardChangesAbort() throws Exception {
645 final ConfigSubsystemFacade facade = mock(ConfigSubsystemFacade.class);
646 doThrow(new RuntimeException("Mocked runtime exception, Abort has to fail")).when(facade).abortConfiguration();
648 DiscardChanges discardOp = new DiscardChanges(facade, NETCONF_SESSION_ID);
651 executeOp(discardOp, "netconfMessages/discardChanges.xml");
652 fail("Should've failed, abort on mocked is supposed to throw RuntimeException");
653 } catch (DocumentedException e) {
654 assertTrue(e.getErrorTag() == DocumentedException.ErrorTag.operation_failed);
655 assertTrue(e.getErrorSeverity() == DocumentedException.ErrorSeverity.error);
656 assertTrue(e.getErrorType() == DocumentedException.ErrorType.application);
660 private Document discard() throws ParserConfigurationException, SAXException, IOException, DocumentedException {
661 DiscardChanges discardOp = new DiscardChanges(configSubsystemFacade, NETCONF_SESSION_ID);
662 return executeOp(discardOp, "netconfMessages/discardChanges.xml");
665 private static void checkBinaryLeafEdited(final Document response) throws NodeTestException, SAXException, IOException {
666 assertContainsElement(response, readXmlToElement("<binaryLeaf xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">YmluYXJ5</binaryLeaf>"));
667 assertContainsElement(response, readXmlToElement("<binaryLeaf xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ZGVmYXVsdEJpbg==</binaryLeaf>"));
670 private static void checkTypedefs(final Document response) throws NodeTestException, SAXException, IOException {
672 assertContainsElement(response, readXmlToElement("<extended xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">10</extended>"));
674 assertContainsElement(response, readXmlToElement("<extended xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">1</extended>"));
676 assertContainsElement(response, readXmlToElement("<extended-twice xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">20</extended-twice>"));
678 assertContainsElement(response, readXmlToElement("<extended-twice xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2</extended-twice>"));
680 assertContainsElement(response, readXmlToElement("<extended-enum xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">two</extended-enum>"));
682 assertContainsElement(response, readXmlToElement("<extended-enum xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">one</extended-enum>"));
685 private static void assertContainsString(final String string, final String substring) {
686 assertThat(string, containsString(substring));
689 private static void checkEnum(final Document response) throws Exception {
691 String expectedEnumContent = "two";
693 XMLAssert.assertXpathEvaluatesTo(expectedEnumContent,
694 getXpathForNetconfImplSubnode(INSTANCE_NAME, "extended-enum"),
698 private static void checkTestingDeps(final Document response) {
699 int testingDepsSize = response.getElementsByTagName("testing-deps").getLength();
700 assertEquals(2, testingDepsSize);
703 private static String getXpathForNetconfImplSubnode(final String instanceName, final String subnode) {
704 return "/urn:ietf:params:xml:ns:netconf:base:1.0:rpc-reply" +
705 "/urn:ietf:params:xml:ns:netconf:base:1.0:data" +
706 "/urn:opendaylight:params:xml:ns:yang:controller:config:modules" +
707 "/urn:opendaylight:params:xml:ns:yang:controller:config:module" +
708 "[urn:opendaylight:params:xml:ns:yang:controller:config:name='" + instanceName + "']" +
709 "/urn:opendaylight:params:xml:ns:yang:controller:test:impl:impl-netconf" +
710 "/urn:opendaylight:params:xml:ns:yang:controller:test:impl:" + subnode;
713 private static void checkTypeConfigAttribute(final Document response) throws Exception {
715 Map<String,String> namesToTypeValues = ImmutableMap.of("instance-from-code", "configAttributeType",
716 "test2", "default-string");
717 for (Entry<String, String> nameToExpectedValue : namesToTypeValues.entrySet()) {
718 XMLAssert.assertXpathEvaluatesTo(nameToExpectedValue.getValue(),
719 getXpathForNetconfImplSubnode(nameToExpectedValue.getKey(),"type"),
724 private Map<String, Map<String, ModuleMXBeanEntry>> getMbes() throws Exception {
725 final List<InputStream> yangDependencies = getYangs();
727 final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries = Maps.newHashMap();
729 final SchemaContext schemaContext = parseYangStreams(yangDependencies);
730 YangStoreService yangStoreService = new YangStoreService(new SchemaContextProvider() {
731 @Override public SchemaContext getSchemaContext() {
732 return schemaContext;
734 }, mock(SchemaSourceProvider.class));
735 final BindingRuntimeContext bindingRuntimeContext = mock(BindingRuntimeContext.class);
736 doReturn(schemaContext).when(bindingRuntimeContext).getSchemaContext();
737 doReturn(getEnumMapping()).when(bindingRuntimeContext).getEnumMapping(any(Class.class));
738 yangStoreService.refresh(bindingRuntimeContext);
739 mBeanEntries.putAll(yangStoreService.getModuleMXBeanEntryMap());
744 private static BiMap<String, String> getEnumMapping() {
745 final HashBiMap<String, String> enumBiMap = HashBiMap.create();
746 // Enum constants mapping from yang -> Java and back
747 enumBiMap.put("one", "One");
748 enumBiMap.put("two", "Two");
749 enumBiMap.put("version1", "Version1");
750 enumBiMap.put("version2", "Version2");
754 private Set<org.opendaylight.yangtools.yang.model.api.Module> getModules() throws Exception {
755 SchemaContext resolveSchemaContext = parseYangStreams(getYangs());
756 return resolveSchemaContext.getModules();
759 private static SchemaContext parseYangStreams(final List<InputStream> streams) {
761 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
763 final SchemaContext schemaContext;
765 schemaContext = reactor.buildEffective(streams);
766 } catch (ReactorException e) {
767 throw new RuntimeException("Unable to build schema context from " + streams, e);
769 return schemaContext;
773 public void testConfigNetconfRuntime() throws Exception {
775 createModule(INSTANCE_NAME);
777 edit("netconfMessages/editConfig.xml");
778 checkBinaryLeafEdited(getConfigCandidate());
782 Document response = get();
784 assertEquals(2/*With runtime beans*/ + 2 /*Without runtime beans*/, getElementsSize(response, "module"));
786 assertEquals(2, getElementsSize(response, "asdf"));
787 // data from running config
788 assertEquals(2, getElementsSize(response, "simple-short"));
790 assertEquals(8, getElementsSize(response, "inner-running-data"));
791 assertEquals(8, getElementsSize(response, "deep2"));
792 assertEquals(8 * 4, getElementsSize(response, "inner-inner-running-data"));
793 assertEquals(8 * 4, getElementsSize(response, "deep3"));
794 assertEquals(8 * 4 * 2, getElementsSize(response, "list-of-strings"));
795 assertEquals(8, getElementsSize(response, "inner-running-data-additional", "urn:opendaylight:params:xml:ns:yang:controller:test:impl"));
796 assertEquals(8, getElementsSize(response, "deep4"));
799 RuntimeRpc netconf = new RuntimeRpc(configSubsystemFacade, NETCONF_SESSION_ID);
801 response = executeOp(netconf, "netconfMessages/rpc.xml");
802 assertContainsElementWithText(response, "testarg1");
804 response = executeOp(netconf, "netconfMessages/rpcInner.xml");
805 Document expectedReplyOk = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
806 XMLUnit.setIgnoreWhitespace(true);
807 XMLAssert.assertXMLEqual(expectedReplyOk, response);
809 response = executeOp(netconf, "netconfMessages/rpcInnerInner.xml");
810 assertContainsElementWithText(response, "true");
812 response = executeOp(netconf, "netconfMessages/rpcInnerInner_complex_output.xml");
813 assertContainsElementWithText(response, "1");
814 assertContainsElementWithText(response, "2");
817 private Document get() throws ParserConfigurationException, SAXException, IOException, DocumentedException {
818 Get getOp = new Get(configSubsystemFacade, NETCONF_SESSION_ID);
819 return executeOp(getOp, "netconfMessages/get.xml");
822 private static int getElementsSize(final Document response, final String elementName) {
823 return response.getElementsByTagName(elementName).getLength();
826 private static int getElementsSize(final Document response, final String elementName, final String namespace) {
827 return response.getElementsByTagNameNS(namespace, elementName).getLength();
830 private static Document executeOp(final NetconfOperation op, final String filename) throws ParserConfigurationException,
831 SAXException, IOException, DocumentedException {
833 final Document request = XmlFileLoader.xmlFileToDocument(filename);
835 LOG.debug("Executing netconf operation\n{}", XmlUtil.toString(request));
836 HandlingPriority priority = op.canHandle(request);
838 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
840 final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
841 LOG.debug("Got response\n{}", XmlUtil.toString(response));
845 private List<InputStream> getYangs() {
846 List<String> paths = Arrays.asList("/META-INF/yang/config.yang", "/META-INF/yang/rpc-context.yang",
847 "/META-INF/yang/config-test.yang", "/META-INF/yang/config-test-impl.yang", "/META-INF/yang/test-types.yang",
848 "/META-INF/yang/test-groups.yang", "/META-INF/yang/ietf-inet-types.yang");
849 final Collection<InputStream> yangDependencies = new ArrayList<>();
850 for (String path : paths) {
851 final InputStream is = Preconditions
852 .checkNotNull(getClass().getResourceAsStream(path), path + " not found");
853 yangDependencies.add(is);
855 return Lists.newArrayList(yangDependencies);
858 private void setModule(final NetconfTestImplModuleMXBean mxBean, final ConfigTransactionJMXClient transaction, 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 (Class<? extends AbstractServiceInterface> sInterface : factory2.getImplementedServiceIntefaces()) {
892 ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
893 transaction.saveServiceReference(
894 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + depName + "_" + i++,
898 mxBean.setTestingDep(testingDepOn);
901 private static DtoD getDtoD() {
902 final DtoD dtob = new DtoD();
903 dtob.setSimpleInt1((long) 444);
904 dtob.setSimpleInt2((long) 4444);
905 dtob.setSimpleInt3(454);
906 final ComplexDtoBInner dtobInner = new ComplexDtoBInner();
907 final Deep deep = new Deep();
908 deep.setSimpleInt3(4);
909 dtobInner.setDeep(deep);
910 dtobInner.setSimpleInt3(44);
911 dtobInner.setSimpleList(Lists.newArrayList(4));
912 dtob.setComplexDtoBInner(Lists.newArrayList(dtobInner));
913 dtob.setSimpleList(Lists.newArrayList(4));
917 private static DtoC getDtoC() {
918 final DtoC dtoa = new DtoC();
919 // dtoa.setSimpleArg((long) 55);
920 final DtoAInner dtoAInner = new DtoAInner();
921 final DtoAInnerInner dtoAInnerInner = new DtoAInnerInner();
922 dtoAInnerInner.setSimpleArg(456L);
923 dtoAInner.setDtoAInnerInner(dtoAInnerInner);
924 dtoAInner.setSimpleArg(44L);
925 dtoa.setDtoAInner(dtoAInner);