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;
29 import com.google.common.base.Optional;
30 import com.google.common.base.Preconditions;
31 import com.google.common.collect.BiMap;
32 import com.google.common.collect.HashBiMap;
33 import com.google.common.collect.ImmutableMap;
34 import com.google.common.collect.Lists;
35 import com.google.common.collect.Maps;
36 import com.google.common.collect.Sets;
37 import io.netty.channel.Channel;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.math.BigInteger;
41 import java.net.URISyntaxException;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.Collection;
45 import java.util.Collections;
46 import java.util.HashSet;
47 import java.util.List;
49 import java.util.Map.Entry;
51 import java.util.regex.Matcher;
52 import java.util.regex.Pattern;
53 import javax.management.InstanceAlreadyExistsException;
54 import javax.management.InstanceNotFoundException;
55 import javax.management.ObjectName;
56 import javax.xml.parsers.ParserConfigurationException;
57 import org.custommonkey.xmlunit.AbstractNodeTester;
58 import org.custommonkey.xmlunit.NodeTest;
59 import org.custommonkey.xmlunit.NodeTestException;
60 import org.custommonkey.xmlunit.NodeTester;
61 import org.custommonkey.xmlunit.XMLAssert;
62 import org.custommonkey.xmlunit.XMLUnit;
63 import org.junit.Before;
64 import org.junit.Ignore;
65 import org.junit.Test;
66 import org.mockito.Mock;
67 import org.mockito.MockitoAnnotations;
68 import org.opendaylight.controller.config.api.ConflictingVersionException;
69 import org.opendaylight.controller.config.api.ValidationException;
70 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
71 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
72 import org.opendaylight.controller.config.facade.xml.ConfigSubsystemFacade;
73 import org.opendaylight.controller.config.facade.xml.osgi.EnumResolver;
74 import org.opendaylight.controller.config.facade.xml.osgi.YangStoreService;
75 import org.opendaylight.controller.config.facade.xml.transactions.TransactionProvider;
76 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
77 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
78 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
79 import org.opendaylight.controller.config.util.xml.DocumentedException;
80 import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
81 import org.opendaylight.controller.config.util.xml.XmlUtil;
82 import org.opendaylight.controller.config.yang.test.impl.ComplexDtoBInner;
83 import org.opendaylight.controller.config.yang.test.impl.ComplexList;
84 import org.opendaylight.controller.config.yang.test.impl.Deep;
85 import org.opendaylight.controller.config.yang.test.impl.DepTestImplModuleFactory;
86 import org.opendaylight.controller.config.yang.test.impl.DtoAInner;
87 import org.opendaylight.controller.config.yang.test.impl.DtoAInnerInner;
88 import org.opendaylight.controller.config.yang.test.impl.DtoC;
89 import org.opendaylight.controller.config.yang.test.impl.DtoD;
90 import org.opendaylight.controller.config.yang.test.impl.IdentityTestModuleFactory;
91 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleFactory;
92 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleMXBean;
93 import org.opendaylight.controller.config.yang.test.impl.Peers;
94 import org.opendaylight.controller.config.yang.test.impl.TestImplModuleFactory;
95 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
96 import org.opendaylight.netconf.confignetconfconnector.operations.Commit;
97 import org.opendaylight.netconf.confignetconfconnector.operations.DiscardChanges;
98 import org.opendaylight.netconf.confignetconfconnector.operations.Lock;
99 import org.opendaylight.netconf.confignetconfconnector.operations.UnLock;
100 import org.opendaylight.netconf.confignetconfconnector.operations.editconfig.EditConfig;
101 import org.opendaylight.netconf.confignetconfconnector.operations.get.Get;
102 import org.opendaylight.netconf.confignetconfconnector.operations.getconfig.GetConfig;
103 import org.opendaylight.netconf.confignetconfconnector.operations.runtimerpc.RuntimeRpc;
104 import org.opendaylight.netconf.impl.NetconfServerSession;
105 import org.opendaylight.netconf.impl.NetconfServerSessionListener;
106 import org.opendaylight.netconf.impl.mapping.operations.DefaultCloseSession;
107 import org.opendaylight.netconf.impl.osgi.AggregatedNetconfOperationServiceFactory;
108 import org.opendaylight.netconf.impl.osgi.NetconfOperationRouter;
109 import org.opendaylight.netconf.mapping.api.HandlingPriority;
110 import org.opendaylight.netconf.mapping.api.NetconfOperation;
111 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
112 import org.opendaylight.netconf.api.messages.NetconfHelloMessageAdditionalHeader;
113 import org.opendaylight.netconf.util.messages.NetconfMessageUtil;
114 import org.opendaylight.netconf.util.test.XmlFileLoader;
115 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity1;
116 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity2;
117 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
118 import org.opendaylight.yangtools.yang.model.api.Module;
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.impl.YangParserImpl;
123 import org.osgi.framework.Filter;
124 import org.osgi.framework.ServiceListener;
125 import org.osgi.framework.ServiceReference;
126 import org.slf4j.Logger;
127 import org.slf4j.LoggerFactory;
128 import org.w3c.dom.Document;
129 import org.w3c.dom.Element;
130 import org.w3c.dom.Node;
131 import org.w3c.dom.NodeList;
132 import org.w3c.dom.Text;
133 import org.w3c.dom.traversal.DocumentTraversal;
134 import org.xml.sax.SAXException;
137 public class NetconfMappingTest extends AbstractConfigTest {
138 private static final Logger LOG = LoggerFactory.getLogger(NetconfMappingTest.class);
140 private static final String INSTANCE_NAME = "instance-from-code";
141 private static final String NETCONF_SESSION_ID = "foo";
142 private static final String TEST_NAMESPACE= "urn:opendaylight:params:xml:ns:yang:controller:test:impl";
143 private NetconfTestImplModuleFactory factory;
144 private DepTestImplModuleFactory factory2;
145 private IdentityTestModuleFactory factory3;
146 private TestImplModuleFactory factory4;
149 YangStoreService yangStoreSnapshot;
151 NetconfOperationRouter netconfOperationRouter;
153 AggregatedNetconfOperationServiceFactory netconfOperationServiceSnapshot;
155 private AutoCloseable sessionCloseable;
157 private TransactionProvider transactionProvider;
159 private ConfigSubsystemFacade configSubsystemFacade;
162 public void setUp() throws Exception {
163 MockitoAnnotations.initMocks(this);
166 final Filter filter = mock(Filter.class);
167 doReturn(filter).when(mockedContext).createFilter(anyString());
168 doNothing().when(mockedContext).addServiceListener(any(ServiceListener.class), anyString());
169 doReturn(new ServiceReference<?>[]{}).when(mockedContext).getServiceReferences(anyString(), anyString());
171 doReturn(yangStoreSnapshot).when(yangStoreSnapshot).getCurrentSnapshot();
172 doReturn(getMbes()).when(this.yangStoreSnapshot).getModuleMXBeanEntryMap();
173 doReturn(getModules()).when(this.yangStoreSnapshot).getModules();
174 doReturn(new EnumResolver() {
176 public String fromYang(final String enumType, final String enumYangValue) {
177 return Preconditions.checkNotNull(getEnumMapping().get(enumYangValue),
178 "Unable to resolve enum value %s, for enum %s with mappings %s", enumYangValue, enumType, getEnumMapping());
182 public String toYang(final String enumType, final String enumYangValue) {
183 return Preconditions.checkNotNull(getEnumMapping().inverse().get(enumYangValue),
184 "Unable to resolve enum value %s, for enum %s with mappings %s", enumYangValue, enumType, getEnumMapping().inverse());
186 }).when(this.yangStoreSnapshot).getEnumResolver();
188 this.factory = new NetconfTestImplModuleFactory();
189 this.factory2 = new DepTestImplModuleFactory();
190 this.factory3 = new IdentityTestModuleFactory();
191 factory4 = new TestImplModuleFactory();
192 doNothing().when(sessionCloseable).close();
194 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, this.factory, this.factory2,
195 this.factory3, factory4));
197 transactionProvider = new TransactionProvider(this.configRegistryClient, NETCONF_SESSION_ID);
199 configSubsystemFacade = new ConfigSubsystemFacade(configRegistryClient, configRegistryClient, yangStoreSnapshot, "mapping-test");
202 private ObjectName createModule(final String instanceName) throws InstanceAlreadyExistsException, InstanceNotFoundException, URISyntaxException, ValidationException, ConflictingVersionException {
203 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
205 final ObjectName on = transaction.createModule(this.factory.getImplementationName(), instanceName);
206 final NetconfTestImplModuleMXBean mxBean = transaction.newMXBeanProxy(on, NetconfTestImplModuleMXBean.class);
207 setModule(mxBean, transaction, instanceName + "_dep");
210 for (Class<? extends AbstractServiceInterface> sInterface : factory.getImplementedServiceIntefaces()) {
211 ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
212 transaction.saveServiceReference(
213 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + instanceName + "_" + i++,
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 (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 NodeList modulesList = config.getElementsByTagName("modules");
327 assertEquals(1, modulesList.getLength());
329 NodeTest nt = new NodeTest((DocumentTraversal) config, modulesList.item(0));
330 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, final Set<String> refNames) throws NodeTestException {
353 final Set<String> refNames2 = new HashSet<>(refNames);
354 NodeList servicesNodes = configCandidate.getElementsByTagName("services");
355 assertEquals(1, servicesNodes.getLength());
357 NodeTest nt = new NodeTest((DocumentTraversal) configCandidate, servicesNodes.item(0));
358 NodeTester tester = new AbstractNodeTester() {
361 public void testElement(final Element element) throws NodeTestException {
362 if(element.getNodeName() != null) {
363 if(element.getNodeName().equals("name")) {
364 String elmText = element.getTextContent();
365 if(refNames2.contains(elmText)) {
366 refNames2.remove(elmText);
368 throw new NodeTestException("Unexpected services defined: " + elmText);
375 public void noMoreNodes(final NodeTest forTest) throws NodeTestException {
376 assertEquals(Collections.<String>emptySet(), refNames2);
377 assertTrue(refNames2.toString(), refNames2.isEmpty());
380 nt.performTest(tester, Node.ELEMENT_NODE);
384 public void testConfigNetconfUnionTypes() throws Exception {
386 createModule(INSTANCE_NAME);
388 edit("netconfMessages/editConfig.xml");
390 Document response = getConfigRunning();
391 Element ipElement = readXmlToElement("<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">0:0:0:0:0:0:0:1</ip>");
392 assertContainsElement(response, 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("<union-test-attr xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">456</union-test-attr>"));
397 edit("netconfMessages/editConfig_setUnions.xml");
399 response = getConfigRunning();
400 assertContainsElement(response, readXmlToElement("<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">127.1.2.3</ip>"));
401 assertContainsElement(response, readXmlToElement("<union-test-attr xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">randomStringForUnion</union-test-attr>"));
406 public void testConfigNetconf() throws Exception {
408 createModule(INSTANCE_NAME);
410 edit("netconfMessages/editConfig.xml");
411 Document configCandidate = getConfigCandidate();
412 checkBinaryLeafEdited(configCandidate);
415 // default-operation:none, should not affect binary leaf
416 edit("netconfMessages/editConfig_none.xml");
417 checkBinaryLeafEdited(getConfigCandidate());
421 Document response = getConfigRunning();
423 checkBinaryLeafEdited(response);
424 checkTypeConfigAttribute(response);
425 checkTypedefs(response);
426 checkTestingDeps(response);
428 checkBigDecimal(response);
430 edit("netconfMessages/editConfig_remove.xml");
433 assertXMLEqual(getConfigCandidate(), getConfigRunning());
435 final Document expectedResult = XmlFileLoader.xmlFileToDocument("netconfMessages/editConfig_expectedResult.xml");
436 XMLUnit.setIgnoreWhitespace(true);
437 assertXMLEqual(expectedResult, getConfigRunning());
438 assertXMLEqual(expectedResult, getConfigCandidate());
440 edit("netconfMessages/editConfig_none.xml");
442 verify(sessionCloseable).close();
443 verifyNoMoreInteractions(netconfOperationRouter);
444 verifyNoMoreInteractions(netconfOperationServiceSnapshot);
447 private static void checkBigDecimal(final Document response) throws NodeTestException, SAXException, IOException {
448 assertContainsElement(response, readXmlToElement("<sleep-factor xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.58</sleep-factor>"));
450 assertContainsElement(response, readXmlToElement("<sleep-factor xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.00</sleep-factor>"));
453 private void closeSession() throws ParserConfigurationException, SAXException,
454 IOException, DocumentedException {
455 final Channel channel = mock(Channel.class);
456 doReturn("channel").when(channel).toString();
457 final NetconfServerSessionListener listener = mock(NetconfServerSessionListener.class);
458 final NetconfServerSession session =
459 new NetconfServerSession(listener, channel, 1L,
460 NetconfHelloMessageAdditionalHeader.fromString("[netconf;10.12.0.102:48528;ssh;;;;;;]"));
461 DefaultCloseSession closeOp = new DefaultCloseSession(NETCONF_SESSION_ID, sessionCloseable);
462 closeOp.setNetconfSession(session);
463 executeOp(closeOp, "netconfMessages/closeSession.xml");
466 private void edit(final String resource) throws ParserConfigurationException, SAXException, IOException,
467 DocumentedException {
468 EditConfig editOp = new EditConfig(configSubsystemFacade, NETCONF_SESSION_ID);
469 executeOp(editOp, resource);
472 private void commit() throws ParserConfigurationException, SAXException, IOException, DocumentedException {
473 Commit commitOp = new Commit(configSubsystemFacade, NETCONF_SESSION_ID);
474 executeOp(commitOp, "netconfMessages/commit.xml");
477 private static Document lockCandidate() throws ParserConfigurationException, SAXException, IOException, DocumentedException {
478 Lock commitOp = new Lock(NETCONF_SESSION_ID);
479 return executeOp(commitOp, "netconfMessages/lock.xml");
482 private static Document unlockCandidate() throws ParserConfigurationException, SAXException, IOException, DocumentedException {
483 UnLock commitOp = new UnLock(NETCONF_SESSION_ID);
484 return executeOp(commitOp, "netconfMessages/unlock.xml");
487 private Document getConfigCandidate() throws ParserConfigurationException, SAXException, IOException,
488 DocumentedException {
489 GetConfig getConfigOp = new GetConfig(configSubsystemFacade, Optional.<String> absent(), NETCONF_SESSION_ID);
490 return executeOp(getConfigOp, "netconfMessages/getConfig_candidate.xml");
493 private Document getConfigRunning() throws ParserConfigurationException, SAXException, IOException,
494 DocumentedException {
495 GetConfig getConfigOp = new GetConfig(configSubsystemFacade, Optional.<String> absent(), NETCONF_SESSION_ID);
496 return executeOp(getConfigOp, "netconfMessages/getConfig.xml");
499 @Ignore("second edit message corrupted")
500 @Test(expected = DocumentedException.class)
501 public void testConfigNetconfReplaceDefaultEx() throws Exception {
503 createModule(INSTANCE_NAME);
505 edit("netconfMessages/editConfig.xml");
506 edit("netconfMessages/editConfig_replace_default_ex.xml");
510 public void testConfigNetconfReplaceDefault() throws Exception {
512 createModule(INSTANCE_NAME);
514 edit("netconfMessages/editConfig.xml");
516 Document response = getConfigRunning();
517 final int allInstances = response.getElementsByTagName("module").getLength();
519 edit("netconfMessages/editConfig_replace_default.xml");
522 response = getConfigRunning();
524 final int afterReplace = response.getElementsByTagName("module").getLength();
525 assertEquals(4, allInstances);
526 assertEquals(2, afterReplace);
530 public void testSameAttrDifferentNamespaces() throws Exception {
532 edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespaces.xml");
534 } catch (DocumentedException e) {
535 String message = e.getMessage();
536 assertContainsString(message, "Element simpleInt present multiple times with different namespaces");
537 assertContainsString(message, TEST_NAMESPACE);
538 assertContainsString(message, XmlMappingConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
543 public void testDifferentNamespaceInTO() throws Exception {
545 edit("netconfMessages/namespaces/editConfig_differentNamespaceTO.xml");
547 } catch (DocumentedException e) {
548 String message = e.getMessage();
549 assertContainsString(message, "Unrecognised elements");
550 assertContainsString(message, "simple-int2");
551 assertContainsString(message, "dto_d");
556 public void testSameAttrDifferentNamespacesList() throws Exception {
558 edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespacesList.xml");
560 } catch (DocumentedException e) {
561 String message = e.getMessage();
562 assertContainsString(message, "Element allow-user 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 testTypeNameConfigAttributeMatching() throws Exception {
570 edit("netconfMessages/editConfig.xml");
572 edit("netconfMessages/namespaces/editConfig_typeNameConfigAttributeMatching.xml");
575 Document response = getConfigRunning();
576 checkTypeConfigAttribute(response);
579 // TODO add <modules operation="replace"> functionality
580 @Test(expected = DocumentedException.class)
581 public void testConfigNetconfReplaceModuleEx() throws Exception {
583 createModule(INSTANCE_NAME);
585 edit("netconfMessages/editConfig.xml");
586 edit("netconfMessages/editConfig_replace_module_ex.xml");
590 public void testUnrecognisedConfigElements() throws Exception {
592 String format = "netconfMessages/unrecognised/editConfig_unrecognised%d.xml";
593 final int TESTS_COUNT = 8;
595 for (int i = 0; i < TESTS_COUNT; i++) {
596 String file = String.format(format, i + 1);
597 LOG.info("Reading {}", file);
600 } catch (DocumentedException e) {
601 assertContainsString(e.getMessage(), "Unrecognised elements");
602 assertContainsString(e.getMessage(), "unknownAttribute");
605 fail("Unrecognised test should throw exception " + file);
612 public void testConfigNetconfReplaceModule() throws Exception {
614 createModule(INSTANCE_NAME);
616 edit("netconfMessages/editConfig.xml");
618 Document response = getConfigRunning();
619 final int allInstances = response.getElementsByTagName("instance").getLength();
621 edit("netconfMessages/editConfig_replace_module.xml");
624 response = getConfigRunning();
625 final int afterReplace = response.getElementsByTagName("instance").getLength();
627 assertEquals(4 + 4 /* Instances from services */, allInstances);
628 assertEquals(3 + 3, afterReplace);
632 public void testEx2() throws Exception {
633 //check abort before tx creation
634 assertContainsElement(discard(), readXmlToElement("<ok xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
636 //check abort after tx creation
637 edit("netconfMessages/editConfig.xml");
638 assertContainsElement(discard(), readXmlToElement("<ok xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
642 public void testFailedDiscardChangesAbort() throws Exception {
643 final ConfigSubsystemFacade facade = mock(ConfigSubsystemFacade.class);
644 doThrow(new RuntimeException("Mocked runtime exception, Abort has to fail")).when(facade).abortConfiguration();
646 DiscardChanges discardOp = new DiscardChanges(facade, NETCONF_SESSION_ID);
649 executeOp(discardOp, "netconfMessages/discardChanges.xml");
650 fail("Should've failed, abort on mocked is supposed to throw RuntimeException");
651 } catch (DocumentedException e) {
652 assertTrue(e.getErrorTag() == DocumentedException.ErrorTag.operation_failed);
653 assertTrue(e.getErrorSeverity() == DocumentedException.ErrorSeverity.error);
654 assertTrue(e.getErrorType() == DocumentedException.ErrorType.application);
658 private Document discard() throws ParserConfigurationException, SAXException, IOException, DocumentedException {
659 DiscardChanges discardOp = new DiscardChanges(configSubsystemFacade, NETCONF_SESSION_ID);
660 return executeOp(discardOp, "netconfMessages/discardChanges.xml");
663 private static void checkBinaryLeafEdited(final Document response) throws NodeTestException, SAXException, IOException {
664 assertContainsElement(response, readXmlToElement("<binaryLeaf xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">YmluYXJ5</binaryLeaf>"));
665 assertContainsElement(response, readXmlToElement("<binaryLeaf xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ZGVmYXVsdEJpbg==</binaryLeaf>"));
668 private static void checkTypedefs(final Document response) throws NodeTestException, SAXException, IOException {
670 assertContainsElement(response, readXmlToElement("<extended xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">10</extended>"));
672 assertContainsElement(response, readXmlToElement("<extended xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">1</extended>"));
674 assertContainsElement(response, readXmlToElement("<extended-twice xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">20</extended-twice>"));
676 assertContainsElement(response, readXmlToElement("<extended-twice xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2</extended-twice>"));
678 assertContainsElement(response, readXmlToElement("<extended-enum xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">two</extended-enum>"));
680 assertContainsElement(response, readXmlToElement("<extended-enum xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">one</extended-enum>"));
683 private static void assertContainsString(final String string, final String substring) {
684 assertThat(string, containsString(substring));
687 private static void checkEnum(final Document response) throws Exception {
689 String expectedEnumContent = "two";
691 XMLAssert.assertXpathEvaluatesTo(expectedEnumContent,
692 getXpathForNetconfImplSubnode(INSTANCE_NAME,"extended-enum"),
696 private static void checkTestingDeps(final Document response) {
697 int testingDepsSize = response.getElementsByTagName("testing-deps").getLength();
698 assertEquals(2, testingDepsSize);
701 private static String getXpathForNetconfImplSubnode(final String instanceName, final String subnode) {
702 return "/urn:ietf:params:xml:ns:netconf:base:1.0:rpc-reply" +
703 "/urn:ietf:params:xml:ns:netconf:base:1.0:data" +
704 "/urn:opendaylight:params:xml:ns:yang:controller:config:modules" +
705 "/module[name='"+instanceName+"']" +
706 "/urn:opendaylight:params:xml:ns:yang:controller:test:impl:impl-netconf" +
707 "/urn:opendaylight:params:xml:ns:yang:controller:test:impl:"+subnode;
710 private static void checkTypeConfigAttribute(final Document response) throws Exception {
712 Map<String,String> namesToTypeValues = ImmutableMap.of("instance-from-code", "configAttributeType",
713 "test2", "default-string");
714 for (Entry<String, String> nameToExpectedValue : namesToTypeValues.entrySet()) {
715 XMLAssert.assertXpathEvaluatesTo(nameToExpectedValue.getValue(),
716 getXpathForNetconfImplSubnode(nameToExpectedValue.getKey(),"type"),
721 private Map<String, Map<String, ModuleMXBeanEntry>> getMbes() throws Exception {
722 final List<InputStream> yangDependencies = getYangs();
724 final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries = Maps.newHashMap();
726 YangParserImpl yangParser = new YangParserImpl();
727 final SchemaContext schemaContext = yangParser.resolveSchemaContext(new HashSet<>(yangParser.parseYangModelsFromStreamsMapped(yangDependencies).values()));
728 YangStoreService yangStoreService = new YangStoreService(new SchemaContextProvider() {
729 @Override public SchemaContext getSchemaContext() {
730 return schemaContext;
732 }, mock(SchemaSourceProvider.class));
733 final BindingRuntimeContext bindingRuntimeContext = mock(BindingRuntimeContext.class);
734 doReturn(schemaContext).when(bindingRuntimeContext).getSchemaContext();
735 doReturn(getEnumMapping()).when(bindingRuntimeContext).getEnumMapping(any(Class.class));
736 yangStoreService.refresh(bindingRuntimeContext);
737 mBeanEntries.putAll(yangStoreService.getModuleMXBeanEntryMap());
742 private static BiMap<String, String> getEnumMapping() {
743 final HashBiMap<String, String> enumBiMap = HashBiMap.create();
744 // Enum constants mapping from yang -> Java and back
745 enumBiMap.put("one", "One");
746 enumBiMap.put("two", "Two");
747 enumBiMap.put("version1", "Version1");
748 enumBiMap.put("version2", "Version2");
752 private Set<org.opendaylight.yangtools.yang.model.api.Module> getModules() throws Exception {
753 SchemaContext resolveSchemaContext = getSchemaContext();
754 return resolveSchemaContext.getModules();
757 private SchemaContext getSchemaContext() {
758 final List<InputStream> yangDependencies = getYangs();
759 YangParserImpl parser = new YangParserImpl();
761 Set<Module> allYangModules = parser.parseYangModelsFromStreams(yangDependencies);
763 return parser.resolveSchemaContext(Sets
764 .newHashSet(allYangModules));
768 public void testConfigNetconfRuntime() throws Exception {
770 createModule(INSTANCE_NAME);
772 edit("netconfMessages/editConfig.xml");
773 checkBinaryLeafEdited(getConfigCandidate());
777 Document response = get();
779 assertEquals(2/*With runtime beans*/ + 2 /*Without runtime beans*/, getElementsSize(response, "module"));
781 assertEquals(2, getElementsSize(response, "asdf"));
782 // data from running config
783 assertEquals(2, getElementsSize(response, "simple-short"));
785 assertEquals(8, getElementsSize(response, "inner-running-data"));
786 assertEquals(8, getElementsSize(response, "deep2"));
787 assertEquals(8 * 4, getElementsSize(response, "inner-inner-running-data"));
788 assertEquals(8 * 4, getElementsSize(response, "deep3"));
789 assertEquals(8 * 4 * 2, getElementsSize(response, "list-of-strings"));
790 assertEquals(8, getElementsSize(response, "inner-running-data-additional", "urn:opendaylight:params:xml:ns:yang:controller:test:impl"));
791 assertEquals(8, getElementsSize(response, "deep4"));
794 RuntimeRpc netconf = new RuntimeRpc(configSubsystemFacade, NETCONF_SESSION_ID);
796 response = executeOp(netconf, "netconfMessages/rpc.xml");
797 assertContainsElementWithText(response, "testarg1");
799 response = executeOp(netconf, "netconfMessages/rpcInner.xml");
800 Document expectedReplyOk = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
801 XMLUnit.setIgnoreWhitespace(true);
802 XMLAssert.assertXMLEqual(expectedReplyOk, response);
804 response = executeOp(netconf, "netconfMessages/rpcInnerInner.xml");
805 assertContainsElementWithText(response, "true");
807 response = executeOp(netconf, "netconfMessages/rpcInnerInner_complex_output.xml");
808 assertContainsElementWithText(response, "1");
809 assertContainsElementWithText(response, "2");
812 private Document get() throws ParserConfigurationException, SAXException, IOException, DocumentedException {
813 Get getOp = new Get(configSubsystemFacade, NETCONF_SESSION_ID);
814 return executeOp(getOp, "netconfMessages/get.xml");
817 private static int getElementsSize(final Document response, final String elementName) {
818 return response.getElementsByTagName(elementName).getLength();
821 private static int getElementsSize(final Document response, final String elementName, final String namespace) {
822 return response.getElementsByTagNameNS(namespace, elementName).getLength();
825 private static Document executeOp(final NetconfOperation op, final String filename) throws ParserConfigurationException,
826 SAXException, IOException, DocumentedException {
828 final Document request = XmlFileLoader.xmlFileToDocument(filename);
830 LOG.debug("Executing netconf operation\n{}", XmlUtil.toString(request));
831 HandlingPriority priority = op.canHandle(request);
833 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
835 final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
836 LOG.debug("Got response\n{}", XmlUtil.toString(response));
840 private List<InputStream> getYangs() {
841 List<String> paths = Arrays.asList("/META-INF/yang/config.yang", "/META-INF/yang/rpc-context.yang",
842 "/META-INF/yang/config-test.yang", "/META-INF/yang/config-test-impl.yang", "/META-INF/yang/test-types.yang",
843 "/META-INF/yang/test-groups.yang", "/META-INF/yang/ietf-inet-types.yang");
844 final Collection<InputStream> yangDependencies = new ArrayList<>();
845 for (String path : paths) {
846 final InputStream is = Preconditions
847 .checkNotNull(getClass().getResourceAsStream(path), path + " not found");
848 yangDependencies.add(is);
850 return Lists.newArrayList(yangDependencies);
853 private void setModule(final NetconfTestImplModuleMXBean mxBean, final ConfigTransactionJMXClient transaction, final String depName)
854 throws InstanceAlreadyExistsException, InstanceNotFoundException {
855 mxBean.setSimpleInt((long) 44);
856 mxBean.setBinaryLeaf(new byte[] { 8, 7, 9 });
857 final DtoD dtob = getDtoD();
858 mxBean.setDtoD(dtob);
860 final DtoC dtoa = getDtoC();
861 mxBean.setDtoC(dtoa);
862 mxBean.setSimpleBoolean(false);
864 final Peers p1 = new Peers();
867 p1.setSimpleInt3(456);
868 final Peers p2 = new Peers();
870 p2.setPort("port23");
871 p2.setSimpleInt3(456);
872 mxBean.setPeers(Lists.<Peers> newArrayList(p1, p2));
874 mxBean.setSimpleLong(454545L);
875 mxBean.setSimpleLong2(44L);
876 mxBean.setSimpleBigInteger(BigInteger.valueOf(999L));
877 mxBean.setSimpleByte(new Byte((byte) 4));
878 mxBean.setSimpleShort(new Short((short) 4));
879 mxBean.setSimpleTest(545);
881 mxBean.setComplexList(Lists.<ComplexList> newArrayList());
882 mxBean.setSimpleList(Lists.<Integer> newArrayList());
884 final ObjectName testingDepOn = transaction.createModule(this.factory2.getImplementationName(), depName);
886 for (Class<? extends AbstractServiceInterface> sInterface : factory2.getImplementedServiceIntefaces()) {
887 ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
888 transaction.saveServiceReference(
889 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + depName + "_" + i++,
893 mxBean.setTestingDep(testingDepOn);
896 private static DtoD getDtoD() {
897 final DtoD dtob = new DtoD();
898 dtob.setSimpleInt1((long) 444);
899 dtob.setSimpleInt2((long) 4444);
900 dtob.setSimpleInt3(454);
901 final ComplexDtoBInner dtobInner = new ComplexDtoBInner();
902 final Deep deep = new Deep();
903 deep.setSimpleInt3(4);
904 dtobInner.setDeep(deep);
905 dtobInner.setSimpleInt3(44);
906 dtobInner.setSimpleList(Lists.newArrayList(4));
907 dtob.setComplexDtoBInner(Lists.newArrayList(dtobInner));
908 dtob.setSimpleList(Lists.newArrayList(4));
912 private static DtoC getDtoC() {
913 final DtoC dtoa = new DtoC();
914 // dtoa.setSimpleArg((long) 55);
915 final DtoAInner dtoAInner = new DtoAInner();
916 final DtoAInnerInner dtoAInnerInner = new DtoAInnerInner();
917 dtoAInnerInner.setSimpleArg(456L);
918 dtoAInner.setDtoAInnerInner(dtoAInnerInner);
919 dtoAInner.setSimpleArg(44L);
920 dtoa.setDtoAInner(dtoAInner);