ccb793149c4d21162ac11e3495981e4de1898faa
[controller.git] / opendaylight / netconf / config-netconf-connector / src / test / java / org / opendaylight / controller / netconf / confignetconfconnector / NetconfMappingTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.confignetconfconnector;
10
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 org.apache.commons.lang3.StringUtils;
16 import org.junit.Before;
17 import org.junit.Ignore;
18 import org.junit.Test;
19 import org.junit.matchers.JUnitMatchers;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
23 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
24 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
25 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
26 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
27 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
28 import org.opendaylight.controller.config.yang.store.impl.MbeParser;
29 import org.opendaylight.controller.config.yang.test.impl.ComplexDtoBInner;
30 import org.opendaylight.controller.config.yang.test.impl.ComplexList;
31 import org.opendaylight.controller.config.yang.test.impl.Deep;
32 import org.opendaylight.controller.config.yang.test.impl.DepTestImplModuleFactory;
33 import org.opendaylight.controller.config.yang.test.impl.DtoAInner;
34 import org.opendaylight.controller.config.yang.test.impl.DtoAInnerInner;
35 import org.opendaylight.controller.config.yang.test.impl.DtoC;
36 import org.opendaylight.controller.config.yang.test.impl.DtoD;
37 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleFactory;
38 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleMXBean;
39 import org.opendaylight.controller.config.yang.test.impl.Peers;
40 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
41 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
42 import org.opendaylight.controller.netconf.api.NetconfOperationRouter;
43 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Commit;
44 import org.opendaylight.controller.netconf.confignetconfconnector.operations.DiscardChanges;
45 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
46 import org.opendaylight.controller.netconf.confignetconfconnector.operations.get.Get;
47 import org.opendaylight.controller.netconf.confignetconfconnector.operations.getconfig.GetConfig;
48 import org.opendaylight.controller.netconf.confignetconfconnector.operations.runtimerpc.RuntimeRpc;
49 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
50 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCloseSession;
51 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
52 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
53 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
54 import org.opendaylight.controller.netconf.util.xml.XmlElement;
55 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
56 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59 import org.w3c.dom.Document;
60 import org.w3c.dom.Element;
61 import org.w3c.dom.NodeList;
62 import org.xml.sax.SAXException;
63
64 import javax.management.InstanceAlreadyExistsException;
65 import javax.management.InstanceNotFoundException;
66 import javax.management.ObjectName;
67 import javax.xml.parsers.ParserConfigurationException;
68 import java.io.FileNotFoundException;
69 import java.io.IOException;
70 import java.io.InputStream;
71 import java.math.BigInteger;
72 import java.net.URISyntaxException;
73 import java.util.ArrayList;
74 import java.util.Arrays;
75 import java.util.Collection;
76 import java.util.List;
77 import java.util.Map;
78
79 import static org.junit.Assert.assertEquals;
80 import static org.junit.Assert.assertThat;
81 import static org.junit.Assert.fail;
82 import static org.mockito.Mockito.doNothing;
83 import static org.mockito.Mockito.doReturn;
84 import static org.mockito.Mockito.verify;
85 import static org.mockito.Mockito.verifyNoMoreInteractions;
86
87
88 public class NetconfMappingTest extends AbstractConfigTest {
89     private static final Logger logger = LoggerFactory.getLogger(NetconfMappingTest.class);
90
91     private static final String INSTANCE_NAME = "instance-from-code";
92     private static final String NETCONF_SESSION_ID = "foo";
93     private NetconfTestImplModuleFactory factory;
94     private DepTestImplModuleFactory factory2;
95
96     @Mock
97     YangStoreSnapshot yangStoreSnapshot;
98     @Mock
99     NetconfOperationRouter netconfOperationRouter;
100
101     private TransactionProvider transactionProvider;
102
103     @Before
104     public void setUp() throws Exception {
105         MockitoAnnotations.initMocks(this);
106         doReturn(getMbes()).when(this.yangStoreSnapshot).getModuleMXBeanEntryMap();
107         this.factory = new NetconfTestImplModuleFactory();
108         this.factory2 = new DepTestImplModuleFactory();
109         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(this.factory, this.factory2));
110
111         transactionProvider = new TransactionProvider(this.configRegistryClient, NETCONF_SESSION_ID);
112     }
113
114     private ObjectName createModule(final String instanceName) throws InstanceAlreadyExistsException, InstanceNotFoundException, URISyntaxException {
115         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
116
117         final ObjectName on = transaction.createModule(this.factory.getImplementationName(), instanceName);
118         final NetconfTestImplModuleMXBean mxBean = transaction.newMXBeanProxy(on, NetconfTestImplModuleMXBean.class);
119         setModule(mxBean, transaction, instanceName + "_dep");
120
121         int i = 1;
122         for (Class<? extends AbstractServiceInterface> sInterface : factory.getImplementedServiceIntefaces()) {
123             ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
124             transaction.saveServiceReference(
125                     transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + instanceName + "_" + i++,
126                     on);
127
128         }
129         transaction.commit();
130         return on;
131     }
132
133     @Test
134     public void testServicePersistance() throws Exception {
135         createModule(INSTANCE_NAME);
136
137         edit("netconfMessages/editConfig.xml");
138         Element config = getConfigCandidate();
139         assertCorrectServiceNames(config, 6, "ref_test2", "user_to_instance_from_code", "ref_dep_user",
140                 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
141                 "ref_from_code_to_instance-from-code_1");
142
143         edit("netconfMessages/editConfig_addServiceName.xml");
144          config = getConfigCandidate();
145         assertCorrectServiceNames(config, 7, "ref_test2", "user_to_instance_from_code", "ref_dep_user",
146                 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
147                 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another");
148
149         commit();
150         config = getConfigRunning();
151         assertCorrectRefNamesForDependencies(config);
152         assertCorrectServiceNames(config, 7, "ref_test2", "user_to_instance_from_code", "ref_dep_user",
153                 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
154                 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another");
155
156         edit("netconfMessages/editConfig_replace_default.xml");
157         config = getConfigCandidate();
158         assertCorrectServiceNames(config, 2, "ref_dep", "ref_dep2");
159
160         edit("netconfMessages/editConfig_remove.xml");
161         config = getConfigCandidate();
162         assertCorrectServiceNames(config, 0);
163
164         commit();
165         config = getConfigCandidate();
166         assertCorrectServiceNames(config, 0);
167
168     }
169
170     private void assertCorrectRefNamesForDependencies(Element config) {
171         NodeList modulesList = config.getElementsByTagName("modules");
172         assertEquals(1, modulesList.getLength());
173
174         Element modules = (Element) modulesList.item(0);
175
176         String trimmedModules = XmlUtil.toString(modules).replaceAll("\\s", "");
177         int defaultRefNameCount = StringUtils.countMatches(trimmedModules, "ref_dep2");
178         int userRefNameCount = StringUtils.countMatches(trimmedModules, "ref_dep_user_two");
179
180         assertEquals(0, defaultRefNameCount);
181         assertEquals(2, userRefNameCount);
182     }
183
184     private void assertCorrectServiceNames(Element configCandidate, int servicesSize, String... refNames) {
185         NodeList elements = configCandidate.getElementsByTagName("provider");
186         assertEquals(servicesSize, elements.getLength());
187
188         NodeList servicesList = configCandidate.getElementsByTagName("services");
189         assertEquals(1, servicesList.getLength());
190
191         Element services = (Element) servicesList.item(0);
192         String trimmedServices = XmlUtil.toString(services).replaceAll("\\s", "");
193
194         for (String s : refNames) {
195             assertThat(trimmedServices, JUnitMatchers.containsString(s));
196         }
197     }
198
199     @Test
200     public void testConfigNetconf() throws Exception {
201
202         createModule(INSTANCE_NAME);
203
204         edit("netconfMessages/editConfig.xml");
205         Element configCandidate = getConfigCandidate();
206         checkBinaryLeafEdited(configCandidate);
207
208
209         // default-operation:none, should not affect binary leaf
210         edit("netconfMessages/editConfig_none.xml");
211         checkBinaryLeafEdited(getConfigCandidate());
212
213         // check after edit
214         commit();
215         Element response = getConfigRunning();
216
217         checkBinaryLeafEdited(response);
218         checkTypeConfigAttribute(response);
219         checkTypedefs(response);
220         checkTestingDeps(response);
221         checkEnum(response);
222         checkBigDecimal(response);
223
224         edit("netconfMessages/editConfig_remove.xml");
225
226         commit();
227         response = getConfigCandidate();
228         final String responseFromCandidate = XmlUtil.toString(response).replaceAll("\\s+", "");
229         // System.out.println(responseFromCandidate);
230         response = getConfigRunning();
231         final String responseFromRunning = XmlUtil.toString(response).replaceAll("\\s+", "");
232         // System.out.println(responseFromRunning);
233         assertEquals(responseFromCandidate, responseFromRunning);
234
235         final String expectedResult = XmlFileLoader.fileToString("netconfMessages/editConfig_expectedResult.xml")
236                 .replaceAll("\\s+", "");
237
238         assertEquals(expectedResult, responseFromRunning);
239         assertEquals(expectedResult, responseFromCandidate);
240
241         edit("netconfMessages/editConfig_none.xml");
242         doNothing().when(netconfOperationRouter).close();
243         closeSession();
244         verify(netconfOperationRouter).close();
245         verifyNoMoreInteractions(netconfOperationRouter);
246     }
247
248     private void checkBigDecimal(Element response) {
249         String responseTrimmed = XmlUtil.toString(response).replaceAll("\\s", "");
250
251         assertContainsString(responseTrimmed, "<sleep-factorxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.58</sleep-factor>");
252         // Default
253         assertContainsString(responseTrimmed, "<sleep-factorxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.00</sleep-factor>");
254
255     }
256
257     private void closeSession() throws NetconfDocumentedException, ParserConfigurationException, SAXException,
258             IOException {
259         DefaultCloseSession closeOp = new DefaultCloseSession(NETCONF_SESSION_ID);
260         executeOp(closeOp, "netconfMessages/closeSession.xml");
261     }
262
263     private void edit(String resource) throws ParserConfigurationException, SAXException, IOException,
264             NetconfDocumentedException {
265         EditConfig editOp = new EditConfig(yangStoreSnapshot, transactionProvider, configRegistryClient,
266                 NETCONF_SESSION_ID);
267         executeOp(editOp, resource);
268     }
269
270     private void commit() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
271         Commit commitOp = new Commit(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
272         executeOp(commitOp, "netconfMessages/commit.xml");
273     }
274
275     private Element getConfigCandidate() throws ParserConfigurationException, SAXException, IOException,
276             NetconfDocumentedException {
277         GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
278                 configRegistryClient, NETCONF_SESSION_ID);
279         return executeOp(getConfigOp, "netconfMessages/getConfig_candidate.xml");
280     }
281
282     private Element getConfigRunning() throws ParserConfigurationException, SAXException, IOException,
283             NetconfDocumentedException {
284         GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
285                 configRegistryClient, NETCONF_SESSION_ID);
286         return executeOp(getConfigOp, "netconfMessages/getConfig.xml");
287     }
288
289     @Ignore("second edit message corrupted")
290     @Test(expected = NetconfDocumentedException.class)
291     public void testConfigNetconfReplaceDefaultEx() throws Exception {
292
293         createModule(INSTANCE_NAME);
294
295         edit("netconfMessages/editConfig.xml");
296         edit("netconfMessages/editConfig_replace_default_ex.xml");
297     }
298
299     @Test
300     public void testConfigNetconfReplaceDefault() throws Exception {
301
302         createModule(INSTANCE_NAME);
303
304         edit("netconfMessages/editConfig.xml");
305         commit();
306         Element response = getConfigRunning();
307         final int allInstances = response.getElementsByTagName("module").getLength();
308
309         edit("netconfMessages/editConfig_replace_default.xml");
310
311         commit();
312         response = getConfigRunning();
313
314         final int afterReplace = response.getElementsByTagName("module").getLength();
315         assertEquals(4, allInstances);
316         assertEquals(2, afterReplace);
317     }
318
319     @Test(expected = NetconfDocumentedException.class)
320     public void testSameAttrDifferentNamespaces() throws Exception {
321         try {
322             edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespaces.xml");
323         } catch (NetconfDocumentedException e) {
324             String message = e.getMessage();
325             assertContainsString(message, "Element simple-long-2 present multiple times with different namespaces");
326             assertContainsString(message, "urn:opendaylight:params:xml:ns:yang:controller:test:impl");
327             assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
328             throw e;
329         }
330     }
331
332     @Test(expected = NetconfDocumentedException.class)
333     public void testDifferentNamespaceInTO() throws Exception {
334         try {
335             edit("netconfMessages/namespaces/editConfig_differentNamespaceTO.xml");
336         } catch (NetconfDocumentedException e) {
337             String message = e.getMessage();
338             assertContainsString(message, "Unrecognised elements");
339             assertContainsString(message, "simple-int2");
340             assertContainsString(message, "dto_d");
341             throw e;
342         }
343     }
344
345     @Test(expected = NetconfDocumentedException.class)
346     public void testSameAttrDifferentNamespacesList() throws Exception {
347         try {
348             edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespacesList.xml");
349         } catch (NetconfDocumentedException e) {
350             String message = e.getMessage();
351             assertContainsString(message, "Element binaryLeaf present multiple times with different namespaces");
352             assertContainsString(message, "urn:opendaylight:params:xml:ns:yang:controller:test:impl");
353             assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
354             throw e;
355         }
356     }
357
358     @Test
359     public void testTypeNameConfigAttributeMatching() throws Exception {
360         edit("netconfMessages/editConfig.xml");
361         commit();
362         edit("netconfMessages/namespaces/editConfig_typeNameConfigAttributeMatching.xml");
363         commit();
364
365         Element response = getConfigRunning();
366         checkTypeConfigAttribute(response);
367     }
368
369     // TODO add <modules operation="replace"> functionality
370     @Test(expected = NetconfDocumentedException.class)
371     public void testConfigNetconfReplaceModuleEx() throws Exception {
372
373         createModule(INSTANCE_NAME);
374
375         edit("netconfMessages/editConfig.xml");
376         edit("netconfMessages/editConfig_replace_module_ex.xml");
377     }
378
379     @Test
380     public void testUnrecognisedConfigElements() throws Exception {
381
382         String format = "netconfMessages/unrecognised/editConfig_unrecognised%d.xml";
383         final int TESTS_COUNT = 8;
384
385         for (int i = 0; i < TESTS_COUNT; i++) {
386             String file = String.format(format, i + 1);
387             try {
388                 edit(file);
389             } catch (NetconfDocumentedException e) {
390                 assertContainsString(e.getMessage(), "Unrecognised elements");
391                 assertContainsString(e.getMessage(), "unknownAttribute");
392                 continue;
393             }
394             fail("Unrecognised test should throw exception " + file);
395         }
396     }
397
398     @Test
399     @Ignore
400     // FIXME
401     public void testConfigNetconfReplaceModule() throws Exception {
402
403         createModule(INSTANCE_NAME);
404
405         edit("netconfMessages/editConfig.xml");
406         commit();
407         Element response = getConfigRunning();
408         final int allInstances = response.getElementsByTagName("instance").getLength();
409
410         edit("netconfMessages/editConfig_replace_module.xml");
411
412         commit();
413         response = getConfigRunning();
414         final int afterReplace = response.getElementsByTagName("instance").getLength();
415
416         assertEquals(4 + 4 /* Instances from services */, allInstances);
417         assertEquals(3 + 3, afterReplace);
418     }
419
420     @Test(expected = NetconfDocumentedException.class)
421     public void testEx() throws Exception {
422
423         commit();
424     }
425
426     @Test(expected = NetconfDocumentedException.class)
427     public void testEx2() throws Exception {
428         discard();
429     }
430
431     private void discard() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
432         DiscardChanges discardOp = new DiscardChanges(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
433         executeOp(discardOp, "netconfMessages/discardChanges.xml");
434     }
435
436     private void checkBinaryLeafEdited(final Element response) {
437         String responseTrimmed = XmlUtil.toString(response).replaceAll("\\s", "");
438         String substring = "<binaryLeafxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">YmluYXJ5</binaryLeaf>";
439         assertContainsString(responseTrimmed, substring);
440         substring = "<binaryLeafxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ZGVmYXVsdEJpbg==</binaryLeaf>";
441         assertContainsString(responseTrimmed, substring);
442     }
443
444     private void checkTypedefs(final Element response) {
445         String responseTrimmed = XmlUtil.toString(response).replaceAll("\\s", "");
446
447         String substring = "<extendedxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">10</extended>";
448         assertContainsString(responseTrimmed, substring);
449         // Default
450         assertContainsString(responseTrimmed,
451                 "<extendedxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">1</extended>");
452
453         assertContainsString(responseTrimmed,
454                 "<extended-twicexmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">20</extended-twice>");
455         // Default
456         assertContainsString(responseTrimmed,
457                 "<extended-twicexmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2</extended-twice>");
458
459         assertContainsString(responseTrimmed,
460                 "<extended-enumxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">TWO</extended-enum>");
461         // Default
462         assertContainsString(responseTrimmed,
463                 "<extended-enumxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ONE</extended-enum>");
464     }
465
466     private void assertContainsString(String string, String substring) {
467         assertThat(string, JUnitMatchers.containsString(substring));
468     }
469
470     private void checkEnum(final Element response) {
471         XmlElement modulesElement = XmlElement.fromDomElement(response).getOnlyChildElement("data")
472                 .getOnlyChildElement("modules");
473
474         String enumName = "extended-enum";
475         String enumContent = "TWO";
476
477         for (XmlElement moduleElement : modulesElement.getChildElements("module")) {
478             String name = moduleElement.getOnlyChildElement("name").getTextContent();
479             if(name.equals(INSTANCE_NAME)) {
480                 XmlElement enumAttr = moduleElement.getOnlyChildElement(enumName);
481                 assertEquals(enumContent, enumAttr.getTextContent());
482
483                 return;
484             }
485         }
486
487         fail("Enum attribute " + enumName + ":" + enumContent + " not present in " + XmlUtil.toString(response));
488     }
489
490     private void checkTestingDeps(Element response) {
491         int testingDepsSize = response.getElementsByTagName("testing-deps").getLength();
492         assertEquals(2, testingDepsSize);
493     }
494
495     private void checkTypeConfigAttribute(Element response) {
496
497         XmlElement modulesElement = XmlElement.fromDomElement(response).getOnlyChildElement("data")
498                 .getOnlyChildElement("modules");
499
500         XmlElement configAttributeType = null;
501         for (XmlElement moduleElement : modulesElement.getChildElements("module")) {
502             for (XmlElement type : moduleElement.getChildElements("type")) {
503                 if (type.getAttribute(XmlUtil.XMLNS_ATTRIBUTE_KEY).equals("") == false) {
504                     configAttributeType = type;
505                 }
506             }
507         }
508
509         // TODO verify if should be default value
510         assertEquals("default-string", configAttributeType.getTextContent());
511     }
512
513     private Map<String, Map<String, ModuleMXBeanEntry>> getMbes() throws Exception {
514         final List<InputStream> yangDependencies = getYangs();
515
516         final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries = Maps.newHashMap();
517         mBeanEntries.putAll(new MbeParser().parseYangFiles(yangDependencies).getModuleMXBeanEntryMap());
518
519         return mBeanEntries;
520     }
521
522     @Test
523     public void testConfigNetconfRuntime() throws Exception {
524
525         createModule(INSTANCE_NAME);
526
527         edit("netconfMessages/editConfig.xml");
528         checkBinaryLeafEdited(getConfigCandidate());
529
530         // check after edit
531         commit();
532         Element response = get();
533
534         assertEquals(2/*With runtime beans*/ + 2 /*Without runtime beans*/, getElementsSize(response, "module"));
535         // data from state
536         assertEquals(2, getElementsSize(response, "asdf"));
537         // data from running config
538         assertEquals(2, getElementsSize(response, "simple-short"));
539
540         assertEquals(8, getElementsSize(response, "inner-running-data"));
541         assertEquals(8, getElementsSize(response, "deep2"));
542         assertEquals(8 * 4, getElementsSize(response, "inner-inner-running-data"));
543         assertEquals(8 * 4, getElementsSize(response, "deep3"));
544         assertEquals(8 * 4 * 2, getElementsSize(response, "list-of-strings"));
545         assertEquals(8, getElementsSize(response, "inner-running-data-additional"));
546         assertEquals(8, getElementsSize(response, "deep4"));
547         // TODO assert keys
548
549         RuntimeRpc netconf = new RuntimeRpc(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID);
550
551         response = executeOp(netconf, "netconfMessages/rpc.xml");
552         assertContainsString(XmlUtil.toString(response), "testarg1".toUpperCase());
553
554         response = executeOp(netconf, "netconfMessages/rpcInner.xml");
555         assertContainsString(XmlUtil.toString(response), "ok");
556
557         response = executeOp(netconf, "netconfMessages/rpcInnerInner.xml");
558         assertContainsString(XmlUtil.toString(response), "true");
559
560         response = executeOp(netconf, "netconfMessages/rpcInnerInner_complex_output.xml");
561         assertContainsString(XmlUtil.toString(response), "1");
562         assertContainsString(XmlUtil.toString(response), "2");
563     }
564
565     private Element get() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
566         Get getOp = new Get(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID, transactionProvider);
567         return executeOp(getOp, "netconfMessages/get.xml");
568     }
569
570     private int getElementsSize(Element response, String elementName) {
571         return response.getElementsByTagName(elementName).getLength();
572     }
573
574     private Element executeOp(final NetconfOperation op, final String filename) throws ParserConfigurationException,
575             SAXException, IOException, NetconfDocumentedException {
576
577         final Document request = XmlFileLoader.xmlFileToDocument(filename);
578
579         logger.debug("Executing netconf operation\n{}", XmlUtil.toString(request));
580         HandlingPriority priority = op.canHandle(request);
581
582         Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
583
584         final Document response = op.handle(request, netconfOperationRouter);
585         logger.debug("Got response\n{}", XmlUtil.toString(response));
586         return response.getDocumentElement();
587     }
588
589     private List<InputStream> getYangs() throws FileNotFoundException {
590         List<String> paths = Arrays.asList("/META-INF/yang/config.yang", "/META-INF/yang/rpc-context.yang",
591                 "/META-INF/yang/config-test.yang", "/META-INF/yang/config-test-impl.yang", "/META-INF/yang/test-types.yang",
592                 "/META-INF/yang/ietf-inet-types.yang");
593         final Collection<InputStream> yangDependencies = new ArrayList<>();
594         for (String path : paths) {
595             final InputStream is = Preconditions
596                     .checkNotNull(getClass().getResourceAsStream(path), path + " not found");
597             yangDependencies.add(is);
598         }
599         return Lists.newArrayList(yangDependencies);
600     }
601
602     private void setModule(final NetconfTestImplModuleMXBean mxBean, final ConfigTransactionJMXClient transaction, String depName)
603             throws InstanceAlreadyExistsException, InstanceNotFoundException {
604         mxBean.setSimpleInt((long) 44);
605         mxBean.setBinaryLeaf(new byte[] { 8, 7, 9 });
606         final DtoD dtob = getDtoD();
607         mxBean.setDtoD(dtob);
608         //
609         final DtoC dtoa = getDtoC();
610         mxBean.setDtoC(dtoa);
611         mxBean.setSimpleBoolean(false);
612         //
613         final Peers p1 = new Peers();
614         p1.setCoreSize(44L);
615         p1.setPort("port1");
616         p1.setSimpleInt3(456);
617         final Peers p2 = new Peers();
618         p2.setCoreSize(44L);
619         p2.setPort("port23");
620         p2.setSimpleInt3(456);
621         mxBean.setPeers(Lists.<Peers> newArrayList(p1, p2));
622         // //
623         mxBean.setSimpleLong(454545L);
624         mxBean.setSimpleLong2(44L);
625         mxBean.setSimpleBigInteger(BigInteger.valueOf(999L));
626         mxBean.setSimpleByte(new Byte((byte) 4));
627         mxBean.setSimpleShort(new Short((short) 4));
628         mxBean.setSimpleTest(545);
629
630         mxBean.setComplexList(Lists.<ComplexList> newArrayList());
631         mxBean.setSimpleList(Lists.<Integer> newArrayList());
632
633         final ObjectName testingDepOn = transaction.createModule(this.factory2.getImplementationName(), depName);
634         int i = 1;
635         for (Class<? extends AbstractServiceInterface> sInterface : factory2.getImplementedServiceIntefaces()) {
636             ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
637             transaction.saveServiceReference(
638                     transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + depName + "_" + i++,
639                     testingDepOn);
640
641         }
642         mxBean.setTestingDep(testingDepOn);
643     }
644
645     private static DtoD getDtoD() {
646         final DtoD dtob = new DtoD();
647         dtob.setSimpleInt1((long) 444);
648         dtob.setSimpleInt2((long) 4444);
649         dtob.setSimpleInt3(454);
650         final ComplexDtoBInner dtobInner = new ComplexDtoBInner();
651         final Deep deep = new Deep();
652         deep.setSimpleInt3(4);
653         dtobInner.setDeep(deep);
654         dtobInner.setSimpleInt3(44);
655         dtobInner.setSimpleList(Lists.newArrayList(4));
656         dtob.setComplexDtoBInner(Lists.newArrayList(dtobInner));
657         dtob.setSimpleList(Lists.newArrayList(4));
658         return dtob;
659     }
660
661     private static DtoC getDtoC() {
662         final DtoC dtoa = new DtoC();
663         // dtoa.setSimpleArg((long) 55);
664         final DtoAInner dtoAInner = new DtoAInner();
665         final DtoAInnerInner dtoAInnerInner = new DtoAInnerInner();
666         dtoAInnerInner.setSimpleArg(456L);
667         dtoAInner.setDtoAInnerInner(dtoAInnerInner);
668         dtoAInner.setSimpleArg(44L);
669         dtoa.setDtoAInner(dtoAInner);
670         return dtoa;
671     }
672
673 }