2d5e971dc333595e60db3ff1b0c26f839c17729f
[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 testLock() throws Exception {
332
333         verifyResponse(lockCandidate(), RPC_REPLY_OK);
334
335         try {
336             lock();
337             fail("Should have failed - locking of running datastore is not supported");
338         } catch (final DocumentedException e) {
339             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
340             assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
341             assertTrue(e.getErrorType() == ErrorType.APPLICATION);
342         }
343
344
345         try {
346             lockWithoutTarget();
347             fail("Should have failed, target is missing");
348         } catch (final DocumentedException e) {
349             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
350             assertTrue(e.getErrorTag() == ErrorTag.INVALID_VALUE);
351             assertTrue(e.getErrorType() == ErrorType.APPLICATION);
352         }
353     }
354
355     @Test
356     public void testUnlock() throws Exception {
357
358         verifyResponse(unlockCandidate(), RPC_REPLY_OK);
359
360         try {
361             unlock();
362             fail("Should have failed - unlocking of running datastore is not supported");
363         } catch (final DocumentedException e) {
364             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
365             assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
366             assertTrue(e.getErrorType() == ErrorType.APPLICATION);
367         }
368
369         try {
370             unlockWithoutTarget();
371             fail("Should have failed, target is missing");
372         } catch (final DocumentedException e) {
373             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
374             assertTrue(e.getErrorTag() == ErrorTag.INVALID_VALUE);
375             assertTrue(e.getErrorType() == ErrorType.APPLICATION);
376         }
377     }
378
379     @Test
380     public void testEditWithCreate() throws Exception {
381
382         verifyResponse(edit("messages/mapping/editConfigs/editConfig_create.xml"), RPC_REPLY_OK);
383         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_create_n1_control.xml"));
384
385
386         try {
387             edit("messages/mapping/editConfigs/editConfig_create.xml");
388             fail("Create should have failed - data already exists");
389         } catch (final DocumentedException e) {
390             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
391             assertTrue(e.getErrorTag() == ErrorTag.DATA_EXISTS);
392             assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
393         }
394
395         verifyResponse(discardChanges(), RPC_REPLY_OK);
396
397     }
398
399     @Test
400     public void testDeleteNonExisting() throws Exception {
401
402         assertEmptyDatastore(getConfigCandidate());
403         assertEmptyDatastore(getConfigRunning());
404
405         try {
406             edit("messages/mapping/editConfigs/editConfig_delete-top.xml");
407             fail("Delete should have failed - data is missing");
408         } catch (final DocumentedException e) {
409             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
410             assertTrue(e.getErrorTag() == ErrorTag.DATA_MISSING);
411             assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
412         }
413
414     }
415
416     @Test
417     public void testEditMissingDefaultOperation() throws Exception {
418
419         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_1.xml"), RPC_REPLY_OK);
420         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_2.xml"), RPC_REPLY_OK);
421         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
422
423         verifyResponse(commit(), RPC_REPLY_OK);
424         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
425
426         deleteDatastore();
427     }
428
429     public static void printDocument(final Document doc) throws IOException, TransformerException {
430         final TransformerFactory tf = TransformerFactory.newInstance();
431         final Transformer transformer = tf.newTransformer();
432         transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
433         transformer.setOutputProperty(OutputKeys.METHOD, "xml");
434         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
435         transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
436         transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
437
438         final StringWriter writer = new StringWriter();
439         transformer.transform(new DOMSource(doc),
440                 new StreamResult(writer));
441         LOG.warn(writer.getBuffer().toString());
442     }
443
444     @Test
445     public void testEditConfigWithMultipleOperations() throws Exception {
446         deleteDatastore();
447
448         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_setup.xml"), RPC_REPLY_OK);
449         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_1.xml"), RPC_REPLY_OK);
450
451         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_2.xml"), RPC_REPLY_OK);
452         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_2_control.xml"));
453
454         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_leaf_operations.xml"), RPC_REPLY_OK);
455         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_control.xml"));
456
457         deleteDatastore();
458
459         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_setup.xml"), RPC_REPLY_OK);
460         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_default-replace.xml"), RPC_REPLY_OK);
461
462         try {
463             edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_create_existing.xml");
464             fail();
465         } catch (final DocumentedException e) {
466             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
467             assertTrue(e.getErrorTag() == ErrorTag.DATA_EXISTS);
468             assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
469         }
470
471         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete_children_operations.xml"), RPC_REPLY_OK);
472         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
473         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_remove-non-existing.xml"), RPC_REPLY_OK);
474
475         try {
476             edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete-non-existing.xml");
477             fail();
478         } catch (final DocumentedException e) {
479             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
480             assertTrue(e.getErrorTag() == ErrorTag.DATA_MISSING);
481             assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
482         }
483
484         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup.xml"), RPC_REPLY_OK);
485         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup-control.xml"));
486
487         // Test files have been modified. RFC6020 requires that at most once case inside a choice is present at any time
488         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2.xml"), RPC_REPLY_OK);
489         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2-control.xml"));
490
491         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_delete.xml"), RPC_REPLY_OK);
492         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
493
494         deleteDatastore();
495     }
496
497     @Test
498     public void testReplaceMapEntry() throws Exception {
499         verifyResponse(edit("messages/mapping/editConfigs/edit-config-replace-map-entry.xml"), RPC_REPLY_OK);
500         verifyResponse(commit(), RPC_REPLY_OK);
501         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-config-map-entry.xml"));
502     }
503
504     @Test
505     public void testMergeMapEntry() throws Exception {
506         verifyResponse(edit("messages/mapping/editConfigs/edit-config-merge-map-entry.xml"), RPC_REPLY_OK);
507         verifyResponse(commit(), RPC_REPLY_OK);
508         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-config-map-entry.xml"));
509     }
510
511     @Test
512     public void testFiltering() throws Exception {
513
514         assertEmptyDatastore(getConfigCandidate());
515         assertEmptyDatastore(getConfigRunning());
516
517         verifyResponse(getConfigWithFilter("messages/mapping/filters/get-config-empty-filter.xml"),
518                 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
519         verifyResponse(getWithFilter("messages/mapping/filters/get-empty-filter.xml"),
520                 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
521
522         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
523         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
524         verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-users.xml"),
525                 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
526
527         verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup.xml"), RPC_REPLY_OK);
528         verifyResponse(commit(), RPC_REPLY_OK);
529
530         verifyFilterIdentifier("messages/mapping/filters/get-filter-alluser.xml",
531                 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
532         verifyFilterIdentifier("messages/mapping/filters/get-filter-company-info.xml",
533                 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
534         verifyFilterIdentifier("messages/mapping/filters/get-filter-modules-and-admin.xml",
535                 YangInstanceIdentifier.builder().node(TOP).build());
536         verifyFilterIdentifier("messages/mapping/filters/get-filter-only-names-types.xml",
537                 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
538         verifyFilterIdentifier("messages/mapping/filters/get-filter-specific-module-type-and-user.xml",
539                 YangInstanceIdentifier.builder().node(TOP).build());
540         verifyFilterIdentifier("messages/mapping/filters/get-filter-superuser.xml",
541                 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
542         verifyFilterIdentifier("messages/mapping/filters/get-filter-users.xml",
543                 YangInstanceIdentifier.builder().node(TOP).node(USERS).build());
544
545         final YangInstanceIdentifier ident = YangInstanceIdentifier.
546                 builder(AUGMENTED_CONTAINER_IN_MODULES).
547                 node(AUGMENTED_CONTAINER).
548                 node(AUGMENTED_STRING_IN_CONT).build();
549
550         verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-string.xml", ident);
551         verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case.xml",
552                 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(AUGMENTED_CASE).build());
553
554         verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case.xml"),
555                 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case.xml"));
556
557         /*
558          *  RFC6020 requires that at most once case inside a choice is present at any time.
559          *  Therefore
560          *  <augmented-case>augmented case</augmented-case>
561          *  from
562          *  messages/mapping/editConfigs/editConfig-filtering-setup.xml
563          *  cannot exists together with
564          *  <text>augmented nested choice text1</text>
565          *  from
566          *  messages/mapping/editConfigs/editConfig-filtering-setup2.xml
567          */
568         //verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup2.xml"), RPC_REPLY_OK);
569         //verifyResponse(commit(), RPC_REPLY_OK);
570
571         verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml",
572                 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).build());
573         verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-case.xml",
574                 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).node(INNER_CHOICE).node(INNER_CHOICE_TEXT).build());
575
576 //        verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-string.xml"),
577 //                XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-string.xml"));
578 //        verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml"),
579 //                XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
580 //        verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-case.xml"),
581 //                XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
582
583         verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-top.xml"), RPC_REPLY_OK);
584         verifyResponse(commit(), RPC_REPLY_OK);
585
586     }
587
588     private void verifyFilterIdentifier(final String resource, final YangInstanceIdentifier identifier) throws Exception {
589         final TestingGetConfig getConfig = new TestingGetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
590         final Document request = XmlFileLoader.xmlFileToDocument(resource);
591         final YangInstanceIdentifier iid = getConfig.getInstanceIdentifierFromDocument(request);
592         assertEquals(identifier, iid);
593     }
594
595     private class TestingGetConfig extends GetConfig{
596         public TestingGetConfig(final String sessionId, final CurrentSchemaContext schemaContext, final TransactionProvider transactionProvider) {
597             super(sessionId, schemaContext, transactionProvider);
598         }
599
600         public YangInstanceIdentifier getInstanceIdentifierFromDocument(final Document request) throws DocumentedException {
601             final XmlElement filterElement = XmlElement.fromDomDocument(request).getOnlyChildElement(GET_CONFIG).getOnlyChildElement(FILTER_NODE);
602             return getInstanceIdentifierFromFilter(filterElement);
603         }
604     }
605
606     private void deleteDatastore() throws Exception{
607         verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-root.xml"), RPC_REPLY_OK);
608         assertEmptyDatastore(getConfigCandidate());
609
610         verifyResponse(commit(), RPC_REPLY_OK);
611         assertEmptyDatastore(getConfigRunning());
612     }
613
614     private void verifyResponse(final Document response, final Document template) throws IOException, TransformerException {
615         final DetailedDiff dd = new DetailedDiff(new Diff(response, template));
616         dd.overrideElementQualifier(new NetconfXmlUnitRecursiveQualifier());
617
618         printDocument(response);
619         printDocument(template);
620
621         assertTrue(dd.toString(), dd.similar());
622     }
623
624     private void assertEmptyDatastore(final Document response) {
625
626         final NodeList nodes = response.getChildNodes();
627         assertTrue(nodes.getLength() == 1);
628
629         assertEquals(nodes.item(0).getLocalName(), RPC_REPLY_ELEMENT);
630
631         final NodeList replyNodes = nodes.item(0).getChildNodes();
632         assertTrue(replyNodes.getLength() == 1);
633
634         final Node dataNode = replyNodes.item(0);
635         assertEquals(dataNode.getLocalName(), DATA_ELEMENT);
636         assertFalse(dataNode.hasChildNodes());
637
638     }
639
640     private Document commit() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
641         final Commit commit = new Commit(sessionIdForReporting, transactionProvider);
642         return executeOperation(commit, "messages/mapping/commit.xml");
643     }
644
645     private Document discardChanges() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
646         final DiscardChanges discardOp = new DiscardChanges(sessionIdForReporting, transactionProvider);
647         return executeOperation(discardOp, "messages/mapping/discardChanges.xml");
648     }
649
650     private Document edit(final String resource) throws DocumentedException, ParserConfigurationException, SAXException, IOException {
651         final EditConfig editConfig = new EditConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
652         return executeOperation(editConfig, resource);
653     }
654
655     private Document get() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
656         final Get get = new Get(sessionIdForReporting, currentSchemaContext, transactionProvider);
657         return executeOperation(get, "messages/mapping/get.xml");
658     }
659
660     private Document getWithFilter(final String resource) throws DocumentedException, ParserConfigurationException, SAXException, IOException {
661         final Get get = new Get(sessionIdForReporting, currentSchemaContext, transactionProvider);
662         return executeOperation(get, resource);
663     }
664
665     private Document getConfigRunning() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
666         final GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
667         return executeOperation(getConfig, "messages/mapping/getConfig.xml");
668     }
669
670     private Document getConfigCandidate() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
671         final GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
672         return executeOperation(getConfig, "messages/mapping/getConfig_candidate.xml");
673     }
674
675     private Document getConfigWithFilter(final String resource) throws DocumentedException, ParserConfigurationException, SAXException, IOException {
676         final GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
677         return executeOperation(getConfig, resource);
678     }
679
680     private Document lock() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
681         final Lock lock = new Lock(sessionIdForReporting);
682         return executeOperation(lock, "messages/mapping/lock.xml");
683     }
684
685     private Document unlock() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
686         final Unlock unlock = new Unlock(sessionIdForReporting);
687         return executeOperation(unlock, "messages/mapping/unlock.xml");
688     }
689
690     private Document lockWithoutTarget() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
691         final Lock lock = new Lock(sessionIdForReporting);
692         return executeOperation(lock, "messages/mapping/lock_notarget.xml");
693     }
694
695     private Document unlockWithoutTarget() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
696         final Unlock unlock = new Unlock(sessionIdForReporting);
697         return executeOperation(unlock, "messages/mapping/unlock_notarget.xml");
698     }
699
700     private Document lockCandidate() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
701         final Lock lock = new Lock(sessionIdForReporting);
702         return executeOperation(lock, "messages/mapping/lock_candidate.xml");
703     }
704
705     private Document unlockCandidate() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
706         final Unlock unlock = new Unlock(sessionIdForReporting);
707         return executeOperation(unlock, "messages/mapping/unlock_candidate.xml");
708     }
709
710     private Document executeOperation(final NetconfOperation op, final String filename) throws ParserConfigurationException, SAXException, IOException, DocumentedException {
711         final Document request = XmlFileLoader.xmlFileToDocument(filename);
712         final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
713
714         LOG.debug("Got response {}" , response);
715         return response;
716     }
717
718     private List<InputStream> getYangSchemas() {
719         final List<String> schemaPaths = Arrays.asList("/META-INF/yang/config.yang", "/yang/mdsal-netconf-mapping-test.yang");
720         final List<InputStream> schemas = new ArrayList<>();
721
722         for (final String schemaPath : schemaPaths) {
723             final InputStream resourceAsStream = getClass().getResourceAsStream(schemaPath);
724             schemas.add(resourceAsStream);
725         }
726
727         return schemas;
728     }
729
730     private SchemaService createSchemaService() {
731         return new SchemaService() {
732
733             @Override
734             public void addModule(final Module module) {
735             }
736
737             @Override
738             public void removeModule(final Module module) {
739
740             }
741
742             @Override
743             public SchemaContext getSessionContext() {
744                 return schemaContext;
745             }
746
747             @Override
748             public SchemaContext getGlobalContext() {
749                 return schemaContext;
750             }
751
752             @Override
753             public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(final SchemaContextListener listener) {
754                 listener.onGlobalContextUpdated(getGlobalContext());
755                 return new ListenerRegistration<SchemaContextListener>() {
756                     @Override
757                     public void close() {
758
759                     }
760
761                     @Override
762                     public SchemaContextListener getInstance() {
763                         return listener;
764                     }
765                 };
766             }
767         };
768     }
769 }