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