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.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertThat;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.verifyNoMoreInteractions;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.math.BigInteger;
25 import java.net.URISyntaxException;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Collection;
29 import java.util.HashSet;
30 import java.util.List;
34 import javax.management.InstanceAlreadyExistsException;
35 import javax.management.InstanceNotFoundException;
36 import javax.management.ObjectName;
37 import javax.xml.parsers.ParserConfigurationException;
39 import org.apache.commons.lang3.StringUtils;
40 import org.junit.Before;
41 import org.junit.Ignore;
42 import org.junit.Test;
43 import org.junit.matchers.JUnitMatchers;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.opendaylight.controller.config.api.ConflictingVersionException;
47 import org.opendaylight.controller.config.api.ValidationException;
48 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
49 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
50 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
51 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
52 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
53 import org.opendaylight.controller.config.yang.test.impl.ComplexDtoBInner;
54 import org.opendaylight.controller.config.yang.test.impl.ComplexList;
55 import org.opendaylight.controller.config.yang.test.impl.Deep;
56 import org.opendaylight.controller.config.yang.test.impl.DepTestImplModuleFactory;
57 import org.opendaylight.controller.config.yang.test.impl.DtoAInner;
58 import org.opendaylight.controller.config.yang.test.impl.DtoAInnerInner;
59 import org.opendaylight.controller.config.yang.test.impl.DtoC;
60 import org.opendaylight.controller.config.yang.test.impl.DtoD;
61 import org.opendaylight.controller.config.yang.test.impl.IdentityTestModuleFactory;
62 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleFactory;
63 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleMXBean;
64 import org.opendaylight.controller.config.yang.test.impl.Peers;
65 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
66 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
67 import org.opendaylight.controller.netconf.api.NetconfOperationRouter;
68 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Commit;
69 import org.opendaylight.controller.netconf.confignetconfconnector.operations.DiscardChanges;
70 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
71 import org.opendaylight.controller.netconf.confignetconfconnector.operations.get.Get;
72 import org.opendaylight.controller.netconf.confignetconfconnector.operations.getconfig.GetConfig;
73 import org.opendaylight.controller.netconf.confignetconfconnector.operations.runtimerpc.RuntimeRpc;
74 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreServiceImpl;
75 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreSnapshot;
76 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
77 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCloseSession;
78 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceSnapshotImpl;
79 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
80 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
81 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
82 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
83 import org.opendaylight.controller.netconf.util.xml.XmlElement;
84 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
85 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity1;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity2;
88 import org.opendaylight.yangtools.yang.data.impl.codec.CodecRegistry;
89 import org.opendaylight.yangtools.yang.data.impl.codec.IdentityCodec;
90 import org.opendaylight.yangtools.yang.model.api.Module;
91 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
92 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
93 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
94 import org.slf4j.Logger;
95 import org.slf4j.LoggerFactory;
96 import org.w3c.dom.Document;
97 import org.w3c.dom.Element;
98 import org.w3c.dom.NodeList;
99 import org.xml.sax.SAXException;
101 import com.google.common.base.Optional;
102 import com.google.common.base.Preconditions;
103 import com.google.common.collect.Lists;
104 import com.google.common.collect.Maps;
105 import com.google.common.collect.Sets;
108 public class NetconfMappingTest extends AbstractConfigTest {
109 private static final Logger logger = LoggerFactory.getLogger(NetconfMappingTest.class);
111 private static final String INSTANCE_NAME = "instance-from-code";
112 private static final String NETCONF_SESSION_ID = "foo";
113 private NetconfTestImplModuleFactory factory;
114 private DepTestImplModuleFactory factory2;
115 private IdentityTestModuleFactory factory3;
118 YangStoreSnapshot yangStoreSnapshot;
120 NetconfOperationRouter netconfOperationRouter;
122 NetconfOperationServiceSnapshotImpl netconfOperationServiceSnapshot;
124 private TransactionProvider transactionProvider;
127 public void setUp() throws Exception {
128 MockitoAnnotations.initMocks(this);
129 doReturn(getMbes()).when(this.yangStoreSnapshot).getModuleMXBeanEntryMap();
130 doReturn(getModules()).when(this.yangStoreSnapshot).getModules();
131 doNothing().when(netconfOperationServiceSnapshot).close();
133 this.factory = new NetconfTestImplModuleFactory();
134 this.factory2 = new DepTestImplModuleFactory();
135 this.factory3 = new IdentityTestModuleFactory();
136 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(this.factory, this.factory2,
139 transactionProvider = new TransactionProvider(this.configRegistryClient, NETCONF_SESSION_ID);
142 private ObjectName createModule(final String instanceName) throws InstanceAlreadyExistsException, InstanceNotFoundException, URISyntaxException, ValidationException, ConflictingVersionException {
143 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
145 final ObjectName on = transaction.createModule(this.factory.getImplementationName(), instanceName);
146 final NetconfTestImplModuleMXBean mxBean = transaction.newMXBeanProxy(on, NetconfTestImplModuleMXBean.class);
147 setModule(mxBean, transaction, instanceName + "_dep");
150 for (Class<? extends AbstractServiceInterface> sInterface : factory.getImplementedServiceIntefaces()) {
151 ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
152 transaction.saveServiceReference(
153 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + instanceName + "_" + i++,
157 transaction.commit();
162 public void testIdentityRefs() throws Exception {
163 edit("netconfMessages/editConfig_identities.xml");
170 protected CodecRegistry getCodecRegistry() {
171 IdentityCodec<?> idCodec = mock(IdentityCodec.class);
172 doReturn(TestIdentity1.class).when(idCodec).deserialize(TestIdentity1.QNAME);
173 doReturn(TestIdentity2.class).when(idCodec).deserialize(TestIdentity2.QNAME);
175 CodecRegistry codecReg = super.getCodecRegistry();
176 doReturn(idCodec).when(codecReg).getIdentityCodec();
181 public void testServicePersistance() throws Exception {
182 createModule(INSTANCE_NAME);
184 edit("netconfMessages/editConfig.xml");
185 Element config = getConfigCandidate();
186 assertCorrectServiceNames(config, 6, "ref_test2", "user_to_instance_from_code", "ref_dep_user",
187 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
188 "ref_from_code_to_instance-from-code_1");
191 edit("netconfMessages/editConfig_addServiceName.xml");
192 config = getConfigCandidate();
193 assertCorrectServiceNames(config, 7, "ref_test2", "user_to_instance_from_code", "ref_dep_user",
194 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
195 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another");
197 edit("netconfMessages/editConfig_addServiceNameOnTest.xml");
198 config = getConfigCandidate();
199 assertCorrectServiceNames(config, 7, "ref_test2", "user_to_instance_from_code", "ref_dep_user",
200 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
201 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another");
204 config = getConfigRunning();
205 assertCorrectRefNamesForDependencies(config);
206 assertCorrectServiceNames(config, 7, "ref_test2", "user_to_instance_from_code", "ref_dep_user",
207 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
208 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another");
210 edit("netconfMessages/editConfig_replace_default.xml");
211 config = getConfigCandidate();
212 assertCorrectServiceNames(config, 2, "ref_dep", "ref_dep2");
214 edit("netconfMessages/editConfig_remove.xml");
215 config = getConfigCandidate();
216 assertCorrectServiceNames(config, 0);
219 config = getConfigCandidate();
220 assertCorrectServiceNames(config, 0);
224 private void assertCorrectRefNamesForDependencies(Element config) {
225 NodeList modulesList = config.getElementsByTagName("modules");
226 assertEquals(1, modulesList.getLength());
228 Element modules = (Element) modulesList.item(0);
230 String trimmedModules = XmlUtil.toString(modules).replaceAll("\\s", "");
231 int defaultRefNameCount = StringUtils.countMatches(trimmedModules, "ref_dep2");
232 int userRefNameCount = StringUtils.countMatches(trimmedModules, "ref_dep_user_two");
234 assertEquals(0, defaultRefNameCount);
235 assertEquals(2, userRefNameCount);
238 private void assertCorrectServiceNames(Element configCandidate, int servicesSize, String... refNames) {
239 NodeList elements = configCandidate.getElementsByTagName("provider");
240 assertEquals(servicesSize, elements.getLength());
242 NodeList servicesList = configCandidate.getElementsByTagName("services");
243 assertEquals(1, servicesList.getLength());
245 Element services = (Element) servicesList.item(0);
246 String trimmedServices = XmlUtil.toString(services).replaceAll("\\s", "");
248 for (String s : refNames) {
249 assertThat(trimmedServices, JUnitMatchers.containsString(s));
254 public void testConfigNetconfUnionTypes() throws Exception {
256 createModule(INSTANCE_NAME);
258 edit("netconfMessages/editConfig.xml");
260 Element response = getConfigRunning();
261 String trimmedResponse = XmlUtil.toString(response).replaceAll("\\s", "");
262 assertContainsString(trimmedResponse, "<ipxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">0:0:0:0:0:0:0:1</ip>");
263 assertContainsString(trimmedResponse, "<union-test-attrxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">456</union-test-attr>");
266 edit("netconfMessages/editConfig_setUnions.xml");
268 response = getConfigRunning();
270 trimmedResponse = XmlUtil.toString(response).replaceAll("\\s", "");
271 assertContainsString(trimmedResponse, "<ipxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">127.1.2.3</ip>");
272 assertContainsString(trimmedResponse, "<union-test-attrxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">randomStringForUnion</union-test-attr>");
277 public void testConfigNetconf() throws Exception {
279 createModule(INSTANCE_NAME);
281 edit("netconfMessages/editConfig.xml");
282 Element configCandidate = getConfigCandidate();
283 checkBinaryLeafEdited(configCandidate);
286 // default-operation:none, should not affect binary leaf
287 edit("netconfMessages/editConfig_none.xml");
288 checkBinaryLeafEdited(getConfigCandidate());
292 Element response = getConfigRunning();
294 checkBinaryLeafEdited(response);
295 checkTypeConfigAttribute(response);
296 checkTypedefs(response);
297 checkTestingDeps(response);
299 checkBigDecimal(response);
301 edit("netconfMessages/editConfig_remove.xml");
304 response = getConfigCandidate();
305 final String responseFromCandidate = XmlUtil.toString(response).replaceAll("\\s+", "");
306 response = getConfigRunning();
307 final String responseFromRunning = XmlUtil.toString(response).replaceAll("\\s+", "");
308 assertEquals(responseFromCandidate, responseFromRunning);
310 final String expectedResult = XmlFileLoader.fileToString("netconfMessages/editConfig_expectedResult.xml")
311 .replaceAll("\\s+", "");
313 assertEquals(expectedResult, responseFromRunning);
314 assertEquals(expectedResult, responseFromCandidate);
316 edit("netconfMessages/editConfig_none.xml");
318 verify(netconfOperationServiceSnapshot).close();
319 verifyNoMoreInteractions(netconfOperationRouter);
320 verifyNoMoreInteractions(netconfOperationServiceSnapshot);
323 private void checkBigDecimal(Element response) {
324 String responseTrimmed = XmlUtil.toString(response).replaceAll("\\s", "");
326 assertContainsString(responseTrimmed, "<sleep-factorxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.58</sleep-factor>");
328 assertContainsString(responseTrimmed, "<sleep-factorxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.00</sleep-factor>");
332 private void closeSession() throws NetconfDocumentedException, ParserConfigurationException, SAXException,
334 DefaultCloseSession closeOp = new DefaultCloseSession(NETCONF_SESSION_ID, netconfOperationServiceSnapshot);
335 executeOp(closeOp, "netconfMessages/closeSession.xml");
338 private void edit(String resource) throws ParserConfigurationException, SAXException, IOException,
339 NetconfDocumentedException {
340 EditConfig editOp = new EditConfig(yangStoreSnapshot, transactionProvider, configRegistryClient,
342 executeOp(editOp, resource);
345 private void commit() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
346 Commit commitOp = new Commit(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
347 executeOp(commitOp, "netconfMessages/commit.xml");
350 private Element getConfigCandidate() throws ParserConfigurationException, SAXException, IOException,
351 NetconfDocumentedException {
352 GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
353 configRegistryClient, NETCONF_SESSION_ID);
354 return executeOp(getConfigOp, "netconfMessages/getConfig_candidate.xml");
357 private Element getConfigRunning() throws ParserConfigurationException, SAXException, IOException,
358 NetconfDocumentedException {
359 GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
360 configRegistryClient, NETCONF_SESSION_ID);
361 return executeOp(getConfigOp, "netconfMessages/getConfig.xml");
364 @Ignore("second edit message corrupted")
365 @Test(expected = NetconfDocumentedException.class)
366 public void testConfigNetconfReplaceDefaultEx() throws Exception {
368 createModule(INSTANCE_NAME);
370 edit("netconfMessages/editConfig.xml");
371 edit("netconfMessages/editConfig_replace_default_ex.xml");
375 public void testConfigNetconfReplaceDefault() throws Exception {
377 createModule(INSTANCE_NAME);
379 edit("netconfMessages/editConfig.xml");
381 Element response = getConfigRunning();
382 final int allInstances = response.getElementsByTagName("module").getLength();
384 edit("netconfMessages/editConfig_replace_default.xml");
387 response = getConfigRunning();
389 final int afterReplace = response.getElementsByTagName("module").getLength();
390 assertEquals(4, allInstances);
391 assertEquals(2, afterReplace);
394 @Test(expected = NetconfDocumentedException.class)
395 public void testSameAttrDifferentNamespaces() throws Exception {
397 edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespaces.xml");
398 } catch (NetconfDocumentedException e) {
399 String message = e.getMessage();
400 assertContainsString(message, "Element simple-long-2 present multiple times with different namespaces");
401 assertContainsString(message, "urn:opendaylight:params:xml:ns:yang:controller:test:impl");
402 assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
407 @Test(expected = NetconfDocumentedException.class)
408 public void testDifferentNamespaceInTO() throws Exception {
410 edit("netconfMessages/namespaces/editConfig_differentNamespaceTO.xml");
411 } catch (NetconfDocumentedException e) {
412 String message = e.getMessage();
413 assertContainsString(message, "Unrecognised elements");
414 assertContainsString(message, "simple-int2");
415 assertContainsString(message, "dto_d");
420 @Test(expected = NetconfDocumentedException.class)
421 public void testSameAttrDifferentNamespacesList() throws Exception {
423 edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespacesList.xml");
424 } catch (NetconfDocumentedException e) {
425 String message = e.getMessage();
426 assertContainsString(message, "Element binaryLeaf present multiple times with different namespaces");
427 assertContainsString(message, "urn:opendaylight:params:xml:ns:yang:controller:test:impl");
428 assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
434 public void testTypeNameConfigAttributeMatching() throws Exception {
435 edit("netconfMessages/editConfig.xml");
437 edit("netconfMessages/namespaces/editConfig_typeNameConfigAttributeMatching.xml");
440 Element response = getConfigRunning();
441 checkTypeConfigAttribute(response);
444 // TODO add <modules operation="replace"> functionality
445 @Test(expected = NetconfDocumentedException.class)
446 public void testConfigNetconfReplaceModuleEx() throws Exception {
448 createModule(INSTANCE_NAME);
450 edit("netconfMessages/editConfig.xml");
451 edit("netconfMessages/editConfig_replace_module_ex.xml");
455 public void testUnrecognisedConfigElements() throws Exception {
457 String format = "netconfMessages/unrecognised/editConfig_unrecognised%d.xml";
458 final int TESTS_COUNT = 8;
460 for (int i = 0; i < TESTS_COUNT; i++) {
461 String file = String.format(format, i + 1);
464 } catch (NetconfDocumentedException e) {
465 assertContainsString(e.getMessage(), "Unrecognised elements");
466 assertContainsString(e.getMessage(), "unknownAttribute");
469 fail("Unrecognised test should throw exception " + file);
476 public void testConfigNetconfReplaceModule() throws Exception {
478 createModule(INSTANCE_NAME);
480 edit("netconfMessages/editConfig.xml");
482 Element response = getConfigRunning();
483 final int allInstances = response.getElementsByTagName("instance").getLength();
485 edit("netconfMessages/editConfig_replace_module.xml");
488 response = getConfigRunning();
489 final int afterReplace = response.getElementsByTagName("instance").getLength();
491 assertEquals(4 + 4 /* Instances from services */, allInstances);
492 assertEquals(3 + 3, afterReplace);
495 @Test(expected = NetconfDocumentedException.class)
496 public void testEx() throws Exception {
501 @Test(expected = NetconfDocumentedException.class)
502 public void testEx2() throws Exception {
506 private void discard() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
507 DiscardChanges discardOp = new DiscardChanges(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
508 executeOp(discardOp, "netconfMessages/discardChanges.xml");
511 private void checkBinaryLeafEdited(final Element response) {
512 String responseTrimmed = XmlUtil.toString(response).replaceAll("\\s", "");
513 String substring = "<binaryLeafxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">YmluYXJ5</binaryLeaf>";
514 assertContainsString(responseTrimmed, substring);
515 substring = "<binaryLeafxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ZGVmYXVsdEJpbg==</binaryLeaf>";
516 assertContainsString(responseTrimmed, substring);
519 private void checkTypedefs(final Element response) {
520 String responseTrimmed = XmlUtil.toString(response).replaceAll("\\s", "");
522 String substring = "<extendedxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">10</extended>";
523 assertContainsString(responseTrimmed, substring);
525 assertContainsString(responseTrimmed,
526 "<extendedxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">1</extended>");
528 assertContainsString(responseTrimmed,
529 "<extended-twicexmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">20</extended-twice>");
531 assertContainsString(responseTrimmed,
532 "<extended-twicexmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2</extended-twice>");
534 assertContainsString(responseTrimmed,
535 "<extended-enumxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">TWO</extended-enum>");
537 assertContainsString(responseTrimmed,
538 "<extended-enumxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ONE</extended-enum>");
541 private void assertContainsString(String string, String substring) {
542 assertThat(string, JUnitMatchers.containsString(substring));
545 private void checkEnum(final Element response) {
546 XmlElement modulesElement = XmlElement.fromDomElement(response).getOnlyChildElement("data")
547 .getOnlyChildElement("modules");
549 String enumName = "extended-enum";
550 String enumContent = "TWO";
552 for (XmlElement moduleElement : modulesElement.getChildElements("module")) {
553 String name = moduleElement.getOnlyChildElement("prefix:name").getTextContent();
554 if(name.equals(INSTANCE_NAME)) {
555 XmlElement enumAttr = moduleElement.getOnlyChildElement(enumName);
556 assertEquals(enumContent, enumAttr.getTextContent());
562 fail("Enum attribute " + enumName + ":" + enumContent + " not present in " + XmlUtil.toString(response));
565 private void checkTestingDeps(Element response) {
566 int testingDepsSize = response.getElementsByTagName("testing-deps").getLength();
567 assertEquals(2, testingDepsSize);
570 private void checkTypeConfigAttribute(Element response) {
572 XmlElement modulesElement = XmlElement.fromDomElement(response).getOnlyChildElement("data")
573 .getOnlyChildElement("modules");
575 List<String> expectedValues = Lists.newArrayList("default-string", "configAttributeType");
576 Set<String> configAttributeType = Sets.newHashSet();
578 for (XmlElement moduleElement : modulesElement.getChildElements("module")) {
579 for (XmlElement type : moduleElement.getChildElements("type")) {
580 if (type.getAttribute(XmlUtil.XMLNS_ATTRIBUTE_KEY).equals("") == false) {
581 configAttributeType.add(type.getTextContent());
586 for (String expectedValue : expectedValues) {
587 assertTrue(configAttributeType.contains(expectedValue));
591 private Map<String, Map<String, ModuleMXBeanEntry>> getMbes() throws Exception {
592 final List<InputStream> yangDependencies = getYangs();
594 final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries = Maps.newHashMap();
596 YangParserImpl yangParser = new YangParserImpl();
597 final SchemaContext schemaContext = yangParser.resolveSchemaContext(new HashSet<>(yangParser.parseYangModelsFromStreamsMapped(yangDependencies).values()));
598 YangStoreServiceImpl yangStoreService = new YangStoreServiceImpl(new SchemaContextProvider() {
600 public SchemaContext getSchemaContext() {
601 return schemaContext ;
604 mBeanEntries.putAll(yangStoreService.getYangStoreSnapshot().getModuleMXBeanEntryMap());
609 private Set<org.opendaylight.yangtools.yang.model.api.Module> getModules() throws Exception {
610 SchemaContext resolveSchemaContext = getSchemaContext();
611 return resolveSchemaContext.getModules();
614 private SchemaContext getSchemaContext() throws Exception {
615 final List<InputStream> yangDependencies = getYangs();
616 YangParserImpl parser = new YangParserImpl();
618 Set<Module> allYangModules = parser.parseYangModelsFromStreams(yangDependencies);
620 return parser.resolveSchemaContext(Sets
621 .newHashSet(allYangModules));
625 public void testConfigNetconfRuntime() throws Exception {
627 createModule(INSTANCE_NAME);
629 edit("netconfMessages/editConfig.xml");
630 checkBinaryLeafEdited(getConfigCandidate());
634 Element response = get();
636 assertEquals(2/*With runtime beans*/ + 2 /*Without runtime beans*/, getElementsSize(response, "module"));
638 assertEquals(2, getElementsSize(response, "asdf"));
639 // data from running config
640 assertEquals(2, getElementsSize(response, "simple-short"));
642 assertEquals(8, getElementsSize(response, "inner-running-data"));
643 assertEquals(8, getElementsSize(response, "deep2"));
644 assertEquals(8 * 4, getElementsSize(response, "inner-inner-running-data"));
645 assertEquals(8 * 4, getElementsSize(response, "deep3"));
646 assertEquals(8 * 4 * 2, getElementsSize(response, "list-of-strings"));
647 assertEquals(8, getElementsSize(response, "inner-running-data-additional"));
648 assertEquals(8, getElementsSize(response, "deep4"));
651 RuntimeRpc netconf = new RuntimeRpc(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID);
653 response = executeOp(netconf, "netconfMessages/rpc.xml");
654 assertContainsString(XmlUtil.toString(response), "testarg1".toUpperCase());
656 response = executeOp(netconf, "netconfMessages/rpcInner.xml");
657 assertContainsString(XmlUtil.toString(response), "ok");
659 response = executeOp(netconf, "netconfMessages/rpcInnerInner.xml");
660 assertContainsString(XmlUtil.toString(response), "true");
662 response = executeOp(netconf, "netconfMessages/rpcInnerInner_complex_output.xml");
663 assertContainsString(XmlUtil.toString(response), "1");
664 assertContainsString(XmlUtil.toString(response), "2");
667 private Element get() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
668 Get getOp = new Get(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID, transactionProvider);
669 return executeOp(getOp, "netconfMessages/get.xml");
672 private int getElementsSize(Element response, String elementName) {
673 return response.getElementsByTagName(elementName).getLength();
676 private Element executeOp(final NetconfOperation op, final String filename) throws ParserConfigurationException,
677 SAXException, IOException, NetconfDocumentedException {
679 final Document request = XmlFileLoader.xmlFileToDocument(filename);
681 logger.debug("Executing netconf operation\n{}", XmlUtil.toString(request));
682 HandlingPriority priority = op.canHandle(request);
684 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
686 final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
687 logger.debug("Got response\n{}", XmlUtil.toString(response));
688 return response.getDocumentElement();
691 private List<InputStream> getYangs() throws FileNotFoundException {
692 List<String> paths = Arrays.asList("/META-INF/yang/config.yang", "/META-INF/yang/rpc-context.yang",
693 "/META-INF/yang/config-test.yang", "/META-INF/yang/config-test-impl.yang", "/META-INF/yang/test-types.yang",
694 "/META-INF/yang/ietf-inet-types.yang");
695 final Collection<InputStream> yangDependencies = new ArrayList<>();
696 for (String path : paths) {
697 final InputStream is = Preconditions
698 .checkNotNull(getClass().getResourceAsStream(path), path + " not found");
699 yangDependencies.add(is);
701 return Lists.newArrayList(yangDependencies);
704 private void setModule(final NetconfTestImplModuleMXBean mxBean, final ConfigTransactionJMXClient transaction, String depName)
705 throws InstanceAlreadyExistsException, InstanceNotFoundException {
706 mxBean.setSimpleInt((long) 44);
707 mxBean.setBinaryLeaf(new byte[] { 8, 7, 9 });
708 final DtoD dtob = getDtoD();
709 mxBean.setDtoD(dtob);
711 final DtoC dtoa = getDtoC();
712 mxBean.setDtoC(dtoa);
713 mxBean.setSimpleBoolean(false);
715 final Peers p1 = new Peers();
718 p1.setSimpleInt3(456);
719 final Peers p2 = new Peers();
721 p2.setPort("port23");
722 p2.setSimpleInt3(456);
723 mxBean.setPeers(Lists.<Peers> newArrayList(p1, p2));
725 mxBean.setSimpleLong(454545L);
726 mxBean.setSimpleLong2(44L);
727 mxBean.setSimpleBigInteger(BigInteger.valueOf(999L));
728 mxBean.setSimpleByte(new Byte((byte) 4));
729 mxBean.setSimpleShort(new Short((short) 4));
730 mxBean.setSimpleTest(545);
732 mxBean.setComplexList(Lists.<ComplexList> newArrayList());
733 mxBean.setSimpleList(Lists.<Integer> newArrayList());
735 final ObjectName testingDepOn = transaction.createModule(this.factory2.getImplementationName(), depName);
737 for (Class<? extends AbstractServiceInterface> sInterface : factory2.getImplementedServiceIntefaces()) {
738 ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
739 transaction.saveServiceReference(
740 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + depName + "_" + i++,
744 mxBean.setTestingDep(testingDepOn);
747 private static DtoD getDtoD() {
748 final DtoD dtob = new DtoD();
749 dtob.setSimpleInt1((long) 444);
750 dtob.setSimpleInt2((long) 4444);
751 dtob.setSimpleInt3(454);
752 final ComplexDtoBInner dtobInner = new ComplexDtoBInner();
753 final Deep deep = new Deep();
754 deep.setSimpleInt3(4);
755 dtobInner.setDeep(deep);
756 dtobInner.setSimpleInt3(44);
757 dtobInner.setSimpleList(Lists.newArrayList(4));
758 dtob.setComplexDtoBInner(Lists.newArrayList(dtobInner));
759 dtob.setSimpleList(Lists.newArrayList(4));
763 private static DtoC getDtoC() {
764 final DtoC dtoa = new DtoC();
765 // dtoa.setSimpleArg((long) 55);
766 final DtoAInner dtoAInner = new DtoAInner();
767 final DtoAInnerInner dtoAInnerInner = new DtoAInnerInner();
768 dtoAInnerInner.setSimpleArg(456L);
769 dtoAInner.setDtoAInnerInner(dtoAInnerInner);
770 dtoAInner.setSimpleArg(44L);
771 dtoa.setDtoAInner(dtoAInner);