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