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