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.base.Preconditions;
19 import com.google.common.io.ByteSource;
20 import com.google.common.util.concurrent.Futures;
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.transform.OutputKeys;
29 import javax.xml.transform.Transformer;
30 import javax.xml.transform.TransformerFactory;
31 import javax.xml.transform.dom.DOMSource;
32 import javax.xml.transform.stream.StreamResult;
33 import org.custommonkey.xmlunit.DetailedDiff;
34 import org.custommonkey.xmlunit.Diff;
35 import org.custommonkey.xmlunit.XMLUnit;
36 import org.junit.Assert;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mock;
40 import org.mockito.Mockito;
41 import org.mockito.MockitoAnnotations;
42 import org.opendaylight.controller.cluster.databroker.ConcurrentDOMDataBroker;
43 import org.opendaylight.controller.config.util.xml.DocumentedException;
44 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorSeverity;
45 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorTag;
46 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorType;
47 import org.opendaylight.controller.config.util.xml.XmlElement;
48 import org.opendaylight.controller.config.util.xml.XmlUtil;
49 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
50 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
51 import org.opendaylight.controller.sal.core.api.model.SchemaService;
52 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
53 import org.opendaylight.netconf.mapping.api.NetconfOperation;
54 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
55 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
56 import org.opendaylight.netconf.mdsal.connector.TransactionProvider;
57 import org.opendaylight.netconf.mdsal.connector.ops.get.Get;
58 import org.opendaylight.netconf.mdsal.connector.ops.get.GetConfig;
59 import org.opendaylight.netconf.util.test.NetconfXmlUnitRecursiveQualifier;
60 import org.opendaylight.netconf.util.test.XmlFileLoader;
61 import org.opendaylight.yangtools.concepts.ListenerRegistration;
62 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
63 import org.opendaylight.yangtools.yang.common.QName;
64 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
65 import org.opendaylight.yangtools.yang.model.api.Module;
66 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
67 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
68 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
69 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
70 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
71 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
72 import org.slf4j.Logger;
73 import org.slf4j.LoggerFactory;
74 import org.w3c.dom.Document;
75 import org.w3c.dom.Element;
76 import org.w3c.dom.Node;
77 import org.w3c.dom.NodeList;
79 public class NetconfMDSalMappingTest {
80 private static final Logger LOG = LoggerFactory.getLogger(NetconfMDSalMappingTest.class);
82 private static final String TARGET_KEY = "target";
83 private static final String RPC_REPLY_ELEMENT = "rpc-reply";
84 private static final String DATA_ELEMENT = "data";
85 private static final String FILTER_NODE = "filter";
86 private static final String GET_CONFIG = "get-config";
87 private static final QName TOP = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "top");
88 private static final QName USERS = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "users");
89 private static final QName USER = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "user");
90 private static final QName MODULES = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "modules");
91 private static final QName AUGMENTED_CONTAINER = QName.create("urn:opendaylight:mdsal:mapping:test",
92 "2015-02-26", "augmented-container");
93 private static final QName AUGMENTED_STRING_IN_CONT = QName.create("urn:opendaylight:mdsal:mapping:test",
94 "2015-02-26", "identifier");
95 private static final QName CHOICE_NODE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
97 private static final QName AUGMENTED_CASE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
99 private static final QName CHOICE_WRAPPER = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
101 private static final QName INNER_CHOICE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
103 private static final QName INNER_CHOICE_TEXT = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
106 private static final YangInstanceIdentifier AUGMENTED_CONTAINER_IN_MODULES =
107 YangInstanceIdentifier.builder().node(TOP).node(MODULES).build();
108 private static final String SESSION_ID_FOR_REPORTING = "netconf-test-session1";
109 private static final Document RPC_REPLY_OK = NetconfMDSalMappingTest.getReplyOk();
111 private CurrentSchemaContext currentSchemaContext = null;
112 private SchemaContext schemaContext = null;
113 private TransactionProvider transactionProvider = null;
116 private SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
118 @SuppressWarnings("illegalCatch")
119 private static Document getReplyOk() {
122 doc = XmlFileLoader.xmlFileToDocument("messages/mapping/rpc-reply_ok.xml");
123 } catch (final Exception e) {
124 LOG.debug("unable to load rpc reply ok.", e);
125 doc = XmlUtil.newDocument();
131 public void setUp() throws Exception {
132 MockitoAnnotations.initMocks(this);
134 XMLUnit.setIgnoreWhitespace(true);
135 XMLUnit.setIgnoreAttributeOrder(true);
137 this.schemaContext = YangParserTestUtils.parseYangStreams(getYangSchemas());
138 schemaContext.getModules();
139 final SchemaService schemaService = createSchemaService();
141 final DOMStore operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", schemaService);
142 final DOMStore configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", schemaService);
144 final EnumMap<LogicalDatastoreType, DOMStore> datastores = new EnumMap<>(LogicalDatastoreType.class);
145 datastores.put(LogicalDatastoreType.CONFIGURATION, configStore);
146 datastores.put(LogicalDatastoreType.OPERATIONAL, operStore);
148 final ExecutorService listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
149 16, 16, "CommitFutures");
151 final ConcurrentDOMDataBroker cdb = new ConcurrentDOMDataBroker(datastores, listenableFutureExecutor);
152 this.transactionProvider = new TransactionProvider(cdb, SESSION_ID_FOR_REPORTING);
154 doAnswer(invocationOnMock -> {
155 final SourceIdentifier sId = (SourceIdentifier) invocationOnMock.getArguments()[0];
156 final YangTextSchemaSource yangTextSchemaSource =
157 YangTextSchemaSource.delegateForByteSource(sId, ByteSource.wrap("module test".getBytes()));
158 return Futures.immediateCheckedFuture(yangTextSchemaSource);
160 }).when(sourceProvider).getSource(any(SourceIdentifier.class));
162 this.currentSchemaContext = new CurrentSchemaContext(schemaService, sourceProvider);
166 public void testEmptyDatastore() throws Exception {
167 assertEmptyDatastore(get());
168 assertEmptyDatastore(getConfigCandidate());
169 assertEmptyDatastore(getConfigRunning());
173 public void testIncorrectGet() throws Exception {
175 executeOperation(new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider),
176 "messages/mapping/bad_getConfig.xml");
177 fail("Should have failed, this is an incorrect request");
178 } catch (final DocumentedException e) {
179 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
180 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_FAILED);
181 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
185 executeOperation(new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider),
186 "messages/mapping/bad_namespace_getConfig.xml");
187 fail("Should have failed, this is an incorrect request");
188 } catch (final DocumentedException e) {
189 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
190 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_FAILED);
191 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
196 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);
208 public void testCommitWithoutOpenTransaction() throws Exception {
209 verifyResponse(commit(), RPC_REPLY_OK);
210 assertEmptyDatastore(getConfigCandidate());
214 public void testCandidateTransaction() throws Exception {
215 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_n1.xml"), RPC_REPLY_OK);
216 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
217 "messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
218 assertEmptyDatastore(getConfigRunning());
220 verifyResponse(discardChanges(), RPC_REPLY_OK);
221 assertEmptyDatastore(getConfigCandidate());
225 public void testEditWithCommit() throws Exception {
226 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_n1.xml"), RPC_REPLY_OK);
227 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
228 "messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
230 verifyResponse(commit(), RPC_REPLY_OK);
231 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
232 "messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
238 public void testKeyOrder() throws Exception {
239 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_keys_1.xml"), RPC_REPLY_OK);
240 verifyResponse(commit(), RPC_REPLY_OK);
241 final Document configRunning = getConfigRunning();
242 final String responseAsString = XmlUtil.toString(configRunning);
243 verifyResponse(configRunning, XmlFileLoader.xmlFileToDocument(
244 "messages/mapping/editConfigs/editConfig_merge_multiple_keys_1_control.xml"));
246 final int key3 = responseAsString.indexOf("key3");
247 final int key1 = responseAsString.indexOf("key1");
248 final int key2 = responseAsString.indexOf("key2");
250 assertTrue(String.format("Key ordering invalid, should be key3(%d) < key1(%d) < key2(%d)", key3, key1, key2),
251 key3 < key1 && key1 < key2);
258 public void testMultipleEditsWithMerge() throws Exception {
259 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
260 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
261 "messages/mapping/editConfigs/editConfig_merge_multiple_control_1.xml"));
262 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
263 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
264 "messages/mapping/editConfigs/editConfig_merge_multiple_control_2.xml"));
265 assertEmptyDatastore(getConfigRunning());
267 verifyResponse(commit(), RPC_REPLY_OK);
268 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
269 "messages/mapping/editConfigs/editConfig_merge_multiple_control_2.xml"));
275 public void testMoreComplexEditConfigs() throws Exception {
276 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
277 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
279 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_2.xml"), RPC_REPLY_OK);
280 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
281 "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(
285 "messages/mapping/editConfigs/editConfig_merge_multiple_after_more_complex_merge_2.xml"));
287 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_4_replace.xml"), RPC_REPLY_OK);
288 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
289 "messages/mapping/editConfigs/editConfig_merge_multiple_after_replace.xml"));
290 verifyResponse(commit(), RPC_REPLY_OK);
292 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
293 "messages/mapping/editConfigs/editConfig_merge_multiple_after_replace.xml"));
295 verifyResponse(edit("messages/mapping/editConfigs/editConfig_replace_default.xml"), RPC_REPLY_OK);
296 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
297 "messages/mapping/editConfigs/editConfig_replace_default_control.xml"));
298 verifyResponse(commit(), RPC_REPLY_OK);
300 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
301 "messages/mapping/editConfigs/editConfig_replace_default_control.xml"));
307 public void testOrderedListEdits() throws Exception {
309 verifyResponse(edit("messages/mapping/editConfigs/editConfig_ordered_list_create.xml"), RPC_REPLY_OK);
310 verifyResponse(commit(), RPC_REPLY_OK);
312 verifyResponse(edit("messages/mapping/editConfigs/editConfig_ordered_list_replace.xml"), RPC_REPLY_OK);
313 verifyResponse(commit(), RPC_REPLY_OK);
320 public void testAugmentedOrderedListEdits() throws Exception {
322 verifyResponse(edit("messages/mapping/editConfigs/editConfig_augmented_ordered_list_create.xml"),
324 verifyResponse(commit(), RPC_REPLY_OK);
326 verifyResponse(edit("messages/mapping/editConfigs/editConfig_augmented_ordered_list_replace.xml"),
328 verifyResponse(commit(), RPC_REPLY_OK);
335 public void testAugmentedContainerReplace() throws Exception {
336 verifyResponse(edit("messages/mapping/editConfigs/editConfig_empty_modules_create.xml"),
338 verifyResponse(commit(), RPC_REPLY_OK);
340 verifyResponse(edit("messages/mapping/editConfigs/editConfig_augmented_container_replace.xml"),
342 verifyResponse(commit(), RPC_REPLY_OK);
348 public void testLeafFromAugmentReplace() throws Exception {
349 verifyResponse(edit("messages/mapping/editConfigs/editConfig_empty_modules_create.xml"),
351 verifyResponse(commit(), RPC_REPLY_OK);
353 verifyResponse(edit("messages/mapping/editConfigs/editConfig_leaf_from_augment_replace.xml"),
355 verifyResponse(commit(), RPC_REPLY_OK);
361 public void testLock() throws Exception {
362 verifyResponse(lockCandidate(), RPC_REPLY_OK);
366 fail("Should have failed - locking of running datastore is not supported");
367 } catch (final DocumentedException e) {
368 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
369 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
370 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
375 fail("Should have failed, target is missing");
376 } catch (final DocumentedException e) {
377 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
378 assertTrue(e.getErrorTag() == ErrorTag.INVALID_VALUE);
379 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
384 public void testUnlock() throws Exception {
385 verifyResponse(unlockCandidate(), RPC_REPLY_OK);
389 fail("Should have failed - unlocking of running datastore is not supported");
390 } catch (final DocumentedException e) {
391 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
392 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
393 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
397 unlockWithoutTarget();
398 fail("Should have failed, target is missing");
399 } catch (final DocumentedException e) {
400 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
401 assertTrue(e.getErrorTag() == ErrorTag.INVALID_VALUE);
402 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
407 public void testEditWithCreate() throws Exception {
408 verifyResponse(edit("messages/mapping/editConfigs/editConfig_create.xml"), RPC_REPLY_OK);
409 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
410 "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);
425 public void testDeleteNonExisting() throws Exception {
426 assertEmptyDatastore(getConfigCandidate());
427 assertEmptyDatastore(getConfigRunning());
430 edit("messages/mapping/editConfigs/editConfig_delete-top.xml");
431 fail("Delete should have failed - data is missing");
432 } catch (final DocumentedException e) {
433 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
434 assertTrue(e.getErrorTag() == ErrorTag.DATA_MISSING);
435 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
440 public void testEditMissingDefaultOperation() throws Exception {
441 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_1.xml"),
443 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_2.xml"),
445 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
446 "messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
448 verifyResponse(commit(), RPC_REPLY_OK);
449 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
450 "messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
455 private static void printDocument(final Document doc) throws Exception {
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"),
476 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_1.xml"),
479 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_2.xml"),
481 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
482 "messages/mapping/editConfigs/editConfig_merge_multiple_operations_2_control.xml"));
484 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_leaf_operations.xml"),
486 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
487 "messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_control.xml"));
491 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_setup.xml"),
493 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_default-replace.xml"),
497 edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_create_existing.xml");
499 } catch (final DocumentedException e) {
500 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
501 assertTrue(e.getErrorTag() == ErrorTag.DATA_EXISTS);
502 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
506 "messages/mapping/editConfigs/"
507 + "editConfig_merge_multiple_operations_4_delete_children_operations.xml"),
509 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
510 "messages/mapping/editConfigs/"
511 + "editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
513 "messages/mapping/editConfigs/"
514 + "editConfig_merge_multiple_operations_4_remove-non-existing.xml"),
517 edit("messages/mapping/editConfigs/"
518 + "editConfig_merge_multiple_operations_4_delete-non-existing.xml");
520 } catch (final DocumentedException e) {
521 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
522 assertTrue(e.getErrorTag() == ErrorTag.DATA_MISSING);
523 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
526 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup.xml"),
528 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
529 "messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup-control.xml"));
531 // Test files have been modified. RFC6020 requires that at most once case inside a choice is present at any time
532 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2.xml"),
534 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
535 "messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2-control.xml"));
537 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_delete.xml"),
539 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
540 "messages/mapping/editConfigs"
541 + "/editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
547 public void testEditConfigGetElementByTagName() throws Exception {
548 EditConfig editConfig = new EditConfig("test_edit-config", Mockito.mock(CurrentSchemaContext.class),
549 Mockito.mock(TransactionProvider.class));
551 String stringWithoutPrefix =
552 "<rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n"
557 + " </edit-config>\n"
559 XmlElement xe = getXmlElement(stringWithoutPrefix);
560 NodeList nodeList = editConfig.getElementsByTagName(xe, TARGET_KEY);
561 Assert.assertEquals(1, nodeList.getLength());
563 String stringWithPrefix =
564 "<nc:rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n"
565 + " <nc:edit-config>\n"
567 + " <nc:candidate/>\n"
569 + " </nc:edit-config>\n"
572 xe = getXmlElement(stringWithPrefix);
573 nodeList = editConfig.getElementsByTagName(xe, TARGET_KEY);
574 Assert.assertEquals(1, nodeList.getLength());
576 String stringWithoutTarget =
577 "<nc:rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n"
578 + " <nc:edit-config>\n"
581 + " </nc:edit-config>\n"
583 xe = getXmlElement(stringWithoutTarget);
586 nodeList = editConfig.getElementsByTagName(xe, TARGET_KEY);
587 XmlElement.fromDomElement((Element) nodeList.item(0)).getOnlyChildElement();
588 Assert.fail("Not specified target, we should fail");
589 } catch (DocumentedException documentedException) {
595 private XmlElement getXmlElement(final String elementAsString) throws Exception {
596 Document document = XmlUtil.readXmlToDocument(elementAsString);
597 Element element = document.getDocumentElement();
598 return XmlElement.fromDomElement(element);
602 public void testReplaceMapEntry() throws Exception {
603 verifyResponse(edit("messages/mapping/editConfigs/edit-config-replace-map-entry.xml"), RPC_REPLY_OK);
604 verifyResponse(commit(), RPC_REPLY_OK);
605 verifyResponse(getConfigRunning(),
606 XmlFileLoader.xmlFileToDocument("messages/mapping/get-config-map-entry.xml"));
610 public void testMergeMapEntry() throws Exception {
611 verifyResponse(edit("messages/mapping/editConfigs/edit-config-merge-map-entry.xml"), RPC_REPLY_OK);
612 verifyResponse(commit(), RPC_REPLY_OK);
613 verifyResponse(getConfigRunning(),
614 XmlFileLoader.xmlFileToDocument("messages/mapping/get-config-map-entry.xml"));
618 public void testFiltering() throws Exception {
619 assertEmptyDatastore(getConfigCandidate());
620 assertEmptyDatastore(getConfigRunning());
622 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-config-empty-filter.xml"),
623 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
624 verifyResponse(getWithFilter("messages/mapping/filters/get-empty-filter.xml"),
625 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
627 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response"
629 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
630 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-users.xml"),
631 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
633 verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup.xml"), RPC_REPLY_OK);
634 verifyResponse(commit(), RPC_REPLY_OK);
636 verifyFilterIdentifier("messages/mapping/filters/get-filter-alluser.xml",
637 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
638 verifyFilterIdentifier("messages/mapping/filters/get-filter-company-info.xml",
639 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
640 verifyFilterIdentifier("messages/mapping/filters/get-filter-modules-and-admin.xml",
641 YangInstanceIdentifier.builder().node(TOP).build());
642 verifyFilterIdentifier("messages/mapping/filters/get-filter-only-names-types.xml",
643 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
644 verifyFilterIdentifier("messages/mapping/filters/get-filter-specific-module-type-and-user.xml",
645 YangInstanceIdentifier.builder().node(TOP).build());
646 verifyFilterIdentifier("messages/mapping/filters/get-filter-superuser.xml",
647 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
648 verifyFilterIdentifier("messages/mapping/filters/get-filter-users.xml",
649 YangInstanceIdentifier.builder().node(TOP).node(USERS).build());
651 final YangInstanceIdentifier ident = YangInstanceIdentifier
652 .builder(AUGMENTED_CONTAINER_IN_MODULES)
653 .node(AUGMENTED_CONTAINER)
654 .node(AUGMENTED_STRING_IN_CONT).build();
656 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-string.xml", ident);
657 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case.xml",
658 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(AUGMENTED_CASE).build());
660 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case.xml"),
661 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case.xml"));
664 * RFC6020 requires that at most once case inside a choice is present at any time.
666 * <augmented-case>augmented case</augmented-case>
668 * messages/mapping/editConfigs/editConfig-filtering-setup.xml
669 * cannot exists together with
670 * <text>augmented nested choice text1</text>
672 * messages/mapping/editConfigs/editConfig-filtering-setup2.xml
674 //verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup2.xml"), RPC_REPLY_OK);
675 //verifyResponse(commit(), RPC_REPLY_OK);
677 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml",
678 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).build());
679 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-case.xml",
680 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).node(INNER_CHOICE)
681 .node(INNER_CHOICE_TEXT).build());
683 // verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-string.xml"),
684 // XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-string.xml"));
685 // verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml"),
686 // XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
687 // verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-case.xml"),
688 // XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
690 verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-top.xml"), RPC_REPLY_OK);
691 verifyResponse(commit(), RPC_REPLY_OK);
695 private void verifyFilterIdentifier(final String resource, final YangInstanceIdentifier identifier)
697 final TestingGetConfig getConfig = new TestingGetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext,
698 transactionProvider);
699 final Document request = XmlFileLoader.xmlFileToDocument(resource);
700 final YangInstanceIdentifier iid = getConfig.getInstanceIdentifierFromDocument(request);
701 assertEquals(identifier, iid);
704 private class TestingGetConfig extends GetConfig {
705 TestingGetConfig(final String sessionId, final CurrentSchemaContext schemaContext,
706 final TransactionProvider transactionProvider) {
707 super(sessionId, schemaContext, transactionProvider);
710 YangInstanceIdentifier getInstanceIdentifierFromDocument(final Document request) throws DocumentedException {
711 final XmlElement filterElement = XmlElement.fromDomDocument(request).getOnlyChildElement(GET_CONFIG)
712 .getOnlyChildElement(FILTER_NODE);
713 return getInstanceIdentifierFromFilter(filterElement);
717 private void deleteDatastore() throws Exception {
718 verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-root.xml"), RPC_REPLY_OK);
719 assertEmptyDatastore(getConfigCandidate());
721 verifyResponse(commit(), RPC_REPLY_OK);
722 assertEmptyDatastore(getConfigRunning());
725 private static void verifyResponse(final Document response, final Document template) throws Exception {
726 final DetailedDiff dd = new DetailedDiff(new Diff(response, template));
727 dd.overrideElementQualifier(new NetconfXmlUnitRecursiveQualifier());
729 printDocument(response);
730 printDocument(template);
732 assertTrue(dd.toString(), dd.similar());
735 private static void assertEmptyDatastore(final Document response) {
736 final NodeList nodes = response.getChildNodes();
737 assertTrue(nodes.getLength() == 1);
739 assertEquals(nodes.item(0).getLocalName(), RPC_REPLY_ELEMENT);
741 final NodeList replyNodes = nodes.item(0).getChildNodes();
742 assertTrue(replyNodes.getLength() == 1);
744 final Node dataNode = replyNodes.item(0);
745 assertEquals(dataNode.getLocalName(), DATA_ELEMENT);
746 assertFalse(dataNode.hasChildNodes());
749 private Document commit() throws Exception {
750 final Commit commit = new Commit(SESSION_ID_FOR_REPORTING, transactionProvider);
751 return executeOperation(commit, "messages/mapping/commit.xml");
754 private Document discardChanges() throws Exception {
755 final DiscardChanges discardOp = new DiscardChanges(SESSION_ID_FOR_REPORTING, transactionProvider);
756 return executeOperation(discardOp, "messages/mapping/discardChanges.xml");
759 private Document edit(final String resource) throws Exception {
760 final EditConfig editConfig = new EditConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext,
761 transactionProvider);
762 return executeOperation(editConfig, resource);
765 private Document get() throws Exception {
766 final Get get = new Get(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
767 return executeOperation(get, "messages/mapping/get.xml");
770 private Document getWithFilter(final String resource) throws Exception {
771 final Get get = new Get(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
772 return executeOperation(get, resource);
775 private Document getConfigRunning() throws Exception {
776 final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
777 return executeOperation(getConfig, "messages/mapping/getConfig.xml");
780 private Document getConfigCandidate() throws Exception {
781 final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
782 return executeOperation(getConfig, "messages/mapping/getConfig_candidate.xml");
785 private Document getConfigWithFilter(final String resource) throws Exception {
786 final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
787 return executeOperation(getConfig, resource);
790 private static Document lock() throws Exception {
791 final Lock lock = new Lock(SESSION_ID_FOR_REPORTING);
792 return executeOperation(lock, "messages/mapping/lock.xml");
795 private static Document unlock() throws Exception {
796 final Unlock unlock = new Unlock(SESSION_ID_FOR_REPORTING);
797 return executeOperation(unlock, "messages/mapping/unlock.xml");
800 private static Document lockWithoutTarget() throws Exception {
801 final Lock lock = new Lock(SESSION_ID_FOR_REPORTING);
802 return executeOperation(lock, "messages/mapping/lock_notarget.xml");
805 private static Document unlockWithoutTarget() throws Exception {
806 final Unlock unlock = new Unlock(SESSION_ID_FOR_REPORTING);
807 return executeOperation(unlock, "messages/mapping/unlock_notarget.xml");
810 private static Document lockCandidate() throws Exception {
811 final Lock lock = new Lock(SESSION_ID_FOR_REPORTING);
812 return executeOperation(lock, "messages/mapping/lock_candidate.xml");
815 private static Document unlockCandidate() throws Exception {
816 final Unlock unlock = new Unlock(SESSION_ID_FOR_REPORTING);
817 return executeOperation(unlock, "messages/mapping/unlock_candidate.xml");
820 private static Document executeOperation(final NetconfOperation op, final String filename) throws Exception {
821 final Document request = XmlFileLoader.xmlFileToDocument(filename);
822 final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
824 LOG.debug("Got response {}", response);
828 private List<InputStream> getYangSchemas() {
829 final List<String> schemaPaths = Arrays.asList("/META-INF/yang/config@2013-04-05.yang",
830 "/yang/mdsal-netconf-mapping-test.yang");
831 final List<InputStream> schemas = new ArrayList<>();
833 for (final String schemaPath : schemaPaths) {
834 final InputStream resourceAsStream =
835 Preconditions.checkNotNull(
836 getClass().getResourceAsStream(schemaPath), "Resource " + schemaPath + " not found.");
837 schemas.add(resourceAsStream);
843 private SchemaService createSchemaService() {
844 return new SchemaService() {
847 public void addModule(final Module module) {
851 public void removeModule(final Module module) {
856 public SchemaContext getSessionContext() {
857 return schemaContext;
861 public SchemaContext getGlobalContext() {
862 return schemaContext;
866 public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(
867 final SchemaContextListener listener) {
868 listener.onGlobalContextUpdated(getGlobalContext());
869 return new ListenerRegistration<SchemaContextListener>() {
871 public void close() {
876 public SchemaContextListener getInstance() {