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