Adjust tests for yangtools changes
[netconf.git] / netconf / mdsal-netconf-connector / src / test / java / org / opendaylight / netconf / mdsal / connector / ops / NetconfMDSalMappingTest.java
1 /*
2  * Copyright (c) 2015 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.netconf.mdsal.connector.ops;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.mockito.Matchers.any;
16 import static org.mockito.Mockito.doAnswer;
17
18 import com.google.common.base.Preconditions;
19 import com.google.common.io.ByteSource;
20 import com.google.common.util.concurrent.Futures;
21 import java.io.InputStream;
22 import java.io.StringWriter;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.EnumMap;
26 import java.util.List;
27 import java.util.concurrent.ExecutorService;
28 import javax.xml.transform.OutputKeys;
29 import javax.xml.transform.Transformer;
30 import javax.xml.transform.TransformerFactory;
31 import javax.xml.transform.dom.DOMSource;
32 import javax.xml.transform.stream.StreamResult;
33 import org.custommonkey.xmlunit.DetailedDiff;
34 import org.custommonkey.xmlunit.Diff;
35 import org.custommonkey.xmlunit.XMLUnit;
36 import org.junit.Assert;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mock;
40 import org.mockito.Mockito;
41 import org.mockito.MockitoAnnotations;
42 import org.opendaylight.controller.cluster.databroker.ConcurrentDOMDataBroker;
43 import org.opendaylight.controller.config.util.xml.DocumentedException;
44 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorSeverity;
45 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorTag;
46 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorType;
47 import org.opendaylight.controller.config.util.xml.XmlElement;
48 import org.opendaylight.controller.config.util.xml.XmlUtil;
49 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
50 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
51 import org.opendaylight.controller.sal.core.api.model.SchemaService;
52 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
53 import org.opendaylight.netconf.mapping.api.NetconfOperation;
54 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
55 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
56 import org.opendaylight.netconf.mdsal.connector.TransactionProvider;
57 import org.opendaylight.netconf.mdsal.connector.ops.get.Get;
58 import org.opendaylight.netconf.mdsal.connector.ops.get.GetConfig;
59 import org.opendaylight.netconf.util.test.NetconfXmlUnitRecursiveQualifier;
60 import org.opendaylight.netconf.util.test.XmlFileLoader;
61 import org.opendaylight.yangtools.concepts.ListenerRegistration;
62 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
63 import org.opendaylight.yangtools.yang.common.QName;
64 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
65 import org.opendaylight.yangtools.yang.model.api.Module;
66 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
67 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
68 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
69 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
70 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
71 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
72 import org.slf4j.Logger;
73 import org.slf4j.LoggerFactory;
74 import org.w3c.dom.Document;
75 import org.w3c.dom.Element;
76 import org.w3c.dom.Node;
77 import org.w3c.dom.NodeList;
78
79 public class NetconfMDSalMappingTest {
80     private static final Logger LOG = LoggerFactory.getLogger(NetconfMDSalMappingTest.class);
81
82     private static final String TARGET_KEY = "target";
83     private static final String RPC_REPLY_ELEMENT = "rpc-reply";
84     private static final String DATA_ELEMENT = "data";
85     private static final String FILTER_NODE = "filter";
86     private static final String GET_CONFIG = "get-config";
87     private static final QName TOP = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "top");
88     private static final QName USERS = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "users");
89     private static final QName USER = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "user");
90     private static final QName MODULES = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "modules");
91     private static final QName AUGMENTED_CONTAINER = QName.create("urn:opendaylight:mdsal:mapping:test",
92             "2015-02-26", "augmented-container");
93     private static final QName AUGMENTED_STRING_IN_CONT = QName.create("urn:opendaylight:mdsal:mapping:test",
94             "2015-02-26", "identifier");
95     private static final QName CHOICE_NODE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
96             "choice-node");
97     private static final QName AUGMENTED_CASE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
98             "augmented-case");
99     private static final QName CHOICE_WRAPPER = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
100             "choice-wrapper");
101     private static final QName INNER_CHOICE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
102             "inner-choice");
103     private static final QName INNER_CHOICE_TEXT = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
104             "text");
105
106     private static final YangInstanceIdentifier AUGMENTED_CONTAINER_IN_MODULES =
107             YangInstanceIdentifier.builder().node(TOP).node(MODULES).build();
108     private static final String SESSION_ID_FOR_REPORTING = "netconf-test-session1";
109     private static final Document RPC_REPLY_OK = NetconfMDSalMappingTest.getReplyOk();
110
111     private CurrentSchemaContext currentSchemaContext = null;
112     private SchemaContext schemaContext = null;
113     private TransactionProvider transactionProvider = null;
114
115     @Mock
116     private SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
117
118     @SuppressWarnings("illegalCatch")
119     private static Document getReplyOk() {
120         Document doc;
121         try {
122             doc = XmlFileLoader.xmlFileToDocument("messages/mapping/rpc-reply_ok.xml");
123         } catch (final Exception e) {
124             LOG.debug("unable to load rpc reply ok.", e);
125             doc = XmlUtil.newDocument();
126         }
127         return doc;
128     }
129
130     @Before
131     public void setUp() throws Exception {
132         MockitoAnnotations.initMocks(this);
133
134         XMLUnit.setIgnoreWhitespace(true);
135         XMLUnit.setIgnoreAttributeOrder(true);
136
137         this.schemaContext = YangParserTestUtils.parseYangStreams(getYangSchemas());
138         schemaContext.getModules();
139         final SchemaService schemaService = createSchemaService();
140
141         final DOMStore operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", schemaService);
142         final DOMStore configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", schemaService);
143
144         final EnumMap<LogicalDatastoreType, DOMStore> datastores = new EnumMap<>(LogicalDatastoreType.class);
145         datastores.put(LogicalDatastoreType.CONFIGURATION, configStore);
146         datastores.put(LogicalDatastoreType.OPERATIONAL, operStore);
147
148         final ExecutorService listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
149                 16, 16, "CommitFutures");
150
151         final ConcurrentDOMDataBroker cdb = new ConcurrentDOMDataBroker(datastores, listenableFutureExecutor);
152         this.transactionProvider = new TransactionProvider(cdb, SESSION_ID_FOR_REPORTING);
153
154         doAnswer(invocationOnMock -> {
155             final SourceIdentifier sId = (SourceIdentifier) invocationOnMock.getArguments()[0];
156             final YangTextSchemaSource yangTextSchemaSource =
157                     YangTextSchemaSource.delegateForByteSource(sId, ByteSource.wrap("module test".getBytes()));
158             return Futures.immediateCheckedFuture(yangTextSchemaSource);
159
160         }).when(sourceProvider).getSource(any(SourceIdentifier.class));
161
162         this.currentSchemaContext = new CurrentSchemaContext(schemaService, sourceProvider);
163     }
164
165     @Test
166     public void testEmptyDatastore() throws Exception {
167         assertEmptyDatastore(get());
168         assertEmptyDatastore(getConfigCandidate());
169         assertEmptyDatastore(getConfigRunning());
170     }
171
172     @Test
173     public void testIncorrectGet() throws Exception {
174         try {
175             executeOperation(new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider),
176                     "messages/mapping/bad_getConfig.xml");
177             fail("Should have failed, this is an incorrect request");
178         } catch (final DocumentedException e) {
179             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
180             assertTrue(e.getErrorTag() == ErrorTag.OPERATION_FAILED);
181             assertTrue(e.getErrorType() == ErrorType.APPLICATION);
182         }
183
184         try {
185             executeOperation(new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider),
186                     "messages/mapping/bad_namespace_getConfig.xml");
187             fail("Should have failed, this is an incorrect request");
188         } catch (final DocumentedException e) {
189             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
190             assertTrue(e.getErrorTag() == ErrorTag.OPERATION_FAILED);
191             assertTrue(e.getErrorType() == ErrorType.APPLICATION);
192         }
193     }
194
195     @Test
196     public void testEditRunning() throws Exception {
197         try {
198             edit("messages/mapping/editConfigs/editConfig_running.xml");
199             fail("Should have failed - edit config on running datastore is not supported");
200         } catch (final DocumentedException e) {
201             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
202             assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
203             assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
204         }
205     }
206
207     @Test
208     public void testCommitWithoutOpenTransaction() throws Exception {
209         verifyResponse(commit(), RPC_REPLY_OK);
210         assertEmptyDatastore(getConfigCandidate());
211     }
212
213     @Test
214     public void testCandidateTransaction() throws Exception {
215         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_n1.xml"), RPC_REPLY_OK);
216         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
217                 "messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
218         assertEmptyDatastore(getConfigRunning());
219
220         verifyResponse(discardChanges(), RPC_REPLY_OK);
221         assertEmptyDatastore(getConfigCandidate());
222     }
223
224     @Test
225     public void testEditWithCommit() throws Exception {
226         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_n1.xml"), RPC_REPLY_OK);
227         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
228                 "messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
229
230         verifyResponse(commit(), RPC_REPLY_OK);
231         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
232                 "messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
233
234         deleteDatastore();
235     }
236
237     @Test
238     public void testKeyOrder() throws Exception {
239         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_keys_1.xml"), RPC_REPLY_OK);
240         verifyResponse(commit(), RPC_REPLY_OK);
241         final Document configRunning = getConfigRunning();
242         final String responseAsString = XmlUtil.toString(configRunning);
243         verifyResponse(configRunning, XmlFileLoader.xmlFileToDocument(
244                 "messages/mapping/editConfigs/editConfig_merge_multiple_keys_1_control.xml"));
245
246         final int key3 = responseAsString.indexOf("key3");
247         final int key1 = responseAsString.indexOf("key1");
248         final int key2 = responseAsString.indexOf("key2");
249
250         assertTrue(String.format("Key ordering invalid, should be key3(%d) < key1(%d) < key2(%d)", key3, key1, key2),
251                 key3 < key1 && key1 < key2);
252
253         deleteDatastore();
254     }
255
256
257     @Test
258     public void testMultipleEditsWithMerge() throws Exception {
259         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
260         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
261                 "messages/mapping/editConfigs/editConfig_merge_multiple_control_1.xml"));
262         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
263         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
264                 "messages/mapping/editConfigs/editConfig_merge_multiple_control_2.xml"));
265         assertEmptyDatastore(getConfigRunning());
266
267         verifyResponse(commit(), RPC_REPLY_OK);
268         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
269                 "messages/mapping/editConfigs/editConfig_merge_multiple_control_2.xml"));
270
271         deleteDatastore();
272     }
273
274     @Test
275     public void testMoreComplexEditConfigs() throws Exception {
276         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
277         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
278
279         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_2.xml"), RPC_REPLY_OK);
280         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
281                 "messages/mapping/editConfigs/editConfig_merge_multiple_after_more_complex_merge.xml"));
282
283         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_3.xml"), RPC_REPLY_OK);
284         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
285                 "messages/mapping/editConfigs/editConfig_merge_multiple_after_more_complex_merge_2.xml"));
286
287         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_4_replace.xml"), RPC_REPLY_OK);
288         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
289                 "messages/mapping/editConfigs/editConfig_merge_multiple_after_replace.xml"));
290         verifyResponse(commit(), RPC_REPLY_OK);
291
292         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
293                 "messages/mapping/editConfigs/editConfig_merge_multiple_after_replace.xml"));
294
295         verifyResponse(edit("messages/mapping/editConfigs/editConfig_replace_default.xml"), RPC_REPLY_OK);
296         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
297                 "messages/mapping/editConfigs/editConfig_replace_default_control.xml"));
298         verifyResponse(commit(), RPC_REPLY_OK);
299
300         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
301                 "messages/mapping/editConfigs/editConfig_replace_default_control.xml"));
302
303         deleteDatastore();
304     }
305
306     @Test
307     public void testOrderedListEdits() throws Exception {
308
309         verifyResponse(edit("messages/mapping/editConfigs/editConfig_ordered_list_create.xml"), RPC_REPLY_OK);
310         verifyResponse(commit(), RPC_REPLY_OK);
311
312         verifyResponse(edit("messages/mapping/editConfigs/editConfig_ordered_list_replace.xml"), RPC_REPLY_OK);
313         verifyResponse(commit(), RPC_REPLY_OK);
314
315         deleteDatastore();
316
317     }
318
319     @Test
320     public void testAugmentedOrderedListEdits() throws Exception {
321
322         verifyResponse(edit("messages/mapping/editConfigs/editConfig_augmented_ordered_list_create.xml"),
323                 RPC_REPLY_OK);
324         verifyResponse(commit(), RPC_REPLY_OK);
325
326         verifyResponse(edit("messages/mapping/editConfigs/editConfig_augmented_ordered_list_replace.xml"),
327                 RPC_REPLY_OK);
328         verifyResponse(commit(), RPC_REPLY_OK);
329
330         deleteDatastore();
331
332     }
333
334     @Test
335     public void testAugmentedContainerReplace() throws Exception {
336         verifyResponse(edit("messages/mapping/editConfigs/editConfig_empty_modules_create.xml"),
337                 RPC_REPLY_OK);
338         verifyResponse(commit(), RPC_REPLY_OK);
339
340         verifyResponse(edit("messages/mapping/editConfigs/editConfig_augmented_container_replace.xml"),
341                 RPC_REPLY_OK);
342         verifyResponse(commit(), RPC_REPLY_OK);
343
344         deleteDatastore();
345     }
346
347     @Test
348     public void testLeafFromAugmentReplace() throws Exception {
349         verifyResponse(edit("messages/mapping/editConfigs/editConfig_empty_modules_create.xml"),
350                 RPC_REPLY_OK);
351         verifyResponse(commit(), RPC_REPLY_OK);
352
353         verifyResponse(edit("messages/mapping/editConfigs/editConfig_leaf_from_augment_replace.xml"),
354                 RPC_REPLY_OK);
355         verifyResponse(commit(), RPC_REPLY_OK);
356
357         deleteDatastore();
358     }
359
360     @Test
361     public void testLock() throws Exception {
362         verifyResponse(lockCandidate(), RPC_REPLY_OK);
363
364         try {
365             lock();
366             fail("Should have failed - locking of running datastore is not supported");
367         } catch (final DocumentedException e) {
368             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
369             assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
370             assertTrue(e.getErrorType() == ErrorType.APPLICATION);
371         }
372
373         try {
374             lockWithoutTarget();
375             fail("Should have failed, target is missing");
376         } catch (final DocumentedException e) {
377             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
378             assertTrue(e.getErrorTag() == ErrorTag.INVALID_VALUE);
379             assertTrue(e.getErrorType() == ErrorType.APPLICATION);
380         }
381     }
382
383     @Test
384     public void testUnlock() throws Exception {
385         verifyResponse(unlockCandidate(), RPC_REPLY_OK);
386
387         try {
388             unlock();
389             fail("Should have failed - unlocking of running datastore is not supported");
390         } catch (final DocumentedException e) {
391             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
392             assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
393             assertTrue(e.getErrorType() == ErrorType.APPLICATION);
394         }
395
396         try {
397             unlockWithoutTarget();
398             fail("Should have failed, target is missing");
399         } catch (final DocumentedException e) {
400             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
401             assertTrue(e.getErrorTag() == ErrorTag.INVALID_VALUE);
402             assertTrue(e.getErrorType() == ErrorType.APPLICATION);
403         }
404     }
405
406     @Test
407     public void testEditWithCreate() throws Exception {
408         verifyResponse(edit("messages/mapping/editConfigs/editConfig_create.xml"), RPC_REPLY_OK);
409         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
410                 "messages/mapping/editConfig_create_n1_control.xml"));
411
412         try {
413             edit("messages/mapping/editConfigs/editConfig_create.xml");
414             fail("Create should have failed - data already exists");
415         } catch (final DocumentedException e) {
416             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
417             assertTrue(e.getErrorTag() == ErrorTag.DATA_EXISTS);
418             assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
419         }
420
421         verifyResponse(discardChanges(), RPC_REPLY_OK);
422     }
423
424     @Test
425     public void testDeleteNonExisting() throws Exception {
426         assertEmptyDatastore(getConfigCandidate());
427         assertEmptyDatastore(getConfigRunning());
428
429         try {
430             edit("messages/mapping/editConfigs/editConfig_delete-top.xml");
431             fail("Delete should have failed - data is missing");
432         } catch (final DocumentedException e) {
433             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
434             assertTrue(e.getErrorTag() == ErrorTag.DATA_MISSING);
435             assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
436         }
437     }
438
439     @Test
440     public void testEditMissingDefaultOperation() throws Exception {
441         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_1.xml"),
442                 RPC_REPLY_OK);
443         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_2.xml"),
444                 RPC_REPLY_OK);
445         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
446                 "messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
447
448         verifyResponse(commit(), RPC_REPLY_OK);
449         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
450                 "messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
451
452         deleteDatastore();
453     }
454
455     private static void printDocument(final Document doc) throws Exception {
456         final TransformerFactory tf = TransformerFactory.newInstance();
457         final Transformer transformer = tf.newTransformer();
458         transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
459         transformer.setOutputProperty(OutputKeys.METHOD, "xml");
460         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
461         transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
462         transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
463
464         final StringWriter writer = new StringWriter();
465         transformer.transform(new DOMSource(doc),
466                 new StreamResult(writer));
467         LOG.warn(writer.getBuffer().toString());
468     }
469
470     @Test
471     public void testEditConfigWithMultipleOperations() throws Exception {
472         deleteDatastore();
473
474         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_setup.xml"),
475                 RPC_REPLY_OK);
476         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_1.xml"),
477                 RPC_REPLY_OK);
478
479         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_2.xml"),
480                 RPC_REPLY_OK);
481         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
482                 "messages/mapping/editConfigs/editConfig_merge_multiple_operations_2_control.xml"));
483
484         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_leaf_operations.xml"),
485                 RPC_REPLY_OK);
486         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
487                 "messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_control.xml"));
488
489         deleteDatastore();
490
491         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_setup.xml"),
492                 RPC_REPLY_OK);
493         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_default-replace.xml"),
494                 RPC_REPLY_OK);
495
496         try {
497             edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_create_existing.xml");
498             fail();
499         } catch (final DocumentedException e) {
500             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
501             assertTrue(e.getErrorTag() == ErrorTag.DATA_EXISTS);
502             assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
503         }
504
505         verifyResponse(edit(
506                 "messages/mapping/editConfigs/"
507                         + "editConfig_merge_multiple_operations_4_delete_children_operations.xml"),
508                 RPC_REPLY_OK);
509         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
510                 "messages/mapping/editConfigs/"
511                         + "editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
512         verifyResponse(edit(
513                 "messages/mapping/editConfigs/"
514                         + "editConfig_merge_multiple_operations_4_remove-non-existing.xml"),
515                 RPC_REPLY_OK);
516         try {
517             edit("messages/mapping/editConfigs/"
518                     + "editConfig_merge_multiple_operations_4_delete-non-existing.xml");
519             fail();
520         } catch (final DocumentedException e) {
521             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
522             assertTrue(e.getErrorTag() == ErrorTag.DATA_MISSING);
523             assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
524         }
525
526         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup.xml"),
527                 RPC_REPLY_OK);
528         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
529                 "messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup-control.xml"));
530
531         // Test files have been modified. RFC6020 requires that at most once case inside a choice is present at any time
532         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2.xml"),
533                 RPC_REPLY_OK);
534         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
535                 "messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2-control.xml"));
536
537         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_delete.xml"),
538                 RPC_REPLY_OK);
539         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
540                 "messages/mapping/editConfigs"
541                         + "/editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
542
543         deleteDatastore();
544     }
545
546     @Test
547     public void testEditConfigGetElementByTagName() throws Exception {
548         EditConfig editConfig = new EditConfig("test_edit-config", Mockito.mock(CurrentSchemaContext.class),
549                 Mockito.mock(TransactionProvider.class));
550
551         String stringWithoutPrefix =
552                 "<rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n"
553                         + "  <edit-config>\n"
554                         + "    <target>\n"
555                         + "      <candidate/>\n"
556                         + "    </target>\n"
557                         + "  </edit-config>\n"
558                         + "</rpc>";
559         XmlElement xe = getXmlElement(stringWithoutPrefix);
560         NodeList nodeList = editConfig.getElementsByTagName(xe, TARGET_KEY);
561         Assert.assertEquals(1, nodeList.getLength());
562
563         String stringWithPrefix =
564                 "<nc:rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n"
565                         + "  <nc:edit-config>\n"
566                         + "    <nc:target>\n"
567                         + "      <nc:candidate/>\n"
568                         + "    </nc:target>\n"
569                         + "  </nc:edit-config>\n"
570                         + "</nc:rpc>";
571
572         xe = getXmlElement(stringWithPrefix);
573         nodeList = editConfig.getElementsByTagName(xe, TARGET_KEY);
574         Assert.assertEquals(1, nodeList.getLength());
575
576         String stringWithoutTarget =
577                 "<nc:rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n"
578                         + "  <nc:edit-config>\n"
579                         + "    <nc:target>\n"
580                         + "    </nc:target>\n"
581                         + "  </nc:edit-config>\n"
582                         + "</nc:rpc>";
583         xe = getXmlElement(stringWithoutTarget);
584
585         try {
586             nodeList = editConfig.getElementsByTagName(xe, TARGET_KEY);
587             XmlElement.fromDomElement((Element) nodeList.item(0)).getOnlyChildElement();
588             Assert.fail("Not specified target, we should fail");
589         } catch (DocumentedException documentedException) {
590             // Ignore
591         }
592
593     }
594
595     private XmlElement getXmlElement(final String elementAsString) throws Exception {
596         Document document = XmlUtil.readXmlToDocument(elementAsString);
597         Element element = document.getDocumentElement();
598         return XmlElement.fromDomElement(element);
599     }
600
601     @Test
602     public void testReplaceMapEntry() throws Exception {
603         verifyResponse(edit("messages/mapping/editConfigs/edit-config-replace-map-entry.xml"), RPC_REPLY_OK);
604         verifyResponse(commit(), RPC_REPLY_OK);
605         verifyResponse(getConfigRunning(),
606                 XmlFileLoader.xmlFileToDocument("messages/mapping/get-config-map-entry.xml"));
607     }
608
609     @Test
610     public void testMergeMapEntry() throws Exception {
611         verifyResponse(edit("messages/mapping/editConfigs/edit-config-merge-map-entry.xml"), RPC_REPLY_OK);
612         verifyResponse(commit(), RPC_REPLY_OK);
613         verifyResponse(getConfigRunning(),
614                 XmlFileLoader.xmlFileToDocument("messages/mapping/get-config-map-entry.xml"));
615     }
616
617     @Test
618     public void testFiltering() throws Exception {
619         assertEmptyDatastore(getConfigCandidate());
620         assertEmptyDatastore(getConfigRunning());
621
622         verifyResponse(getConfigWithFilter("messages/mapping/filters/get-config-empty-filter.xml"),
623                 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
624         verifyResponse(getWithFilter("messages/mapping/filters/get-empty-filter.xml"),
625                 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
626
627         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response"
628                 + ".xml"));
629         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
630         verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-users.xml"),
631                 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
632
633         verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup.xml"), RPC_REPLY_OK);
634         verifyResponse(commit(), RPC_REPLY_OK);
635
636         verifyFilterIdentifier("messages/mapping/filters/get-filter-alluser.xml",
637                 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
638         verifyFilterIdentifier("messages/mapping/filters/get-filter-company-info.xml",
639                 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
640         verifyFilterIdentifier("messages/mapping/filters/get-filter-modules-and-admin.xml",
641                 YangInstanceIdentifier.builder().node(TOP).build());
642         verifyFilterIdentifier("messages/mapping/filters/get-filter-only-names-types.xml",
643                 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
644         verifyFilterIdentifier("messages/mapping/filters/get-filter-specific-module-type-and-user.xml",
645                 YangInstanceIdentifier.builder().node(TOP).build());
646         verifyFilterIdentifier("messages/mapping/filters/get-filter-superuser.xml",
647                 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
648         verifyFilterIdentifier("messages/mapping/filters/get-filter-users.xml",
649                 YangInstanceIdentifier.builder().node(TOP).node(USERS).build());
650
651         final YangInstanceIdentifier ident = YangInstanceIdentifier
652                 .builder(AUGMENTED_CONTAINER_IN_MODULES)
653                 .node(AUGMENTED_CONTAINER)
654                 .node(AUGMENTED_STRING_IN_CONT).build();
655
656         verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-string.xml", ident);
657         verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case.xml",
658                 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(AUGMENTED_CASE).build());
659
660         verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case.xml"),
661                 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case.xml"));
662
663         /*
664          *  RFC6020 requires that at most once case inside a choice is present at any time.
665          *  Therefore
666          *  <augmented-case>augmented case</augmented-case>
667          *  from
668          *  messages/mapping/editConfigs/editConfig-filtering-setup.xml
669          *  cannot exists together with
670          *  <text>augmented nested choice text1</text>
671          *  from
672          *  messages/mapping/editConfigs/editConfig-filtering-setup2.xml
673          */
674         //verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup2.xml"), RPC_REPLY_OK);
675         //verifyResponse(commit(), RPC_REPLY_OK);
676
677         verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml",
678                 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).build());
679         verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-case.xml",
680                 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).node(INNER_CHOICE)
681                         .node(INNER_CHOICE_TEXT).build());
682
683 //        verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-string.xml"),
684 //                XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-string.xml"));
685 //        verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml"),
686 //                XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
687 //        verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-case.xml"),
688 //                XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
689
690         verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-top.xml"), RPC_REPLY_OK);
691         verifyResponse(commit(), RPC_REPLY_OK);
692
693     }
694
695     private void verifyFilterIdentifier(final String resource, final YangInstanceIdentifier identifier)
696             throws Exception {
697         final TestingGetConfig getConfig = new TestingGetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext,
698                 transactionProvider);
699         final Document request = XmlFileLoader.xmlFileToDocument(resource);
700         final YangInstanceIdentifier iid = getConfig.getInstanceIdentifierFromDocument(request);
701         assertEquals(identifier, iid);
702     }
703
704     private class TestingGetConfig extends GetConfig {
705         TestingGetConfig(final String sessionId, final CurrentSchemaContext schemaContext,
706                          final TransactionProvider transactionProvider) {
707             super(sessionId, schemaContext, transactionProvider);
708         }
709
710         YangInstanceIdentifier getInstanceIdentifierFromDocument(final Document request) throws DocumentedException {
711             final XmlElement filterElement = XmlElement.fromDomDocument(request).getOnlyChildElement(GET_CONFIG)
712                     .getOnlyChildElement(FILTER_NODE);
713             return getInstanceIdentifierFromFilter(filterElement);
714         }
715     }
716
717     private void deleteDatastore() throws Exception {
718         verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-root.xml"), RPC_REPLY_OK);
719         assertEmptyDatastore(getConfigCandidate());
720
721         verifyResponse(commit(), RPC_REPLY_OK);
722         assertEmptyDatastore(getConfigRunning());
723     }
724
725     private static void verifyResponse(final Document response, final Document template) throws Exception {
726         final DetailedDiff dd = new DetailedDiff(new Diff(response, template));
727         dd.overrideElementQualifier(new NetconfXmlUnitRecursiveQualifier());
728
729         printDocument(response);
730         printDocument(template);
731
732         assertTrue(dd.toString(), dd.similar());
733     }
734
735     private static void assertEmptyDatastore(final Document response) {
736         final NodeList nodes = response.getChildNodes();
737         assertTrue(nodes.getLength() == 1);
738
739         assertEquals(nodes.item(0).getLocalName(), RPC_REPLY_ELEMENT);
740
741         final NodeList replyNodes = nodes.item(0).getChildNodes();
742         assertTrue(replyNodes.getLength() == 1);
743
744         final Node dataNode = replyNodes.item(0);
745         assertEquals(dataNode.getLocalName(), DATA_ELEMENT);
746         assertFalse(dataNode.hasChildNodes());
747     }
748
749     private Document commit() throws Exception {
750         final Commit commit = new Commit(SESSION_ID_FOR_REPORTING, transactionProvider);
751         return executeOperation(commit, "messages/mapping/commit.xml");
752     }
753
754     private Document discardChanges() throws Exception {
755         final DiscardChanges discardOp = new DiscardChanges(SESSION_ID_FOR_REPORTING, transactionProvider);
756         return executeOperation(discardOp, "messages/mapping/discardChanges.xml");
757     }
758
759     private Document edit(final String resource) throws Exception {
760         final EditConfig editConfig = new EditConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext,
761                 transactionProvider);
762         return executeOperation(editConfig, resource);
763     }
764
765     private Document get() throws Exception {
766         final Get get = new Get(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
767         return executeOperation(get, "messages/mapping/get.xml");
768     }
769
770     private Document getWithFilter(final String resource) throws Exception {
771         final Get get = new Get(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
772         return executeOperation(get, resource);
773     }
774
775     private Document getConfigRunning() throws Exception {
776         final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
777         return executeOperation(getConfig, "messages/mapping/getConfig.xml");
778     }
779
780     private Document getConfigCandidate() throws Exception {
781         final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
782         return executeOperation(getConfig, "messages/mapping/getConfig_candidate.xml");
783     }
784
785     private Document getConfigWithFilter(final String resource) throws Exception {
786         final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
787         return executeOperation(getConfig, resource);
788     }
789
790     private static Document lock() throws Exception {
791         final Lock lock = new Lock(SESSION_ID_FOR_REPORTING);
792         return executeOperation(lock, "messages/mapping/lock.xml");
793     }
794
795     private static Document unlock() throws Exception {
796         final Unlock unlock = new Unlock(SESSION_ID_FOR_REPORTING);
797         return executeOperation(unlock, "messages/mapping/unlock.xml");
798     }
799
800     private static Document lockWithoutTarget() throws Exception {
801         final Lock lock = new Lock(SESSION_ID_FOR_REPORTING);
802         return executeOperation(lock, "messages/mapping/lock_notarget.xml");
803     }
804
805     private static Document unlockWithoutTarget() throws Exception {
806         final Unlock unlock = new Unlock(SESSION_ID_FOR_REPORTING);
807         return executeOperation(unlock, "messages/mapping/unlock_notarget.xml");
808     }
809
810     private static Document lockCandidate() throws Exception {
811         final Lock lock = new Lock(SESSION_ID_FOR_REPORTING);
812         return executeOperation(lock, "messages/mapping/lock_candidate.xml");
813     }
814
815     private static Document unlockCandidate() throws Exception {
816         final Unlock unlock = new Unlock(SESSION_ID_FOR_REPORTING);
817         return executeOperation(unlock, "messages/mapping/unlock_candidate.xml");
818     }
819
820     private static Document executeOperation(final NetconfOperation op, final String filename) throws Exception {
821         final Document request = XmlFileLoader.xmlFileToDocument(filename);
822         final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
823
824         LOG.debug("Got response {}", response);
825         return response;
826     }
827
828     private List<InputStream> getYangSchemas() {
829         final List<String> schemaPaths = Arrays.asList("/META-INF/yang/config@2013-04-05.yang",
830                 "/yang/mdsal-netconf-mapping-test.yang");
831         final List<InputStream> schemas = new ArrayList<>();
832
833         for (final String schemaPath : schemaPaths) {
834             final InputStream resourceAsStream =
835                     Preconditions.checkNotNull(
836                             getClass().getResourceAsStream(schemaPath), "Resource " + schemaPath + " not found.");
837             schemas.add(resourceAsStream);
838         }
839
840         return schemas;
841     }
842
843     private SchemaService createSchemaService() {
844         return new SchemaService() {
845
846             @Override
847             public void addModule(final Module module) {
848             }
849
850             @Override
851             public void removeModule(final Module module) {
852
853             }
854
855             @Override
856             public SchemaContext getSessionContext() {
857                 return schemaContext;
858             }
859
860             @Override
861             public SchemaContext getGlobalContext() {
862                 return schemaContext;
863             }
864
865             @Override
866             public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(
867                     final SchemaContextListener listener) {
868                 listener.onGlobalContextUpdated(getGlobalContext());
869                 return new ListenerRegistration<SchemaContextListener>() {
870                     @Override
871                     public void close() {
872
873                     }
874
875                     @Override
876                     public SchemaContextListener getInstance() {
877                         return listener;
878                     }
879                 };
880             }
881         };
882     }
883 }