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