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