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.controller.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 com.google.common.base.Preconditions;
17 import com.google.common.io.ByteSource;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.StringWriter;
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.EnumMap;
26 import java.util.List;
27 import java.util.concurrent.ExecutorService;
28 import javax.xml.parsers.ParserConfigurationException;
29 import javax.xml.transform.OutputKeys;
30 import javax.xml.transform.Transformer;
31 import javax.xml.transform.TransformerException;
32 import javax.xml.transform.TransformerFactory;
33 import javax.xml.transform.dom.DOMSource;
34 import javax.xml.transform.stream.StreamResult;
35 import org.custommonkey.xmlunit.DetailedDiff;
36 import org.custommonkey.xmlunit.Diff;
37 import org.custommonkey.xmlunit.XMLUnit;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.opendaylight.controller.cluster.datastore.ConcurrentDOMDataBroker;
41 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
42 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
43 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
44 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
45 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
46 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
47 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
48 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
49 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
50 import org.opendaylight.controller.netconf.mdsal.connector.CurrentSchemaContext;
51 import org.opendaylight.controller.netconf.mdsal.connector.TransactionProvider;
52 import org.opendaylight.controller.netconf.mdsal.connector.ops.get.Get;
53 import org.opendaylight.controller.netconf.mdsal.connector.ops.get.GetConfig;
54 import org.opendaylight.controller.netconf.util.test.NetconfXmlUnitRecursiveQualifier;
55 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
56 import org.opendaylight.controller.netconf.util.xml.XmlElement;
57 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
58 import org.opendaylight.controller.sal.core.api.model.SchemaService;
59 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
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.data.api.YangInstanceIdentifier.AugmentationIdentifier;
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.parser.api.YangSyntaxErrorException;
69 import org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils;
70 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
71 import org.slf4j.Logger;
72 import org.slf4j.LoggerFactory;
73 import org.w3c.dom.Document;
74 import org.w3c.dom.Node;
75 import org.w3c.dom.NodeList;
76 import org.xml.sax.SAXException;
78 public class NetconfMDSalMappingTest {
80 private static final Logger LOG = LoggerFactory.getLogger(NetconfMDSalMappingTest.class);
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", "2015-02-26", "augmented-container");
91 private static final QName AUGMENTED_STRING_IN_CONT = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "identifier");
92 private static final QName CHOICE_NODE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "choice-node");
93 private static final QName AUGMENTED_CASE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "augmented-case");
94 private static final QName CHOICE_WRAPPER = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "choice-wrapper");
95 private static final QName INNER_CHOICE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "inner-choice");
96 private static final QName INNER_CHOICE_TEXT = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "text");
98 private static final YangInstanceIdentifier AUGMENTED_CONTAINER_IN_MODULES =
99 YangInstanceIdentifier.builder().node(TOP).node(MODULES).build().node(new AugmentationIdentifier(Collections.singleton(AUGMENTED_CONTAINER)));
101 private static Document RPC_REPLY_OK = null;
105 RPC_REPLY_OK = XmlFileLoader.xmlFileToDocument("messages/mapping/rpc-reply_ok.xml");
106 } catch (Exception e) {
107 LOG.debug("unable to load rpc reply ok.", e);
108 RPC_REPLY_OK = XmlUtil.newDocument();
112 private CurrentSchemaContext currentSchemaContext = null;
113 private SchemaContext schemaContext = null;
114 private String sessionIdForReporting = "netconf-test-session1";
116 private TransactionProvider transactionProvider = null;
119 public void setUp() throws Exception {
121 XMLUnit.setIgnoreWhitespace(true);
122 XMLUnit.setIgnoreAttributeOrder(true);
124 this.schemaContext = parseSchemas(getYangSchemas());
125 schemaContext.getModules();
126 final SchemaService schemaService = createSchemaService();
128 final DOMStore operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", schemaService);
129 final DOMStore configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", schemaService);
131 final EnumMap<LogicalDatastoreType, DOMStore> datastores = new EnumMap<>(LogicalDatastoreType.class);
132 datastores.put(LogicalDatastoreType.CONFIGURATION, configStore);
133 datastores.put(LogicalDatastoreType.OPERATIONAL, operStore);
135 ExecutorService listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
136 16, 16, "CommitFutures");
138 final ConcurrentDOMDataBroker cdb = new ConcurrentDOMDataBroker(datastores, listenableFutureExecutor);
139 this.transactionProvider = new TransactionProvider(cdb, sessionIdForReporting);
140 this.currentSchemaContext = new CurrentSchemaContext(schemaService);
145 public void testEmptyDatastore() throws Exception {
146 assertEmptyDatastore(get());
147 assertEmptyDatastore(getConfigCandidate());
148 assertEmptyDatastore(getConfigRunning());
152 public void testEditRunning() throws Exception {
155 edit("messages/mapping/editConfigs/editConfig_running.xml");
156 fail("Should have failed - edit config on running datastore is not supported");
157 } catch (NetconfDocumentedException e) {
158 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
159 assertTrue(e.getErrorTag() == ErrorTag.operation_not_supported);
160 assertTrue(e.getErrorType() == ErrorType.protocol);
166 public void testCandidateTransaction() throws Exception {
168 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_n1.xml"), RPC_REPLY_OK);
169 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
170 assertEmptyDatastore(getConfigRunning());
172 verifyResponse(discardChanges(), RPC_REPLY_OK);
173 assertEmptyDatastore(getConfigCandidate());
178 public void testEditWithCommit() throws Exception {
180 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_n1.xml"), RPC_REPLY_OK);
181 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
183 verifyResponse(commit(), RPC_REPLY_OK);
184 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
191 public void testKeyOrder() throws Exception {
192 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_keys_1.xml"), RPC_REPLY_OK);
193 verifyResponse(commit(), RPC_REPLY_OK);
194 final Document configRunning = getConfigRunning();
195 final String responseAsString = XmlUtil.toString(configRunning);
196 verifyResponse(configRunning, XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_keys_1_control.xml"));
198 final int key3 = responseAsString.indexOf("key3");
199 final int key1 = responseAsString.indexOf("key1");
200 final int key2 = responseAsString.indexOf("key2");
202 assertTrue(String.format("Key ordering invalid, should be key3(%d) < key1(%d) < key2(%d)", key3, key1, key2),
203 key3 < key1 && key1 < key2);
209 public void testMultipleEditsWithMerge() throws Exception {
211 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
212 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_control_1.xml"));
213 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
214 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_control_2.xml"));
215 assertEmptyDatastore(getConfigRunning());
217 verifyResponse(commit(), RPC_REPLY_OK);
218 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_control_2.xml"));
225 public void testMoreComplexEditConfigs() throws Exception {
227 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
228 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
230 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_2.xml"), RPC_REPLY_OK);
231 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_more_complex_merge.xml"));
233 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_3.xml"), RPC_REPLY_OK);
234 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_more_complex_merge_2.xml"));
236 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_4_replace.xml"), RPC_REPLY_OK);
237 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_replace.xml"));
238 verifyResponse(commit(), RPC_REPLY_OK);
240 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_replace.xml"));
242 verifyResponse(edit("messages/mapping/editConfigs/editConfig_replace_default.xml"), RPC_REPLY_OK);
243 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_replace_default_control.xml"));
244 verifyResponse(commit(), RPC_REPLY_OK);
246 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_replace_default_control.xml"));
253 public void testLock() throws Exception {
255 verifyResponse(lockCandidate(), RPC_REPLY_OK);
259 fail("Should have failed - locking of running datastore is not supported");
260 } catch (NetconfDocumentedException e) {
261 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
262 assertTrue(e.getErrorTag() == ErrorTag.operation_not_supported);
263 assertTrue(e.getErrorType() == ErrorType.application);
268 public void testUnlock() throws Exception {
270 verifyResponse(unlockCandidate(), RPC_REPLY_OK);
274 fail("Should have failed - unlocking of running datastore is not supported");
275 } catch (NetconfDocumentedException e) {
276 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
277 assertTrue(e.getErrorTag() == ErrorTag.operation_not_supported);
278 assertTrue(e.getErrorType() == ErrorType.application);
283 public void testEditWithCreate() throws Exception {
285 verifyResponse(edit("messages/mapping/editConfigs/editConfig_create.xml"), RPC_REPLY_OK);
286 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_create_n1_control.xml"));
290 edit("messages/mapping/editConfigs/editConfig_create.xml");
291 fail("Create should have failed - data already exists");
292 } catch (NetconfDocumentedException e) {
293 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
294 assertTrue(e.getErrorTag() == ErrorTag.data_exists);
295 assertTrue(e.getErrorType() == ErrorType.protocol);
298 verifyResponse(discardChanges(), RPC_REPLY_OK);
303 public void testDeleteNonExisting() throws Exception {
305 assertEmptyDatastore(getConfigCandidate());
306 assertEmptyDatastore(getConfigRunning());
309 edit("messages/mapping/editConfigs/editConfig_delete-top.xml");
310 fail("Delete should have failed - data is missing");
311 } catch (NetconfDocumentedException e) {
312 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
313 assertTrue(e.getErrorTag() == ErrorTag.data_missing);
314 assertTrue(e.getErrorType() == ErrorType.protocol);
320 public void testEditMissingDefaultOperation() throws Exception {
322 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_1.xml"), RPC_REPLY_OK);
323 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_2.xml"), RPC_REPLY_OK);
324 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
326 verifyResponse(commit(), RPC_REPLY_OK);
327 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
332 public static void printDocument(Document doc) throws IOException, TransformerException {
333 TransformerFactory tf = TransformerFactory.newInstance();
334 Transformer transformer = tf.newTransformer();
335 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
336 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
337 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
338 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
339 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
341 StringWriter writer = new StringWriter();
342 transformer.transform(new DOMSource(doc),
343 new StreamResult(writer));
344 LOG.warn(writer.getBuffer().toString());
348 public void testEditConfigWithMultipleOperations() throws Exception {
351 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_setup.xml"), RPC_REPLY_OK);
352 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_1.xml"), RPC_REPLY_OK);
354 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_2.xml"), RPC_REPLY_OK);
355 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_2_control.xml"));
357 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_leaf_operations.xml"), RPC_REPLY_OK);
358 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_control.xml"));
362 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_setup.xml"), RPC_REPLY_OK);
363 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_default-replace.xml"), RPC_REPLY_OK);
366 edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_create_existing.xml");
368 } catch (NetconfDocumentedException e) {
369 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
370 assertTrue(e.getErrorTag() == ErrorTag.data_exists);
371 assertTrue(e.getErrorType() == ErrorType.protocol);
374 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete_children_operations.xml"), RPC_REPLY_OK);
375 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
376 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_remove-non-existing.xml"), RPC_REPLY_OK);
379 edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete-non-existing.xml");
381 } catch (NetconfDocumentedException e) {
382 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
383 assertTrue(e.getErrorTag() == ErrorTag.data_missing);
384 assertTrue(e.getErrorType() == ErrorType.protocol);
387 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup.xml"), RPC_REPLY_OK);
388 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup-control.xml"));
390 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2.xml"), RPC_REPLY_OK);
391 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2-control.xml"));
393 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_delete.xml"), RPC_REPLY_OK);
394 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
400 public void testFiltering() throws Exception {
402 assertEmptyDatastore(getConfigCandidate());
403 assertEmptyDatastore(getConfigRunning());
405 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
406 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
407 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-users.xml"),
408 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
410 verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup.xml"), RPC_REPLY_OK);
411 verifyResponse(commit(), RPC_REPLY_OK);
413 //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
414 // verifyFilterIdentifier("messages/mapping/filters/get-filter-alluser.xml",
415 // YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
416 verifyFilterIdentifier("messages/mapping/filters/get-filter-company-info.xml",
417 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
418 verifyFilterIdentifier("messages/mapping/filters/get-filter-modules-and-admin.xml",
419 YangInstanceIdentifier.builder().node(TOP).build());
420 verifyFilterIdentifier("messages/mapping/filters/get-filter-only-names-types.xml",
421 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
422 // verifyFilterIdentifier("messages/mapping/filters/get-filter-specific-module-type-and-user.xml",
423 // YangInstanceIdentifier.builder().node(TOP).build());
424 // verifyFilterIdentifier("messages/mapping/filters/get-filter-superuser.xml",
425 // YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
426 verifyFilterIdentifier("messages/mapping/filters/get-filter-users.xml",
427 YangInstanceIdentifier.builder().node(TOP).node(USERS).build());
429 YangInstanceIdentifier ident = YangInstanceIdentifier.
430 builder(AUGMENTED_CONTAINER_IN_MODULES).
431 node(AUGMENTED_CONTAINER).
432 node(AUGMENTED_STRING_IN_CONT).build();
434 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-string.xml", ident);
435 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case.xml",
436 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(AUGMENTED_CASE).build());
438 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case.xml"),
439 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case.xml"));
440 verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup2.xml"), RPC_REPLY_OK);
441 verifyResponse(commit(), RPC_REPLY_OK);
443 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml",
444 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).build());
445 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-case.xml",
446 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).node(INNER_CHOICE).node(INNER_CHOICE_TEXT).build());
448 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-string.xml"),
449 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-string.xml"));
450 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml"),
451 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
452 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-case.xml"),
453 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
455 verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-top.xml"), RPC_REPLY_OK);
456 verifyResponse(commit(), RPC_REPLY_OK);
460 private void verifyFilterIdentifier(String resource, YangInstanceIdentifier identifier) throws Exception{
461 TestingGetConfig getConfig = new TestingGetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
462 Document request = XmlFileLoader.xmlFileToDocument(resource);
463 YangInstanceIdentifier iid = getConfig.getInstanceIdentifierFromDocument(request);
464 assertTrue(iid.equals(identifier));
467 private class TestingGetConfig extends GetConfig{
468 public TestingGetConfig(String sessionId, CurrentSchemaContext schemaContext, TransactionProvider transactionProvider) {
469 super(sessionId, schemaContext, transactionProvider);
472 public YangInstanceIdentifier getInstanceIdentifierFromDocument(Document request) throws NetconfDocumentedException {
473 XmlElement filterElement = XmlElement.fromDomDocument(request).getOnlyChildElement(GET_CONFIG).getOnlyChildElement(FILTER_NODE);
474 return getInstanceIdentifierFromFilter(filterElement);
478 private void deleteDatastore() throws Exception{
479 verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-root.xml"), RPC_REPLY_OK);
480 assertEmptyDatastore(getConfigCandidate());
482 verifyResponse(commit(), RPC_REPLY_OK);
483 assertEmptyDatastore(getConfigRunning());
486 private void verifyResponse(Document response, Document template) throws IOException, TransformerException {
487 DetailedDiff dd = new DetailedDiff(new Diff(response, template));
488 dd.overrideElementQualifier(new NetconfXmlUnitRecursiveQualifier());
490 printDocument(response);
491 printDocument(template);
493 assertTrue(dd.toString(), dd.similar());
496 private void assertEmptyDatastore(Document response) {
498 NodeList nodes = response.getChildNodes();
499 assertTrue(nodes.getLength() == 1);
501 assertEquals(nodes.item(0).getLocalName(),RPC_REPLY_ELEMENT);
503 NodeList replyNodes = nodes.item(0).getChildNodes();
504 assertTrue(replyNodes.getLength() == 1);
506 Node dataNode = replyNodes.item(0);
507 assertEquals(dataNode.getLocalName(), DATA_ELEMENT);
508 assertFalse(dataNode.hasChildNodes());
512 private Document commit() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
513 Commit commit = new Commit(sessionIdForReporting, transactionProvider);
514 return executeOperation(commit, "messages/mapping/commit.xml");
517 private Document discardChanges() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
518 DiscardChanges discardOp = new DiscardChanges(sessionIdForReporting, transactionProvider);
519 return executeOperation(discardOp, "messages/mapping/discardChanges.xml");
522 private Document edit(String resource) throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
523 EditConfig editConfig = new EditConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
524 return executeOperation(editConfig, resource);
527 private Document get() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
528 Get get = new Get(sessionIdForReporting, currentSchemaContext, transactionProvider);
529 return executeOperation(get, "messages/mapping/get.xml");
532 private Document getConfigRunning() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
533 GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
534 return executeOperation(getConfig, "messages/mapping/getConfig.xml");
537 private Document getConfigCandidate() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
538 GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
539 return executeOperation(getConfig, "messages/mapping/getConfig_candidate.xml");
542 private Document getConfigWithFilter(String resource) throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
543 GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
544 return executeOperation(getConfig, resource);
547 private Document lock() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
548 Lock lock = new Lock(sessionIdForReporting);
549 return executeOperation(lock, "messages/mapping/lock.xml");
552 private Document unlock() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
553 Unlock unlock = new Unlock(sessionIdForReporting);
554 return executeOperation(unlock, "messages/mapping/unlock.xml");
557 private Document lockCandidate() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
558 Lock lock = new Lock(sessionIdForReporting);
559 return executeOperation(lock, "messages/mapping/lock_candidate.xml");
562 private Document unlockCandidate() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
563 Unlock unlock = new Unlock(sessionIdForReporting);
564 return executeOperation(unlock, "messages/mapping/unlock_candidate.xml");
567 private Document executeOperation(NetconfOperation op, String filename) throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
568 final Document request = XmlFileLoader.xmlFileToDocument(filename);
570 HandlingPriority priority = op.canHandle(request);
571 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
573 final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
575 LOG.debug("Got response {}" , response);
580 private Collection<InputStream> getYangSchemas() {
581 final List<String> schemaPaths = Arrays.asList("/META-INF/yang/config.yang", "/yang/mdsal-netconf-mapping-test.yang");
582 final List<InputStream> schemas = new ArrayList<>();
584 for (String schemaPath : schemaPaths) {
585 InputStream resourceAsStream = getClass().getResourceAsStream(schemaPath);
586 schemas.add(resourceAsStream);
592 private SchemaContext parseSchemas(Collection<InputStream> schemas) throws IOException, YangSyntaxErrorException {
593 final YangParserImpl parser = new YangParserImpl();
594 Collection<ByteSource> sources = BuilderUtils.streamsToByteSources(schemas);
595 return parser.parseSources(sources);
598 private SchemaService createSchemaService() {
599 return new SchemaService() {
602 public void addModule(Module module) {
606 public void removeModule(Module module) {
611 public SchemaContext getSessionContext() {
612 return schemaContext;
616 public SchemaContext getGlobalContext() {
617 return schemaContext;
621 public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(final SchemaContextListener listener) {
622 listener.onGlobalContextUpdated(getGlobalContext());
623 return new ListenerRegistration<SchemaContextListener>() {
625 public void close() {
630 public SchemaContextListener getInstance() {