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