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