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