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 com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Lists;
14 import com.google.common.collect.Maps;
15 import com.google.common.collect.Sets;
16 import org.apache.commons.lang3.StringUtils;
17 import org.junit.Before;
18 import org.junit.Ignore;
19 import org.junit.Test;
20 import org.junit.matchers.JUnitMatchers;
21 import org.mockito.Mock;
22 import org.mockito.MockitoAnnotations;
23 import org.opendaylight.controller.config.api.ConflictingVersionException;
24 import org.opendaylight.controller.config.api.ValidationException;
25 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
26 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
27 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
28 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
29 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
30 import org.opendaylight.controller.config.yang.test.impl.ComplexDtoBInner;
31 import org.opendaylight.controller.config.yang.test.impl.ComplexList;
32 import org.opendaylight.controller.config.yang.test.impl.Deep;
33 import org.opendaylight.controller.config.yang.test.impl.DepTestImplModuleFactory;
34 import org.opendaylight.controller.config.yang.test.impl.DtoAInner;
35 import org.opendaylight.controller.config.yang.test.impl.DtoAInnerInner;
36 import org.opendaylight.controller.config.yang.test.impl.DtoC;
37 import org.opendaylight.controller.config.yang.test.impl.DtoD;
38 import org.opendaylight.controller.config.yang.test.impl.IdentityTestModuleFactory;
39 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleFactory;
40 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleMXBean;
41 import org.opendaylight.controller.config.yang.test.impl.Peers;
42 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
43 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
44 import org.opendaylight.controller.netconf.api.NetconfOperationRouter;
45 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Commit;
46 import org.opendaylight.controller.netconf.confignetconfconnector.operations.DiscardChanges;
47 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
48 import org.opendaylight.controller.netconf.confignetconfconnector.operations.get.Get;
49 import org.opendaylight.controller.netconf.confignetconfconnector.operations.getconfig.GetConfig;
50 import org.opendaylight.controller.netconf.confignetconfconnector.operations.runtimerpc.RuntimeRpc;
51 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreServiceImpl;
52 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreSnapshot;
53 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
54 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCloseSession;
55 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceSnapshot;
56 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
57 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
58 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
59 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
60 import org.opendaylight.controller.netconf.util.xml.XmlElement;
61 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
62 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity1;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity2;
65 import org.opendaylight.yangtools.yang.data.impl.codec.CodecRegistry;
66 import org.opendaylight.yangtools.yang.data.impl.codec.IdentityCodec;
67 import org.opendaylight.yangtools.yang.model.api.Module;
68 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
69 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
70 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
73 import org.w3c.dom.Document;
74 import org.w3c.dom.Element;
75 import org.w3c.dom.NodeList;
76 import org.xml.sax.SAXException;
78 import javax.management.InstanceAlreadyExistsException;
79 import javax.management.InstanceNotFoundException;
80 import javax.management.ObjectName;
81 import javax.xml.parsers.ParserConfigurationException;
82 import java.io.FileNotFoundException;
83 import java.io.IOException;
84 import java.io.InputStream;
85 import java.math.BigInteger;
86 import java.net.URISyntaxException;
87 import java.util.ArrayList;
88 import java.util.Arrays;
89 import java.util.Collection;
90 import java.util.HashSet;
91 import java.util.List;
95 import static org.junit.Assert.assertEquals;
96 import static org.junit.Assert.assertThat;
97 import static org.junit.Assert.assertTrue;
98 import static org.junit.Assert.fail;
99 import static org.mockito.Mockito.doNothing;
100 import static org.mockito.Mockito.doReturn;
101 import static org.mockito.Mockito.mock;
102 import static org.mockito.Mockito.verify;
103 import static org.mockito.Mockito.verifyNoMoreInteractions;
106 public class NetconfMappingTest extends AbstractConfigTest {
107 private static final Logger logger = LoggerFactory.getLogger(NetconfMappingTest.class);
109 private static final String INSTANCE_NAME = "instance-from-code";
110 private static final String NETCONF_SESSION_ID = "foo";
111 private NetconfTestImplModuleFactory factory;
112 private DepTestImplModuleFactory factory2;
113 private IdentityTestModuleFactory factory3;
116 YangStoreSnapshot yangStoreSnapshot;
118 NetconfOperationRouter netconfOperationRouter;
120 NetconfOperationServiceSnapshot netconfOperationServiceSnapshot;
122 private TransactionProvider transactionProvider;
125 public void setUp() throws Exception {
126 MockitoAnnotations.initMocks(this);
127 doReturn(getMbes()).when(this.yangStoreSnapshot).getModuleMXBeanEntryMap();
128 doReturn(getModules()).when(this.yangStoreSnapshot).getModules();
129 doNothing().when(netconfOperationServiceSnapshot).close();
131 this.factory = new NetconfTestImplModuleFactory();
132 this.factory2 = new DepTestImplModuleFactory();
133 this.factory3 = new IdentityTestModuleFactory();
134 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(this.factory, this.factory2,
137 transactionProvider = new TransactionProvider(this.configRegistryClient, NETCONF_SESSION_ID);
140 private ObjectName createModule(final String instanceName) throws InstanceAlreadyExistsException, InstanceNotFoundException, URISyntaxException, ValidationException, ConflictingVersionException {
141 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
143 final ObjectName on = transaction.createModule(this.factory.getImplementationName(), instanceName);
144 final NetconfTestImplModuleMXBean mxBean = transaction.newMXBeanProxy(on, NetconfTestImplModuleMXBean.class);
145 setModule(mxBean, transaction, instanceName + "_dep");
148 for (Class<? extends AbstractServiceInterface> sInterface : factory.getImplementedServiceIntefaces()) {
149 ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
150 transaction.saveServiceReference(
151 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + instanceName + "_" + i++,
155 transaction.commit();
160 public void testIdentityRefs() throws Exception {
161 edit("netconfMessages/editConfig_identities.xml");
168 protected CodecRegistry getCodecRegistry() {
169 IdentityCodec<?> idCodec = mock(IdentityCodec.class);
170 doReturn(TestIdentity1.class).when(idCodec).deserialize(TestIdentity1.QNAME);
171 doReturn(TestIdentity2.class).when(idCodec).deserialize(TestIdentity2.QNAME);
173 CodecRegistry codecReg = super.getCodecRegistry();
174 doReturn(idCodec).when(codecReg).getIdentityCodec();
179 public void testServicePersistance() throws Exception {
180 createModule(INSTANCE_NAME);
182 edit("netconfMessages/editConfig.xml");
183 Element config = getConfigCandidate();
184 assertCorrectServiceNames(config, 6, "ref_test2", "user_to_instance_from_code", "ref_dep_user",
185 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
186 "ref_from_code_to_instance-from-code_1");
189 edit("netconfMessages/editConfig_addServiceName.xml");
190 config = getConfigCandidate();
191 assertCorrectServiceNames(config, 7, "ref_test2", "user_to_instance_from_code", "ref_dep_user",
192 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
193 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another");
195 edit("netconfMessages/editConfig_addServiceNameOnTest.xml");
196 config = getConfigCandidate();
197 assertCorrectServiceNames(config, 7, "ref_test2", "user_to_instance_from_code", "ref_dep_user",
198 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
199 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another");
202 config = getConfigRunning();
203 assertCorrectRefNamesForDependencies(config);
204 assertCorrectServiceNames(config, 7, "ref_test2", "user_to_instance_from_code", "ref_dep_user",
205 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
206 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another");
208 edit("netconfMessages/editConfig_replace_default.xml");
209 config = getConfigCandidate();
210 assertCorrectServiceNames(config, 2, "ref_dep", "ref_dep2");
212 edit("netconfMessages/editConfig_remove.xml");
213 config = getConfigCandidate();
214 assertCorrectServiceNames(config, 0);
217 config = getConfigCandidate();
218 assertCorrectServiceNames(config, 0);
222 private void assertCorrectRefNamesForDependencies(Element config) {
223 NodeList modulesList = config.getElementsByTagName("modules");
224 assertEquals(1, modulesList.getLength());
226 Element modules = (Element) modulesList.item(0);
228 String trimmedModules = XmlUtil.toString(modules).replaceAll("\\s", "");
229 int defaultRefNameCount = StringUtils.countMatches(trimmedModules, "ref_dep2");
230 int userRefNameCount = StringUtils.countMatches(trimmedModules, "ref_dep_user_two");
232 assertEquals(0, defaultRefNameCount);
233 assertEquals(2, userRefNameCount);
236 private void assertCorrectServiceNames(Element configCandidate, int servicesSize, String... refNames) {
237 NodeList elements = configCandidate.getElementsByTagName("provider");
238 assertEquals(servicesSize, elements.getLength());
240 NodeList servicesList = configCandidate.getElementsByTagName("services");
241 assertEquals(1, servicesList.getLength());
243 Element services = (Element) servicesList.item(0);
244 String trimmedServices = XmlUtil.toString(services).replaceAll("\\s", "");
246 for (String s : refNames) {
247 assertThat(trimmedServices, JUnitMatchers.containsString(s));
252 public void testConfigNetconfUnionTypes() throws Exception {
254 createModule(INSTANCE_NAME);
256 edit("netconfMessages/editConfig.xml");
258 Element response = getConfigRunning();
259 String trimmedResponse = XmlUtil.toString(response).replaceAll("\\s", "");
260 assertContainsString(trimmedResponse, "<ipxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">0:0:0:0:0:0:0:1</ip>");
261 assertContainsString(trimmedResponse, "<union-test-attrxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">456</union-test-attr>");
264 edit("netconfMessages/editConfig_setUnions.xml");
266 response = getConfigRunning();
268 trimmedResponse = XmlUtil.toString(response).replaceAll("\\s", "");
269 assertContainsString(trimmedResponse, "<ipxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">127.1.2.3</ip>");
270 assertContainsString(trimmedResponse, "<union-test-attrxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">randomStringForUnion</union-test-attr>");
275 public void testConfigNetconf() throws Exception {
277 createModule(INSTANCE_NAME);
279 edit("netconfMessages/editConfig.xml");
280 Element configCandidate = getConfigCandidate();
281 checkBinaryLeafEdited(configCandidate);
284 // default-operation:none, should not affect binary leaf
285 edit("netconfMessages/editConfig_none.xml");
286 checkBinaryLeafEdited(getConfigCandidate());
290 Element response = getConfigRunning();
292 checkBinaryLeafEdited(response);
293 checkTypeConfigAttribute(response);
294 checkTypedefs(response);
295 checkTestingDeps(response);
297 checkBigDecimal(response);
299 edit("netconfMessages/editConfig_remove.xml");
302 response = getConfigCandidate();
303 final String responseFromCandidate = XmlUtil.toString(response).replaceAll("\\s+", "");
304 response = getConfigRunning();
305 final String responseFromRunning = XmlUtil.toString(response).replaceAll("\\s+", "");
306 assertEquals(responseFromCandidate, responseFromRunning);
308 final String expectedResult = XmlFileLoader.fileToString("netconfMessages/editConfig_expectedResult.xml")
309 .replaceAll("\\s+", "");
311 assertEquals(expectedResult, responseFromRunning);
312 assertEquals(expectedResult, responseFromCandidate);
314 edit("netconfMessages/editConfig_none.xml");
316 verify(netconfOperationServiceSnapshot).close();
317 verifyNoMoreInteractions(netconfOperationRouter);
318 verifyNoMoreInteractions(netconfOperationServiceSnapshot);
321 private void checkBigDecimal(Element response) {
322 String responseTrimmed = XmlUtil.toString(response).replaceAll("\\s", "");
324 assertContainsString(responseTrimmed, "<sleep-factorxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.58</sleep-factor>");
326 assertContainsString(responseTrimmed, "<sleep-factorxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.00</sleep-factor>");
330 private void closeSession() throws NetconfDocumentedException, ParserConfigurationException, SAXException,
332 DefaultCloseSession closeOp = new DefaultCloseSession(NETCONF_SESSION_ID, netconfOperationServiceSnapshot);
333 executeOp(closeOp, "netconfMessages/closeSession.xml");
336 private void edit(String resource) throws ParserConfigurationException, SAXException, IOException,
337 NetconfDocumentedException {
338 EditConfig editOp = new EditConfig(yangStoreSnapshot, transactionProvider, configRegistryClient,
340 executeOp(editOp, resource);
343 private void commit() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
344 Commit commitOp = new Commit(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
345 executeOp(commitOp, "netconfMessages/commit.xml");
348 private Element getConfigCandidate() throws ParserConfigurationException, SAXException, IOException,
349 NetconfDocumentedException {
350 GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
351 configRegistryClient, NETCONF_SESSION_ID);
352 return executeOp(getConfigOp, "netconfMessages/getConfig_candidate.xml");
355 private Element getConfigRunning() throws ParserConfigurationException, SAXException, IOException,
356 NetconfDocumentedException {
357 GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
358 configRegistryClient, NETCONF_SESSION_ID);
359 return executeOp(getConfigOp, "netconfMessages/getConfig.xml");
362 @Ignore("second edit message corrupted")
363 @Test(expected = NetconfDocumentedException.class)
364 public void testConfigNetconfReplaceDefaultEx() throws Exception {
366 createModule(INSTANCE_NAME);
368 edit("netconfMessages/editConfig.xml");
369 edit("netconfMessages/editConfig_replace_default_ex.xml");
373 public void testConfigNetconfReplaceDefault() throws Exception {
375 createModule(INSTANCE_NAME);
377 edit("netconfMessages/editConfig.xml");
379 Element response = getConfigRunning();
380 final int allInstances = response.getElementsByTagName("module").getLength();
382 edit("netconfMessages/editConfig_replace_default.xml");
385 response = getConfigRunning();
387 final int afterReplace = response.getElementsByTagName("module").getLength();
388 assertEquals(4, allInstances);
389 assertEquals(2, afterReplace);
392 @Test(expected = NetconfDocumentedException.class)
393 public void testSameAttrDifferentNamespaces() throws Exception {
395 edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespaces.xml");
396 } catch (NetconfDocumentedException e) {
397 String message = e.getMessage();
398 assertContainsString(message, "Element simple-long-2 present multiple times with different namespaces");
399 assertContainsString(message, "urn:opendaylight:params:xml:ns:yang:controller:test:impl");
400 assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
405 @Test(expected = NetconfDocumentedException.class)
406 public void testDifferentNamespaceInTO() throws Exception {
408 edit("netconfMessages/namespaces/editConfig_differentNamespaceTO.xml");
409 } catch (NetconfDocumentedException e) {
410 String message = e.getMessage();
411 assertContainsString(message, "Unrecognised elements");
412 assertContainsString(message, "simple-int2");
413 assertContainsString(message, "dto_d");
418 @Test(expected = NetconfDocumentedException.class)
419 public void testSameAttrDifferentNamespacesList() throws Exception {
421 edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespacesList.xml");
422 } catch (NetconfDocumentedException e) {
423 String message = e.getMessage();
424 assertContainsString(message, "Element binaryLeaf present multiple times with different namespaces");
425 assertContainsString(message, "urn:opendaylight:params:xml:ns:yang:controller:test:impl");
426 assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
432 public void testTypeNameConfigAttributeMatching() throws Exception {
433 edit("netconfMessages/editConfig.xml");
435 edit("netconfMessages/namespaces/editConfig_typeNameConfigAttributeMatching.xml");
438 Element response = getConfigRunning();
439 checkTypeConfigAttribute(response);
442 // TODO add <modules operation="replace"> functionality
443 @Test(expected = NetconfDocumentedException.class)
444 public void testConfigNetconfReplaceModuleEx() throws Exception {
446 createModule(INSTANCE_NAME);
448 edit("netconfMessages/editConfig.xml");
449 edit("netconfMessages/editConfig_replace_module_ex.xml");
453 public void testUnrecognisedConfigElements() throws Exception {
455 String format = "netconfMessages/unrecognised/editConfig_unrecognised%d.xml";
456 final int TESTS_COUNT = 8;
458 for (int i = 0; i < TESTS_COUNT; i++) {
459 String file = String.format(format, i + 1);
462 } catch (NetconfDocumentedException e) {
463 assertContainsString(e.getMessage(), "Unrecognised elements");
464 assertContainsString(e.getMessage(), "unknownAttribute");
467 fail("Unrecognised test should throw exception " + file);
474 public void testConfigNetconfReplaceModule() throws Exception {
476 createModule(INSTANCE_NAME);
478 edit("netconfMessages/editConfig.xml");
480 Element response = getConfigRunning();
481 final int allInstances = response.getElementsByTagName("instance").getLength();
483 edit("netconfMessages/editConfig_replace_module.xml");
486 response = getConfigRunning();
487 final int afterReplace = response.getElementsByTagName("instance").getLength();
489 assertEquals(4 + 4 /* Instances from services */, allInstances);
490 assertEquals(3 + 3, afterReplace);
493 @Test(expected = NetconfDocumentedException.class)
494 public void testEx() throws Exception {
499 @Test(expected = NetconfDocumentedException.class)
500 public void testEx2() throws Exception {
504 private void discard() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
505 DiscardChanges discardOp = new DiscardChanges(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
506 executeOp(discardOp, "netconfMessages/discardChanges.xml");
509 private void checkBinaryLeafEdited(final Element response) {
510 String responseTrimmed = XmlUtil.toString(response).replaceAll("\\s", "");
511 String substring = "<binaryLeafxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">YmluYXJ5</binaryLeaf>";
512 assertContainsString(responseTrimmed, substring);
513 substring = "<binaryLeafxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ZGVmYXVsdEJpbg==</binaryLeaf>";
514 assertContainsString(responseTrimmed, substring);
517 private void checkTypedefs(final Element response) {
518 String responseTrimmed = XmlUtil.toString(response).replaceAll("\\s", "");
520 String substring = "<extendedxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">10</extended>";
521 assertContainsString(responseTrimmed, substring);
523 assertContainsString(responseTrimmed,
524 "<extendedxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">1</extended>");
526 assertContainsString(responseTrimmed,
527 "<extended-twicexmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">20</extended-twice>");
529 assertContainsString(responseTrimmed,
530 "<extended-twicexmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2</extended-twice>");
532 assertContainsString(responseTrimmed,
533 "<extended-enumxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">TWO</extended-enum>");
535 assertContainsString(responseTrimmed,
536 "<extended-enumxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ONE</extended-enum>");
539 private void assertContainsString(String string, String substring) {
540 assertThat(string, JUnitMatchers.containsString(substring));
543 private void checkEnum(final Element response) {
544 XmlElement modulesElement = XmlElement.fromDomElement(response).getOnlyChildElement("data")
545 .getOnlyChildElement("modules");
547 String enumName = "extended-enum";
548 String enumContent = "TWO";
550 for (XmlElement moduleElement : modulesElement.getChildElements("module")) {
551 String name = moduleElement.getOnlyChildElement("name").getTextContent();
552 if(name.equals(INSTANCE_NAME)) {
553 XmlElement enumAttr = moduleElement.getOnlyChildElement(enumName);
554 assertEquals(enumContent, enumAttr.getTextContent());
560 fail("Enum attribute " + enumName + ":" + enumContent + " not present in " + XmlUtil.toString(response));
563 private void checkTestingDeps(Element response) {
564 int testingDepsSize = response.getElementsByTagName("testing-deps").getLength();
565 assertEquals(2, testingDepsSize);
568 private void checkTypeConfigAttribute(Element response) {
570 XmlElement modulesElement = XmlElement.fromDomElement(response).getOnlyChildElement("data")
571 .getOnlyChildElement("modules");
573 List<String> expectedValues = Lists.newArrayList("default-string", "configAttributeType");
574 Set<String> configAttributeType = Sets.newHashSet();
576 for (XmlElement moduleElement : modulesElement.getChildElements("module")) {
577 for (XmlElement type : moduleElement.getChildElements("type")) {
578 if (type.getAttribute(XmlUtil.XMLNS_ATTRIBUTE_KEY).equals("") == false) {
579 configAttributeType.add(type.getTextContent());
584 for (String expectedValue : expectedValues) {
585 assertTrue(configAttributeType.contains(expectedValue));
589 private Map<String, Map<String, ModuleMXBeanEntry>> getMbes() throws Exception {
590 final List<InputStream> yangDependencies = getYangs();
592 final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries = Maps.newHashMap();
594 YangParserImpl yangParser = new YangParserImpl();
595 final SchemaContext schemaContext = yangParser.resolveSchemaContext(new HashSet<>(yangParser.parseYangModelsFromStreamsMapped(yangDependencies).values()));
596 YangStoreServiceImpl yangStoreService = new YangStoreServiceImpl(new SchemaContextProvider() {
598 public SchemaContext getSchemaContext() {
599 return schemaContext ;
602 mBeanEntries.putAll(yangStoreService.getYangStoreSnapshot().getModuleMXBeanEntryMap());
607 private Set<org.opendaylight.yangtools.yang.model.api.Module> getModules() throws Exception {
608 SchemaContext resolveSchemaContext = getSchemaContext();
609 return resolveSchemaContext.getModules();
612 private SchemaContext getSchemaContext() throws Exception {
613 final List<InputStream> yangDependencies = getYangs();
614 YangParserImpl parser = new YangParserImpl();
616 Set<Module> allYangModules = parser.parseYangModelsFromStreams(yangDependencies);
618 return parser.resolveSchemaContext(Sets
619 .newHashSet(allYangModules));
623 public void testConfigNetconfRuntime() throws Exception {
625 createModule(INSTANCE_NAME);
627 edit("netconfMessages/editConfig.xml");
628 checkBinaryLeafEdited(getConfigCandidate());
632 Element response = get();
634 assertEquals(2/*With runtime beans*/ + 2 /*Without runtime beans*/, getElementsSize(response, "module"));
636 assertEquals(2, getElementsSize(response, "asdf"));
637 // data from running config
638 assertEquals(2, getElementsSize(response, "simple-short"));
640 assertEquals(8, getElementsSize(response, "inner-running-data"));
641 assertEquals(8, getElementsSize(response, "deep2"));
642 assertEquals(8 * 4, getElementsSize(response, "inner-inner-running-data"));
643 assertEquals(8 * 4, getElementsSize(response, "deep3"));
644 assertEquals(8 * 4 * 2, getElementsSize(response, "list-of-strings"));
645 assertEquals(8, getElementsSize(response, "inner-running-data-additional"));
646 assertEquals(8, getElementsSize(response, "deep4"));
649 RuntimeRpc netconf = new RuntimeRpc(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID);
651 response = executeOp(netconf, "netconfMessages/rpc.xml");
652 assertContainsString(XmlUtil.toString(response), "testarg1".toUpperCase());
654 response = executeOp(netconf, "netconfMessages/rpcInner.xml");
655 assertContainsString(XmlUtil.toString(response), "ok");
657 response = executeOp(netconf, "netconfMessages/rpcInnerInner.xml");
658 assertContainsString(XmlUtil.toString(response), "true");
660 response = executeOp(netconf, "netconfMessages/rpcInnerInner_complex_output.xml");
661 assertContainsString(XmlUtil.toString(response), "1");
662 assertContainsString(XmlUtil.toString(response), "2");
665 private Element get() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
666 Get getOp = new Get(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID, transactionProvider);
667 return executeOp(getOp, "netconfMessages/get.xml");
670 private int getElementsSize(Element response, String elementName) {
671 return response.getElementsByTagName(elementName).getLength();
674 private Element executeOp(final NetconfOperation op, final String filename) throws ParserConfigurationException,
675 SAXException, IOException, NetconfDocumentedException {
677 final Document request = XmlFileLoader.xmlFileToDocument(filename);
679 logger.debug("Executing netconf operation\n{}", XmlUtil.toString(request));
680 HandlingPriority priority = op.canHandle(request);
682 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
684 final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
685 logger.debug("Got response\n{}", XmlUtil.toString(response));
686 return response.getDocumentElement();
689 private List<InputStream> getYangs() throws FileNotFoundException {
690 List<String> paths = Arrays.asList("/META-INF/yang/config.yang", "/META-INF/yang/rpc-context.yang",
691 "/META-INF/yang/config-test.yang", "/META-INF/yang/config-test-impl.yang", "/META-INF/yang/test-types.yang",
692 "/META-INF/yang/ietf-inet-types.yang");
693 final Collection<InputStream> yangDependencies = new ArrayList<>();
694 for (String path : paths) {
695 final InputStream is = Preconditions
696 .checkNotNull(getClass().getResourceAsStream(path), path + " not found");
697 yangDependencies.add(is);
699 return Lists.newArrayList(yangDependencies);
702 private void setModule(final NetconfTestImplModuleMXBean mxBean, final ConfigTransactionJMXClient transaction, String depName)
703 throws InstanceAlreadyExistsException, InstanceNotFoundException {
704 mxBean.setSimpleInt((long) 44);
705 mxBean.setBinaryLeaf(new byte[] { 8, 7, 9 });
706 final DtoD dtob = getDtoD();
707 mxBean.setDtoD(dtob);
709 final DtoC dtoa = getDtoC();
710 mxBean.setDtoC(dtoa);
711 mxBean.setSimpleBoolean(false);
713 final Peers p1 = new Peers();
716 p1.setSimpleInt3(456);
717 final Peers p2 = new Peers();
719 p2.setPort("port23");
720 p2.setSimpleInt3(456);
721 mxBean.setPeers(Lists.<Peers> newArrayList(p1, p2));
723 mxBean.setSimpleLong(454545L);
724 mxBean.setSimpleLong2(44L);
725 mxBean.setSimpleBigInteger(BigInteger.valueOf(999L));
726 mxBean.setSimpleByte(new Byte((byte) 4));
727 mxBean.setSimpleShort(new Short((short) 4));
728 mxBean.setSimpleTest(545);
730 mxBean.setComplexList(Lists.<ComplexList> newArrayList());
731 mxBean.setSimpleList(Lists.<Integer> newArrayList());
733 final ObjectName testingDepOn = transaction.createModule(this.factory2.getImplementationName(), depName);
735 for (Class<? extends AbstractServiceInterface> sInterface : factory2.getImplementedServiceIntefaces()) {
736 ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
737 transaction.saveServiceReference(
738 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + depName + "_" + i++,
742 mxBean.setTestingDep(testingDepOn);
745 private static DtoD getDtoD() {
746 final DtoD dtob = new DtoD();
747 dtob.setSimpleInt1((long) 444);
748 dtob.setSimpleInt2((long) 4444);
749 dtob.setSimpleInt3(454);
750 final ComplexDtoBInner dtobInner = new ComplexDtoBInner();
751 final Deep deep = new Deep();
752 deep.setSimpleInt3(4);
753 dtobInner.setDeep(deep);
754 dtobInner.setSimpleInt3(44);
755 dtobInner.setSimpleList(Lists.newArrayList(4));
756 dtob.setComplexDtoBInner(Lists.newArrayList(dtobInner));
757 dtob.setSimpleList(Lists.newArrayList(4));
761 private static DtoC getDtoC() {
762 final DtoC dtoa = new DtoC();
763 // dtoa.setSimpleArg((long) 55);
764 final DtoAInner dtoAInner = new DtoAInner();
765 final DtoAInnerInner dtoAInnerInner = new DtoAInnerInner();
766 dtoAInnerInner.setSimpleArg(456L);
767 dtoAInner.setDtoAInnerInner(dtoAInnerInner);
768 dtoAInner.setSimpleArg(44L);
769 dtoa.setDtoAInner(dtoAInner);