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.InputStream;
21 import java.io.StringWriter;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.EnumMap;
25 import java.util.List;
26 import java.util.concurrent.ExecutorService;
27 import javax.xml.transform.OutputKeys;
28 import javax.xml.transform.Transformer;
29 import javax.xml.transform.TransformerFactory;
30 import javax.xml.transform.dom.DOMSource;
31 import javax.xml.transform.stream.StreamResult;
32 import org.custommonkey.xmlunit.DetailedDiff;
33 import org.custommonkey.xmlunit.Diff;
34 import org.custommonkey.xmlunit.XMLUnit;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.opendaylight.controller.cluster.databroker.ConcurrentDOMDataBroker;
40 import org.opendaylight.controller.config.util.xml.DocumentedException;
41 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorSeverity;
42 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorTag;
43 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorType;
44 import org.opendaylight.controller.config.util.xml.XmlElement;
45 import org.opendaylight.controller.config.util.xml.XmlUtil;
46 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
47 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
48 import org.opendaylight.controller.sal.core.api.model.SchemaService;
49 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
50 import org.opendaylight.netconf.mapping.api.NetconfOperation;
51 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
52 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
53 import org.opendaylight.netconf.mdsal.connector.TransactionProvider;
54 import org.opendaylight.netconf.mdsal.connector.ops.get.Get;
55 import org.opendaylight.netconf.mdsal.connector.ops.get.GetConfig;
56 import org.opendaylight.netconf.util.test.NetconfXmlUnitRecursiveQualifier;
57 import org.opendaylight.netconf.util.test.XmlFileLoader;
58 import org.opendaylight.yangtools.concepts.ListenerRegistration;
59 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
60 import org.opendaylight.yangtools.yang.common.QName;
61 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
62 import org.opendaylight.yangtools.yang.model.api.Module;
63 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
64 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
65 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
66 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
67 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
68 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
69 import org.slf4j.Logger;
70 import org.slf4j.LoggerFactory;
71 import org.w3c.dom.Document;
72 import org.w3c.dom.Node;
73 import org.w3c.dom.NodeList;
75 public class NetconfMDSalMappingTest {
76 private static final Logger LOG = LoggerFactory.getLogger(NetconfMDSalMappingTest.class);
78 private static final String RPC_REPLY_ELEMENT = "rpc-reply";
79 private static final String DATA_ELEMENT = "data";
80 private static final String FILTER_NODE = "filter";
81 private static final String GET_CONFIG = "get-config";
82 private static final QName TOP = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "top");
83 private static final QName USERS = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "users");
84 private static final QName USER = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "user");
85 private static final QName MODULES = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "modules");
86 private static final QName AUGMENTED_CONTAINER = QName.create("urn:opendaylight:mdsal:mapping:test",
87 "2015-02-26", "augmented-container");
88 private static final QName AUGMENTED_STRING_IN_CONT = QName.create("urn:opendaylight:mdsal:mapping:test",
89 "2015-02-26", "identifier");
90 private static final QName CHOICE_NODE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
92 private static final QName AUGMENTED_CASE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
94 private static final QName CHOICE_WRAPPER = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
96 private static final QName INNER_CHOICE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
98 private static final QName INNER_CHOICE_TEXT = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
101 private static final YangInstanceIdentifier AUGMENTED_CONTAINER_IN_MODULES =
102 YangInstanceIdentifier.builder().node(TOP).node(MODULES).build();
103 private static final String SESSION_ID_FOR_REPORTING = "netconf-test-session1";
104 private static final Document RPC_REPLY_OK = NetconfMDSalMappingTest.getReplyOk();
106 private CurrentSchemaContext currentSchemaContext = null;
107 private SchemaContext schemaContext = null;
108 private TransactionProvider transactionProvider = null;
111 private SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
113 @SuppressWarnings("illegalCatch")
114 private static Document getReplyOk() {
117 doc = XmlFileLoader.xmlFileToDocument("messages/mapping/rpc-reply_ok.xml");
118 } catch (final Exception e) {
119 LOG.debug("unable to load rpc reply ok.", e);
120 doc = XmlUtil.newDocument();
126 public void setUp() throws Exception {
127 MockitoAnnotations.initMocks(this);
129 XMLUnit.setIgnoreWhitespace(true);
130 XMLUnit.setIgnoreAttributeOrder(true);
132 this.schemaContext = YangParserTestUtils.parseYangStreams(getYangSchemas());
133 schemaContext.getModules();
134 final SchemaService schemaService = createSchemaService();
136 final DOMStore operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", schemaService);
137 final DOMStore configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", schemaService);
139 final EnumMap<LogicalDatastoreType, DOMStore> datastores = new EnumMap<>(LogicalDatastoreType.class);
140 datastores.put(LogicalDatastoreType.CONFIGURATION, configStore);
141 datastores.put(LogicalDatastoreType.OPERATIONAL, operStore);
143 final ExecutorService listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
144 16, 16, "CommitFutures");
146 final ConcurrentDOMDataBroker cdb = new ConcurrentDOMDataBroker(datastores, listenableFutureExecutor);
147 this.transactionProvider = new TransactionProvider(cdb, SESSION_ID_FOR_REPORTING);
149 doAnswer(invocationOnMock -> {
150 final SourceIdentifier sId = (SourceIdentifier) invocationOnMock.getArguments()[0];
151 final YangTextSchemaSource yangTextSchemaSource =
152 YangTextSchemaSource.delegateForByteSource(sId, ByteSource.wrap("module test".getBytes()));
153 return Futures.immediateCheckedFuture(yangTextSchemaSource);
155 }).when(sourceProvider).getSource(any(SourceIdentifier.class));
157 this.currentSchemaContext = new CurrentSchemaContext(schemaService, sourceProvider);
161 public void testEmptyDatastore() throws Exception {
162 assertEmptyDatastore(get());
163 assertEmptyDatastore(getConfigCandidate());
164 assertEmptyDatastore(getConfigRunning());
168 public void testIncorrectGet() throws Exception {
170 executeOperation(new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider),
171 "messages/mapping/bad_getConfig.xml");
172 fail("Should have failed, this is an incorrect request");
173 } catch (final DocumentedException e) {
174 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
175 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_FAILED);
176 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
180 executeOperation(new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider),
181 "messages/mapping/bad_namespace_getConfig.xml");
182 fail("Should have failed, this is an incorrect request");
183 } catch (final DocumentedException e) {
184 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
185 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_FAILED);
186 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
191 public void testEditRunning() throws Exception {
193 edit("messages/mapping/editConfigs/editConfig_running.xml");
194 fail("Should have failed - edit config on running datastore is not supported");
195 } catch (final DocumentedException e) {
196 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
197 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
198 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
203 public void testCommitWithoutOpenTransaction() throws Exception {
204 verifyResponse(commit(), RPC_REPLY_OK);
205 assertEmptyDatastore(getConfigCandidate());
209 public void testCandidateTransaction() throws Exception {
210 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_n1.xml"), RPC_REPLY_OK);
211 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
212 "messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
213 assertEmptyDatastore(getConfigRunning());
215 verifyResponse(discardChanges(), RPC_REPLY_OK);
216 assertEmptyDatastore(getConfigCandidate());
220 public void testEditWithCommit() throws Exception {
221 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_n1.xml"), RPC_REPLY_OK);
222 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
223 "messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
225 verifyResponse(commit(), RPC_REPLY_OK);
226 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
227 "messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
233 public void testKeyOrder() throws Exception {
234 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_keys_1.xml"), RPC_REPLY_OK);
235 verifyResponse(commit(), RPC_REPLY_OK);
236 final Document configRunning = getConfigRunning();
237 final String responseAsString = XmlUtil.toString(configRunning);
238 verifyResponse(configRunning, XmlFileLoader.xmlFileToDocument(
239 "messages/mapping/editConfigs/editConfig_merge_multiple_keys_1_control.xml"));
241 final int key3 = responseAsString.indexOf("key3");
242 final int key1 = responseAsString.indexOf("key1");
243 final int key2 = responseAsString.indexOf("key2");
245 assertTrue(String.format("Key ordering invalid, should be key3(%d) < key1(%d) < key2(%d)", key3, key1, key2),
246 key3 < key1 && key1 < key2);
253 public void testMultipleEditsWithMerge() throws Exception {
254 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
255 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
256 "messages/mapping/editConfigs/editConfig_merge_multiple_control_1.xml"));
257 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
258 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
259 "messages/mapping/editConfigs/editConfig_merge_multiple_control_2.xml"));
260 assertEmptyDatastore(getConfigRunning());
262 verifyResponse(commit(), RPC_REPLY_OK);
263 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
264 "messages/mapping/editConfigs/editConfig_merge_multiple_control_2.xml"));
270 public void testMoreComplexEditConfigs() throws Exception {
271 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
272 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
274 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_2.xml"), RPC_REPLY_OK);
275 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
276 "messages/mapping/editConfigs/editConfig_merge_multiple_after_more_complex_merge.xml"));
278 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_3.xml"), RPC_REPLY_OK);
279 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
280 "messages/mapping/editConfigs/editConfig_merge_multiple_after_more_complex_merge_2.xml"));
282 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_4_replace.xml"), RPC_REPLY_OK);
283 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
284 "messages/mapping/editConfigs/editConfig_merge_multiple_after_replace.xml"));
285 verifyResponse(commit(), RPC_REPLY_OK);
287 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
288 "messages/mapping/editConfigs/editConfig_merge_multiple_after_replace.xml"));
290 verifyResponse(edit("messages/mapping/editConfigs/editConfig_replace_default.xml"), RPC_REPLY_OK);
291 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
292 "messages/mapping/editConfigs/editConfig_replace_default_control.xml"));
293 verifyResponse(commit(), RPC_REPLY_OK);
295 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
296 "messages/mapping/editConfigs/editConfig_replace_default_control.xml"));
302 public void testOrderedListEdits() throws Exception {
304 verifyResponse(edit("messages/mapping/editConfigs/editConfig_ordered_list_create.xml"), RPC_REPLY_OK);
305 verifyResponse(commit(), RPC_REPLY_OK);
307 verifyResponse(edit("messages/mapping/editConfigs/editConfig_ordered_list_replace.xml"), RPC_REPLY_OK);
308 verifyResponse(commit(), RPC_REPLY_OK);
315 public void testAugmentedOrderedListEdits() throws Exception {
317 verifyResponse(edit("messages/mapping/editConfigs/editConfig_augmented_ordered_list_create.xml"),
319 verifyResponse(commit(), RPC_REPLY_OK);
321 verifyResponse(edit("messages/mapping/editConfigs/editConfig_augmented_ordered_list_replace.xml"),
323 verifyResponse(commit(), RPC_REPLY_OK);
330 public void testAugmentedContainerReplace() throws Exception {
331 verifyResponse(edit("messages/mapping/editConfigs/editConfig_empty_modules_create.xml"),
333 verifyResponse(commit(), RPC_REPLY_OK);
335 verifyResponse(edit("messages/mapping/editConfigs/editConfig_augmented_container_replace.xml"),
337 verifyResponse(commit(), RPC_REPLY_OK);
343 public void testLeafFromAugmentReplace() throws Exception {
344 verifyResponse(edit("messages/mapping/editConfigs/editConfig_empty_modules_create.xml"),
346 verifyResponse(commit(), RPC_REPLY_OK);
348 verifyResponse(edit("messages/mapping/editConfigs/editConfig_leaf_from_augment_replace.xml"),
350 verifyResponse(commit(), RPC_REPLY_OK);
356 public void testLock() throws Exception {
357 verifyResponse(lockCandidate(), RPC_REPLY_OK);
361 fail("Should have failed - locking of running datastore is not supported");
362 } catch (final DocumentedException e) {
363 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
364 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
365 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
370 fail("Should have failed, target is missing");
371 } catch (final DocumentedException e) {
372 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
373 assertTrue(e.getErrorTag() == ErrorTag.INVALID_VALUE);
374 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
379 public void testUnlock() throws Exception {
380 verifyResponse(unlockCandidate(), RPC_REPLY_OK);
384 fail("Should have failed - unlocking of running datastore is not supported");
385 } catch (final DocumentedException e) {
386 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
387 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
388 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
392 unlockWithoutTarget();
393 fail("Should have failed, target is missing");
394 } catch (final DocumentedException e) {
395 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
396 assertTrue(e.getErrorTag() == ErrorTag.INVALID_VALUE);
397 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
402 public void testEditWithCreate() throws Exception {
403 verifyResponse(edit("messages/mapping/editConfigs/editConfig_create.xml"), RPC_REPLY_OK);
404 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
405 "messages/mapping/editConfig_create_n1_control.xml"));
408 edit("messages/mapping/editConfigs/editConfig_create.xml");
409 fail("Create should have failed - data already exists");
410 } catch (final DocumentedException e) {
411 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
412 assertTrue(e.getErrorTag() == ErrorTag.DATA_EXISTS);
413 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
416 verifyResponse(discardChanges(), RPC_REPLY_OK);
420 public void testDeleteNonExisting() throws Exception {
421 assertEmptyDatastore(getConfigCandidate());
422 assertEmptyDatastore(getConfigRunning());
425 edit("messages/mapping/editConfigs/editConfig_delete-top.xml");
426 fail("Delete should have failed - data is missing");
427 } catch (final DocumentedException e) {
428 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
429 assertTrue(e.getErrorTag() == ErrorTag.DATA_MISSING);
430 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
435 public void testEditMissingDefaultOperation() throws Exception {
436 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_1.xml"),
438 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_2.xml"),
440 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
441 "messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
443 verifyResponse(commit(), RPC_REPLY_OK);
444 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
445 "messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
450 private static void printDocument(final Document doc) throws Exception {
451 final TransformerFactory tf = TransformerFactory.newInstance();
452 final Transformer transformer = tf.newTransformer();
453 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
454 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
455 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
456 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
457 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
459 final StringWriter writer = new StringWriter();
460 transformer.transform(new DOMSource(doc),
461 new StreamResult(writer));
462 LOG.warn(writer.getBuffer().toString());
466 public void testEditConfigWithMultipleOperations() throws Exception {
469 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_setup.xml"),
471 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_1.xml"),
474 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_2.xml"),
476 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
477 "messages/mapping/editConfigs/editConfig_merge_multiple_operations_2_control.xml"));
479 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_leaf_operations.xml"),
481 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
482 "messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_control.xml"));
486 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_setup.xml"),
488 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_default-replace.xml"),
492 edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_create_existing.xml");
494 } catch (final DocumentedException e) {
495 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
496 assertTrue(e.getErrorTag() == ErrorTag.DATA_EXISTS);
497 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
501 "messages/mapping/editConfigs/"
502 + "editConfig_merge_multiple_operations_4_delete_children_operations.xml"),
504 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
505 "messages/mapping/editConfigs/"
506 + "editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
508 "messages/mapping/editConfigs/"
509 + "editConfig_merge_multiple_operations_4_remove-non-existing.xml"),
512 edit("messages/mapping/editConfigs/"
513 + "editConfig_merge_multiple_operations_4_delete-non-existing.xml");
515 } catch (final DocumentedException e) {
516 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
517 assertTrue(e.getErrorTag() == ErrorTag.DATA_MISSING);
518 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
521 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup.xml"),
523 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
524 "messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup-control.xml"));
526 // Test files have been modified. RFC6020 requires that at most once case inside a choice is present at any time
527 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2.xml"),
529 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
530 "messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2-control.xml"));
532 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_delete.xml"),
534 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
535 "messages/mapping/editConfigs"
536 + "/editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
542 public void testReplaceMapEntry() throws Exception {
543 verifyResponse(edit("messages/mapping/editConfigs/edit-config-replace-map-entry.xml"), RPC_REPLY_OK);
544 verifyResponse(commit(), RPC_REPLY_OK);
545 verifyResponse(getConfigRunning(),
546 XmlFileLoader.xmlFileToDocument("messages/mapping/get-config-map-entry.xml"));
550 public void testMergeMapEntry() throws Exception {
551 verifyResponse(edit("messages/mapping/editConfigs/edit-config-merge-map-entry.xml"), RPC_REPLY_OK);
552 verifyResponse(commit(), RPC_REPLY_OK);
553 verifyResponse(getConfigRunning(),
554 XmlFileLoader.xmlFileToDocument("messages/mapping/get-config-map-entry.xml"));
558 public void testFiltering() throws Exception {
559 assertEmptyDatastore(getConfigCandidate());
560 assertEmptyDatastore(getConfigRunning());
562 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-config-empty-filter.xml"),
563 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
564 verifyResponse(getWithFilter("messages/mapping/filters/get-empty-filter.xml"),
565 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
567 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response"
569 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
570 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-users.xml"),
571 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
573 verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup.xml"), RPC_REPLY_OK);
574 verifyResponse(commit(), RPC_REPLY_OK);
576 verifyFilterIdentifier("messages/mapping/filters/get-filter-alluser.xml",
577 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
578 verifyFilterIdentifier("messages/mapping/filters/get-filter-company-info.xml",
579 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
580 verifyFilterIdentifier("messages/mapping/filters/get-filter-modules-and-admin.xml",
581 YangInstanceIdentifier.builder().node(TOP).build());
582 verifyFilterIdentifier("messages/mapping/filters/get-filter-only-names-types.xml",
583 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
584 verifyFilterIdentifier("messages/mapping/filters/get-filter-specific-module-type-and-user.xml",
585 YangInstanceIdentifier.builder().node(TOP).build());
586 verifyFilterIdentifier("messages/mapping/filters/get-filter-superuser.xml",
587 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
588 verifyFilterIdentifier("messages/mapping/filters/get-filter-users.xml",
589 YangInstanceIdentifier.builder().node(TOP).node(USERS).build());
591 final YangInstanceIdentifier ident = YangInstanceIdentifier
592 .builder(AUGMENTED_CONTAINER_IN_MODULES)
593 .node(AUGMENTED_CONTAINER)
594 .node(AUGMENTED_STRING_IN_CONT).build();
596 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-string.xml", ident);
597 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case.xml",
598 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(AUGMENTED_CASE).build());
600 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case.xml"),
601 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case.xml"));
604 * RFC6020 requires that at most once case inside a choice is present at any time.
606 * <augmented-case>augmented case</augmented-case>
608 * messages/mapping/editConfigs/editConfig-filtering-setup.xml
609 * cannot exists together with
610 * <text>augmented nested choice text1</text>
612 * messages/mapping/editConfigs/editConfig-filtering-setup2.xml
614 //verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup2.xml"), RPC_REPLY_OK);
615 //verifyResponse(commit(), RPC_REPLY_OK);
617 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml",
618 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).build());
619 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-case.xml",
620 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).node(INNER_CHOICE)
621 .node(INNER_CHOICE_TEXT).build());
623 // verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-string.xml"),
624 // XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-string.xml"));
625 // verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml"),
626 // XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
627 // verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-case.xml"),
628 // XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
630 verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-top.xml"), RPC_REPLY_OK);
631 verifyResponse(commit(), RPC_REPLY_OK);
635 private void verifyFilterIdentifier(final String resource, final YangInstanceIdentifier identifier)
637 final TestingGetConfig getConfig = new TestingGetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext,
638 transactionProvider);
639 final Document request = XmlFileLoader.xmlFileToDocument(resource);
640 final YangInstanceIdentifier iid = getConfig.getInstanceIdentifierFromDocument(request);
641 assertEquals(identifier, iid);
644 private class TestingGetConfig extends GetConfig {
645 TestingGetConfig(final String sessionId, final CurrentSchemaContext schemaContext,
646 final TransactionProvider transactionProvider) {
647 super(sessionId, schemaContext, transactionProvider);
650 YangInstanceIdentifier getInstanceIdentifierFromDocument(final Document request) throws DocumentedException {
651 final XmlElement filterElement = XmlElement.fromDomDocument(request).getOnlyChildElement(GET_CONFIG)
652 .getOnlyChildElement(FILTER_NODE);
653 return getInstanceIdentifierFromFilter(filterElement);
657 private void deleteDatastore() throws Exception {
658 verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-root.xml"), RPC_REPLY_OK);
659 assertEmptyDatastore(getConfigCandidate());
661 verifyResponse(commit(), RPC_REPLY_OK);
662 assertEmptyDatastore(getConfigRunning());
665 private static void verifyResponse(final Document response, final Document template) throws Exception {
666 final DetailedDiff dd = new DetailedDiff(new Diff(response, template));
667 dd.overrideElementQualifier(new NetconfXmlUnitRecursiveQualifier());
669 printDocument(response);
670 printDocument(template);
672 assertTrue(dd.toString(), dd.similar());
675 private static void assertEmptyDatastore(final Document response) {
676 final NodeList nodes = response.getChildNodes();
677 assertTrue(nodes.getLength() == 1);
679 assertEquals(nodes.item(0).getLocalName(), RPC_REPLY_ELEMENT);
681 final NodeList replyNodes = nodes.item(0).getChildNodes();
682 assertTrue(replyNodes.getLength() == 1);
684 final Node dataNode = replyNodes.item(0);
685 assertEquals(dataNode.getLocalName(), DATA_ELEMENT);
686 assertFalse(dataNode.hasChildNodes());
689 private Document commit() throws Exception {
690 final Commit commit = new Commit(SESSION_ID_FOR_REPORTING, transactionProvider);
691 return executeOperation(commit, "messages/mapping/commit.xml");
694 private Document discardChanges() throws Exception {
695 final DiscardChanges discardOp = new DiscardChanges(SESSION_ID_FOR_REPORTING, transactionProvider);
696 return executeOperation(discardOp, "messages/mapping/discardChanges.xml");
699 private Document edit(final String resource) throws Exception {
700 final EditConfig editConfig = new EditConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext,
701 transactionProvider);
702 return executeOperation(editConfig, resource);
705 private Document get() throws Exception {
706 final Get get = new Get(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
707 return executeOperation(get, "messages/mapping/get.xml");
710 private Document getWithFilter(final String resource) throws Exception {
711 final Get get = new Get(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
712 return executeOperation(get, resource);
715 private Document getConfigRunning() throws Exception {
716 final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
717 return executeOperation(getConfig, "messages/mapping/getConfig.xml");
720 private Document getConfigCandidate() throws Exception {
721 final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
722 return executeOperation(getConfig, "messages/mapping/getConfig_candidate.xml");
725 private Document getConfigWithFilter(final String resource) throws Exception {
726 final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
727 return executeOperation(getConfig, resource);
730 private static Document lock() throws Exception {
731 final Lock lock = new Lock(SESSION_ID_FOR_REPORTING);
732 return executeOperation(lock, "messages/mapping/lock.xml");
735 private static Document unlock() throws Exception {
736 final Unlock unlock = new Unlock(SESSION_ID_FOR_REPORTING);
737 return executeOperation(unlock, "messages/mapping/unlock.xml");
740 private static Document lockWithoutTarget() throws Exception {
741 final Lock lock = new Lock(SESSION_ID_FOR_REPORTING);
742 return executeOperation(lock, "messages/mapping/lock_notarget.xml");
745 private static Document unlockWithoutTarget() throws Exception {
746 final Unlock unlock = new Unlock(SESSION_ID_FOR_REPORTING);
747 return executeOperation(unlock, "messages/mapping/unlock_notarget.xml");
750 private static Document lockCandidate() throws Exception {
751 final Lock lock = new Lock(SESSION_ID_FOR_REPORTING);
752 return executeOperation(lock, "messages/mapping/lock_candidate.xml");
755 private static Document unlockCandidate() throws Exception {
756 final Unlock unlock = new Unlock(SESSION_ID_FOR_REPORTING);
757 return executeOperation(unlock, "messages/mapping/unlock_candidate.xml");
760 private static Document executeOperation(final NetconfOperation op, final String filename) throws Exception {
761 final Document request = XmlFileLoader.xmlFileToDocument(filename);
762 final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
764 LOG.debug("Got response {}", response);
768 private List<InputStream> getYangSchemas() {
769 final List<String> schemaPaths = Arrays.asList("/META-INF/yang/config.yang",
770 "/yang/mdsal-netconf-mapping-test.yang");
771 final List<InputStream> schemas = new ArrayList<>();
773 for (final String schemaPath : schemaPaths) {
774 final InputStream resourceAsStream = getClass().getResourceAsStream(schemaPath);
775 schemas.add(resourceAsStream);
781 private SchemaService createSchemaService() {
782 return new SchemaService() {
785 public void addModule(final Module module) {
789 public void removeModule(final Module module) {
794 public SchemaContext getSessionContext() {
795 return schemaContext;
799 public SchemaContext getGlobalContext() {
800 return schemaContext;
804 public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(
805 final SchemaContextListener listener) {
806 listener.onGlobalContextUpdated(getGlobalContext());
807 return new ListenerRegistration<SchemaContextListener>() {
809 public void close() {
814 public SchemaContextListener getInstance() {