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