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