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;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.StringWriter;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.Collections;
23 import java.util.EnumMap;
24 import java.util.List;
25 import java.util.concurrent.ExecutorService;
27 import javax.xml.parsers.ParserConfigurationException;
28 import javax.xml.transform.OutputKeys;
29 import javax.xml.transform.Transformer;
30 import javax.xml.transform.TransformerException;
31 import javax.xml.transform.TransformerFactory;
32 import javax.xml.transform.dom.DOMSource;
33 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.opendaylight.controller.cluster.datastore.ConcurrentDOMDataBroker;
41 import org.opendaylight.controller.config.util.xml.DocumentedException;
42 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorSeverity;
43 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorTag;
44 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorType;
45 import org.opendaylight.controller.config.util.xml.XmlElement;
46 import org.opendaylight.controller.config.util.xml.XmlUtil;
47 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
48 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
49 import org.opendaylight.controller.sal.core.api.model.SchemaService;
50 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
51 import org.opendaylight.netconf.mapping.api.NetconfOperation;
52 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
53 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
54 import org.opendaylight.netconf.mdsal.connector.TransactionProvider;
55 import org.opendaylight.netconf.mdsal.connector.ops.get.Get;
56 import org.opendaylight.netconf.mdsal.connector.ops.get.GetConfig;
57 import org.opendaylight.netconf.util.test.NetconfXmlUnitRecursiveQualifier;
58 import org.opendaylight.netconf.util.test.XmlFileLoader;
59 import org.opendaylight.yangtools.concepts.ListenerRegistration;
60 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
61 import org.opendaylight.yangtools.yang.common.QName;
62 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
63 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
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.parser.api.YangSyntaxErrorException;
68 import org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils;
69 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
72 import org.w3c.dom.Document;
73 import org.w3c.dom.Node;
74 import org.w3c.dom.NodeList;
75 import org.xml.sax.SAXException;
77 import com.google.common.io.ByteSource;
79 public class NetconfMDSalMappingTest {
81 private static final Logger LOG = LoggerFactory.getLogger(NetconfMDSalMappingTest.class);
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", "2015-02-26", "augmented-container");
92 private static final QName AUGMENTED_STRING_IN_CONT = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "identifier");
93 private static final QName CHOICE_NODE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "choice-node");
94 private static final QName AUGMENTED_CASE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "augmented-case");
95 private static final QName CHOICE_WRAPPER = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "choice-wrapper");
96 private static final QName INNER_CHOICE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "inner-choice");
97 private static final QName INNER_CHOICE_TEXT = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "text");
99 private static final YangInstanceIdentifier AUGMENTED_CONTAINER_IN_MODULES =
100 YangInstanceIdentifier.builder().node(TOP).node(MODULES).build().node(new AugmentationIdentifier(Collections.singleton(AUGMENTED_CONTAINER)));
102 private static Document RPC_REPLY_OK = null;
106 RPC_REPLY_OK = XmlFileLoader.xmlFileToDocument("messages/mapping/rpc-reply_ok.xml");
107 } catch (Exception e) {
108 LOG.debug("unable to load rpc reply ok.", e);
109 RPC_REPLY_OK = XmlUtil.newDocument();
113 private CurrentSchemaContext currentSchemaContext = null;
114 private SchemaContext schemaContext = null;
115 private String sessionIdForReporting = "netconf-test-session1";
117 private TransactionProvider transactionProvider = null;
120 public void setUp() throws Exception {
122 XMLUnit.setIgnoreWhitespace(true);
123 XMLUnit.setIgnoreAttributeOrder(true);
125 this.schemaContext = parseSchemas(getYangSchemas());
126 schemaContext.getModules();
127 final SchemaService schemaService = createSchemaService();
129 final DOMStore operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", schemaService);
130 final DOMStore configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", schemaService);
132 final EnumMap<LogicalDatastoreType, DOMStore> datastores = new EnumMap<>(LogicalDatastoreType.class);
133 datastores.put(LogicalDatastoreType.CONFIGURATION, configStore);
134 datastores.put(LogicalDatastoreType.OPERATIONAL, operStore);
136 ExecutorService listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
137 16, 16, "CommitFutures");
139 final ConcurrentDOMDataBroker cdb = new ConcurrentDOMDataBroker(datastores, listenableFutureExecutor);
140 this.transactionProvider = new TransactionProvider(cdb, sessionIdForReporting);
141 this.currentSchemaContext = new CurrentSchemaContext(schemaService);
146 public void testEmptyDatastore() throws Exception {
147 assertEmptyDatastore(get());
148 assertEmptyDatastore(getConfigCandidate());
149 assertEmptyDatastore(getConfigRunning());
153 public void testIncorrectGet() throws Exception {
156 executeOperation(new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider), "messages/mapping/bad_getConfig.xml");
157 fail("Should have failed, this is an incorrect request");
158 } catch (DocumentedException e) {
159 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
160 assertTrue(e.getErrorTag() == ErrorTag.operation_failed);
161 assertTrue(e.getErrorType() == ErrorType.application);
165 executeOperation(new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider), "messages/mapping/bad_namespace_getConfig.xml");
166 fail("Should have failed, this is an incorrect request");
167 } catch (DocumentedException e) {
168 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
169 assertTrue(e.getErrorTag() == ErrorTag.operation_failed);
170 assertTrue(e.getErrorType() == ErrorType.application);
177 public void testEditRunning() throws Exception {
180 edit("messages/mapping/editConfigs/editConfig_running.xml");
181 fail("Should have failed - edit config on running datastore is not supported");
182 } catch (DocumentedException e) {
183 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
184 assertTrue(e.getErrorTag() == ErrorTag.operation_not_supported);
185 assertTrue(e.getErrorType() == ErrorType.protocol);
191 public void testCandidateTransaction() throws Exception {
193 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_n1.xml"), RPC_REPLY_OK);
194 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
195 assertEmptyDatastore(getConfigRunning());
197 verifyResponse(discardChanges(), RPC_REPLY_OK);
198 assertEmptyDatastore(getConfigCandidate());
203 public void testEditWithCommit() throws Exception {
205 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_n1.xml"), RPC_REPLY_OK);
206 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
208 verifyResponse(commit(), RPC_REPLY_OK);
209 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
216 public void testKeyOrder() throws Exception {
217 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_keys_1.xml"), RPC_REPLY_OK);
218 verifyResponse(commit(), RPC_REPLY_OK);
219 final Document configRunning = getConfigRunning();
220 final String responseAsString = XmlUtil.toString(configRunning);
221 verifyResponse(configRunning, XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_keys_1_control.xml"));
223 final int key3 = responseAsString.indexOf("key3");
224 final int key1 = responseAsString.indexOf("key1");
225 final int key2 = responseAsString.indexOf("key2");
227 assertTrue(String.format("Key ordering invalid, should be key3(%d) < key1(%d) < key2(%d)", key3, key1, key2),
228 key3 < key1 && key1 < key2);
235 public void testMultipleEditsWithMerge() throws Exception {
237 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
238 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_control_1.xml"));
239 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
240 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_control_2.xml"));
241 assertEmptyDatastore(getConfigRunning());
243 verifyResponse(commit(), RPC_REPLY_OK);
244 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_control_2.xml"));
251 public void testMoreComplexEditConfigs() throws Exception {
253 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
254 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
256 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_2.xml"), RPC_REPLY_OK);
257 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_more_complex_merge.xml"));
259 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_3.xml"), RPC_REPLY_OK);
260 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_more_complex_merge_2.xml"));
262 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_4_replace.xml"), RPC_REPLY_OK);
263 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_replace.xml"));
264 verifyResponse(commit(), RPC_REPLY_OK);
266 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_replace.xml"));
268 verifyResponse(edit("messages/mapping/editConfigs/editConfig_replace_default.xml"), RPC_REPLY_OK);
269 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_replace_default_control.xml"));
270 verifyResponse(commit(), RPC_REPLY_OK);
272 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_replace_default_control.xml"));
279 public void testLock() throws Exception {
281 verifyResponse(lockCandidate(), RPC_REPLY_OK);
285 fail("Should have failed - locking of running datastore is not supported");
286 } catch (DocumentedException e) {
287 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
288 assertTrue(e.getErrorTag() == ErrorTag.operation_not_supported);
289 assertTrue(e.getErrorType() == ErrorType.application);
295 fail("Should have failed, target is missing");
296 } catch (DocumentedException e) {
297 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
298 assertTrue(e.getErrorTag() == ErrorTag.invalid_value);
299 assertTrue(e.getErrorType() == ErrorType.application);
304 public void testUnlock() throws Exception {
306 verifyResponse(unlockCandidate(), RPC_REPLY_OK);
310 fail("Should have failed - unlocking of running datastore is not supported");
311 } catch (DocumentedException e) {
312 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
313 assertTrue(e.getErrorTag() == ErrorTag.operation_not_supported);
314 assertTrue(e.getErrorType() == ErrorType.application);
318 unlockWithoutTarget();
319 fail("Should have failed, target is missing");
320 } catch (DocumentedException e) {
321 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
322 assertTrue(e.getErrorTag() == ErrorTag.invalid_value);
323 assertTrue(e.getErrorType() == ErrorType.application);
328 public void testEditWithCreate() throws Exception {
330 verifyResponse(edit("messages/mapping/editConfigs/editConfig_create.xml"), RPC_REPLY_OK);
331 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_create_n1_control.xml"));
335 edit("messages/mapping/editConfigs/editConfig_create.xml");
336 fail("Create should have failed - data already exists");
337 } catch (DocumentedException e) {
338 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
339 assertTrue(e.getErrorTag() == ErrorTag.data_exists);
340 assertTrue(e.getErrorType() == ErrorType.protocol);
343 verifyResponse(discardChanges(), RPC_REPLY_OK);
348 public void testDeleteNonExisting() throws Exception {
350 assertEmptyDatastore(getConfigCandidate());
351 assertEmptyDatastore(getConfigRunning());
354 edit("messages/mapping/editConfigs/editConfig_delete-top.xml");
355 fail("Delete should have failed - data is missing");
356 } catch (DocumentedException e) {
357 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
358 assertTrue(e.getErrorTag() == ErrorTag.data_missing);
359 assertTrue(e.getErrorType() == ErrorType.protocol);
365 public void testEditMissingDefaultOperation() throws Exception {
367 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_1.xml"), RPC_REPLY_OK);
368 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_2.xml"), RPC_REPLY_OK);
369 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
371 verifyResponse(commit(), RPC_REPLY_OK);
372 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
377 public static void printDocument(Document doc) throws IOException, TransformerException {
378 TransformerFactory tf = TransformerFactory.newInstance();
379 Transformer transformer = tf.newTransformer();
380 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
381 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
382 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
383 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
384 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
386 StringWriter writer = new StringWriter();
387 transformer.transform(new DOMSource(doc),
388 new StreamResult(writer));
389 LOG.warn(writer.getBuffer().toString());
393 public void testEditConfigWithMultipleOperations() throws Exception {
396 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_setup.xml"), RPC_REPLY_OK);
397 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_1.xml"), RPC_REPLY_OK);
399 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_2.xml"), RPC_REPLY_OK);
400 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_2_control.xml"));
402 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_leaf_operations.xml"), RPC_REPLY_OK);
403 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_control.xml"));
407 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_setup.xml"), RPC_REPLY_OK);
408 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_default-replace.xml"), RPC_REPLY_OK);
411 edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_create_existing.xml");
413 } catch (DocumentedException e) {
414 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
415 assertTrue(e.getErrorTag() == ErrorTag.data_exists);
416 assertTrue(e.getErrorType() == ErrorType.protocol);
419 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete_children_operations.xml"), RPC_REPLY_OK);
420 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
421 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_remove-non-existing.xml"), RPC_REPLY_OK);
424 edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete-non-existing.xml");
426 } catch (DocumentedException e) {
427 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
428 assertTrue(e.getErrorTag() == ErrorTag.data_missing);
429 assertTrue(e.getErrorType() == ErrorType.protocol);
432 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup.xml"), RPC_REPLY_OK);
433 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup-control.xml"));
435 // Test files have been modified. RFC6020 requires that at most once case inside a choice is present at any time
436 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2.xml"), RPC_REPLY_OK);
437 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2-control.xml"));
439 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_delete.xml"), RPC_REPLY_OK);
440 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
446 public void testFiltering() throws Exception {
448 assertEmptyDatastore(getConfigCandidate());
449 assertEmptyDatastore(getConfigRunning());
451 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-config-empty-filter.xml"),
452 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
453 verifyResponse(getWithFilter("messages/mapping/filters/get-empty-filter.xml"),
454 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
456 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
457 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
458 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-users.xml"),
459 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
461 verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup.xml"), RPC_REPLY_OK);
462 verifyResponse(commit(), RPC_REPLY_OK);
464 //TODO uncomment these tests once we can parse KeyedListNode as a selection node, currently you cannot use a KeyedList as a selection node in filter
465 // verifyFilterIdentifier("messages/mapping/filters/get-filter-alluser.xml",
466 // YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
467 verifyFilterIdentifier("messages/mapping/filters/get-filter-company-info.xml",
468 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
469 verifyFilterIdentifier("messages/mapping/filters/get-filter-modules-and-admin.xml",
470 YangInstanceIdentifier.builder().node(TOP).build());
471 verifyFilterIdentifier("messages/mapping/filters/get-filter-only-names-types.xml",
472 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
473 // verifyFilterIdentifier("messages/mapping/filters/get-filter-specific-module-type-and-user.xml",
474 // YangInstanceIdentifier.builder().node(TOP).build());
475 // verifyFilterIdentifier("messages/mapping/filters/get-filter-superuser.xml",
476 // YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
477 verifyFilterIdentifier("messages/mapping/filters/get-filter-users.xml",
478 YangInstanceIdentifier.builder().node(TOP).node(USERS).build());
480 YangInstanceIdentifier ident = YangInstanceIdentifier.
481 builder(AUGMENTED_CONTAINER_IN_MODULES).
482 node(AUGMENTED_CONTAINER).
483 node(AUGMENTED_STRING_IN_CONT).build();
485 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-string.xml", ident);
486 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case.xml",
487 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(AUGMENTED_CASE).build());
489 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case.xml"),
490 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case.xml"));
493 * RFC6020 requires that at most once case inside a choice is present at any time.
495 * <augmented-case>augmented case</augmented-case>
497 * messages/mapping/editConfigs/editConfig-filtering-setup.xml
498 * cannot exists together with
499 * <text>augmented nested choice text1</text>
501 * messages/mapping/editConfigs/editConfig-filtering-setup2.xml
503 //verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup2.xml"), RPC_REPLY_OK);
504 //verifyResponse(commit(), RPC_REPLY_OK);
506 // verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml",
507 // YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).build());
508 // verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-case.xml",
509 // YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).node(INNER_CHOICE).node(INNER_CHOICE_TEXT).build());
511 // verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-string.xml"),
512 // XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-string.xml"));
513 // verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml"),
514 // XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
515 // verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-case.xml"),
516 // XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
518 verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-top.xml"), RPC_REPLY_OK);
519 verifyResponse(commit(), RPC_REPLY_OK);
523 private void verifyFilterIdentifier(String resource, YangInstanceIdentifier identifier) throws Exception{
524 TestingGetConfig getConfig = new TestingGetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
525 Document request = XmlFileLoader.xmlFileToDocument(resource);
526 YangInstanceIdentifier iid = getConfig.getInstanceIdentifierFromDocument(request);
527 assertTrue(iid.equals(identifier));
530 private class TestingGetConfig extends GetConfig{
531 public TestingGetConfig(String sessionId, CurrentSchemaContext schemaContext, TransactionProvider transactionProvider) {
532 super(sessionId, schemaContext, transactionProvider);
535 public YangInstanceIdentifier getInstanceIdentifierFromDocument(Document request) throws DocumentedException {
536 XmlElement filterElement = XmlElement.fromDomDocument(request).getOnlyChildElement(GET_CONFIG).getOnlyChildElement(FILTER_NODE);
537 return getInstanceIdentifierFromFilter(filterElement);
541 private void deleteDatastore() throws Exception{
542 verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-root.xml"), RPC_REPLY_OK);
543 assertEmptyDatastore(getConfigCandidate());
545 verifyResponse(commit(), RPC_REPLY_OK);
546 assertEmptyDatastore(getConfigRunning());
549 private void verifyResponse(Document response, Document template) throws IOException, TransformerException {
550 DetailedDiff dd = new DetailedDiff(new Diff(response, template));
551 dd.overrideElementQualifier(new NetconfXmlUnitRecursiveQualifier());
553 printDocument(response);
554 printDocument(template);
556 assertTrue(dd.toString(), dd.similar());
559 private void assertEmptyDatastore(Document response) {
561 NodeList nodes = response.getChildNodes();
562 assertTrue(nodes.getLength() == 1);
564 assertEquals(nodes.item(0).getLocalName(), RPC_REPLY_ELEMENT);
566 NodeList replyNodes = nodes.item(0).getChildNodes();
567 assertTrue(replyNodes.getLength() == 1);
569 Node dataNode = replyNodes.item(0);
570 assertEquals(dataNode.getLocalName(), DATA_ELEMENT);
571 assertFalse(dataNode.hasChildNodes());
575 private Document commit() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
576 Commit commit = new Commit(sessionIdForReporting, transactionProvider);
577 return executeOperation(commit, "messages/mapping/commit.xml");
580 private Document discardChanges() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
581 DiscardChanges discardOp = new DiscardChanges(sessionIdForReporting, transactionProvider);
582 return executeOperation(discardOp, "messages/mapping/discardChanges.xml");
585 private Document edit(String resource) throws DocumentedException, ParserConfigurationException, SAXException, IOException {
586 EditConfig editConfig = new EditConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
587 return executeOperation(editConfig, resource);
590 private Document get() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
591 Get get = new Get(sessionIdForReporting, currentSchemaContext, transactionProvider);
592 return executeOperation(get, "messages/mapping/get.xml");
595 private Document getWithFilter(String resource) throws DocumentedException, ParserConfigurationException, SAXException, IOException {
596 Get get = new Get(sessionIdForReporting, currentSchemaContext, transactionProvider);
597 return executeOperation(get, resource);
600 private Document getConfigRunning() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
601 GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
602 return executeOperation(getConfig, "messages/mapping/getConfig.xml");
605 private Document getConfigCandidate() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
606 GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
607 return executeOperation(getConfig, "messages/mapping/getConfig_candidate.xml");
610 private Document getConfigWithFilter(String resource) throws DocumentedException, ParserConfigurationException, SAXException, IOException {
611 GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
612 return executeOperation(getConfig, resource);
615 private Document lock() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
616 Lock lock = new Lock(sessionIdForReporting);
617 return executeOperation(lock, "messages/mapping/lock.xml");
620 private Document unlock() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
621 Unlock unlock = new Unlock(sessionIdForReporting);
622 return executeOperation(unlock, "messages/mapping/unlock.xml");
625 private Document lockWithoutTarget() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
626 Lock lock = new Lock(sessionIdForReporting);
627 return executeOperation(lock, "messages/mapping/lock_notarget.xml");
630 private Document unlockWithoutTarget() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
631 Unlock unlock = new Unlock(sessionIdForReporting);
632 return executeOperation(unlock, "messages/mapping/unlock_notarget.xml");
635 private Document lockCandidate() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
636 Lock lock = new Lock(sessionIdForReporting);
637 return executeOperation(lock, "messages/mapping/lock_candidate.xml");
640 private Document unlockCandidate() throws DocumentedException, ParserConfigurationException, SAXException, IOException {
641 Unlock unlock = new Unlock(sessionIdForReporting);
642 return executeOperation(unlock, "messages/mapping/unlock_candidate.xml");
645 private Document executeOperation(NetconfOperation op, String filename) throws ParserConfigurationException, SAXException, IOException, DocumentedException {
646 final Document request = XmlFileLoader.xmlFileToDocument(filename);
647 final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
649 LOG.debug("Got response {}" , response);
653 private Collection<InputStream> getYangSchemas() {
654 final List<String> schemaPaths = Arrays.asList("/META-INF/yang/config.yang", "/yang/mdsal-netconf-mapping-test.yang");
655 final List<InputStream> schemas = new ArrayList<>();
657 for (String schemaPath : schemaPaths) {
658 InputStream resourceAsStream = getClass().getResourceAsStream(schemaPath);
659 schemas.add(resourceAsStream);
665 private SchemaContext parseSchemas(Collection<InputStream> schemas) throws IOException, YangSyntaxErrorException {
666 final YangParserImpl parser = new YangParserImpl();
667 Collection<ByteSource> sources = BuilderUtils.streamsToByteSources(schemas);
668 return parser.parseSources(sources);
671 private SchemaService createSchemaService() {
672 return new SchemaService() {
675 public void addModule(Module module) {
679 public void removeModule(Module module) {
684 public SchemaContext getSessionContext() {
685 return schemaContext;
689 public SchemaContext getGlobalContext() {
690 return schemaContext;
694 public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(final SchemaContextListener listener) {
695 listener.onGlobalContextUpdated(getGlobalContext());
696 return new ListenerRegistration<SchemaContextListener>() {
698 public void close() {
703 public SchemaContextListener getInstance() {