2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.netconf.confignetconfconnector;
11 import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
12 import static org.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.netconf.util.test.XmlUnitUtil.assertContainsElement;
27 import static org.opendaylight.controller.netconf.util.test.XmlUnitUtil.assertContainsElementWithText;
28 import static org.opendaylight.controller.netconf.util.xml.XmlUtil.readXmlToElement;
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.FileNotFoundException;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.math.BigInteger;
43 import java.net.URISyntaxException;
44 import java.util.ArrayList;
45 import java.util.Arrays;
46 import java.util.Collection;
47 import java.util.Collections;
48 import java.util.HashSet;
49 import java.util.List;
51 import java.util.Map.Entry;
53 import java.util.regex.Matcher;
54 import java.util.regex.Pattern;
55 import javax.management.InstanceAlreadyExistsException;
56 import javax.management.InstanceNotFoundException;
57 import javax.management.ObjectName;
58 import javax.xml.parsers.ParserConfigurationException;
59 import org.custommonkey.xmlunit.AbstractNodeTester;
60 import org.custommonkey.xmlunit.NodeTest;
61 import org.custommonkey.xmlunit.NodeTestException;
62 import org.custommonkey.xmlunit.NodeTester;
63 import org.custommonkey.xmlunit.XMLAssert;
64 import org.custommonkey.xmlunit.XMLUnit;
65 import org.junit.Before;
66 import org.junit.Ignore;
67 import org.junit.Test;
68 import org.mockito.Mock;
69 import org.mockito.MockitoAnnotations;
70 import org.opendaylight.controller.config.api.ConflictingVersionException;
71 import org.opendaylight.controller.config.api.ValidationException;
72 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
73 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
74 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
75 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
76 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
77 import org.opendaylight.controller.config.yang.test.impl.ComplexDtoBInner;
78 import org.opendaylight.controller.config.yang.test.impl.ComplexList;
79 import org.opendaylight.controller.config.yang.test.impl.Deep;
80 import org.opendaylight.controller.config.yang.test.impl.DepTestImplModuleFactory;
81 import org.opendaylight.controller.config.yang.test.impl.DtoAInner;
82 import org.opendaylight.controller.config.yang.test.impl.DtoAInnerInner;
83 import org.opendaylight.controller.config.yang.test.impl.DtoC;
84 import org.opendaylight.controller.config.yang.test.impl.DtoD;
85 import org.opendaylight.controller.config.yang.test.impl.IdentityTestModuleFactory;
86 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleFactory;
87 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleMXBean;
88 import org.opendaylight.controller.config.yang.test.impl.Peers;
89 import org.opendaylight.controller.config.yang.test.impl.TestImplModuleFactory;
90 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
91 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
92 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
93 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
94 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
95 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
96 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Commit;
97 import org.opendaylight.controller.netconf.confignetconfconnector.operations.DiscardChanges;
98 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Lock;
99 import org.opendaylight.controller.netconf.confignetconfconnector.operations.UnLock;
100 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
101 import org.opendaylight.controller.netconf.confignetconfconnector.operations.get.Get;
102 import org.opendaylight.controller.netconf.confignetconfconnector.operations.getconfig.GetConfig;
103 import org.opendaylight.controller.netconf.confignetconfconnector.operations.runtimerpc.RuntimeRpc;
104 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.EnumResolver;
105 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreContext;
106 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreService;
107 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
108 import org.opendaylight.controller.netconf.impl.NetconfServerSession;
109 import org.opendaylight.controller.netconf.impl.NetconfServerSessionListener;
110 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCloseSession;
111 import org.opendaylight.controller.netconf.impl.osgi.AggregatedNetconfOperationServiceFactory;
112 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouter;
113 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
114 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
115 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
116 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader;
117 import org.opendaylight.controller.netconf.util.messages.NetconfMessageUtil;
118 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
119 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
120 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity1;
121 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity2;
122 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
123 import org.opendaylight.yangtools.yang.model.api.Module;
124 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
125 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
126 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
127 import org.osgi.framework.Filter;
128 import org.osgi.framework.ServiceListener;
129 import org.osgi.framework.ServiceReference;
130 import org.slf4j.Logger;
131 import org.slf4j.LoggerFactory;
132 import org.w3c.dom.Document;
133 import org.w3c.dom.Element;
134 import org.w3c.dom.Node;
135 import org.w3c.dom.NodeList;
136 import org.w3c.dom.Text;
137 import org.w3c.dom.traversal.DocumentTraversal;
138 import org.xml.sax.SAXException;
141 public class NetconfMappingTest extends AbstractConfigTest {
142 private static final Logger LOG = LoggerFactory.getLogger(NetconfMappingTest.class);
144 private static final String INSTANCE_NAME = "instance-from-code";
145 private static final String NETCONF_SESSION_ID = "foo";
146 private static final String TEST_NAMESPACE= "urn:opendaylight:params:xml:ns:yang:controller:test:impl";
147 private NetconfTestImplModuleFactory factory;
148 private DepTestImplModuleFactory factory2;
149 private IdentityTestModuleFactory factory3;
150 private TestImplModuleFactory factory4;
153 YangStoreContext yangStoreSnapshot;
155 NetconfOperationRouter netconfOperationRouter;
157 AggregatedNetconfOperationServiceFactory netconfOperationServiceSnapshot;
159 private AutoCloseable sessionCloseable;
161 private TransactionProvider transactionProvider;
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(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", enumYangValue, enumType, getEnumMapping());
183 public String toYang(final String enumType, final String enumYangValue) {
184 return Preconditions.checkNotNull(getEnumMapping().inverse().get(enumYangValue),
185 "Unable to resolve enum value %s, for enum %s with mappings %s", enumYangValue, enumType, getEnumMapping().inverse());
187 }).when(this.yangStoreSnapshot).getEnumResolver();
189 this.factory = new NetconfTestImplModuleFactory();
190 this.factory2 = new DepTestImplModuleFactory();
191 this.factory3 = new IdentityTestModuleFactory();
192 factory4 = new TestImplModuleFactory();
193 doNothing().when(sessionCloseable).close();
195 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, this.factory, this.factory2,
196 this.factory3, factory4));
198 transactionProvider = new TransactionProvider(this.configRegistryClient, NETCONF_SESSION_ID);
201 private ObjectName createModule(final String instanceName) throws InstanceAlreadyExistsException, InstanceNotFoundException, URISyntaxException, ValidationException, ConflictingVersionException {
202 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
204 final ObjectName on = transaction.createModule(this.factory.getImplementationName(), instanceName);
205 final NetconfTestImplModuleMXBean mxBean = transaction.newMXBeanProxy(on, NetconfTestImplModuleMXBean.class);
206 setModule(mxBean, transaction, instanceName + "_dep");
209 for (Class<? extends AbstractServiceInterface> sInterface : factory.getImplementedServiceIntefaces()) {
210 ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
211 transaction.saveServiceReference(
212 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + instanceName + "_" + i++,
216 transaction.commit();
221 public void testIdentityRefs() throws Exception {
222 edit("netconfMessages/editConfig_identities.xml");
225 Document configRunning = getConfigRunning();
226 String asString = XmlUtil.toString(configRunning);
227 assertThat(asString, containsString("test-identity2"));
228 assertThat(asString, containsString("test-identity1"));
229 assertEquals(2, countSubstringOccurence(asString, "</identities>"));
231 edit("netconfMessages/editConfig_identities_inner_replace.xml");
233 configRunning = getConfigRunning();
234 asString = XmlUtil.toString(configRunning);
235 // test-identity1 was removed by replace
236 assertThat(asString, not(containsString("test-identity2")));
237 // now only 1 identities entry is present
238 assertEquals(1, countSubstringOccurence(asString, "</identities>"));
241 private int countSubstringOccurence(final String string, final String substring) {
242 final Matcher matches = Pattern.compile(substring).matcher(string);
244 while (matches.find()) {
251 protected BindingRuntimeContext getBindingRuntimeContext() {
252 final BindingRuntimeContext ret = super.getBindingRuntimeContext();
253 doReturn(TestIdentity1.class).when(ret).getIdentityClass(TestIdentity1.QNAME);
254 doReturn(TestIdentity2.class).when(ret).getIdentityClass(TestIdentity2.QNAME);
259 public void testServicePersistance() throws Exception {
260 createModule(INSTANCE_NAME);
262 edit("netconfMessages/editConfig.xml");
263 Document config = getConfigCandidate();
264 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
265 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
266 "ref_from_code_to_instance-from-code_1"));
269 edit("netconfMessages/editConfig_addServiceName.xml");
270 config = getConfigCandidate();
271 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
272 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
273 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
275 edit("netconfMessages/editConfig_addServiceNameOnTest.xml");
276 config = getConfigCandidate();
277 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
278 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
279 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
282 config = getConfigRunning();
283 assertCorrectRefNamesForDependencies(config);
284 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
285 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
286 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another"));
288 edit("netconfMessages/editConfig_removeServiceNameOnTest.xml");
289 config = getConfigCandidate();
290 assertCorrectServiceNames(config, Sets.newHashSet("user_to_instance_from_code", "ref_dep_user",
291 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
292 "ref_from_code_to_instance-from-code_1"));
295 edit("netconfMessages/editConfig_removeServiceNameOnTest.xml");
296 fail("Should've failed, non-existing service instance");
297 } catch (NetconfDocumentedException e) {
298 assertEquals(e.getErrorSeverity(), ErrorSeverity.error);
299 assertEquals(e.getErrorTag(), ErrorTag.operation_failed);
300 assertEquals(e.getErrorType(), ErrorType.application);
303 edit("netconfMessages/editConfig_replace_default.xml");
304 config = getConfigCandidate();
305 assertCorrectServiceNames(config, Collections.<String>emptySet());
307 edit("netconfMessages/editConfig_remove.xml");
308 config = getConfigCandidate();
309 assertCorrectServiceNames(config, Collections.<String>emptySet());
312 config = getConfigCandidate();
313 assertCorrectServiceNames(config, Collections.<String>emptySet());
318 public void testUnLock() throws Exception {
319 assertTrue(NetconfMessageUtil.isOKMessage(lockCandidate()));
320 assertTrue(NetconfMessageUtil.isOKMessage(unlockCandidate()));
323 private void assertCorrectRefNamesForDependencies(Document config) throws NodeTestException {
324 NodeList modulesList = config.getElementsByTagName("modules");
325 assertEquals(1, modulesList.getLength());
327 NodeTest nt = new NodeTest((DocumentTraversal) config, modulesList.item(0));
328 NodeTester tester = new AbstractNodeTester() {
329 private int defaultRefNameCount = 0;
330 private int userRefNameCount = 0;
333 public void testText(Text text) throws NodeTestException {
334 if(text.getData().equals("ref_dep2")) {
335 defaultRefNameCount++;
336 } else if(text.getData().equals("ref_dep_user_two")) {
342 public void noMoreNodes(NodeTest forTest) throws NodeTestException {
343 assertEquals(0, defaultRefNameCount);
344 assertEquals(2, userRefNameCount);
347 nt.performTest(tester, Node.TEXT_NODE);
350 private void assertCorrectServiceNames(Document configCandidate, Set<String> refNames) throws NodeTestException {
351 final Set<String> refNames2 = new HashSet<>(refNames);
352 NodeList servicesNodes = configCandidate.getElementsByTagName("services");
353 assertEquals(1, servicesNodes.getLength());
355 NodeTest nt = new NodeTest((DocumentTraversal) configCandidate, servicesNodes.item(0));
356 NodeTester tester = new AbstractNodeTester() {
359 public void testElement(Element element) throws NodeTestException {
360 if(element.getNodeName() != null) {
361 if(element.getNodeName().equals("name")) {
362 String elmText = element.getTextContent();
363 if(refNames2.contains(elmText)) {
364 refNames2.remove(elmText);
366 throw new NodeTestException("Unexpected services defined: " + elmText);
373 public void noMoreNodes(NodeTest forTest) throws NodeTestException {
374 assertEquals(Collections.<String>emptySet(), refNames2);
375 assertTrue(refNames2.toString(), refNames2.isEmpty());
378 nt.performTest(tester, Node.ELEMENT_NODE);
382 public void testConfigNetconfUnionTypes() throws Exception {
384 createModule(INSTANCE_NAME);
386 edit("netconfMessages/editConfig.xml");
388 Document response = getConfigRunning();
389 Element ipElement = readXmlToElement("<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">0:0:0:0:0:0:0:1</ip>");
390 assertContainsElement(response, 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("<union-test-attr xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">456</union-test-attr>"));
395 edit("netconfMessages/editConfig_setUnions.xml");
397 response = getConfigRunning();
398 assertContainsElement(response, readXmlToElement("<ip xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">127.1.2.3</ip>"));
399 assertContainsElement(response, readXmlToElement("<union-test-attr xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">randomStringForUnion</union-test-attr>"));
404 public void testConfigNetconf() throws Exception {
406 createModule(INSTANCE_NAME);
408 edit("netconfMessages/editConfig.xml");
409 Document configCandidate = getConfigCandidate();
410 checkBinaryLeafEdited(configCandidate);
413 // default-operation:none, should not affect binary leaf
414 edit("netconfMessages/editConfig_none.xml");
415 checkBinaryLeafEdited(getConfigCandidate());
419 Document response = getConfigRunning();
421 checkBinaryLeafEdited(response);
422 checkTypeConfigAttribute(response);
423 checkTypedefs(response);
424 checkTestingDeps(response);
426 checkBigDecimal(response);
428 edit("netconfMessages/editConfig_remove.xml");
431 assertXMLEqual(getConfigCandidate(), getConfigRunning());
433 final Document expectedResult = XmlFileLoader.xmlFileToDocument("netconfMessages/editConfig_expectedResult.xml");
434 XMLUnit.setIgnoreWhitespace(true);
435 assertXMLEqual(expectedResult, getConfigRunning());
436 assertXMLEqual(expectedResult, getConfigCandidate());
438 edit("netconfMessages/editConfig_none.xml");
440 verify(sessionCloseable).close();
441 verifyNoMoreInteractions(netconfOperationRouter);
442 verifyNoMoreInteractions(netconfOperationServiceSnapshot);
445 private void checkBigDecimal(Document response) throws NodeTestException, SAXException, IOException {
446 assertContainsElement(response, readXmlToElement("<sleep-factor xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.58</sleep-factor>"));
448 assertContainsElement(response, readXmlToElement("<sleep-factor xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.00</sleep-factor>"));
452 private void closeSession() throws NetconfDocumentedException, ParserConfigurationException, SAXException,
454 final Channel channel = mock(Channel.class);
455 doReturn("channel").when(channel).toString();
456 final NetconfServerSessionListener listener = mock(NetconfServerSessionListener.class);
457 final NetconfServerSession session =
458 new NetconfServerSession(listener, channel, 1L,
459 NetconfHelloMessageAdditionalHeader.fromString("[netconf;10.12.0.102:48528;ssh;;;;;;]"));
460 DefaultCloseSession closeOp = new DefaultCloseSession(NETCONF_SESSION_ID, sessionCloseable);
461 closeOp.setNetconfSession(session);
462 executeOp(closeOp, "netconfMessages/closeSession.xml");
465 private void edit(String resource) throws ParserConfigurationException, SAXException, IOException,
466 NetconfDocumentedException {
467 EditConfig editOp = new EditConfig(yangStoreSnapshot, transactionProvider, configRegistryClient,
469 executeOp(editOp, resource);
472 private void commit() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
473 Commit commitOp = new Commit(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
474 executeOp(commitOp, "netconfMessages/commit.xml");
477 private Document lockCandidate() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
478 Lock commitOp = new Lock(NETCONF_SESSION_ID);
479 return executeOp(commitOp, "netconfMessages/lock.xml");
482 private Document unlockCandidate() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
483 UnLock commitOp = new UnLock(NETCONF_SESSION_ID);
484 return executeOp(commitOp, "netconfMessages/unlock.xml");
487 private Document getConfigCandidate() throws ParserConfigurationException, SAXException, IOException,
488 NetconfDocumentedException {
489 GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
490 configRegistryClient, NETCONF_SESSION_ID);
491 return executeOp(getConfigOp, "netconfMessages/getConfig_candidate.xml");
494 private Document getConfigRunning() throws ParserConfigurationException, SAXException, IOException,
495 NetconfDocumentedException {
496 GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
497 configRegistryClient, NETCONF_SESSION_ID);
498 return executeOp(getConfigOp, "netconfMessages/getConfig.xml");
501 @Ignore("second edit message corrupted")
502 @Test(expected = NetconfDocumentedException.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 (NetconfDocumentedException 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, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
545 public void testDifferentNamespaceInTO() throws Exception {
547 edit("netconfMessages/namespaces/editConfig_differentNamespaceTO.xml");
549 } catch (NetconfDocumentedException 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 (NetconfDocumentedException 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, XmlNetconfConstants.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 = NetconfDocumentedException.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 (NetconfDocumentedException 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);
633 @Test(expected = NetconfDocumentedException.class)
634 public void testEx() throws Exception {
640 public void testEx2() throws Exception {
641 //check abort before tx creation
642 assertContainsElement(discard(), readXmlToElement("<ok xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
644 //check abort after tx creation
645 edit("netconfMessages/editConfig.xml");
646 assertContainsElement(discard(), readXmlToElement("<ok xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
650 public void testFailedDiscardChangesAbort() throws Exception {
652 TransactionProvider mockedTxProvider = mock(TransactionProvider.class);
653 doThrow(new RuntimeException("Mocked runtime exception, Abort has to fail")).when(mockedTxProvider).abortTransaction();
654 doReturn(Optional.of(ObjectName.getInstance("dummyDomain", "DummyKey", "DummyValue"))).when(mockedTxProvider).getTransaction();
656 DiscardChanges discardOp = new DiscardChanges(mockedTxProvider, configRegistryClient, NETCONF_SESSION_ID);
659 executeOp(discardOp, "netconfMessages/discardChanges.xml");
660 fail("Should've failed, abort on mocked is supposed to throw RuntimeException");
661 } catch (NetconfDocumentedException e) {
662 assertTrue(e.getErrorTag() == ErrorTag.operation_failed);
663 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
664 assertTrue(e.getErrorType() == ErrorType.application);
669 private Document discard() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
670 DiscardChanges discardOp = new DiscardChanges(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
671 return executeOp(discardOp, "netconfMessages/discardChanges.xml");
674 private void checkBinaryLeafEdited(final Document response) throws NodeTestException, SAXException, IOException {
675 assertContainsElement(response, readXmlToElement("<binaryLeaf xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">YmluYXJ5</binaryLeaf>"));
676 assertContainsElement(response, readXmlToElement("<binaryLeaf xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ZGVmYXVsdEJpbg==</binaryLeaf>"));
679 private void checkTypedefs(final Document response) throws NodeTestException, SAXException, IOException {
681 assertContainsElement(response, readXmlToElement("<extended xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">10</extended>"));
683 assertContainsElement(response, readXmlToElement("<extended xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">1</extended>"));
685 assertContainsElement(response, readXmlToElement("<extended-twice xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">20</extended-twice>"));
687 assertContainsElement(response, readXmlToElement("<extended-twice xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2</extended-twice>"));
689 assertContainsElement(response, readXmlToElement("<extended-enum xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">two</extended-enum>"));
691 assertContainsElement(response, readXmlToElement("<extended-enum xmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">one</extended-enum>"));
694 private void assertContainsString(String string, String substring) {
695 assertThat(string, containsString(substring));
698 private void checkEnum(final Document response) throws Exception {
700 String expectedEnumContent = "two";
702 XMLAssert.assertXpathEvaluatesTo(expectedEnumContent,
703 getXpathForNetconfImplSubnode(INSTANCE_NAME,"extended-enum"),
707 private void checkTestingDeps(Document response) {
708 int testingDepsSize = response.getElementsByTagName("testing-deps").getLength();
709 assertEquals(2, testingDepsSize);
712 private String getXpathForNetconfImplSubnode(String instanceName, String subnode) {
713 return "/urn:ietf:params:xml:ns:netconf:base:1.0:rpc-reply" +
714 "/urn:ietf:params:xml:ns:netconf:base:1.0:data" +
715 "/urn:opendaylight:params:xml:ns:yang:controller:config:modules" +
716 "/module[name='"+instanceName+"']" +
717 "/urn:opendaylight:params:xml:ns:yang:controller:test:impl:impl-netconf" +
718 "/urn:opendaylight:params:xml:ns:yang:controller:test:impl:"+subnode;
721 private void checkTypeConfigAttribute(Document response) throws Exception {
723 Map<String,String> namesToTypeValues = ImmutableMap.of("instance-from-code", "configAttributeType",
724 "test2", "default-string");
725 for (Entry<String, String> nameToExpectedValue : namesToTypeValues.entrySet()) {
726 XMLAssert.assertXpathEvaluatesTo(nameToExpectedValue.getValue(),
727 getXpathForNetconfImplSubnode(nameToExpectedValue.getKey(),"type"),
732 private Map<String, Map<String, ModuleMXBeanEntry>> getMbes() throws Exception {
733 final List<InputStream> yangDependencies = getYangs();
735 final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries = Maps.newHashMap();
737 YangParserImpl yangParser = new YangParserImpl();
738 final SchemaContext schemaContext = yangParser.resolveSchemaContext(new HashSet<>(yangParser.parseYangModelsFromStreamsMapped(yangDependencies).values()));
739 YangStoreService yangStoreService = new YangStoreService(new SchemaContextProvider() {
741 public SchemaContext getSchemaContext() {
742 return schemaContext ;
745 final BindingRuntimeContext bindingRuntimeContext = mock(BindingRuntimeContext.class);
746 doReturn(getEnumMapping()).when(bindingRuntimeContext).getEnumMapping(any(Class.class));
747 yangStoreService.refresh(bindingRuntimeContext);
748 mBeanEntries.putAll(yangStoreService.getModuleMXBeanEntryMap());
753 private BiMap<String, String> getEnumMapping() {
754 final HashBiMap<String, String> enumBiMap = HashBiMap.create();
755 // Enum constants mapping from yang -> Java and back
756 enumBiMap.put("one", "One");
757 enumBiMap.put("two", "Two");
758 enumBiMap.put("version1", "Version1");
759 enumBiMap.put("version2", "Version2");
763 private Set<org.opendaylight.yangtools.yang.model.api.Module> getModules() throws Exception {
764 SchemaContext resolveSchemaContext = getSchemaContext();
765 return resolveSchemaContext.getModules();
768 private SchemaContext getSchemaContext() throws Exception {
769 final List<InputStream> yangDependencies = getYangs();
770 YangParserImpl parser = new YangParserImpl();
772 Set<Module> allYangModules = parser.parseYangModelsFromStreams(yangDependencies);
774 return parser.resolveSchemaContext(Sets
775 .newHashSet(allYangModules));
779 public void testConfigNetconfRuntime() throws Exception {
781 createModule(INSTANCE_NAME);
783 edit("netconfMessages/editConfig.xml");
784 checkBinaryLeafEdited(getConfigCandidate());
788 Document response = get();
790 assertEquals(2/*With runtime beans*/ + 2 /*Without runtime beans*/, getElementsSize(response, "module"));
792 assertEquals(2, getElementsSize(response, "asdf"));
793 // data from running config
794 assertEquals(2, getElementsSize(response, "simple-short"));
796 assertEquals(8, getElementsSize(response, "inner-running-data"));
797 assertEquals(8, getElementsSize(response, "deep2"));
798 assertEquals(8 * 4, getElementsSize(response, "inner-inner-running-data"));
799 assertEquals(8 * 4, getElementsSize(response, "deep3"));
800 assertEquals(8 * 4 * 2, getElementsSize(response, "list-of-strings"));
801 assertEquals(8, getElementsSize(response, "inner-running-data-additional", "urn:opendaylight:params:xml:ns:yang:controller:test:impl"));
802 assertEquals(8, getElementsSize(response, "deep4"));
805 RuntimeRpc netconf = new RuntimeRpc(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID);
807 response = executeOp(netconf, "netconfMessages/rpc.xml");
808 assertContainsElementWithText(response, "testarg1");
810 response = executeOp(netconf, "netconfMessages/rpcInner.xml");
811 Document expectedReplyOk = XmlFileLoader.xmlFileToDocument("netconfMessages/rpc-reply_ok.xml");
812 XMLUnit.setIgnoreWhitespace(true);
813 XMLAssert.assertXMLEqual(expectedReplyOk, response);
815 response = executeOp(netconf, "netconfMessages/rpcInnerInner.xml");
816 assertContainsElementWithText(response, "true");
818 response = executeOp(netconf, "netconfMessages/rpcInnerInner_complex_output.xml");
819 assertContainsElementWithText(response, "1");
820 assertContainsElementWithText(response, "2");
823 private Document get() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
824 Get getOp = new Get(transactionProvider, yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID);
825 return executeOp(getOp, "netconfMessages/get.xml");
828 private int getElementsSize(Document response, String elementName) {
829 return response.getElementsByTagName(elementName).getLength();
832 private int getElementsSize(Document response, String elementName, String namespace) {
833 return response.getElementsByTagNameNS(namespace, elementName).getLength();
836 private Document executeOp(final NetconfOperation op, final String filename) throws ParserConfigurationException,
837 SAXException, IOException, NetconfDocumentedException {
839 final Document request = XmlFileLoader.xmlFileToDocument(filename);
841 LOG.debug("Executing netconf operation\n{}", XmlUtil.toString(request));
842 HandlingPriority priority = op.canHandle(request);
844 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
846 final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
847 LOG.debug("Got response\n{}", XmlUtil.toString(response));
851 private List<InputStream> getYangs() throws FileNotFoundException {
852 List<String> paths = Arrays.asList("/META-INF/yang/config.yang", "/META-INF/yang/rpc-context.yang",
853 "/META-INF/yang/config-test.yang", "/META-INF/yang/config-test-impl.yang", "/META-INF/yang/test-types.yang",
854 "/META-INF/yang/test-groups.yang", "/META-INF/yang/ietf-inet-types.yang");
855 final Collection<InputStream> yangDependencies = new ArrayList<>();
856 for (String path : paths) {
857 final InputStream is = Preconditions
858 .checkNotNull(getClass().getResourceAsStream(path), path + " not found");
859 yangDependencies.add(is);
861 return Lists.newArrayList(yangDependencies);
864 private void setModule(final NetconfTestImplModuleMXBean mxBean, final ConfigTransactionJMXClient transaction, String depName)
865 throws InstanceAlreadyExistsException, InstanceNotFoundException {
866 mxBean.setSimpleInt((long) 44);
867 mxBean.setBinaryLeaf(new byte[] { 8, 7, 9 });
868 final DtoD dtob = getDtoD();
869 mxBean.setDtoD(dtob);
871 final DtoC dtoa = getDtoC();
872 mxBean.setDtoC(dtoa);
873 mxBean.setSimpleBoolean(false);
875 final Peers p1 = new Peers();
878 p1.setSimpleInt3(456);
879 final Peers p2 = new Peers();
881 p2.setPort("port23");
882 p2.setSimpleInt3(456);
883 mxBean.setPeers(Lists.<Peers> newArrayList(p1, p2));
885 mxBean.setSimpleLong(454545L);
886 mxBean.setSimpleLong2(44L);
887 mxBean.setSimpleBigInteger(BigInteger.valueOf(999L));
888 mxBean.setSimpleByte(new Byte((byte) 4));
889 mxBean.setSimpleShort(new Short((short) 4));
890 mxBean.setSimpleTest(545);
892 mxBean.setComplexList(Lists.<ComplexList> newArrayList());
893 mxBean.setSimpleList(Lists.<Integer> newArrayList());
895 final ObjectName testingDepOn = transaction.createModule(this.factory2.getImplementationName(), depName);
897 for (Class<? extends AbstractServiceInterface> sInterface : factory2.getImplementedServiceIntefaces()) {
898 ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
899 transaction.saveServiceReference(
900 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + depName + "_" + i++,
904 mxBean.setTestingDep(testingDepOn);
907 private static DtoD getDtoD() {
908 final DtoD dtob = new DtoD();
909 dtob.setSimpleInt1((long) 444);
910 dtob.setSimpleInt2((long) 4444);
911 dtob.setSimpleInt3(454);
912 final ComplexDtoBInner dtobInner = new ComplexDtoBInner();
913 final Deep deep = new Deep();
914 deep.setSimpleInt3(4);
915 dtobInner.setDeep(deep);
916 dtobInner.setSimpleInt3(44);
917 dtobInner.setSimpleList(Lists.newArrayList(4));
918 dtob.setComplexDtoBInner(Lists.newArrayList(dtobInner));
919 dtob.setSimpleList(Lists.newArrayList(4));
923 private static DtoC getDtoC() {
924 final DtoC dtoa = new DtoC();
925 // dtoa.setSimpleArg((long) 55);
926 final DtoAInner dtoAInner = new DtoAInner();
927 final DtoAInnerInner dtoAInnerInner = new DtoAInnerInner();
928 dtoAInnerInner.setSimpleArg(456L);
929 dtoAInner.setDtoAInnerInner(dtoAInnerInner);
930 dtoAInner.setSimpleArg(44L);
931 dtoa.setDtoAInner(dtoAInner);