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