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