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