Use callbacks while parsing to NormalizedNodes.
[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 testMultipleEditsWithMerge() throws Exception {
192
193         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
194         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_control_1.xml"));
195         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
196         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_control_2.xml"));
197         assertEmptyDatastore(getConfigRunning());
198
199         verifyResponse(commit(), RPC_REPLY_OK);
200         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_control_2.xml"));
201
202         deleteDatastore();
203
204     }
205
206     @Test
207     public void testMoreComplexEditConfigs() throws Exception {
208
209         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
210         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
211
212         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_2.xml"), RPC_REPLY_OK);
213         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_more_complex_merge.xml"));
214
215         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_3.xml"), RPC_REPLY_OK);
216         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_more_complex_merge_2.xml"));
217
218         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_4_replace.xml"), RPC_REPLY_OK);
219         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_replace.xml"));
220         verifyResponse(commit(), RPC_REPLY_OK);
221
222         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_replace.xml"));
223
224         verifyResponse(edit("messages/mapping/editConfigs/editConfig_replace_default.xml"), RPC_REPLY_OK);
225         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_replace_default_control.xml"));
226         verifyResponse(commit(), RPC_REPLY_OK);
227
228         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_replace_default_control.xml"));
229
230         deleteDatastore();
231
232     }
233
234     @Test
235     public void testLock() throws Exception {
236
237         verifyResponse(lockCandidate(), RPC_REPLY_OK);
238
239         try {
240             lock();
241             fail("Should have failed - locking of running datastore is not supported");
242         } catch (NetconfDocumentedException e) {
243             assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
244             assertTrue(e.getErrorTag() == ErrorTag.operation_not_supported);
245             assertTrue(e.getErrorType() == ErrorType.application);
246         }
247     }
248
249     @Test
250     public void testUnlock() throws Exception {
251
252         verifyResponse(unlockCandidate(), RPC_REPLY_OK);
253
254         try {
255             unlock();
256             fail("Should have failed - unlocking of running datastore is not supported");
257         } catch (NetconfDocumentedException e) {
258             assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
259             assertTrue(e.getErrorTag() == ErrorTag.operation_not_supported);
260             assertTrue(e.getErrorType() == ErrorType.application);
261         }
262     }
263
264     @Test
265     public void testEditWithCreate() throws Exception {
266
267         verifyResponse(edit("messages/mapping/editConfigs/editConfig_create.xml"), RPC_REPLY_OK);
268         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_create_n1_control.xml"));
269
270
271         try {
272             edit("messages/mapping/editConfigs/editConfig_create.xml");
273             fail("Create should have failed - data already exists");
274         } catch (NetconfDocumentedException e) {
275             assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
276             assertTrue(e.getErrorTag() == ErrorTag.data_exists);
277             assertTrue(e.getErrorType() == ErrorType.protocol);
278         }
279
280         verifyResponse(discardChanges(), RPC_REPLY_OK);
281
282     }
283
284     @Test
285     public void testDeleteNonExisting() throws Exception {
286
287         assertEmptyDatastore(getConfigCandidate());
288         assertEmptyDatastore(getConfigRunning());
289
290         try {
291             edit("messages/mapping/editConfigs/editConfig_delete-top.xml");
292             fail("Delete should have failed - data is missing");
293         } catch (NetconfDocumentedException e) {
294             assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
295             assertTrue(e.getErrorTag() == ErrorTag.data_missing);
296             assertTrue(e.getErrorType() == ErrorType.protocol);
297         }
298
299     }
300
301     @Test
302     public void testEditMissingDefaultOperation() throws Exception {
303
304         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_1.xml"), RPC_REPLY_OK);
305         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_2.xml"), RPC_REPLY_OK);
306         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
307
308         verifyResponse(commit(), RPC_REPLY_OK);
309         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
310
311         deleteDatastore();
312     }
313
314     public static void printDocument(Document doc) throws IOException, TransformerException {
315         TransformerFactory tf = TransformerFactory.newInstance();
316         Transformer transformer = tf.newTransformer();
317         transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
318         transformer.setOutputProperty(OutputKeys.METHOD, "xml");
319         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
320         transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
321         transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
322
323         StringWriter writer = new StringWriter();
324         transformer.transform(new DOMSource(doc),
325                 new StreamResult(writer));
326         LOG.warn(writer.getBuffer().toString());
327     }
328
329     @Test
330     public void testEditConfigWithMultipleOperations() throws Exception {
331         deleteDatastore();
332
333         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_setup.xml"), RPC_REPLY_OK);
334         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_1.xml"), RPC_REPLY_OK);
335
336         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_2.xml"), RPC_REPLY_OK);
337         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_2_control.xml"));
338
339         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_leaf_operations.xml"), RPC_REPLY_OK);
340         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_control.xml"));
341
342         deleteDatastore();
343
344         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_setup.xml"), RPC_REPLY_OK);
345         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_default-replace.xml"), RPC_REPLY_OK);
346
347         try {
348             edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_create_existing.xml");
349             fail();
350         } catch (NetconfDocumentedException e) {
351             assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
352             assertTrue(e.getErrorTag() == ErrorTag.data_exists);
353             assertTrue(e.getErrorType() == ErrorType.protocol);
354         }
355
356         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete_children_operations.xml"), RPC_REPLY_OK);
357         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
358         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_remove-non-existing.xml"), RPC_REPLY_OK);
359
360         try {
361             edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete-non-existing.xml");
362             fail();
363         } catch (NetconfDocumentedException e) {
364             assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
365             assertTrue(e.getErrorTag() == ErrorTag.data_missing);
366             assertTrue(e.getErrorType() == ErrorType.protocol);
367         }
368
369         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup.xml"), RPC_REPLY_OK);
370         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup-control.xml"));
371
372         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2.xml"), RPC_REPLY_OK);
373         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2-control.xml"));
374
375         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_delete.xml"), RPC_REPLY_OK);
376         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
377
378         deleteDatastore();
379     }
380
381     @Test
382     public void testFiltering() throws Exception {
383
384         assertEmptyDatastore(getConfigCandidate());
385         assertEmptyDatastore(getConfigRunning());
386
387         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
388         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
389         verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-users.xml"),
390                 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
391
392         verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup.xml"), RPC_REPLY_OK);
393         verifyResponse(commit(), RPC_REPLY_OK);
394
395         //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
396 //        verifyFilterIdentifier("messages/mapping/filters/get-filter-alluser.xml",
397 //                YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
398         verifyFilterIdentifier("messages/mapping/filters/get-filter-company-info.xml",
399                 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
400         verifyFilterIdentifier("messages/mapping/filters/get-filter-modules-and-admin.xml",
401                 YangInstanceIdentifier.builder().node(TOP).build());
402         verifyFilterIdentifier("messages/mapping/filters/get-filter-only-names-types.xml",
403                 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
404 //        verifyFilterIdentifier("messages/mapping/filters/get-filter-specific-module-type-and-user.xml",
405 //                YangInstanceIdentifier.builder().node(TOP).build());
406 //        verifyFilterIdentifier("messages/mapping/filters/get-filter-superuser.xml",
407 //                YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
408         verifyFilterIdentifier("messages/mapping/filters/get-filter-users.xml",
409                 YangInstanceIdentifier.builder().node(TOP).node(USERS).build());
410
411         YangInstanceIdentifier ident = YangInstanceIdentifier.
412                 builder(AUGMENTED_CONTAINER_IN_MODULES).
413                 node(AUGMENTED_CONTAINER).
414                 node(AUGMENTED_STRING_IN_CONT).build();
415
416         verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-string.xml", ident);
417         verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case.xml",
418                 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(AUGMENTED_CASE).build());
419
420         verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case.xml"),
421                 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case.xml"));
422         verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup2.xml"), RPC_REPLY_OK);
423         verifyResponse(commit(), RPC_REPLY_OK);
424
425         verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml",
426                 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).build());
427         verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-case.xml",
428                 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).node(INNER_CHOICE).node(INNER_CHOICE_TEXT).build());
429
430         verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-string.xml"),
431                 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-string.xml"));
432         verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml"),
433                 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
434         verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-case.xml"),
435                 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
436
437         verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-top.xml"), RPC_REPLY_OK);
438         verifyResponse(commit(), RPC_REPLY_OK);
439
440     }
441
442     private void verifyFilterIdentifier(String resource, YangInstanceIdentifier identifier) throws Exception{
443         TestingGetConfig getConfig = new TestingGetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
444         Document request = XmlFileLoader.xmlFileToDocument(resource);
445         YangInstanceIdentifier iid = getConfig.getInstanceIdentifierFromDocument(request);
446         assertTrue(iid.equals(identifier));
447     }
448
449     private class TestingGetConfig extends GetConfig{
450         public TestingGetConfig(String sessionId, CurrentSchemaContext schemaContext, TransactionProvider transactionProvider) {
451             super(sessionId, schemaContext, transactionProvider);
452         }
453
454         public YangInstanceIdentifier getInstanceIdentifierFromDocument(Document request) throws NetconfDocumentedException {
455             XmlElement filterElement = XmlElement.fromDomDocument(request).getOnlyChildElement(GET_CONFIG).getOnlyChildElement(FILTER_NODE);
456             return getInstanceIdentifierFromFilter(filterElement);
457         }
458     }
459
460     private void deleteDatastore() throws Exception{
461         verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-root.xml"), RPC_REPLY_OK);
462         assertEmptyDatastore(getConfigCandidate());
463
464         verifyResponse(commit(), RPC_REPLY_OK);
465         assertEmptyDatastore(getConfigRunning());
466     }
467
468     private void verifyResponse(Document response, Document template) throws IOException, TransformerException {
469         DetailedDiff dd = new DetailedDiff(new Diff(response, template));
470         dd.overrideElementQualifier(new NetconfXmlUnitRecursiveQualifier());
471
472         printDocument(response);
473         printDocument(template);
474
475         assertTrue(dd.toString(), dd.similar());
476     }
477
478     private void assertEmptyDatastore(Document response) {
479
480         NodeList nodes = response.getChildNodes();
481         assertTrue(nodes.getLength() == 1);
482
483         assertEquals(nodes.item(0).getLocalName(),RPC_REPLY_ELEMENT);
484
485         NodeList replyNodes = nodes.item(0).getChildNodes();
486         assertTrue(replyNodes.getLength() == 1);
487
488         Node dataNode = replyNodes.item(0);
489         assertEquals(dataNode.getLocalName(), DATA_ELEMENT);
490         assertFalse(dataNode.hasChildNodes());
491
492     }
493
494     private Document commit() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
495         Commit commit = new Commit(sessionIdForReporting, transactionProvider);
496         return executeOperation(commit, "messages/mapping/commit.xml");
497     }
498
499     private Document discardChanges() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
500         DiscardChanges discardOp = new DiscardChanges(sessionIdForReporting, transactionProvider);
501         return executeOperation(discardOp, "messages/mapping/discardChanges.xml");
502     }
503
504     private Document edit(String resource) throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
505         EditConfig editConfig = new EditConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
506         return executeOperation(editConfig, resource);
507     }
508
509     private Document get() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
510         Get get = new Get(sessionIdForReporting, currentSchemaContext, transactionProvider);
511         return executeOperation(get, "messages/mapping/get.xml");
512     }
513
514     private Document getConfigRunning() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
515         GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
516         return executeOperation(getConfig, "messages/mapping/getConfig.xml");
517     }
518
519     private Document getConfigCandidate() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
520         GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
521         return executeOperation(getConfig, "messages/mapping/getConfig_candidate.xml");
522     }
523
524     private Document getConfigWithFilter(String resource) throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
525         GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
526         return executeOperation(getConfig, resource);
527     }
528
529     private Document lock() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
530         Lock lock = new Lock(sessionIdForReporting);
531         return executeOperation(lock, "messages/mapping/lock.xml");
532     }
533
534     private Document unlock() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
535         Unlock unlock = new Unlock(sessionIdForReporting);
536         return executeOperation(unlock, "messages/mapping/unlock.xml");
537     }
538
539     private Document lockCandidate() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
540         Lock lock = new Lock(sessionIdForReporting);
541         return executeOperation(lock, "messages/mapping/lock_candidate.xml");
542     }
543
544     private Document unlockCandidate() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
545         Unlock unlock = new Unlock(sessionIdForReporting);
546         return executeOperation(unlock, "messages/mapping/unlock_candidate.xml");
547     }
548
549     private Document executeOperation(NetconfOperation op, String filename) throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
550         final Document request = XmlFileLoader.xmlFileToDocument(filename);
551
552         HandlingPriority priority = op.canHandle(request);
553         Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
554
555         final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
556
557         LOG.debug("Got response {}" , response);
558
559         return response;
560     }
561
562     private Collection<InputStream> getYangSchemas() {
563         final List<String> schemaPaths = Arrays.asList("/META-INF/yang/config.yang", "/yang/mdsal-netconf-mapping-test.yang");
564         final List<InputStream> schemas = new ArrayList<>();
565
566         for (String schemaPath : schemaPaths) {
567             InputStream resourceAsStream = getClass().getResourceAsStream(schemaPath);
568             schemas.add(resourceAsStream);
569         }
570
571         return schemas;
572     }
573
574     private SchemaContext parseSchemas(Collection<InputStream> schemas) throws IOException, YangSyntaxErrorException {
575         final YangParserImpl parser = new YangParserImpl();
576         Collection<ByteSource> sources = BuilderUtils.streamsToByteSources(schemas);
577         return parser.parseSources(sources);
578     }
579
580     private SchemaService createSchemaService() {
581         return new SchemaService() {
582
583             @Override
584             public void addModule(Module module) {
585             }
586
587             @Override
588             public void removeModule(Module module) {
589
590             }
591
592             @Override
593             public SchemaContext getSessionContext() {
594                 return schemaContext;
595             }
596
597             @Override
598             public SchemaContext getGlobalContext() {
599                 return schemaContext;
600             }
601
602             @Override
603             public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(final SchemaContextListener listener) {
604                 listener.onGlobalContextUpdated(getGlobalContext());
605                 return new ListenerRegistration<SchemaContextListener>() {
606                     @Override
607                     public void close() {
608
609                     }
610
611                     @Override
612                     public SchemaContextListener getInstance() {
613                         return listener;
614                     }
615                 };
616             }
617         };
618     }
619 }