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