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.List;
33 import javax.management.InstanceAlreadyExistsException;
34 import javax.management.InstanceNotFoundException;
35 import javax.management.ObjectName;
36 import javax.xml.parsers.ParserConfigurationException;
38 import org.apache.commons.lang3.StringUtils;
39 import org.junit.Before;
40 import org.junit.Ignore;
41 import org.junit.Test;
42 import org.junit.matchers.JUnitMatchers;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
46 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
47 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
48 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
49 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
50 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
51 import org.opendaylight.controller.config.yang.store.impl.MbeParser;
52 import org.opendaylight.controller.config.yang.test.impl.ComplexDtoBInner;
53 import org.opendaylight.controller.config.yang.test.impl.ComplexList;
54 import org.opendaylight.controller.config.yang.test.impl.Deep;
55 import org.opendaylight.controller.config.yang.test.impl.DepTestImplModuleFactory;
56 import org.opendaylight.controller.config.yang.test.impl.DtoAInner;
57 import org.opendaylight.controller.config.yang.test.impl.DtoAInnerInner;
58 import org.opendaylight.controller.config.yang.test.impl.DtoC;
59 import org.opendaylight.controller.config.yang.test.impl.DtoD;
60 import org.opendaylight.controller.config.yang.test.impl.IdentityTestModuleFactory;
61 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleFactory;
62 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleMXBean;
63 import org.opendaylight.controller.config.yang.test.impl.Peers;
64 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
65 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
66 import org.opendaylight.controller.netconf.api.NetconfOperationRouter;
67 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Commit;
68 import org.opendaylight.controller.netconf.confignetconfconnector.operations.DiscardChanges;
69 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
70 import org.opendaylight.controller.netconf.confignetconfconnector.operations.get.Get;
71 import org.opendaylight.controller.netconf.confignetconfconnector.operations.getconfig.GetConfig;
72 import org.opendaylight.controller.netconf.confignetconfconnector.operations.runtimerpc.RuntimeRpc;
73 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
74 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCloseSession;
75 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
76 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
77 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
78 import org.opendaylight.controller.netconf.util.xml.XmlElement;
79 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
80 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.test.impl.rev130403.TestIdentity1;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.test.impl.rev130403.TestIdentity2;
83 import org.opendaylight.yangtools.yang.data.impl.codec.CodecRegistry;
84 import org.opendaylight.yangtools.yang.data.impl.codec.IdentityCodec;
85 import org.opendaylight.yangtools.yang.model.api.Module;
86 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
87 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
88 import org.slf4j.Logger;
89 import org.slf4j.LoggerFactory;
90 import org.w3c.dom.Document;
91 import org.w3c.dom.Element;
92 import org.w3c.dom.NodeList;
93 import org.xml.sax.SAXException;
95 import com.google.common.base.Optional;
96 import com.google.common.base.Preconditions;
97 import com.google.common.collect.Lists;
98 import com.google.common.collect.Maps;
99 import com.google.common.collect.Sets;
102 public class NetconfMappingTest extends AbstractConfigTest {
103 private static final Logger logger = LoggerFactory.getLogger(NetconfMappingTest.class);
105 private static final String INSTANCE_NAME = "instance-from-code";
106 private static final String NETCONF_SESSION_ID = "foo";
107 private NetconfTestImplModuleFactory factory;
108 private DepTestImplModuleFactory factory2;
109 private IdentityTestModuleFactory factory3;
112 YangStoreSnapshot yangStoreSnapshot;
114 NetconfOperationRouter netconfOperationRouter;
116 private TransactionProvider transactionProvider;
119 public void setUp() throws Exception {
120 MockitoAnnotations.initMocks(this);
121 doReturn(getMbes()).when(this.yangStoreSnapshot).getModuleMXBeanEntryMap();
122 doReturn(getModules()).when(this.yangStoreSnapshot).getModules();
124 this.factory = new NetconfTestImplModuleFactory();
125 this.factory2 = new DepTestImplModuleFactory();
126 this.factory3 = new IdentityTestModuleFactory();
127 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(this.factory, this.factory2,
130 transactionProvider = new TransactionProvider(this.configRegistryClient, NETCONF_SESSION_ID);
133 private ObjectName createModule(final String instanceName) throws InstanceAlreadyExistsException, InstanceNotFoundException, URISyntaxException {
134 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
136 final ObjectName on = transaction.createModule(this.factory.getImplementationName(), instanceName);
137 final NetconfTestImplModuleMXBean mxBean = transaction.newMXBeanProxy(on, NetconfTestImplModuleMXBean.class);
138 setModule(mxBean, transaction, instanceName + "_dep");
141 for (Class<? extends AbstractServiceInterface> sInterface : factory.getImplementedServiceIntefaces()) {
142 ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
143 transaction.saveServiceReference(
144 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + instanceName + "_" + i++,
148 transaction.commit();
153 public void testIdentityRefs() throws Exception {
154 edit("netconfMessages/editConfig_identities.xml");
161 protected CodecRegistry getCodecRegistry() {
162 IdentityCodec<?> idCodec = mock(IdentityCodec.class);
163 doReturn(TestIdentity1.class).when(idCodec).deserialize(TestIdentity1.QNAME);
164 doReturn(TestIdentity2.class).when(idCodec).deserialize(TestIdentity2.QNAME);
166 CodecRegistry codecReg = super.getCodecRegistry();
167 doReturn(idCodec).when(codecReg).getIdentityCodec();
172 public void testServicePersistance() throws Exception {
173 createModule(INSTANCE_NAME);
175 edit("netconfMessages/editConfig.xml");
176 Element config = getConfigCandidate();
177 assertCorrectServiceNames(config, 6, "ref_test2", "user_to_instance_from_code", "ref_dep_user",
178 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
179 "ref_from_code_to_instance-from-code_1");
182 edit("netconfMessages/editConfig_addServiceName.xml");
183 config = getConfigCandidate();
184 assertCorrectServiceNames(config, 7, "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", "ref_dep_user_another");
188 edit("netconfMessages/editConfig_addServiceNameOnTest.xml");
189 config = getConfigCandidate();
190 assertCorrectServiceNames(config, 7, "ref_test2", "user_to_instance_from_code", "ref_dep_user",
191 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
192 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another");
195 config = getConfigRunning();
196 assertCorrectRefNamesForDependencies(config);
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");
201 edit("netconfMessages/editConfig_replace_default.xml");
202 config = getConfigCandidate();
203 assertCorrectServiceNames(config, 2, "ref_dep", "ref_dep2");
205 edit("netconfMessages/editConfig_remove.xml");
206 config = getConfigCandidate();
207 assertCorrectServiceNames(config, 0);
210 config = getConfigCandidate();
211 assertCorrectServiceNames(config, 0);
215 private void assertCorrectRefNamesForDependencies(Element config) {
216 NodeList modulesList = config.getElementsByTagName("modules");
217 assertEquals(1, modulesList.getLength());
219 Element modules = (Element) modulesList.item(0);
221 String trimmedModules = XmlUtil.toString(modules).replaceAll("\\s", "");
222 int defaultRefNameCount = StringUtils.countMatches(trimmedModules, "ref_dep2");
223 int userRefNameCount = StringUtils.countMatches(trimmedModules, "ref_dep_user_two");
225 assertEquals(0, defaultRefNameCount);
226 assertEquals(2, userRefNameCount);
229 private void assertCorrectServiceNames(Element configCandidate, int servicesSize, String... refNames) {
230 NodeList elements = configCandidate.getElementsByTagName("provider");
231 assertEquals(servicesSize, elements.getLength());
233 NodeList servicesList = configCandidate.getElementsByTagName("services");
234 assertEquals(1, servicesList.getLength());
236 Element services = (Element) servicesList.item(0);
237 String trimmedServices = XmlUtil.toString(services).replaceAll("\\s", "");
239 for (String s : refNames) {
240 assertThat(trimmedServices, JUnitMatchers.containsString(s));
245 public void testConfigNetconfUnionTypes() throws Exception {
247 createModule(INSTANCE_NAME);
249 edit("netconfMessages/editConfig.xml");
251 Element response = getConfigRunning();
252 String trimmedResponse = XmlUtil.toString(response).replaceAll("\\s", "");
253 assertContainsString(trimmedResponse, "<ipxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">0:0:0:0:0:0:0:1</ip>");
254 assertContainsString(trimmedResponse, "<union-test-attrxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">456</union-test-attr>");
257 edit("netconfMessages/editConfig_setUnions.xml");
259 response = getConfigRunning();
261 trimmedResponse = XmlUtil.toString(response).replaceAll("\\s", "");
262 assertContainsString(trimmedResponse, "<ipxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">127.1.2.3</ip>");
263 assertContainsString(trimmedResponse, "<union-test-attrxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">randomStringForUnion</union-test-attr>");
268 public void testConfigNetconf() throws Exception {
270 createModule(INSTANCE_NAME);
272 edit("netconfMessages/editConfig.xml");
273 Element configCandidate = getConfigCandidate();
274 checkBinaryLeafEdited(configCandidate);
277 // default-operation:none, should not affect binary leaf
278 edit("netconfMessages/editConfig_none.xml");
279 checkBinaryLeafEdited(getConfigCandidate());
283 Element response = getConfigRunning();
285 checkBinaryLeafEdited(response);
286 checkTypeConfigAttribute(response);
287 checkTypedefs(response);
288 checkTestingDeps(response);
290 checkBigDecimal(response);
292 edit("netconfMessages/editConfig_remove.xml");
295 response = getConfigCandidate();
296 final String responseFromCandidate = XmlUtil.toString(response).replaceAll("\\s+", "");
297 response = getConfigRunning();
298 final String responseFromRunning = XmlUtil.toString(response).replaceAll("\\s+", "");
299 assertEquals(responseFromCandidate, responseFromRunning);
301 final String expectedResult = XmlFileLoader.fileToString("netconfMessages/editConfig_expectedResult.xml")
302 .replaceAll("\\s+", "");
304 assertEquals(expectedResult, responseFromRunning);
305 assertEquals(expectedResult, responseFromCandidate);
307 edit("netconfMessages/editConfig_none.xml");
308 doNothing().when(netconfOperationRouter).close();
310 verify(netconfOperationRouter).close();
311 verifyNoMoreInteractions(netconfOperationRouter);
314 private void checkBigDecimal(Element response) {
315 String responseTrimmed = XmlUtil.toString(response).replaceAll("\\s", "");
317 assertContainsString(responseTrimmed, "<sleep-factorxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.58</sleep-factor>");
319 assertContainsString(responseTrimmed, "<sleep-factorxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.00</sleep-factor>");
323 private void closeSession() throws NetconfDocumentedException, ParserConfigurationException, SAXException,
325 DefaultCloseSession closeOp = new DefaultCloseSession(NETCONF_SESSION_ID);
326 executeOp(closeOp, "netconfMessages/closeSession.xml");
329 private void edit(String resource) throws ParserConfigurationException, SAXException, IOException,
330 NetconfDocumentedException {
331 EditConfig editOp = new EditConfig(yangStoreSnapshot, transactionProvider, configRegistryClient,
333 executeOp(editOp, resource);
336 private void commit() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
337 Commit commitOp = new Commit(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
338 executeOp(commitOp, "netconfMessages/commit.xml");
341 private Element getConfigCandidate() throws ParserConfigurationException, SAXException, IOException,
342 NetconfDocumentedException {
343 GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
344 configRegistryClient, NETCONF_SESSION_ID);
345 return executeOp(getConfigOp, "netconfMessages/getConfig_candidate.xml");
348 private Element getConfigRunning() 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.xml");
355 @Ignore("second edit message corrupted")
356 @Test(expected = NetconfDocumentedException.class)
357 public void testConfigNetconfReplaceDefaultEx() throws Exception {
359 createModule(INSTANCE_NAME);
361 edit("netconfMessages/editConfig.xml");
362 edit("netconfMessages/editConfig_replace_default_ex.xml");
366 public void testConfigNetconfReplaceDefault() throws Exception {
368 createModule(INSTANCE_NAME);
370 edit("netconfMessages/editConfig.xml");
372 Element response = getConfigRunning();
373 final int allInstances = response.getElementsByTagName("module").getLength();
375 edit("netconfMessages/editConfig_replace_default.xml");
378 response = getConfigRunning();
380 final int afterReplace = response.getElementsByTagName("module").getLength();
381 assertEquals(4, allInstances);
382 assertEquals(2, afterReplace);
385 @Test(expected = NetconfDocumentedException.class)
386 public void testSameAttrDifferentNamespaces() throws Exception {
388 edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespaces.xml");
389 } catch (NetconfDocumentedException e) {
390 String message = e.getMessage();
391 assertContainsString(message, "Element simple-long-2 present multiple times with different namespaces");
392 assertContainsString(message, "urn:opendaylight:params:xml:ns:yang:controller:test:impl");
393 assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
398 @Test(expected = NetconfDocumentedException.class)
399 public void testDifferentNamespaceInTO() throws Exception {
401 edit("netconfMessages/namespaces/editConfig_differentNamespaceTO.xml");
402 } catch (NetconfDocumentedException e) {
403 String message = e.getMessage();
404 assertContainsString(message, "Unrecognised elements");
405 assertContainsString(message, "simple-int2");
406 assertContainsString(message, "dto_d");
411 @Test(expected = NetconfDocumentedException.class)
412 public void testSameAttrDifferentNamespacesList() throws Exception {
414 edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespacesList.xml");
415 } catch (NetconfDocumentedException e) {
416 String message = e.getMessage();
417 assertContainsString(message, "Element binaryLeaf present multiple times with different namespaces");
418 assertContainsString(message, "urn:opendaylight:params:xml:ns:yang:controller:test:impl");
419 assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
425 public void testTypeNameConfigAttributeMatching() throws Exception {
426 edit("netconfMessages/editConfig.xml");
428 edit("netconfMessages/namespaces/editConfig_typeNameConfigAttributeMatching.xml");
431 Element response = getConfigRunning();
432 checkTypeConfigAttribute(response);
435 // TODO add <modules operation="replace"> functionality
436 @Test(expected = NetconfDocumentedException.class)
437 public void testConfigNetconfReplaceModuleEx() throws Exception {
439 createModule(INSTANCE_NAME);
441 edit("netconfMessages/editConfig.xml");
442 edit("netconfMessages/editConfig_replace_module_ex.xml");
446 public void testUnrecognisedConfigElements() throws Exception {
448 String format = "netconfMessages/unrecognised/editConfig_unrecognised%d.xml";
449 final int TESTS_COUNT = 8;
451 for (int i = 0; i < TESTS_COUNT; i++) {
452 String file = String.format(format, i + 1);
455 } catch (NetconfDocumentedException e) {
456 assertContainsString(e.getMessage(), "Unrecognised elements");
457 assertContainsString(e.getMessage(), "unknownAttribute");
460 fail("Unrecognised test should throw exception " + file);
467 public void testConfigNetconfReplaceModule() throws Exception {
469 createModule(INSTANCE_NAME);
471 edit("netconfMessages/editConfig.xml");
473 Element response = getConfigRunning();
474 final int allInstances = response.getElementsByTagName("instance").getLength();
476 edit("netconfMessages/editConfig_replace_module.xml");
479 response = getConfigRunning();
480 final int afterReplace = response.getElementsByTagName("instance").getLength();
482 assertEquals(4 + 4 /* Instances from services */, allInstances);
483 assertEquals(3 + 3, afterReplace);
486 @Test(expected = NetconfDocumentedException.class)
487 public void testEx() throws Exception {
492 @Test(expected = NetconfDocumentedException.class)
493 public void testEx2() throws Exception {
497 private void discard() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
498 DiscardChanges discardOp = new DiscardChanges(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
499 executeOp(discardOp, "netconfMessages/discardChanges.xml");
502 private void checkBinaryLeafEdited(final Element response) {
503 String responseTrimmed = XmlUtil.toString(response).replaceAll("\\s", "");
504 String substring = "<binaryLeafxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">YmluYXJ5</binaryLeaf>";
505 assertContainsString(responseTrimmed, substring);
506 substring = "<binaryLeafxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ZGVmYXVsdEJpbg==</binaryLeaf>";
507 assertContainsString(responseTrimmed, substring);
510 private void checkTypedefs(final Element response) {
511 String responseTrimmed = XmlUtil.toString(response).replaceAll("\\s", "");
513 String substring = "<extendedxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">10</extended>";
514 assertContainsString(responseTrimmed, substring);
516 assertContainsString(responseTrimmed,
517 "<extendedxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">1</extended>");
519 assertContainsString(responseTrimmed,
520 "<extended-twicexmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">20</extended-twice>");
522 assertContainsString(responseTrimmed,
523 "<extended-twicexmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2</extended-twice>");
525 assertContainsString(responseTrimmed,
526 "<extended-enumxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">TWO</extended-enum>");
528 assertContainsString(responseTrimmed,
529 "<extended-enumxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ONE</extended-enum>");
532 private void assertContainsString(String string, String substring) {
533 assertThat(string, JUnitMatchers.containsString(substring));
536 private void checkEnum(final Element response) {
537 XmlElement modulesElement = XmlElement.fromDomElement(response).getOnlyChildElement("data")
538 .getOnlyChildElement("modules");
540 String enumName = "extended-enum";
541 String enumContent = "TWO";
543 for (XmlElement moduleElement : modulesElement.getChildElements("module")) {
544 String name = moduleElement.getOnlyChildElement("name").getTextContent();
545 if(name.equals(INSTANCE_NAME)) {
546 XmlElement enumAttr = moduleElement.getOnlyChildElement(enumName);
547 assertEquals(enumContent, enumAttr.getTextContent());
553 fail("Enum attribute " + enumName + ":" + enumContent + " not present in " + XmlUtil.toString(response));
556 private void checkTestingDeps(Element response) {
557 int testingDepsSize = response.getElementsByTagName("testing-deps").getLength();
558 assertEquals(2, testingDepsSize);
561 private void checkTypeConfigAttribute(Element response) {
563 XmlElement modulesElement = XmlElement.fromDomElement(response).getOnlyChildElement("data")
564 .getOnlyChildElement("modules");
566 List<String> expectedValues = Lists.newArrayList("default-string", "configAttributeType");
567 Set<String> configAttributeType = Sets.newHashSet();
569 for (XmlElement moduleElement : modulesElement.getChildElements("module")) {
570 for (XmlElement type : moduleElement.getChildElements("type")) {
571 if (type.getAttribute(XmlUtil.XMLNS_ATTRIBUTE_KEY).equals("") == false) {
572 configAttributeType.add(type.getTextContent());
577 for (String expectedValue : expectedValues) {
578 assertTrue(configAttributeType.contains(expectedValue));
582 private Map<String, Map<String, ModuleMXBeanEntry>> getMbes() throws Exception {
583 final List<InputStream> yangDependencies = getYangs();
585 final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries = Maps.newHashMap();
586 mBeanEntries.putAll(new MbeParser().parseYangFiles(yangDependencies).getModuleMXBeanEntryMap());
591 private Set<org.opendaylight.yangtools.yang.model.api.Module> getModules() throws Exception {
592 SchemaContext resolveSchemaContext = getSchemaContext();
593 return resolveSchemaContext.getModules();
596 private SchemaContext getSchemaContext() throws Exception {
597 final List<InputStream> yangDependencies = getYangs();
598 YangParserImpl parser = new YangParserImpl();
600 Set<Module> allYangModules = parser.parseYangModelsFromStreams(yangDependencies);
602 return parser.resolveSchemaContext(Sets
603 .newHashSet(allYangModules));
607 public void testConfigNetconfRuntime() throws Exception {
609 createModule(INSTANCE_NAME);
611 edit("netconfMessages/editConfig.xml");
612 checkBinaryLeafEdited(getConfigCandidate());
616 Element response = get();
618 assertEquals(2/*With runtime beans*/ + 2 /*Without runtime beans*/, getElementsSize(response, "module"));
620 assertEquals(2, getElementsSize(response, "asdf"));
621 // data from running config
622 assertEquals(2, getElementsSize(response, "simple-short"));
624 assertEquals(8, getElementsSize(response, "inner-running-data"));
625 assertEquals(8, getElementsSize(response, "deep2"));
626 assertEquals(8 * 4, getElementsSize(response, "inner-inner-running-data"));
627 assertEquals(8 * 4, getElementsSize(response, "deep3"));
628 assertEquals(8 * 4 * 2, getElementsSize(response, "list-of-strings"));
629 assertEquals(8, getElementsSize(response, "inner-running-data-additional"));
630 assertEquals(8, getElementsSize(response, "deep4"));
633 RuntimeRpc netconf = new RuntimeRpc(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID);
635 response = executeOp(netconf, "netconfMessages/rpc.xml");
636 assertContainsString(XmlUtil.toString(response), "testarg1".toUpperCase());
638 response = executeOp(netconf, "netconfMessages/rpcInner.xml");
639 assertContainsString(XmlUtil.toString(response), "ok");
641 response = executeOp(netconf, "netconfMessages/rpcInnerInner.xml");
642 assertContainsString(XmlUtil.toString(response), "true");
644 response = executeOp(netconf, "netconfMessages/rpcInnerInner_complex_output.xml");
645 assertContainsString(XmlUtil.toString(response), "1");
646 assertContainsString(XmlUtil.toString(response), "2");
649 private Element get() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
650 Get getOp = new Get(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID, transactionProvider);
651 return executeOp(getOp, "netconfMessages/get.xml");
654 private int getElementsSize(Element response, String elementName) {
655 return response.getElementsByTagName(elementName).getLength();
658 private Element executeOp(final NetconfOperation op, final String filename) throws ParserConfigurationException,
659 SAXException, IOException, NetconfDocumentedException {
661 final Document request = XmlFileLoader.xmlFileToDocument(filename);
663 logger.debug("Executing netconf operation\n{}", XmlUtil.toString(request));
664 HandlingPriority priority = op.canHandle(request);
666 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
668 final Document response = op.handle(request, netconfOperationRouter);
669 logger.debug("Got response\n{}", XmlUtil.toString(response));
670 return response.getDocumentElement();
673 private List<InputStream> getYangs() throws FileNotFoundException {
674 List<String> paths = Arrays.asList("/META-INF/yang/config.yang", "/META-INF/yang/rpc-context.yang",
675 "/META-INF/yang/config-test.yang", "/META-INF/yang/config-test-impl.yang", "/META-INF/yang/test-types.yang",
676 "/META-INF/yang/ietf-inet-types.yang");
677 final Collection<InputStream> yangDependencies = new ArrayList<>();
678 for (String path : paths) {
679 final InputStream is = Preconditions
680 .checkNotNull(getClass().getResourceAsStream(path), path + " not found");
681 yangDependencies.add(is);
683 return Lists.newArrayList(yangDependencies);
686 private void setModule(final NetconfTestImplModuleMXBean mxBean, final ConfigTransactionJMXClient transaction, String depName)
687 throws InstanceAlreadyExistsException, InstanceNotFoundException {
688 mxBean.setSimpleInt((long) 44);
689 mxBean.setBinaryLeaf(new byte[] { 8, 7, 9 });
690 final DtoD dtob = getDtoD();
691 mxBean.setDtoD(dtob);
693 final DtoC dtoa = getDtoC();
694 mxBean.setDtoC(dtoa);
695 mxBean.setSimpleBoolean(false);
697 final Peers p1 = new Peers();
700 p1.setSimpleInt3(456);
701 final Peers p2 = new Peers();
703 p2.setPort("port23");
704 p2.setSimpleInt3(456);
705 mxBean.setPeers(Lists.<Peers> newArrayList(p1, p2));
707 mxBean.setSimpleLong(454545L);
708 mxBean.setSimpleLong2(44L);
709 mxBean.setSimpleBigInteger(BigInteger.valueOf(999L));
710 mxBean.setSimpleByte(new Byte((byte) 4));
711 mxBean.setSimpleShort(new Short((short) 4));
712 mxBean.setSimpleTest(545);
714 mxBean.setComplexList(Lists.<ComplexList> newArrayList());
715 mxBean.setSimpleList(Lists.<Integer> newArrayList());
717 final ObjectName testingDepOn = transaction.createModule(this.factory2.getImplementationName(), depName);
719 for (Class<? extends AbstractServiceInterface> sInterface : factory2.getImplementedServiceIntefaces()) {
720 ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
721 transaction.saveServiceReference(
722 transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + depName + "_" + i++,
726 mxBean.setTestingDep(testingDepOn);
729 private static DtoD getDtoD() {
730 final DtoD dtob = new DtoD();
731 dtob.setSimpleInt1((long) 444);
732 dtob.setSimpleInt2((long) 4444);
733 dtob.setSimpleInt3(454);
734 final ComplexDtoBInner dtobInner = new ComplexDtoBInner();
735 final Deep deep = new Deep();
736 deep.setSimpleInt3(4);
737 dtobInner.setDeep(deep);
738 dtobInner.setSimpleInt3(44);
739 dtobInner.setSimpleList(Lists.newArrayList(4));
740 dtob.setComplexDtoBInner(Lists.newArrayList(dtobInner));
741 dtob.setSimpleList(Lists.newArrayList(4));
745 private static DtoC getDtoC() {
746 final DtoC dtoa = new DtoC();
747 // dtoa.setSimpleArg((long) 55);
748 final DtoAInner dtoAInner = new DtoAInner();
749 final DtoAInnerInner dtoAInnerInner = new DtoAInnerInner();
750 dtoAInnerInner.setSimpleArg(456L);
751 dtoAInner.setDtoAInnerInner(dtoAInnerInner);
752 dtoAInner.setSimpleArg(44L);
753 dtoa.setDtoAInner(dtoAInner);