2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.mdsal.connector.ops;
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;
18 import com.google.common.io.ByteSource;
19 import com.google.common.util.concurrent.Futures;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.StringWriter;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.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.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.mockito.invocation.InvocationOnMock;
43 import org.mockito.stubbing.Answer;
44 import org.opendaylight.controller.cluster.databroker.ConcurrentDOMDataBroker;
45 import org.opendaylight.controller.config.util.xml.DocumentedException;
46 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorSeverity;
47 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorTag;
48 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorType;
49 import org.opendaylight.controller.config.util.xml.XmlElement;
50 import org.opendaylight.controller.config.util.xml.XmlUtil;
51 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
52 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
53 import org.opendaylight.controller.sal.core.api.model.SchemaService;
54 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
55 import org.opendaylight.netconf.mapping.api.NetconfOperation;
56 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
57 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
58 import org.opendaylight.netconf.mdsal.connector.TransactionProvider;
59 import org.opendaylight.netconf.mdsal.connector.ops.get.Get;
60 import org.opendaylight.netconf.mdsal.connector.ops.get.GetConfig;
61 import org.opendaylight.netconf.util.test.NetconfXmlUnitRecursiveQualifier;
62 import org.opendaylight.netconf.util.test.XmlFileLoader;
63 import org.opendaylight.yangtools.concepts.ListenerRegistration;
64 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
65 import org.opendaylight.yangtools.yang.common.QName;
66 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
67 import org.opendaylight.yangtools.yang.model.api.Module;
68 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
69 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
70 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
71 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
72 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
73 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
74 import org.slf4j.Logger;
75 import org.slf4j.LoggerFactory;
76 import org.w3c.dom.Document;
77 import org.w3c.dom.Node;
78 import org.w3c.dom.NodeList;
79 import org.xml.sax.SAXException;
81 public class NetconfMDSalMappingTest {
83 private static final Logger LOG = LoggerFactory.getLogger(NetconfMDSalMappingTest.class);
85 private static final String RPC_REPLY_ELEMENT = "rpc-reply";
86 private static final String DATA_ELEMENT = "data";
87 private static final String FILTER_NODE = "filter";
88 private static final String GET_CONFIG = "get-config";
89 private static final QName TOP = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "top");
90 private static final QName USERS = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "users");
91 private static final QName USER = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "user");
92 private static final QName MODULES = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "modules");
93 private static final QName AUGMENTED_CONTAINER = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "augmented-container");
94 private static final QName AUGMENTED_STRING_IN_CONT = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "identifier");
95 private static final QName CHOICE_NODE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "choice-node");
96 private static final QName AUGMENTED_CASE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "augmented-case");
97 private static final QName CHOICE_WRAPPER = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "choice-wrapper");
98 private static final QName INNER_CHOICE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "inner-choice");
99 private static final QName INNER_CHOICE_TEXT = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "text");
101 private static final YangInstanceIdentifier AUGMENTED_CONTAINER_IN_MODULES =
102 YangInstanceIdentifier.builder().node(TOP).node(MODULES).build();
104 private static Document RPC_REPLY_OK = null;
108 RPC_REPLY_OK = XmlFileLoader.xmlFileToDocument("messages/mapping/rpc-reply_ok.xml");
109 } catch (final Exception e) {
110 LOG.debug("unable to load rpc reply ok.", e);
111 RPC_REPLY_OK = XmlUtil.newDocument();
115 private CurrentSchemaContext currentSchemaContext = null;
116 private SchemaContext schemaContext = null;
117 private final String sessionIdForReporting = "netconf-test-session1";
119 private TransactionProvider transactionProvider = null;
122 private SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
125 public void setUp() throws Exception {
126 MockitoAnnotations.initMocks(this);
128 XMLUnit.setIgnoreWhitespace(true);
129 XMLUnit.setIgnoreAttributeOrder(true);
131 this.schemaContext = YangParserTestUtils.parseYangStreams(getYangSchemas());
132 schemaContext.getModules();
133 final SchemaService schemaService = createSchemaService();
135 final DOMStore operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", schemaService);
136 final DOMStore configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", schemaService);
138 final EnumMap<LogicalDatastoreType, DOMStore> datastores = new EnumMap<>(LogicalDatastoreType.class);
139 datastores.put(LogicalDatastoreType.CONFIGURATION, configStore);
140 datastores.put(LogicalDatastoreType.OPERATIONAL, operStore);
142 final ExecutorService listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
143 16, 16, "CommitFutures");
145 final ConcurrentDOMDataBroker cdb = new ConcurrentDOMDataBroker(datastores, listenableFutureExecutor);
146 this.transactionProvider = new TransactionProvider(cdb, sessionIdForReporting);
148 doAnswer(new Answer() {
150 public Object answer(final InvocationOnMock invocationOnMock) throws Throwable {
151 final SourceIdentifier sId = (SourceIdentifier) invocationOnMock.getArguments()[0];
152 final YangTextSchemaSource yangTextSchemaSource =
153 YangTextSchemaSource.delegateForByteSource(sId, ByteSource.wrap("module test".getBytes()));
154 return Futures.immediateCheckedFuture(yangTextSchemaSource);
157 }).when(sourceProvider).getSource(any(SourceIdentifier.class));
159 this.currentSchemaContext = new CurrentSchemaContext(schemaService, sourceProvider);
164 public void testEmptyDatastore() throws Exception {
165 assertEmptyDatastore(get());
166 assertEmptyDatastore(getConfigCandidate());
167 assertEmptyDatastore(getConfigRunning());
171 public void testIncorrectGet() throws Exception {
174 executeOperation(new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider), "messages/mapping/bad_getConfig.xml");
175 fail("Should have failed, this is an incorrect request");
176 } catch (final DocumentedException e) {
177 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
178 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_FAILED);
179 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
183 executeOperation(new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider), "messages/mapping/bad_namespace_getConfig.xml");
184 fail("Should have failed, this is an incorrect request");
185 } catch (final DocumentedException e) {
186 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
187 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_FAILED);
188 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
195 public void testEditRunning() throws Exception {
198 edit("messages/mapping/editConfigs/editConfig_running.xml");
199 fail("Should have failed - edit config on running datastore is not supported");
200 } catch (final DocumentedException e) {
201 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
202 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
203 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
209 public void testCommitWithoutOpenTransaction() throws Exception {
210 verifyResponse(commit(), RPC_REPLY_OK);
211 assertEmptyDatastore(getConfigCandidate());
215 public void testCandidateTransaction() throws Exception {
217 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_n1.xml"), RPC_REPLY_OK);
218 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
219 assertEmptyDatastore(getConfigRunning());
221 verifyResponse(discardChanges(), RPC_REPLY_OK);
222 assertEmptyDatastore(getConfigCandidate());
227 public void testEditWithCommit() throws Exception {
229 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_n1.xml"), RPC_REPLY_OK);
230 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
232 verifyResponse(commit(), RPC_REPLY_OK);
233 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
240 public void testKeyOrder() throws Exception {
241 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_keys_1.xml"), RPC_REPLY_OK);
242 verifyResponse(commit(), RPC_REPLY_OK);
243 final Document configRunning = getConfigRunning();
244 final String responseAsString = XmlUtil.toString(configRunning);
245 verifyResponse(configRunning, XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_keys_1_control.xml"));
247 final int key3 = responseAsString.indexOf("key3");
248 final int key1 = responseAsString.indexOf("key1");
249 final int key2 = responseAsString.indexOf("key2");
251 assertTrue(String.format("Key ordering invalid, should be key3(%d) < key1(%d) < key2(%d)", key3, key1, key2),
252 key3 < key1 && key1 < key2);
259 public void testMultipleEditsWithMerge() throws Exception {
261 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
262 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_control_1.xml"));
263 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
264 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_control_2.xml"));
265 assertEmptyDatastore(getConfigRunning());
267 verifyResponse(commit(), RPC_REPLY_OK);
268 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_control_2.xml"));
275 public void testMoreComplexEditConfigs() throws Exception {
277 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
278 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
280 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_2.xml"), RPC_REPLY_OK);
281 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_more_complex_merge.xml"));
283 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_3.xml"), RPC_REPLY_OK);
284 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_more_complex_merge_2.xml"));
286 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_4_replace.xml"), RPC_REPLY_OK);
287 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_replace.xml"));
288 verifyResponse(commit(), RPC_REPLY_OK);
290 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_replace.xml"));
292 verifyResponse(edit("messages/mapping/editConfigs/editConfig_replace_default.xml"), RPC_REPLY_OK);
293 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_replace_default_control.xml"));
294 verifyResponse(commit(), RPC_REPLY_OK);
296 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_replace_default_control.xml"));
303 public void testOrderedListEdits() throws Exception {
305 verifyResponse(edit("messages/mapping/editConfigs/editConfig_ordered_list_create.xml"), RPC_REPLY_OK);
306 verifyResponse(commit(), RPC_REPLY_OK);
308 verifyResponse(edit("messages/mapping/editConfigs/editConfig_ordered_list_replace.xml"), RPC_REPLY_OK);
309 verifyResponse(commit(), RPC_REPLY_OK);
316 public void testAugmentedOrderedListEdits() throws Exception {
318 verifyResponse(edit("messages/mapping/editConfigs/editConfig_augmented_ordered_list_create.xml"),
320 verifyResponse(commit(), RPC_REPLY_OK);
322 verifyResponse(edit("messages/mapping/editConfigs/editConfig_augmented_ordered_list_replace.xml"),
324 verifyResponse(commit(), RPC_REPLY_OK);
331 public void testAugmentedContainerReplace() throws Exception {
332 verifyResponse(edit("messages/mapping/editConfigs/editConfig_empty_modules_create.xml"),
334 verifyResponse(commit(), RPC_REPLY_OK);
336 verifyResponse(edit("messages/mapping/editConfigs/editConfig_augmented_container_replace.xml"),
338 verifyResponse(commit(), RPC_REPLY_OK);
344 public void testLeafFromAugmentReplace() throws Exception {
345 verifyResponse(edit("messages/mapping/editConfigs/editConfig_empty_modules_create.xml"),
347 verifyResponse(commit(), RPC_REPLY_OK);
349 verifyResponse(edit("messages/mapping/editConfigs/editConfig_leaf_from_augment_replace.xml"),
351 verifyResponse(commit(), RPC_REPLY_OK);
357 public void testLock() throws Exception {
359 verifyResponse(lockCandidate(), RPC_REPLY_OK);
363 fail("Should have failed - locking of running datastore is not supported");
364 } catch (final DocumentedException e) {
365 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
366 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
367 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
373 fail("Should have failed, target is missing");
374 } catch (final DocumentedException e) {
375 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
376 assertTrue(e.getErrorTag() == ErrorTag.INVALID_VALUE);
377 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
382 public void testUnlock() throws Exception {
384 verifyResponse(unlockCandidate(), RPC_REPLY_OK);
388 fail("Should have failed - unlocking of running datastore is not supported");
389 } catch (final DocumentedException e) {
390 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
391 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
392 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
396 unlockWithoutTarget();
397 fail("Should have failed, target is missing");
398 } catch (final DocumentedException e) {
399 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
400 assertTrue(e.getErrorTag() == ErrorTag.INVALID_VALUE);
401 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
406 public void testEditWithCreate() throws Exception {
408 verifyResponse(edit("messages/mapping/editConfigs/editConfig_create.xml"), RPC_REPLY_OK);
409 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_create_n1_control.xml"));
413 edit("messages/mapping/editConfigs/editConfig_create.xml");
414 fail("Create should have failed - data already exists");
415 } catch (final DocumentedException e) {
416 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
417 assertTrue(e.getErrorTag() == ErrorTag.DATA_EXISTS);
418 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
421 verifyResponse(discardChanges(), RPC_REPLY_OK);
426 public void testDeleteNonExisting() throws Exception {
428 assertEmptyDatastore(getConfigCandidate());
429 assertEmptyDatastore(getConfigRunning());
432 edit("messages/mapping/editConfigs/editConfig_delete-top.xml");
433 fail("Delete should have failed - data is missing");
434 } catch (final DocumentedException e) {
435 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
436 assertTrue(e.getErrorTag() == ErrorTag.DATA_MISSING);
437 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
443 public void testEditMissingDefaultOperation() throws Exception {
445 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_1.xml"), RPC_REPLY_OK);
446 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_2.xml"), RPC_REPLY_OK);
447 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
449 verifyResponse(commit(), RPC_REPLY_OK);
450 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
455 public static void printDocument(final Document doc) throws IOException, TransformerException {
456 final TransformerFactory tf = TransformerFactory.newInstance();
457 final Transformer transformer = tf.newTransformer();
458 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
459 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
460 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
461 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
462 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
464 final StringWriter writer = new StringWriter();
465 transformer.transform(new DOMSource(doc),
466 new StreamResult(writer));
467 LOG.warn(writer.getBuffer().toString());
471 public void testEditConfigWithMultipleOperations() throws Exception {
474 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_setup.xml"), RPC_REPLY_OK);
475 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_1.xml"), RPC_REPLY_OK);
477 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_2.xml"), RPC_REPLY_OK);
478 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_2_control.xml"));
480 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_leaf_operations.xml"), RPC_REPLY_OK);
481 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_control.xml"));
485 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_setup.xml"), RPC_REPLY_OK);
486 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_default-replace.xml"), RPC_REPLY_OK);
489 edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_create_existing.xml");
491 } catch (final DocumentedException e) {
492 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
493 assertTrue(e.getErrorTag() == ErrorTag.DATA_EXISTS);
494 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
497 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete_children_operations.xml"), RPC_REPLY_OK);
498 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
499 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_remove-non-existing.xml"), RPC_REPLY_OK);
502 edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete-non-existing.xml");
504 } catch (final DocumentedException e) {
505 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
506 assertTrue(e.getErrorTag() == ErrorTag.DATA_MISSING);
507 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
510 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup.xml"), RPC_REPLY_OK);
511 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup-control.xml"));
513 // Test files have been modified. RFC6020 requires that at most once case inside a choice is present at any time
514 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2.xml"), RPC_REPLY_OK);
515 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2-control.xml"));
517 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_delete.xml"), RPC_REPLY_OK);
518 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
524 public void testReplaceMapEntry() throws Exception {
525 verifyResponse(edit("messages/mapping/editConfigs/edit-config-replace-map-entry.xml"), RPC_REPLY_OK);
526 verifyResponse(commit(), RPC_REPLY_OK);
527 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-config-map-entry.xml"));
531 public void testMergeMapEntry() throws Exception {
532 verifyResponse(edit("messages/mapping/editConfigs/edit-config-merge-map-entry.xml"), RPC_REPLY_OK);
533 verifyResponse(commit(), RPC_REPLY_OK);
534 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-config-map-entry.xml"));
538 public void testFiltering() throws Exception {
540 assertEmptyDatastore(getConfigCandidate());
541 assertEmptyDatastore(getConfigRunning());
543 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-config-empty-filter.xml"),
544 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
545 verifyResponse(getWithFilter("messages/mapping/filters/get-empty-filter.xml"),
546 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
548 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
549 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
550 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-users.xml"),
551 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
553 verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup.xml"), RPC_REPLY_OK);
554 verifyResponse(commit(), RPC_REPLY_OK);
556 verifyFilterIdentifier("messages/mapping/filters/get-filter-alluser.xml",
557 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
558 verifyFilterIdentifier("messages/mapping/filters/get-filter-company-info.xml",
559 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
560 verifyFilterIdentifier("messages/mapping/filters/get-filter-modules-and-admin.xml",
561 YangInstanceIdentifier.builder().node(TOP).build());
562 verifyFilterIdentifier("messages/mapping/filters/get-filter-only-names-types.xml",
563 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
564 verifyFilterIdentifier("messages/mapping/filters/get-filter-specific-module-type-and-user.xml",
565 YangInstanceIdentifier.builder().node(TOP).build());
566 verifyFilterIdentifier("messages/mapping/filters/get-filter-superuser.xml",
567 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
568 verifyFilterIdentifier("messages/mapping/filters/get-filter-users.xml",
569 YangInstanceIdentifier.builder().node(TOP).node(USERS).build());
571 final YangInstanceIdentifier ident = YangInstanceIdentifier.
572 builder(AUGMENTED_CONTAINER_IN_MODULES).
573 node(AUGMENTED_CONTAINER).
574 node(AUGMENTED_STRING_IN_CONT).build();
576 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-string.xml", ident);
577 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case.xml",
578 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(AUGMENTED_CASE).build());
580 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case.xml"),
581 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case.xml"));
584 * RFC6020 requires that at most once case inside a choice is present at any time.
586 * <augmented-case>augmented case</augmented-case>
588 * messages/mapping/editConfigs/editConfig-filtering-setup.xml
589 * cannot exists together with
590 * <text>augmented nested choice text1</text>
592 * messages/mapping/editConfigs/editConfig-filtering-setup2.xml
594 //verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup2.xml"), RPC_REPLY_OK);
595 //verifyResponse(commit(), RPC_REPLY_OK);
597 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml",
598 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).build());
599 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-case.xml",
600 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).node(INNER_CHOICE).node(INNER_CHOICE_TEXT).build());
602 // verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-string.xml"),
603 // XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-string.xml"));
604 // verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml"),
605 // XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
606 // verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-case.xml"),
607 // XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
609 verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-top.xml"), RPC_REPLY_OK);
610 verifyResponse(commit(), RPC_REPLY_OK);
614 private void verifyFilterIdentifier(final String resource, final YangInstanceIdentifier identifier) throws Exception {
615 final TestingGetConfig getConfig = new TestingGetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
616 final Document request = XmlFileLoader.xmlFileToDocument(resource);
617 final YangInstanceIdentifier iid = getConfig.getInstanceIdentifierFromDocument(request);
618 assertEquals(identifier, iid);
621 private class TestingGetConfig extends GetConfig{
622 public TestingGetConfig(final String sessionId, final CurrentSchemaContext schemaContext, final TransactionProvider transactionProvider) {
623 super(sessionId, schemaContext, transactionProvider);
626 public YangInstanceIdentifier getInstanceIdentifierFromDocument(final Document request) throws DocumentedException {
627 final XmlElement filterElement = XmlElement.fromDomDocument(request).getOnlyChildElement(GET_CONFIG).getOnlyChildElement(FILTER_NODE);
628 return getInstanceIdentifierFromFilter(filterElement);
632 private void deleteDatastore() throws Exception{
633 verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-root.xml"), RPC_REPLY_OK);
634 assertEmptyDatastore(getConfigCandidate());
636 verifyResponse(commit(), RPC_REPLY_OK);
637 assertEmptyDatastore(getConfigRunning());
640 private void verifyResponse(final Document response, final Document template) throws IOException, TransformerException {
641 final DetailedDiff dd = new DetailedDiff(new Diff(response, template));
642 dd.overrideElementQualifier(new NetconfXmlUnitRecursiveQualifier());
644 printDocument(response);
645 printDocument(template);
647 assertTrue(dd.toString(), dd.similar());
650 private void assertEmptyDatastore(final Document response) {
652 final NodeList nodes = response.getChildNodes();
653 assertTrue(nodes.getLength() == 1);
655 assertEquals(nodes.item(0).getLocalName(), RPC_REPLY_ELEMENT);
657 final NodeList replyNodes = nodes.item(0).getChildNodes();
658 assertTrue(replyNodes.getLength() == 1);
660 final Node dataNode = replyNodes.item(0);
661 assertEquals(dataNode.getLocalName(), DATA_ELEMENT);
662 assertFalse(dataNode.hasChildNodes());
666 private Document commit() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
667 final Commit commit = new Commit(sessionIdForReporting, transactionProvider);
668 return executeOperation(commit, "messages/mapping/commit.xml");
671 private Document discardChanges() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
672 final DiscardChanges discardOp = new DiscardChanges(sessionIdForReporting, transactionProvider);
673 return executeOperation(discardOp, "messages/mapping/discardChanges.xml");
676 private Document edit(final String resource) throws DocumentedException, ParserConfigurationException, SAXException, IOException {
677 final EditConfig editConfig = new EditConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
678 return executeOperation(editConfig, resource);
681 private Document get() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
682 final Get get = new Get(sessionIdForReporting, currentSchemaContext, transactionProvider);
683 return executeOperation(get, "messages/mapping/get.xml");
686 private Document getWithFilter(final String resource) throws DocumentedException, ParserConfigurationException, SAXException, IOException {
687 final Get get = new Get(sessionIdForReporting, currentSchemaContext, transactionProvider);
688 return executeOperation(get, resource);
691 private Document getConfigRunning() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
692 final GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
693 return executeOperation(getConfig, "messages/mapping/getConfig.xml");
696 private Document getConfigCandidate() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
697 final GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
698 return executeOperation(getConfig, "messages/mapping/getConfig_candidate.xml");
701 private Document getConfigWithFilter(final String resource) throws DocumentedException, ParserConfigurationException, SAXException, IOException {
702 final GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
703 return executeOperation(getConfig, resource);
706 private Document lock() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
707 final Lock lock = new Lock(sessionIdForReporting);
708 return executeOperation(lock, "messages/mapping/lock.xml");
711 private Document unlock() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
712 final Unlock unlock = new Unlock(sessionIdForReporting);
713 return executeOperation(unlock, "messages/mapping/unlock.xml");
716 private Document lockWithoutTarget() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
717 final Lock lock = new Lock(sessionIdForReporting);
718 return executeOperation(lock, "messages/mapping/lock_notarget.xml");
721 private Document unlockWithoutTarget() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
722 final Unlock unlock = new Unlock(sessionIdForReporting);
723 return executeOperation(unlock, "messages/mapping/unlock_notarget.xml");
726 private Document lockCandidate() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
727 final Lock lock = new Lock(sessionIdForReporting);
728 return executeOperation(lock, "messages/mapping/lock_candidate.xml");
731 private Document unlockCandidate() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
732 final Unlock unlock = new Unlock(sessionIdForReporting);
733 return executeOperation(unlock, "messages/mapping/unlock_candidate.xml");
736 private Document executeOperation(final NetconfOperation op, final String filename) throws ParserConfigurationException, SAXException, IOException, DocumentedException {
737 final Document request = XmlFileLoader.xmlFileToDocument(filename);
738 final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
740 LOG.debug("Got response {}" , response);
744 private List<InputStream> getYangSchemas() {
745 final List<String> schemaPaths = Arrays.asList("/META-INF/yang/config.yang", "/yang/mdsal-netconf-mapping-test.yang");
746 final List<InputStream> schemas = new ArrayList<>();
748 for (final String schemaPath : schemaPaths) {
749 final InputStream resourceAsStream = getClass().getResourceAsStream(schemaPath);
750 schemas.add(resourceAsStream);
756 private SchemaService createSchemaService() {
757 return new SchemaService() {
760 public void addModule(final Module module) {
764 public void removeModule(final Module module) {
769 public SchemaContext getSessionContext() {
770 return schemaContext;
774 public SchemaContext getGlobalContext() {
775 return schemaContext;
779 public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(final SchemaContextListener listener) {
780 listener.onGlobalContextUpdated(getGlobalContext());
781 return new ListenerRegistration<SchemaContextListener>() {
783 public void close() {
788 public SchemaContextListener getInstance() {