Merge "BUG 718: changed codec for URI parsing"
[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.ConflictingVersionException;
24 import org.opendaylight.controller.config.api.ValidationException;
25 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
26 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
27 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
28 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
29 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
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.IdentityTestModuleFactory;
39 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleFactory;
40 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleMXBean;
41 import org.opendaylight.controller.config.yang.test.impl.Peers;
42 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
43 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
44 import org.opendaylight.controller.netconf.api.NetconfOperationRouter;
45 import org.opendaylight.controller.netconf.confignetconfconnector.operations.Commit;
46 import org.opendaylight.controller.netconf.confignetconfconnector.operations.DiscardChanges;
47 import org.opendaylight.controller.netconf.confignetconfconnector.operations.editconfig.EditConfig;
48 import org.opendaylight.controller.netconf.confignetconfconnector.operations.get.Get;
49 import org.opendaylight.controller.netconf.confignetconfconnector.operations.getconfig.GetConfig;
50 import org.opendaylight.controller.netconf.confignetconfconnector.operations.runtimerpc.RuntimeRpc;
51 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreServiceImpl;
52 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreSnapshot;
53 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
54 import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCloseSession;
55 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceSnapshotImpl;
56 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
57 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
58 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
59 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
60 import org.opendaylight.controller.netconf.util.xml.XmlElement;
61 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
62 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity1;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.config.test.types.rev131127.TestIdentity2;
65 import org.opendaylight.yangtools.yang.data.impl.codec.CodecRegistry;
66 import org.opendaylight.yangtools.yang.data.impl.codec.IdentityCodec;
67 import org.opendaylight.yangtools.yang.model.api.Module;
68 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
69 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
70 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
73 import org.w3c.dom.Document;
74 import org.w3c.dom.Element;
75 import org.w3c.dom.NodeList;
76 import org.xml.sax.SAXException;
77
78 import javax.management.InstanceAlreadyExistsException;
79 import javax.management.InstanceNotFoundException;
80 import javax.management.ObjectName;
81 import javax.xml.parsers.ParserConfigurationException;
82 import java.io.FileNotFoundException;
83 import java.io.IOException;
84 import java.io.InputStream;
85 import java.math.BigInteger;
86 import java.net.URISyntaxException;
87 import java.util.ArrayList;
88 import java.util.Arrays;
89 import java.util.Collection;
90 import java.util.HashSet;
91 import java.util.List;
92 import java.util.Map;
93 import java.util.Set;
94
95 import static org.junit.Assert.assertEquals;
96 import static org.junit.Assert.assertThat;
97 import static org.junit.Assert.assertTrue;
98 import static org.junit.Assert.fail;
99 import static org.mockito.Mockito.doNothing;
100 import static org.mockito.Mockito.doReturn;
101 import static org.mockito.Mockito.mock;
102 import static org.mockito.Mockito.verify;
103 import static org.mockito.Mockito.verifyNoMoreInteractions;
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     NetconfOperationServiceSnapshotImpl 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
594         YangParserImpl yangParser = new YangParserImpl();
595         final SchemaContext schemaContext = yangParser.resolveSchemaContext(new HashSet<>(yangParser.parseYangModelsFromStreamsMapped(yangDependencies).values()));
596         YangStoreServiceImpl yangStoreService = new YangStoreServiceImpl(new SchemaContextProvider() {
597             @Override
598             public SchemaContext getSchemaContext() {
599                 return schemaContext ;
600             }
601         });
602         mBeanEntries.putAll(yangStoreService.getYangStoreSnapshot().getModuleMXBeanEntryMap());
603
604         return mBeanEntries;
605     }
606
607     private Set<org.opendaylight.yangtools.yang.model.api.Module> getModules() throws Exception {
608         SchemaContext resolveSchemaContext = getSchemaContext();
609         return resolveSchemaContext.getModules();
610     }
611
612     private SchemaContext getSchemaContext() throws Exception {
613         final List<InputStream> yangDependencies = getYangs();
614         YangParserImpl parser = new YangParserImpl();
615
616         Set<Module> allYangModules = parser.parseYangModelsFromStreams(yangDependencies);
617
618         return parser.resolveSchemaContext(Sets
619                 .newHashSet(allYangModules));
620     }
621
622     @Test
623     public void testConfigNetconfRuntime() throws Exception {
624
625         createModule(INSTANCE_NAME);
626
627         edit("netconfMessages/editConfig.xml");
628         checkBinaryLeafEdited(getConfigCandidate());
629
630         // check after edit
631         commit();
632         Element response = get();
633
634         assertEquals(2/*With runtime beans*/ + 2 /*Without runtime beans*/, getElementsSize(response, "module"));
635         // data from state
636         assertEquals(2, getElementsSize(response, "asdf"));
637         // data from running config
638         assertEquals(2, getElementsSize(response, "simple-short"));
639
640         assertEquals(8, getElementsSize(response, "inner-running-data"));
641         assertEquals(8, getElementsSize(response, "deep2"));
642         assertEquals(8 * 4, getElementsSize(response, "inner-inner-running-data"));
643         assertEquals(8 * 4, getElementsSize(response, "deep3"));
644         assertEquals(8 * 4 * 2, getElementsSize(response, "list-of-strings"));
645         assertEquals(8, getElementsSize(response, "inner-running-data-additional"));
646         assertEquals(8, getElementsSize(response, "deep4"));
647         // TODO assert keys
648
649         RuntimeRpc netconf = new RuntimeRpc(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID);
650
651         response = executeOp(netconf, "netconfMessages/rpc.xml");
652         assertContainsString(XmlUtil.toString(response), "testarg1".toUpperCase());
653
654         response = executeOp(netconf, "netconfMessages/rpcInner.xml");
655         assertContainsString(XmlUtil.toString(response), "ok");
656
657         response = executeOp(netconf, "netconfMessages/rpcInnerInner.xml");
658         assertContainsString(XmlUtil.toString(response), "true");
659
660         response = executeOp(netconf, "netconfMessages/rpcInnerInner_complex_output.xml");
661         assertContainsString(XmlUtil.toString(response), "1");
662         assertContainsString(XmlUtil.toString(response), "2");
663     }
664
665     private Element get() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
666         Get getOp = new Get(yangStoreSnapshot, configRegistryClient, NETCONF_SESSION_ID, transactionProvider);
667         return executeOp(getOp, "netconfMessages/get.xml");
668     }
669
670     private int getElementsSize(Element response, String elementName) {
671         return response.getElementsByTagName(elementName).getLength();
672     }
673
674     private Element executeOp(final NetconfOperation op, final String filename) throws ParserConfigurationException,
675             SAXException, IOException, NetconfDocumentedException {
676
677         final Document request = XmlFileLoader.xmlFileToDocument(filename);
678
679         logger.debug("Executing netconf operation\n{}", XmlUtil.toString(request));
680         HandlingPriority priority = op.canHandle(request);
681
682         Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
683
684         final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
685         logger.debug("Got response\n{}", XmlUtil.toString(response));
686         return response.getDocumentElement();
687     }
688
689     private List<InputStream> getYangs() throws FileNotFoundException {
690         List<String> paths = Arrays.asList("/META-INF/yang/config.yang", "/META-INF/yang/rpc-context.yang",
691                 "/META-INF/yang/config-test.yang", "/META-INF/yang/config-test-impl.yang", "/META-INF/yang/test-types.yang",
692                 "/META-INF/yang/ietf-inet-types.yang");
693         final Collection<InputStream> yangDependencies = new ArrayList<>();
694         for (String path : paths) {
695             final InputStream is = Preconditions
696                     .checkNotNull(getClass().getResourceAsStream(path), path + " not found");
697             yangDependencies.add(is);
698         }
699         return Lists.newArrayList(yangDependencies);
700     }
701
702     private void setModule(final NetconfTestImplModuleMXBean mxBean, final ConfigTransactionJMXClient transaction, String depName)
703             throws InstanceAlreadyExistsException, InstanceNotFoundException {
704         mxBean.setSimpleInt((long) 44);
705         mxBean.setBinaryLeaf(new byte[] { 8, 7, 9 });
706         final DtoD dtob = getDtoD();
707         mxBean.setDtoD(dtob);
708         //
709         final DtoC dtoa = getDtoC();
710         mxBean.setDtoC(dtoa);
711         mxBean.setSimpleBoolean(false);
712         //
713         final Peers p1 = new Peers();
714         p1.setCoreSize(44L);
715         p1.setPort("port1");
716         p1.setSimpleInt3(456);
717         final Peers p2 = new Peers();
718         p2.setCoreSize(44L);
719         p2.setPort("port23");
720         p2.setSimpleInt3(456);
721         mxBean.setPeers(Lists.<Peers> newArrayList(p1, p2));
722         // //
723         mxBean.setSimpleLong(454545L);
724         mxBean.setSimpleLong2(44L);
725         mxBean.setSimpleBigInteger(BigInteger.valueOf(999L));
726         mxBean.setSimpleByte(new Byte((byte) 4));
727         mxBean.setSimpleShort(new Short((short) 4));
728         mxBean.setSimpleTest(545);
729
730         mxBean.setComplexList(Lists.<ComplexList> newArrayList());
731         mxBean.setSimpleList(Lists.<Integer> newArrayList());
732
733         final ObjectName testingDepOn = transaction.createModule(this.factory2.getImplementationName(), depName);
734         int i = 1;
735         for (Class<? extends AbstractServiceInterface> sInterface : factory2.getImplementedServiceIntefaces()) {
736             ServiceInterfaceAnnotation annotation = sInterface.getAnnotation(ServiceInterfaceAnnotation.class);
737             transaction.saveServiceReference(
738                     transaction.getServiceInterfaceName(annotation.namespace(), annotation.localName()), "ref_from_code_to_" + depName + "_" + i++,
739                     testingDepOn);
740
741         }
742         mxBean.setTestingDep(testingDepOn);
743     }
744
745     private static DtoD getDtoD() {
746         final DtoD dtob = new DtoD();
747         dtob.setSimpleInt1((long) 444);
748         dtob.setSimpleInt2((long) 4444);
749         dtob.setSimpleInt3(454);
750         final ComplexDtoBInner dtobInner = new ComplexDtoBInner();
751         final Deep deep = new Deep();
752         deep.setSimpleInt3(4);
753         dtobInner.setDeep(deep);
754         dtobInner.setSimpleInt3(44);
755         dtobInner.setSimpleList(Lists.newArrayList(4));
756         dtob.setComplexDtoBInner(Lists.newArrayList(dtobInner));
757         dtob.setSimpleList(Lists.newArrayList(4));
758         return dtob;
759     }
760
761     private static DtoC getDtoC() {
762         final DtoC dtoa = new DtoC();
763         // dtoa.setSimpleArg((long) 55);
764         final DtoAInner dtoAInner = new DtoAInner();
765         final DtoAInnerInner dtoAInnerInner = new DtoAInnerInner();
766         dtoAInnerInner.setSimpleArg(456L);
767         dtoAInner.setDtoAInnerInner(dtoAInnerInner);
768         dtoAInner.setSimpleArg(44L);
769         dtoa.setDtoAInner(dtoAInner);
770         return dtoa;
771     }
772
773 }