Merge "Add test for generated code checking list of dependencies."
[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
147         edit("netconfMessages/editConfig_addServiceName.xml");
148         config = getConfigCandidate();
149         assertCorrectServiceNames(config, 7, "ref_test2", "user_to_instance_from_code", "ref_dep_user",
150                 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
151                 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another");
152
153         edit("netconfMessages/editConfig_addServiceNameOnTest.xml");
154         config = getConfigCandidate();
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         commit();
160         config = getConfigRunning();
161         assertCorrectRefNamesForDependencies(config);
162         assertCorrectServiceNames(config, 7, "ref_test2", "user_to_instance_from_code", "ref_dep_user",
163                 "ref_dep_user_two", "ref_from_code_to_instance-from-code_dep_1",
164                 "ref_from_code_to_instance-from-code_1", "ref_dep_user_another");
165
166         edit("netconfMessages/editConfig_replace_default.xml");
167         config = getConfigCandidate();
168         assertCorrectServiceNames(config, 2, "ref_dep", "ref_dep2");
169
170         edit("netconfMessages/editConfig_remove.xml");
171         config = getConfigCandidate();
172         assertCorrectServiceNames(config, 0);
173
174         commit();
175         config = getConfigCandidate();
176         assertCorrectServiceNames(config, 0);
177
178     }
179
180     private void assertCorrectRefNamesForDependencies(Element config) {
181         NodeList modulesList = config.getElementsByTagName("modules");
182         assertEquals(1, modulesList.getLength());
183
184         Element modules = (Element) modulesList.item(0);
185
186         String trimmedModules = XmlUtil.toString(modules).replaceAll("\\s", "");
187         int defaultRefNameCount = StringUtils.countMatches(trimmedModules, "ref_dep2");
188         int userRefNameCount = StringUtils.countMatches(trimmedModules, "ref_dep_user_two");
189
190         assertEquals(0, defaultRefNameCount);
191         assertEquals(2, userRefNameCount);
192     }
193
194     private void assertCorrectServiceNames(Element configCandidate, int servicesSize, String... refNames) {
195         NodeList elements = configCandidate.getElementsByTagName("provider");
196         assertEquals(servicesSize, elements.getLength());
197
198         NodeList servicesList = configCandidate.getElementsByTagName("services");
199         assertEquals(1, servicesList.getLength());
200
201         Element services = (Element) servicesList.item(0);
202         String trimmedServices = XmlUtil.toString(services).replaceAll("\\s", "");
203
204         for (String s : refNames) {
205             assertThat(trimmedServices, JUnitMatchers.containsString(s));
206         }
207     }
208
209     @Test
210     public void testConfigNetconfUnionTypes() throws Exception {
211
212         createModule(INSTANCE_NAME);
213
214         edit("netconfMessages/editConfig.xml");
215         commit();
216         Element response = getConfigRunning();
217         String trimmedResponse = XmlUtil.toString(response).replaceAll("\\s", "");
218         assertContainsString(trimmedResponse, "<ipxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">0:0:0:0:0:0:0:1</ip>");
219         assertContainsString(trimmedResponse, "<union-test-attrxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">456</union-test-attr>");
220
221
222         edit("netconfMessages/editConfig_setUnions.xml");
223         commit();
224         response = getConfigRunning();
225
226         trimmedResponse = XmlUtil.toString(response).replaceAll("\\s", "");
227         assertContainsString(trimmedResponse, "<ipxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">127.1.2.3</ip>");
228         assertContainsString(trimmedResponse, "<union-test-attrxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">randomStringForUnion</union-test-attr>");
229
230     }
231
232     @Test
233     public void testConfigNetconf() throws Exception {
234
235         createModule(INSTANCE_NAME);
236
237         edit("netconfMessages/editConfig.xml");
238         Element configCandidate = getConfigCandidate();
239         System.err.println(XmlUtil.toString(configCandidate));
240         checkBinaryLeafEdited(configCandidate);
241
242
243         // default-operation:none, should not affect binary leaf
244         edit("netconfMessages/editConfig_none.xml");
245         checkBinaryLeafEdited(getConfigCandidate());
246
247         // check after edit
248         commit();
249         Element response = getConfigRunning();
250
251         checkBinaryLeafEdited(response);
252         checkTypeConfigAttribute(response);
253         checkTypedefs(response);
254         checkTestingDeps(response);
255         checkEnum(response);
256         checkBigDecimal(response);
257
258         edit("netconfMessages/editConfig_remove.xml");
259
260         commit();
261         response = getConfigCandidate();
262         final String responseFromCandidate = XmlUtil.toString(response).replaceAll("\\s+", "");
263         // System.out.println(responseFromCandidate);
264         response = getConfigRunning();
265         final String responseFromRunning = XmlUtil.toString(response).replaceAll("\\s+", "");
266         // System.out.println(responseFromRunning);
267         assertEquals(responseFromCandidate, responseFromRunning);
268
269         final String expectedResult = XmlFileLoader.fileToString("netconfMessages/editConfig_expectedResult.xml")
270                 .replaceAll("\\s+", "");
271
272         assertEquals(expectedResult, responseFromRunning);
273         assertEquals(expectedResult, responseFromCandidate);
274
275         edit("netconfMessages/editConfig_none.xml");
276         doNothing().when(netconfOperationRouter).close();
277         closeSession();
278         verify(netconfOperationRouter).close();
279         verifyNoMoreInteractions(netconfOperationRouter);
280     }
281
282     private void checkBigDecimal(Element response) {
283         String responseTrimmed = XmlUtil.toString(response).replaceAll("\\s", "");
284
285         assertContainsString(responseTrimmed, "<sleep-factorxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.58</sleep-factor>");
286         // Default
287         assertContainsString(responseTrimmed, "<sleep-factorxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2.00</sleep-factor>");
288
289     }
290
291     private void closeSession() throws NetconfDocumentedException, ParserConfigurationException, SAXException,
292             IOException {
293         DefaultCloseSession closeOp = new DefaultCloseSession(NETCONF_SESSION_ID);
294         executeOp(closeOp, "netconfMessages/closeSession.xml");
295     }
296
297     private void edit(String resource) throws ParserConfigurationException, SAXException, IOException,
298             NetconfDocumentedException {
299         EditConfig editOp = new EditConfig(yangStoreSnapshot, transactionProvider, configRegistryClient,
300                 NETCONF_SESSION_ID);
301         executeOp(editOp, resource);
302     }
303
304     private void commit() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
305         Commit commitOp = new Commit(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
306         executeOp(commitOp, "netconfMessages/commit.xml");
307     }
308
309     private Element getConfigCandidate() throws ParserConfigurationException, SAXException, IOException,
310             NetconfDocumentedException {
311         GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
312                 configRegistryClient, NETCONF_SESSION_ID);
313         return executeOp(getConfigOp, "netconfMessages/getConfig_candidate.xml");
314     }
315
316     private Element getConfigRunning() throws ParserConfigurationException, SAXException, IOException,
317             NetconfDocumentedException {
318         GetConfig getConfigOp = new GetConfig(yangStoreSnapshot, Optional.<String> absent(), transactionProvider,
319                 configRegistryClient, NETCONF_SESSION_ID);
320         return executeOp(getConfigOp, "netconfMessages/getConfig.xml");
321     }
322
323     @Ignore("second edit message corrupted")
324     @Test(expected = NetconfDocumentedException.class)
325     public void testConfigNetconfReplaceDefaultEx() throws Exception {
326
327         createModule(INSTANCE_NAME);
328
329         edit("netconfMessages/editConfig.xml");
330         edit("netconfMessages/editConfig_replace_default_ex.xml");
331     }
332
333     @Test
334     public void testConfigNetconfReplaceDefault() throws Exception {
335
336         createModule(INSTANCE_NAME);
337
338         edit("netconfMessages/editConfig.xml");
339         commit();
340         Element response = getConfigRunning();
341         final int allInstances = response.getElementsByTagName("module").getLength();
342
343         edit("netconfMessages/editConfig_replace_default.xml");
344
345         commit();
346         response = getConfigRunning();
347
348         final int afterReplace = response.getElementsByTagName("module").getLength();
349         assertEquals(4, allInstances);
350         assertEquals(2, afterReplace);
351     }
352
353     @Test(expected = NetconfDocumentedException.class)
354     public void testSameAttrDifferentNamespaces() throws Exception {
355         try {
356             edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespaces.xml");
357         } catch (NetconfDocumentedException e) {
358             String message = e.getMessage();
359             assertContainsString(message, "Element simple-long-2 present multiple times with different namespaces");
360             assertContainsString(message, "urn:opendaylight:params:xml:ns:yang:controller:test:impl");
361             assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
362             throw e;
363         }
364     }
365
366     @Test(expected = NetconfDocumentedException.class)
367     public void testDifferentNamespaceInTO() throws Exception {
368         try {
369             edit("netconfMessages/namespaces/editConfig_differentNamespaceTO.xml");
370         } catch (NetconfDocumentedException e) {
371             String message = e.getMessage();
372             assertContainsString(message, "Unrecognised elements");
373             assertContainsString(message, "simple-int2");
374             assertContainsString(message, "dto_d");
375             throw e;
376         }
377     }
378
379     @Test(expected = NetconfDocumentedException.class)
380     public void testSameAttrDifferentNamespacesList() throws Exception {
381         try {
382             edit("netconfMessages/namespaces/editConfig_sameAttrDifferentNamespacesList.xml");
383         } catch (NetconfDocumentedException e) {
384             String message = e.getMessage();
385             assertContainsString(message, "Element binaryLeaf present multiple times with different namespaces");
386             assertContainsString(message, "urn:opendaylight:params:xml:ns:yang:controller:test:impl");
387             assertContainsString(message, XmlNetconfConstants.URN_OPENDAYLIGHT_PARAMS_XML_NS_YANG_CONTROLLER_CONFIG);
388             throw e;
389         }
390     }
391
392     @Test
393     public void testTypeNameConfigAttributeMatching() throws Exception {
394         edit("netconfMessages/editConfig.xml");
395         commit();
396         edit("netconfMessages/namespaces/editConfig_typeNameConfigAttributeMatching.xml");
397         commit();
398
399         Element response = getConfigRunning();
400         checkTypeConfigAttribute(response);
401     }
402
403     // TODO add <modules operation="replace"> functionality
404     @Test(expected = NetconfDocumentedException.class)
405     public void testConfigNetconfReplaceModuleEx() throws Exception {
406
407         createModule(INSTANCE_NAME);
408
409         edit("netconfMessages/editConfig.xml");
410         edit("netconfMessages/editConfig_replace_module_ex.xml");
411     }
412
413     @Test
414     public void testUnrecognisedConfigElements() throws Exception {
415
416         String format = "netconfMessages/unrecognised/editConfig_unrecognised%d.xml";
417         final int TESTS_COUNT = 8;
418
419         for (int i = 0; i < TESTS_COUNT; i++) {
420             String file = String.format(format, i + 1);
421             try {
422                 edit(file);
423             } catch (NetconfDocumentedException e) {
424                 assertContainsString(e.getMessage(), "Unrecognised elements");
425                 assertContainsString(e.getMessage(), "unknownAttribute");
426                 continue;
427             }
428             fail("Unrecognised test should throw exception " + file);
429         }
430     }
431
432     @Test
433     @Ignore
434     // FIXME
435     public void testConfigNetconfReplaceModule() throws Exception {
436
437         createModule(INSTANCE_NAME);
438
439         edit("netconfMessages/editConfig.xml");
440         commit();
441         Element response = getConfigRunning();
442         final int allInstances = response.getElementsByTagName("instance").getLength();
443
444         edit("netconfMessages/editConfig_replace_module.xml");
445
446         commit();
447         response = getConfigRunning();
448         final int afterReplace = response.getElementsByTagName("instance").getLength();
449
450         assertEquals(4 + 4 /* Instances from services */, allInstances);
451         assertEquals(3 + 3, afterReplace);
452     }
453
454     @Test(expected = NetconfDocumentedException.class)
455     public void testEx() throws Exception {
456
457         commit();
458     }
459
460     @Test(expected = NetconfDocumentedException.class)
461     public void testEx2() throws Exception {
462         discard();
463     }
464
465     private void discard() throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
466         DiscardChanges discardOp = new DiscardChanges(transactionProvider, configRegistryClient, NETCONF_SESSION_ID);
467         executeOp(discardOp, "netconfMessages/discardChanges.xml");
468     }
469
470     private void checkBinaryLeafEdited(final Element response) {
471         String responseTrimmed = XmlUtil.toString(response).replaceAll("\\s", "");
472         String substring = "<binaryLeafxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">YmluYXJ5</binaryLeaf>";
473         assertContainsString(responseTrimmed, substring);
474         substring = "<binaryLeafxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ZGVmYXVsdEJpbg==</binaryLeaf>";
475         assertContainsString(responseTrimmed, substring);
476     }
477
478     private void checkTypedefs(final Element response) {
479         String responseTrimmed = XmlUtil.toString(response).replaceAll("\\s", "");
480
481         String substring = "<extendedxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">10</extended>";
482         assertContainsString(responseTrimmed, substring);
483         // Default
484         assertContainsString(responseTrimmed,
485                 "<extendedxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">1</extended>");
486
487         assertContainsString(responseTrimmed,
488                 "<extended-twicexmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">20</extended-twice>");
489         // Default
490         assertContainsString(responseTrimmed,
491                 "<extended-twicexmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">2</extended-twice>");
492
493         assertContainsString(responseTrimmed,
494                 "<extended-enumxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">TWO</extended-enum>");
495         // Default
496         assertContainsString(responseTrimmed,
497                 "<extended-enumxmlns=\"urn:opendaylight:params:xml:ns:yang:controller:test:impl\">ONE</extended-enum>");
498     }
499
500     private void assertContainsString(String string, String substring) {
501         assertThat(string, JUnitMatchers.containsString(substring));
502     }
503
504     private void checkEnum(final Element response) {
505         XmlElement modulesElement = XmlElement.fromDomElement(response).getOnlyChildElement("data")
506                 .getOnlyChildElement("modules");
507
508         String enumName = "extended-enum";
509         String enumContent = "TWO";
510
511         for (XmlElement moduleElement : modulesElement.getChildElements("module")) {
512             String name = moduleElement.getOnlyChildElement("name").getTextContent();
513             if(name.equals(INSTANCE_NAME)) {
514                 XmlElement enumAttr = moduleElement.getOnlyChildElement(enumName);
515                 assertEquals(enumContent, enumAttr.getTextContent());
516
517                 return;
518             }
519         }
520
521         fail("Enum attribute " + enumName + ":" + enumContent + " not present in " + XmlUtil.toString(response));
522     }
523
524     private void checkTestingDeps(Element response) {
525         int testingDepsSize = response.getElementsByTagName("testing-deps").getLength();
526         assertEquals(2, testingDepsSize);
527     }
528
529     private void checkTypeConfigAttribute(Element response) {
530
531         XmlElement modulesElement = XmlElement.fromDomElement(response).getOnlyChildElement("data")
532                 .getOnlyChildElement("modules");
533
534         List<String> expectedValues = Lists.newArrayList("default-string", "configAttributeType");
535         Set<String> configAttributeType = Sets.newHashSet();
536
537         for (XmlElement moduleElement : modulesElement.getChildElements("module")) {
538             for (XmlElement type : moduleElement.getChildElements("type")) {
539                 if (type.getAttribute(XmlUtil.XMLNS_ATTRIBUTE_KEY).equals("") == false) {
540                     configAttributeType.add(type.getTextContent());
541                 }
542             }
543         }
544
545         for (String expectedValue : expectedValues) {
546             assertTrue(configAttributeType.contains(expectedValue));
547         }
548     }
549
550     private Map<String, Map<String, ModuleMXBeanEntry>> getMbes() throws Exception {
551         final List<InputStream> yangDependencies = getYangs();
552
553         final Map<String, Map<String, ModuleMXBeanEntry>> mBeanEntries = Maps.newHashMap();
554         mBeanEntries.putAll(new MbeParser().parseYangFiles(yangDependencies).getModuleMXBeanEntryMap());
555
556         return mBeanEntries;
557     }
558
559     @Test
560     public void testConfigNetconfRuntime() throws Exception {
561
562         createModule(INSTANCE_NAME);
563
564         edit("netconfMessages/editConfig.xml");
565         checkBinaryLeafEdited(getConfigCandidate());
566
567         // check after edit
568         commit();
569         Element response = get();
570
571         assertEquals(2/*With runtime beans*/ + 2 /*Without runtime beans*/, getElementsSize(response, "module"));
572         // data from state
573         assertEquals(2, getElementsSize(response, "asdf"));
574         // data from running config
575         assertEquals(2, getElementsSize(response, "simple-short"));
576
577         assertEquals(8, getElementsSize(response, "inner-running-data"));
578         assertEquals(8, getElementsSize(response, "deep2"));
579         assertEquals(8 * 4, getElementsSize(response, "inner-inner-running-data"));
580         assertEquals(8 * 4, getElementsSize(response, "deep3"));
581         assertEquals(8 * 4 * 2, getElementsSize(response, "list-of-strings"));
582         assertEquals(8, getElementsSize(response, "inner-running-data-additional"));
583         assertEquals(8, getElementsSize(response, "deep4"));
584         // TODO assert keys
585
586         RuntimeRpc netconf = new RuntimeRpc(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID);
587
588         response = executeOp(netconf, "netconfMessages/rpc.xml");
589         assertContainsString(XmlUtil.toString(response), "testarg1".toUpperCase());
590
591         response = executeOp(netconf, "netconfMessages/rpcInner.xml");
592         assertContainsString(XmlUtil.toString(response), "ok");
593
594         response = executeOp(netconf, "netconfMessages/rpcInnerInner.xml");
595         assertContainsString(XmlUtil.toString(response), "true");
596
597         response = executeOp(netconf, "netconfMessages/rpcInnerInner_complex_output.xml");
598         assertContainsString(XmlUtil.toString(response), "1");
599         assertContainsString(XmlUtil.toString(response), "2");
600     }
601
602     private Element get() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
603         Get getOp = new Get(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID, transactionProvider);
604         return executeOp(getOp, "netconfMessages/get.xml");
605     }
606
607     private int getElementsSize(Element response, String elementName) {
608         return response.getElementsByTagName(elementName).getLength();
609     }
610
611     private Element executeOp(final NetconfOperation op, final String filename) throws ParserConfigurationException,
612             SAXException, IOException, NetconfDocumentedException {
613
614         final Document request = XmlFileLoader.xmlFileToDocument(filename);
615
616         logger.debug("Executing netconf operation\n{}", XmlUtil.toString(request));
617         HandlingPriority priority = op.canHandle(request);
618
619         Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
620
621         final Document response = op.handle(request, netconfOperationRouter);
622         logger.debug("Got response\n{}", XmlUtil.toString(response));
623         return response.getDocumentElement();
624     }
625
626     private List<InputStream> getYangs() throws FileNotFoundException {
627         List<String> paths = Arrays.asList("/META-INF/yang/config.yang", "/META-INF/yang/rpc-context.yang",
628                 "/META-INF/yang/config-test.yang", "/META-INF/yang/config-test-impl.yang", "/META-INF/yang/test-types.yang",
629                 "/META-INF/yang/ietf-inet-types.yang");
630         final Collection<InputStream> yangDependencies = new ArrayList<>();
631         for (String path : paths) {
632             final InputStream is = Preconditions
633                     .checkNotNull(getClass().getResourceAsStream(path), path + " not found");
634             yangDependencies.add(is);
635         }
636         return Lists.newArrayList(yangDependencies);
637     }
638
639     private void setModule(final NetconfTestImplModuleMXBean mxBean, final ConfigTransactionJMXClient transaction, String depName)
640             throws InstanceAlreadyExistsException, InstanceNotFoundException {
641         mxBean.setSimpleInt((long) 44);
642         mxBean.setBinaryLeaf(new byte[] { 8, 7, 9 });
643         final DtoD dtob = getDtoD();
644         mxBean.setDtoD(dtob);
645         //
646         final DtoC dtoa = getDtoC();
647         mxBean.setDtoC(dtoa);
648         mxBean.setSimpleBoolean(false);
649         //
650         final Peers p1 = new Peers();
651         p1.setCoreSize(44L);
652         p1.setPort("port1");
653         p1.setSimpleInt3(456);
654         final Peers p2 = new Peers();
655         p2.setCoreSize(44L);
656         p2.setPort("port23");
657         p2.setSimpleInt3(456);
658         mxBean.setPeers(Lists.<Peers> newArrayList(p1, p2));
659         // //
660         mxBean.setSimpleLong(454545L);
661         mxBean.setSimpleLong2(44L);
662         mxBean.setSimpleBigInteger(BigInteger.valueOf(999L));
663         mxBean.setSimpleByte(new Byte((byte) 4));
664         mxBean.setSimpleShort(new Short((short) 4));
665         mxBean.setSimpleTest(545);
666
667         mxBean.setComplexList(Lists.<ComplexList> newArrayList());
668         mxBean.setSimpleList(Lists.<Integer> newArrayList());
669
670         final ObjectName testingDepOn = transaction.createModule(this.factory2.getImplementationName(), depName);
671         int i = 1;
672         for (Class<? extends AbstractServiceInterface> sInterface : factory2.getImplementedServiceIntefaces()) {
673             ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
674             transaction.saveServiceReference(
675                     transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + depName + "_" + i++,
676                     testingDepOn);
677
678         }
679         mxBean.setTestingDep(testingDepOn);
680     }
681
682     private static DtoD getDtoD() {
683         final DtoD dtob = new DtoD();
684         dtob.setSimpleInt1((long) 444);
685         dtob.setSimpleInt2((long) 4444);
686         dtob.setSimpleInt3(454);
687         final ComplexDtoBInner dtobInner = new ComplexDtoBInner();
688         final Deep deep = new Deep();
689         deep.setSimpleInt3(4);
690         dtobInner.setDeep(deep);
691         dtobInner.setSimpleInt3(44);
692         dtobInner.setSimpleList(Lists.newArrayList(4));
693         dtob.setComplexDtoBInner(Lists.newArrayList(dtobInner));
694         dtob.setSimpleList(Lists.newArrayList(4));
695         return dtob;
696     }
697
698     private static DtoC getDtoC() {
699         final DtoC dtoa = new DtoC();
700         // dtoa.setSimpleArg((long) 55);
701         final DtoAInner dtoAInner = new DtoAInner();
702         final DtoAInnerInner dtoAInnerInner = new DtoAInnerInner();
703         dtoAInnerInner.setSimpleArg(456L);
704         dtoAInner.setDtoAInnerInner(dtoAInnerInner);
705         dtoAInner.setSimpleArg(44L);
706         dtoa.setDtoAInner(dtoAInner);
707         return dtoa;
708     }
709
710 }