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