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.Assert;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mock;
39 import org.mockito.Mockito;
40 import org.mockito.MockitoAnnotations;
41 import org.opendaylight.controller.cluster.databroker.ConcurrentDOMDataBroker;
42 import org.opendaylight.controller.config.util.xml.DocumentedException;
43 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorSeverity;
44 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorTag;
45 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorType;
46 import org.opendaylight.controller.config.util.xml.XmlElement;
47 import org.opendaylight.controller.config.util.xml.XmlUtil;
48 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
49 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
50 import org.opendaylight.controller.sal.core.api.model.SchemaService;
51 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
52 import org.opendaylight.netconf.mapping.api.NetconfOperation;
53 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
54 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
55 import org.opendaylight.netconf.mdsal.connector.TransactionProvider;
56 import org.opendaylight.netconf.mdsal.connector.ops.get.Get;
57 import org.opendaylight.netconf.mdsal.connector.ops.get.GetConfig;
58 import org.opendaylight.netconf.util.test.NetconfXmlUnitRecursiveQualifier;
59 import org.opendaylight.netconf.util.test.XmlFileLoader;
60 import org.opendaylight.yangtools.concepts.ListenerRegistration;
61 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
62 import org.opendaylight.yangtools.yang.common.QName;
63 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
64 import org.opendaylight.yangtools.yang.model.api.Module;
65 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
66 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
67 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
68 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
69 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
70 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
73 import org.w3c.dom.Document;
74 import org.w3c.dom.Element;
75 import org.w3c.dom.Node;
76 import org.w3c.dom.NodeList;
78 public class NetconfMDSalMappingTest {
79 private static final Logger LOG = LoggerFactory.getLogger(NetconfMDSalMappingTest.class);
81 private static final String TARGET_KEY = "target";
82 private static final String RPC_REPLY_ELEMENT = "rpc-reply";
83 private static final String DATA_ELEMENT = "data";
84 private static final String FILTER_NODE = "filter";
85 private static final String GET_CONFIG = "get-config";
86 private static final QName TOP = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "top");
87 private static final QName USERS = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "users");
88 private static final QName USER = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "user");
89 private static final QName MODULES = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "modules");
90 private static final QName AUGMENTED_CONTAINER = QName.create("urn:opendaylight:mdsal:mapping:test",
91 "2015-02-26", "augmented-container");
92 private static final QName AUGMENTED_STRING_IN_CONT = QName.create("urn:opendaylight:mdsal:mapping:test",
93 "2015-02-26", "identifier");
94 private static final QName CHOICE_NODE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
96 private static final QName AUGMENTED_CASE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
98 private static final QName CHOICE_WRAPPER = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
100 private static final QName INNER_CHOICE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
102 private static final QName INNER_CHOICE_TEXT = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
105 private static final YangInstanceIdentifier AUGMENTED_CONTAINER_IN_MODULES =
106 YangInstanceIdentifier.builder().node(TOP).node(MODULES).build();
107 private static final String SESSION_ID_FOR_REPORTING = "netconf-test-session1";
108 private static final Document RPC_REPLY_OK = NetconfMDSalMappingTest.getReplyOk();
110 private CurrentSchemaContext currentSchemaContext = null;
111 private SchemaContext schemaContext = null;
112 private TransactionProvider transactionProvider = null;
115 private SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
117 @SuppressWarnings("illegalCatch")
118 private static Document getReplyOk() {
121 doc = XmlFileLoader.xmlFileToDocument("messages/mapping/rpc-reply_ok.xml");
122 } catch (final Exception e) {
123 LOG.debug("unable to load rpc reply ok.", e);
124 doc = XmlUtil.newDocument();
130 public void setUp() throws Exception {
131 MockitoAnnotations.initMocks(this);
133 XMLUnit.setIgnoreWhitespace(true);
134 XMLUnit.setIgnoreAttributeOrder(true);
136 this.schemaContext = YangParserTestUtils.parseYangStreams(getYangSchemas());
137 schemaContext.getModules();
138 final SchemaService schemaService = createSchemaService();
140 final DOMStore operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", schemaService);
141 final DOMStore configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", schemaService);
143 final EnumMap<LogicalDatastoreType, DOMStore> datastores = new EnumMap<>(LogicalDatastoreType.class);
144 datastores.put(LogicalDatastoreType.CONFIGURATION, configStore);
145 datastores.put(LogicalDatastoreType.OPERATIONAL, operStore);
147 final ExecutorService listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
148 16, 16, "CommitFutures");
150 final ConcurrentDOMDataBroker cdb = new ConcurrentDOMDataBroker(datastores, listenableFutureExecutor);
151 this.transactionProvider = new TransactionProvider(cdb, SESSION_ID_FOR_REPORTING);
153 doAnswer(invocationOnMock -> {
154 final SourceIdentifier sId = (SourceIdentifier) invocationOnMock.getArguments()[0];
155 final YangTextSchemaSource yangTextSchemaSource =
156 YangTextSchemaSource.delegateForByteSource(sId, ByteSource.wrap("module test".getBytes()));
157 return Futures.immediateCheckedFuture(yangTextSchemaSource);
159 }).when(sourceProvider).getSource(any(SourceIdentifier.class));
161 this.currentSchemaContext = new CurrentSchemaContext(schemaService, sourceProvider);
165 public void testEmptyDatastore() throws Exception {
166 assertEmptyDatastore(get());
167 assertEmptyDatastore(getConfigCandidate());
168 assertEmptyDatastore(getConfigRunning());
172 public void testIncorrectGet() throws Exception {
174 executeOperation(new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider),
175 "messages/mapping/bad_getConfig.xml");
176 fail("Should have failed, this is an incorrect request");
177 } catch (final DocumentedException e) {
178 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
179 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_FAILED);
180 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
184 executeOperation(new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider),
185 "messages/mapping/bad_namespace_getConfig.xml");
186 fail("Should have failed, this is an incorrect request");
187 } catch (final DocumentedException e) {
188 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
189 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_FAILED);
190 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
195 public void testEditRunning() throws Exception {
197 edit("messages/mapping/editConfigs/editConfig_running.xml");
198 fail("Should have failed - edit config on running datastore is not supported");
199 } catch (final DocumentedException e) {
200 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
201 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
202 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
207 public void testCommitWithoutOpenTransaction() throws Exception {
208 verifyResponse(commit(), RPC_REPLY_OK);
209 assertEmptyDatastore(getConfigCandidate());
213 public void testCandidateTransaction() throws Exception {
214 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_n1.xml"), RPC_REPLY_OK);
215 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
216 "messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
217 assertEmptyDatastore(getConfigRunning());
219 verifyResponse(discardChanges(), RPC_REPLY_OK);
220 assertEmptyDatastore(getConfigCandidate());
224 public void testEditWithCommit() throws Exception {
225 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_n1.xml"), RPC_REPLY_OK);
226 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
227 "messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
229 verifyResponse(commit(), RPC_REPLY_OK);
230 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
231 "messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
237 public void testKeyOrder() throws Exception {
238 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_keys_1.xml"), RPC_REPLY_OK);
239 verifyResponse(commit(), RPC_REPLY_OK);
240 final Document configRunning = getConfigRunning();
241 final String responseAsString = XmlUtil.toString(configRunning);
242 verifyResponse(configRunning, XmlFileLoader.xmlFileToDocument(
243 "messages/mapping/editConfigs/editConfig_merge_multiple_keys_1_control.xml"));
245 final int key3 = responseAsString.indexOf("key3");
246 final int key1 = responseAsString.indexOf("key1");
247 final int key2 = responseAsString.indexOf("key2");
249 assertTrue(String.format("Key ordering invalid, should be key3(%d) < key1(%d) < key2(%d)", key3, key1, key2),
250 key3 < key1 && key1 < key2);
257 public void testMultipleEditsWithMerge() throws Exception {
258 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
259 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
260 "messages/mapping/editConfigs/editConfig_merge_multiple_control_1.xml"));
261 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
262 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
263 "messages/mapping/editConfigs/editConfig_merge_multiple_control_2.xml"));
264 assertEmptyDatastore(getConfigRunning());
266 verifyResponse(commit(), RPC_REPLY_OK);
267 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
268 "messages/mapping/editConfigs/editConfig_merge_multiple_control_2.xml"));
274 public void testMoreComplexEditConfigs() throws Exception {
275 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
276 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
278 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_2.xml"), RPC_REPLY_OK);
279 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
280 "messages/mapping/editConfigs/editConfig_merge_multiple_after_more_complex_merge.xml"));
282 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_3.xml"), RPC_REPLY_OK);
283 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
284 "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(
288 "messages/mapping/editConfigs/editConfig_merge_multiple_after_replace.xml"));
289 verifyResponse(commit(), RPC_REPLY_OK);
291 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
292 "messages/mapping/editConfigs/editConfig_merge_multiple_after_replace.xml"));
294 verifyResponse(edit("messages/mapping/editConfigs/editConfig_replace_default.xml"), RPC_REPLY_OK);
295 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
296 "messages/mapping/editConfigs/editConfig_replace_default_control.xml"));
297 verifyResponse(commit(), RPC_REPLY_OK);
299 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
300 "messages/mapping/editConfigs/editConfig_replace_default_control.xml"));
306 public void testOrderedListEdits() throws Exception {
308 verifyResponse(edit("messages/mapping/editConfigs/editConfig_ordered_list_create.xml"), RPC_REPLY_OK);
309 verifyResponse(commit(), RPC_REPLY_OK);
311 verifyResponse(edit("messages/mapping/editConfigs/editConfig_ordered_list_replace.xml"), RPC_REPLY_OK);
312 verifyResponse(commit(), RPC_REPLY_OK);
319 public void testAugmentedOrderedListEdits() throws Exception {
321 verifyResponse(edit("messages/mapping/editConfigs/editConfig_augmented_ordered_list_create.xml"),
323 verifyResponse(commit(), RPC_REPLY_OK);
325 verifyResponse(edit("messages/mapping/editConfigs/editConfig_augmented_ordered_list_replace.xml"),
327 verifyResponse(commit(), RPC_REPLY_OK);
334 public void testAugmentedContainerReplace() throws Exception {
335 verifyResponse(edit("messages/mapping/editConfigs/editConfig_empty_modules_create.xml"),
337 verifyResponse(commit(), RPC_REPLY_OK);
339 verifyResponse(edit("messages/mapping/editConfigs/editConfig_augmented_container_replace.xml"),
341 verifyResponse(commit(), RPC_REPLY_OK);
347 public void testLeafFromAugmentReplace() throws Exception {
348 verifyResponse(edit("messages/mapping/editConfigs/editConfig_empty_modules_create.xml"),
350 verifyResponse(commit(), RPC_REPLY_OK);
352 verifyResponse(edit("messages/mapping/editConfigs/editConfig_leaf_from_augment_replace.xml"),
354 verifyResponse(commit(), RPC_REPLY_OK);
360 public void testLock() throws Exception {
361 verifyResponse(lockCandidate(), RPC_REPLY_OK);
365 fail("Should have failed - locking of running datastore is not supported");
366 } catch (final DocumentedException e) {
367 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
368 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
369 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
374 fail("Should have failed, target is missing");
375 } catch (final DocumentedException e) {
376 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
377 assertTrue(e.getErrorTag() == ErrorTag.INVALID_VALUE);
378 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
383 public void testUnlock() throws Exception {
384 verifyResponse(unlockCandidate(), RPC_REPLY_OK);
388 fail("Should have failed - unlocking of running datastore is not supported");
389 } catch (final DocumentedException e) {
390 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
391 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
392 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
396 unlockWithoutTarget();
397 fail("Should have failed, target is missing");
398 } catch (final DocumentedException e) {
399 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
400 assertTrue(e.getErrorTag() == ErrorTag.INVALID_VALUE);
401 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
406 public void testEditWithCreate() throws Exception {
407 verifyResponse(edit("messages/mapping/editConfigs/editConfig_create.xml"), RPC_REPLY_OK);
408 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
409 "messages/mapping/editConfig_create_n1_control.xml"));
412 edit("messages/mapping/editConfigs/editConfig_create.xml");
413 fail("Create should have failed - data already exists");
414 } catch (final DocumentedException e) {
415 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
416 assertTrue(e.getErrorTag() == ErrorTag.DATA_EXISTS);
417 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
420 verifyResponse(discardChanges(), RPC_REPLY_OK);
424 public void testDeleteNonExisting() throws Exception {
425 assertEmptyDatastore(getConfigCandidate());
426 assertEmptyDatastore(getConfigRunning());
429 edit("messages/mapping/editConfigs/editConfig_delete-top.xml");
430 fail("Delete should have failed - data is missing");
431 } catch (final DocumentedException e) {
432 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
433 assertTrue(e.getErrorTag() == ErrorTag.DATA_MISSING);
434 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
439 public void testEditMissingDefaultOperation() throws Exception {
440 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_1.xml"),
442 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_2.xml"),
444 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
445 "messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
447 verifyResponse(commit(), RPC_REPLY_OK);
448 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
449 "messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
454 private static void printDocument(final Document doc) throws Exception {
455 final TransformerFactory tf = TransformerFactory.newInstance();
456 final Transformer transformer = tf.newTransformer();
457 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
458 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
459 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
460 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
461 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
463 final StringWriter writer = new StringWriter();
464 transformer.transform(new DOMSource(doc),
465 new StreamResult(writer));
466 LOG.warn(writer.getBuffer().toString());
470 public void testEditConfigWithMultipleOperations() throws Exception {
473 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_setup.xml"),
475 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_1.xml"),
478 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_2.xml"),
480 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
481 "messages/mapping/editConfigs/editConfig_merge_multiple_operations_2_control.xml"));
483 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_leaf_operations.xml"),
485 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
486 "messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_control.xml"));
490 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_setup.xml"),
492 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_default-replace.xml"),
496 edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_create_existing.xml");
498 } catch (final DocumentedException e) {
499 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
500 assertTrue(e.getErrorTag() == ErrorTag.DATA_EXISTS);
501 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
505 "messages/mapping/editConfigs/"
506 + "editConfig_merge_multiple_operations_4_delete_children_operations.xml"),
508 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
509 "messages/mapping/editConfigs/"
510 + "editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
512 "messages/mapping/editConfigs/"
513 + "editConfig_merge_multiple_operations_4_remove-non-existing.xml"),
516 edit("messages/mapping/editConfigs/"
517 + "editConfig_merge_multiple_operations_4_delete-non-existing.xml");
519 } catch (final DocumentedException e) {
520 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
521 assertTrue(e.getErrorTag() == ErrorTag.DATA_MISSING);
522 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
525 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup.xml"),
527 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
528 "messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup-control.xml"));
530 // Test files have been modified. RFC6020 requires that at most once case inside a choice is present at any time
531 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2.xml"),
533 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
534 "messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2-control.xml"));
536 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_delete.xml"),
538 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
539 "messages/mapping/editConfigs"
540 + "/editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
546 public void testEditConfigGetElementByTagName() throws Exception {
547 EditConfig editConfig = new EditConfig("test_edit-config", Mockito.mock(CurrentSchemaContext.class),
548 Mockito.mock(TransactionProvider.class));
550 String stringWithoutPrefix =
551 "<rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n"
556 + " </edit-config>\n"
558 XmlElement xe = getXmlElement(stringWithoutPrefix);
559 NodeList nodeList = editConfig.getElementsByTagName(xe, TARGET_KEY);
560 Assert.assertEquals(1, nodeList.getLength());
562 String stringWithPrefix =
563 "<nc:rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n"
564 + " <nc:edit-config>\n"
566 + " <nc:candidate/>\n"
568 + " </nc:edit-config>\n"
571 xe = getXmlElement(stringWithPrefix);
572 nodeList = editConfig.getElementsByTagName(xe, TARGET_KEY);
573 Assert.assertEquals(1, nodeList.getLength());
575 String stringWithoutTarget =
576 "<nc:rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n"
577 + " <nc:edit-config>\n"
580 + " </nc:edit-config>\n"
582 xe = getXmlElement(stringWithoutTarget);
585 nodeList = editConfig.getElementsByTagName(xe, TARGET_KEY);
586 XmlElement.fromDomElement((Element) nodeList.item(0)).getOnlyChildElement();
587 Assert.fail("Not specified target, we should fail");
588 } catch (DocumentedException documentedException) {
594 private XmlElement getXmlElement(final String elementAsString) throws Exception {
595 Document document = XmlUtil.readXmlToDocument(elementAsString);
596 Element element = document.getDocumentElement();
597 return XmlElement.fromDomElement(element);
601 public void testReplaceMapEntry() throws Exception {
602 verifyResponse(edit("messages/mapping/editConfigs/edit-config-replace-map-entry.xml"), RPC_REPLY_OK);
603 verifyResponse(commit(), RPC_REPLY_OK);
604 verifyResponse(getConfigRunning(),
605 XmlFileLoader.xmlFileToDocument("messages/mapping/get-config-map-entry.xml"));
609 public void testMergeMapEntry() throws Exception {
610 verifyResponse(edit("messages/mapping/editConfigs/edit-config-merge-map-entry.xml"), RPC_REPLY_OK);
611 verifyResponse(commit(), RPC_REPLY_OK);
612 verifyResponse(getConfigRunning(),
613 XmlFileLoader.xmlFileToDocument("messages/mapping/get-config-map-entry.xml"));
617 public void testFiltering() throws Exception {
618 assertEmptyDatastore(getConfigCandidate());
619 assertEmptyDatastore(getConfigRunning());
621 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-config-empty-filter.xml"),
622 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
623 verifyResponse(getWithFilter("messages/mapping/filters/get-empty-filter.xml"),
624 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
626 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response"
628 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
629 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-users.xml"),
630 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
632 verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup.xml"), RPC_REPLY_OK);
633 verifyResponse(commit(), RPC_REPLY_OK);
635 verifyFilterIdentifier("messages/mapping/filters/get-filter-alluser.xml",
636 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
637 verifyFilterIdentifier("messages/mapping/filters/get-filter-company-info.xml",
638 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
639 verifyFilterIdentifier("messages/mapping/filters/get-filter-modules-and-admin.xml",
640 YangInstanceIdentifier.builder().node(TOP).build());
641 verifyFilterIdentifier("messages/mapping/filters/get-filter-only-names-types.xml",
642 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
643 verifyFilterIdentifier("messages/mapping/filters/get-filter-specific-module-type-and-user.xml",
644 YangInstanceIdentifier.builder().node(TOP).build());
645 verifyFilterIdentifier("messages/mapping/filters/get-filter-superuser.xml",
646 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
647 verifyFilterIdentifier("messages/mapping/filters/get-filter-users.xml",
648 YangInstanceIdentifier.builder().node(TOP).node(USERS).build());
650 final YangInstanceIdentifier ident = YangInstanceIdentifier
651 .builder(AUGMENTED_CONTAINER_IN_MODULES)
652 .node(AUGMENTED_CONTAINER)
653 .node(AUGMENTED_STRING_IN_CONT).build();
655 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-string.xml", ident);
656 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case.xml",
657 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(AUGMENTED_CASE).build());
659 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case.xml"),
660 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case.xml"));
663 * RFC6020 requires that at most once case inside a choice is present at any time.
665 * <augmented-case>augmented case</augmented-case>
667 * messages/mapping/editConfigs/editConfig-filtering-setup.xml
668 * cannot exists together with
669 * <text>augmented nested choice text1</text>
671 * messages/mapping/editConfigs/editConfig-filtering-setup2.xml
673 //verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup2.xml"), RPC_REPLY_OK);
674 //verifyResponse(commit(), RPC_REPLY_OK);
676 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml",
677 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).build());
678 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-case.xml",
679 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).node(INNER_CHOICE)
680 .node(INNER_CHOICE_TEXT).build());
682 // verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-string.xml"),
683 // XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-string.xml"));
684 // verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml"),
685 // XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
686 // verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-case.xml"),
687 // XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
689 verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-top.xml"), RPC_REPLY_OK);
690 verifyResponse(commit(), RPC_REPLY_OK);
694 private void verifyFilterIdentifier(final String resource, final YangInstanceIdentifier identifier)
696 final TestingGetConfig getConfig = new TestingGetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext,
697 transactionProvider);
698 final Document request = XmlFileLoader.xmlFileToDocument(resource);
699 final YangInstanceIdentifier iid = getConfig.getInstanceIdentifierFromDocument(request);
700 assertEquals(identifier, iid);
703 private class TestingGetConfig extends GetConfig {
704 TestingGetConfig(final String sessionId, final CurrentSchemaContext schemaContext,
705 final TransactionProvider transactionProvider) {
706 super(sessionId, schemaContext, transactionProvider);
709 YangInstanceIdentifier getInstanceIdentifierFromDocument(final Document request) throws DocumentedException {
710 final XmlElement filterElement = XmlElement.fromDomDocument(request).getOnlyChildElement(GET_CONFIG)
711 .getOnlyChildElement(FILTER_NODE);
712 return getInstanceIdentifierFromFilter(filterElement);
716 private void deleteDatastore() throws Exception {
717 verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-root.xml"), RPC_REPLY_OK);
718 assertEmptyDatastore(getConfigCandidate());
720 verifyResponse(commit(), RPC_REPLY_OK);
721 assertEmptyDatastore(getConfigRunning());
724 private static void verifyResponse(final Document response, final Document template) throws Exception {
725 final DetailedDiff dd = new DetailedDiff(new Diff(response, template));
726 dd.overrideElementQualifier(new NetconfXmlUnitRecursiveQualifier());
728 printDocument(response);
729 printDocument(template);
731 assertTrue(dd.toString(), dd.similar());
734 private static void assertEmptyDatastore(final Document response) {
735 final NodeList nodes = response.getChildNodes();
736 assertTrue(nodes.getLength() == 1);
738 assertEquals(nodes.item(0).getLocalName(), RPC_REPLY_ELEMENT);
740 final NodeList replyNodes = nodes.item(0).getChildNodes();
741 assertTrue(replyNodes.getLength() == 1);
743 final Node dataNode = replyNodes.item(0);
744 assertEquals(dataNode.getLocalName(), DATA_ELEMENT);
745 assertFalse(dataNode.hasChildNodes());
748 private Document commit() throws Exception {
749 final Commit commit = new Commit(SESSION_ID_FOR_REPORTING, transactionProvider);
750 return executeOperation(commit, "messages/mapping/commit.xml");
753 private Document discardChanges() throws Exception {
754 final DiscardChanges discardOp = new DiscardChanges(SESSION_ID_FOR_REPORTING, transactionProvider);
755 return executeOperation(discardOp, "messages/mapping/discardChanges.xml");
758 private Document edit(final String resource) throws Exception {
759 final EditConfig editConfig = new EditConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext,
760 transactionProvider);
761 return executeOperation(editConfig, resource);
764 private Document get() throws Exception {
765 final Get get = new Get(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
766 return executeOperation(get, "messages/mapping/get.xml");
769 private Document getWithFilter(final String resource) throws Exception {
770 final Get get = new Get(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
771 return executeOperation(get, resource);
774 private Document getConfigRunning() throws Exception {
775 final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
776 return executeOperation(getConfig, "messages/mapping/getConfig.xml");
779 private Document getConfigCandidate() throws Exception {
780 final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
781 return executeOperation(getConfig, "messages/mapping/getConfig_candidate.xml");
784 private Document getConfigWithFilter(final String resource) throws Exception {
785 final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
786 return executeOperation(getConfig, resource);
789 private static Document lock() throws Exception {
790 final Lock lock = new Lock(SESSION_ID_FOR_REPORTING);
791 return executeOperation(lock, "messages/mapping/lock.xml");
794 private static Document unlock() throws Exception {
795 final Unlock unlock = new Unlock(SESSION_ID_FOR_REPORTING);
796 return executeOperation(unlock, "messages/mapping/unlock.xml");
799 private static Document lockWithoutTarget() throws Exception {
800 final Lock lock = new Lock(SESSION_ID_FOR_REPORTING);
801 return executeOperation(lock, "messages/mapping/lock_notarget.xml");
804 private static Document unlockWithoutTarget() throws Exception {
805 final Unlock unlock = new Unlock(SESSION_ID_FOR_REPORTING);
806 return executeOperation(unlock, "messages/mapping/unlock_notarget.xml");
809 private static Document lockCandidate() throws Exception {
810 final Lock lock = new Lock(SESSION_ID_FOR_REPORTING);
811 return executeOperation(lock, "messages/mapping/lock_candidate.xml");
814 private static Document unlockCandidate() throws Exception {
815 final Unlock unlock = new Unlock(SESSION_ID_FOR_REPORTING);
816 return executeOperation(unlock, "messages/mapping/unlock_candidate.xml");
819 private static Document executeOperation(final NetconfOperation op, final String filename) throws Exception {
820 final Document request = XmlFileLoader.xmlFileToDocument(filename);
821 final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
823 LOG.debug("Got response {}", response);
827 private List<InputStream> getYangSchemas() {
828 final List<String> schemaPaths = Arrays.asList("/META-INF/yang/config@2013-04-05.yang",
829 "/yang/mdsal-netconf-mapping-test.yang");
830 final List<InputStream> schemas = new ArrayList<>();
832 for (final String schemaPath : schemaPaths) {
833 final InputStream resourceAsStream = getClass().getResourceAsStream(schemaPath);
834 schemas.add(resourceAsStream);
840 private SchemaService createSchemaService() {
841 return new SchemaService() {
844 public void addModule(final Module module) {
848 public void removeModule(final Module module) {
853 public SchemaContext getSessionContext() {
854 return schemaContext;
858 public SchemaContext getGlobalContext() {
859 return schemaContext;
863 public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(
864 final SchemaContextListener listener) {
865 listener.onGlobalContextUpdated(getGlobalContext());
866 return new ListenerRegistration<SchemaContextListener>() {
868 public void close() {
873 public SchemaContextListener getInstance() {