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 testLock() throws Exception {
333 verifyResponse(lockCandidate(), RPC_REPLY_OK);
337 fail("Should have failed - locking of running datastore is not supported");
338 } catch (final DocumentedException e) {
339 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
340 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
341 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
347 fail("Should have failed, target is missing");
348 } catch (final DocumentedException e) {
349 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
350 assertTrue(e.getErrorTag() == ErrorTag.INVALID_VALUE);
351 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
356 public void testUnlock() throws Exception {
358 verifyResponse(unlockCandidate(), RPC_REPLY_OK);
362 fail("Should have failed - unlocking of running datastore is not supported");
363 } catch (final DocumentedException e) {
364 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
365 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
366 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
370 unlockWithoutTarget();
371 fail("Should have failed, target is missing");
372 } catch (final DocumentedException e) {
373 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
374 assertTrue(e.getErrorTag() == ErrorTag.INVALID_VALUE);
375 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
380 public void testEditWithCreate() throws Exception {
382 verifyResponse(edit("messages/mapping/editConfigs/editConfig_create.xml"), RPC_REPLY_OK);
383 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_create_n1_control.xml"));
387 edit("messages/mapping/editConfigs/editConfig_create.xml");
388 fail("Create should have failed - data already exists");
389 } catch (final DocumentedException e) {
390 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
391 assertTrue(e.getErrorTag() == ErrorTag.DATA_EXISTS);
392 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
395 verifyResponse(discardChanges(), RPC_REPLY_OK);
400 public void testDeleteNonExisting() throws Exception {
402 assertEmptyDatastore(getConfigCandidate());
403 assertEmptyDatastore(getConfigRunning());
406 edit("messages/mapping/editConfigs/editConfig_delete-top.xml");
407 fail("Delete should have failed - data is missing");
408 } catch (final DocumentedException e) {
409 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
410 assertTrue(e.getErrorTag() == ErrorTag.DATA_MISSING);
411 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
417 public void testEditMissingDefaultOperation() throws Exception {
419 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_1.xml"), RPC_REPLY_OK);
420 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_2.xml"), RPC_REPLY_OK);
421 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
423 verifyResponse(commit(), RPC_REPLY_OK);
424 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
429 public static void printDocument(final Document doc) throws IOException, TransformerException {
430 final TransformerFactory tf = TransformerFactory.newInstance();
431 final Transformer transformer = tf.newTransformer();
432 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
433 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
434 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
435 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
436 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
438 final StringWriter writer = new StringWriter();
439 transformer.transform(new DOMSource(doc),
440 new StreamResult(writer));
441 LOG.warn(writer.getBuffer().toString());
445 public void testEditConfigWithMultipleOperations() throws Exception {
448 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_setup.xml"), RPC_REPLY_OK);
449 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_1.xml"), RPC_REPLY_OK);
451 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_2.xml"), RPC_REPLY_OK);
452 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_2_control.xml"));
454 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_leaf_operations.xml"), RPC_REPLY_OK);
455 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_control.xml"));
459 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_setup.xml"), RPC_REPLY_OK);
460 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_default-replace.xml"), RPC_REPLY_OK);
463 edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_create_existing.xml");
465 } catch (final DocumentedException e) {
466 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
467 assertTrue(e.getErrorTag() == ErrorTag.DATA_EXISTS);
468 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
471 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete_children_operations.xml"), RPC_REPLY_OK);
472 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
473 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_remove-non-existing.xml"), RPC_REPLY_OK);
476 edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete-non-existing.xml");
478 } catch (final DocumentedException e) {
479 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
480 assertTrue(e.getErrorTag() == ErrorTag.DATA_MISSING);
481 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
484 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup.xml"), RPC_REPLY_OK);
485 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup-control.xml"));
487 // Test files have been modified. RFC6020 requires that at most once case inside a choice is present at any time
488 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2.xml"), RPC_REPLY_OK);
489 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2-control.xml"));
491 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_delete.xml"), RPC_REPLY_OK);
492 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
498 public void testReplaceMapEntry() throws Exception {
499 verifyResponse(edit("messages/mapping/editConfigs/edit-config-replace-map-entry.xml"), RPC_REPLY_OK);
500 verifyResponse(commit(), RPC_REPLY_OK);
501 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-config-map-entry.xml"));
505 public void testMergeMapEntry() throws Exception {
506 verifyResponse(edit("messages/mapping/editConfigs/edit-config-merge-map-entry.xml"), RPC_REPLY_OK);
507 verifyResponse(commit(), RPC_REPLY_OK);
508 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-config-map-entry.xml"));
512 public void testFiltering() throws Exception {
514 assertEmptyDatastore(getConfigCandidate());
515 assertEmptyDatastore(getConfigRunning());
517 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-config-empty-filter.xml"),
518 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
519 verifyResponse(getWithFilter("messages/mapping/filters/get-empty-filter.xml"),
520 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
522 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
523 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
524 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-users.xml"),
525 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
527 verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup.xml"), RPC_REPLY_OK);
528 verifyResponse(commit(), RPC_REPLY_OK);
530 verifyFilterIdentifier("messages/mapping/filters/get-filter-alluser.xml",
531 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
532 verifyFilterIdentifier("messages/mapping/filters/get-filter-company-info.xml",
533 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
534 verifyFilterIdentifier("messages/mapping/filters/get-filter-modules-and-admin.xml",
535 YangInstanceIdentifier.builder().node(TOP).build());
536 verifyFilterIdentifier("messages/mapping/filters/get-filter-only-names-types.xml",
537 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
538 verifyFilterIdentifier("messages/mapping/filters/get-filter-specific-module-type-and-user.xml",
539 YangInstanceIdentifier.builder().node(TOP).build());
540 verifyFilterIdentifier("messages/mapping/filters/get-filter-superuser.xml",
541 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
542 verifyFilterIdentifier("messages/mapping/filters/get-filter-users.xml",
543 YangInstanceIdentifier.builder().node(TOP).node(USERS).build());
545 final YangInstanceIdentifier ident = YangInstanceIdentifier.
546 builder(AUGMENTED_CONTAINER_IN_MODULES).
547 node(AUGMENTED_CONTAINER).
548 node(AUGMENTED_STRING_IN_CONT).build();
550 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-string.xml", ident);
551 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case.xml",
552 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(AUGMENTED_CASE).build());
554 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case.xml"),
555 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case.xml"));
558 * RFC6020 requires that at most once case inside a choice is present at any time.
560 * <augmented-case>augmented case</augmented-case>
562 * messages/mapping/editConfigs/editConfig-filtering-setup.xml
563 * cannot exists together with
564 * <text>augmented nested choice text1</text>
566 * messages/mapping/editConfigs/editConfig-filtering-setup2.xml
568 //verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup2.xml"), RPC_REPLY_OK);
569 //verifyResponse(commit(), RPC_REPLY_OK);
571 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml",
572 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).build());
573 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-case.xml",
574 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).node(INNER_CHOICE).node(INNER_CHOICE_TEXT).build());
576 // verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-string.xml"),
577 // XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-string.xml"));
578 // verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml"),
579 // XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
580 // verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-case.xml"),
581 // XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
583 verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-top.xml"), RPC_REPLY_OK);
584 verifyResponse(commit(), RPC_REPLY_OK);
588 private void verifyFilterIdentifier(final String resource, final YangInstanceIdentifier identifier) throws Exception {
589 final TestingGetConfig getConfig = new TestingGetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
590 final Document request = XmlFileLoader.xmlFileToDocument(resource);
591 final YangInstanceIdentifier iid = getConfig.getInstanceIdentifierFromDocument(request);
592 assertEquals(identifier, iid);
595 private class TestingGetConfig extends GetConfig{
596 public TestingGetConfig(final String sessionId, final CurrentSchemaContext schemaContext, final TransactionProvider transactionProvider) {
597 super(sessionId, schemaContext, transactionProvider);
600 public YangInstanceIdentifier getInstanceIdentifierFromDocument(final Document request) throws DocumentedException {
601 final XmlElement filterElement = XmlElement.fromDomDocument(request).getOnlyChildElement(GET_CONFIG).getOnlyChildElement(FILTER_NODE);
602 return getInstanceIdentifierFromFilter(filterElement);
606 private void deleteDatastore() throws Exception{
607 verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-root.xml"), RPC_REPLY_OK);
608 assertEmptyDatastore(getConfigCandidate());
610 verifyResponse(commit(), RPC_REPLY_OK);
611 assertEmptyDatastore(getConfigRunning());
614 private void verifyResponse(final Document response, final Document template) throws IOException, TransformerException {
615 final DetailedDiff dd = new DetailedDiff(new Diff(response, template));
616 dd.overrideElementQualifier(new NetconfXmlUnitRecursiveQualifier());
618 printDocument(response);
619 printDocument(template);
621 assertTrue(dd.toString(), dd.similar());
624 private void assertEmptyDatastore(final Document response) {
626 final NodeList nodes = response.getChildNodes();
627 assertTrue(nodes.getLength() == 1);
629 assertEquals(nodes.item(0).getLocalName(), RPC_REPLY_ELEMENT);
631 final NodeList replyNodes = nodes.item(0).getChildNodes();
632 assertTrue(replyNodes.getLength() == 1);
634 final Node dataNode = replyNodes.item(0);
635 assertEquals(dataNode.getLocalName(), DATA_ELEMENT);
636 assertFalse(dataNode.hasChildNodes());
640 private Document commit() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
641 final Commit commit = new Commit(sessionIdForReporting, transactionProvider);
642 return executeOperation(commit, "messages/mapping/commit.xml");
645 private Document discardChanges() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
646 final DiscardChanges discardOp = new DiscardChanges(sessionIdForReporting, transactionProvider);
647 return executeOperation(discardOp, "messages/mapping/discardChanges.xml");
650 private Document edit(final String resource) throws DocumentedException, ParserConfigurationException, SAXException, IOException {
651 final EditConfig editConfig = new EditConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
652 return executeOperation(editConfig, resource);
655 private Document get() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
656 final Get get = new Get(sessionIdForReporting, currentSchemaContext, transactionProvider);
657 return executeOperation(get, "messages/mapping/get.xml");
660 private Document getWithFilter(final String resource) throws DocumentedException, ParserConfigurationException, SAXException, IOException {
661 final Get get = new Get(sessionIdForReporting, currentSchemaContext, transactionProvider);
662 return executeOperation(get, resource);
665 private Document getConfigRunning() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
666 final GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
667 return executeOperation(getConfig, "messages/mapping/getConfig.xml");
670 private Document getConfigCandidate() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
671 final GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
672 return executeOperation(getConfig, "messages/mapping/getConfig_candidate.xml");
675 private Document getConfigWithFilter(final String resource) throws DocumentedException, ParserConfigurationException, SAXException, IOException {
676 final GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
677 return executeOperation(getConfig, resource);
680 private Document lock() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
681 final Lock lock = new Lock(sessionIdForReporting);
682 return executeOperation(lock, "messages/mapping/lock.xml");
685 private Document unlock() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
686 final Unlock unlock = new Unlock(sessionIdForReporting);
687 return executeOperation(unlock, "messages/mapping/unlock.xml");
690 private Document lockWithoutTarget() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
691 final Lock lock = new Lock(sessionIdForReporting);
692 return executeOperation(lock, "messages/mapping/lock_notarget.xml");
695 private Document unlockWithoutTarget() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
696 final Unlock unlock = new Unlock(sessionIdForReporting);
697 return executeOperation(unlock, "messages/mapping/unlock_notarget.xml");
700 private Document lockCandidate() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
701 final Lock lock = new Lock(sessionIdForReporting);
702 return executeOperation(lock, "messages/mapping/lock_candidate.xml");
705 private Document unlockCandidate() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
706 final Unlock unlock = new Unlock(sessionIdForReporting);
707 return executeOperation(unlock, "messages/mapping/unlock_candidate.xml");
710 private Document executeOperation(final NetconfOperation op, final String filename) throws ParserConfigurationException, SAXException, IOException, DocumentedException {
711 final Document request = XmlFileLoader.xmlFileToDocument(filename);
712 final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
714 LOG.debug("Got response {}" , response);
718 private List<InputStream> getYangSchemas() {
719 final List<String> schemaPaths = Arrays.asList("/META-INF/yang/config.yang", "/yang/mdsal-netconf-mapping-test.yang");
720 final List<InputStream> schemas = new ArrayList<>();
722 for (final String schemaPath : schemaPaths) {
723 final InputStream resourceAsStream = getClass().getResourceAsStream(schemaPath);
724 schemas.add(resourceAsStream);
730 private SchemaService createSchemaService() {
731 return new SchemaService() {
734 public void addModule(final Module module) {
738 public void removeModule(final Module module) {
743 public SchemaContext getSessionContext() {
744 return schemaContext;
748 public SchemaContext getGlobalContext() {
749 return schemaContext;
753 public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(final SchemaContextListener listener) {
754 listener.onGlobalContextUpdated(getGlobalContext());
755 return new ListenerRegistration<SchemaContextListener>() {
757 public void close() {
762 public SchemaContextListener getInstance() {