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