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 testMultipleEditsWithMerge() throws Exception {
193 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
194 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_control_1.xml"));
195 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
196 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_control_2.xml"));
197 assertEmptyDatastore(getConfigRunning());
199 verifyResponse(commit(), RPC_REPLY_OK);
200 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_control_2.xml"));
207 public void testMoreComplexEditConfigs() throws Exception {
209 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
210 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
212 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_2.xml"), RPC_REPLY_OK);
213 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_more_complex_merge.xml"));
215 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_3.xml"), RPC_REPLY_OK);
216 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_more_complex_merge_2.xml"));
218 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_4_replace.xml"), RPC_REPLY_OK);
219 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_replace.xml"));
220 verifyResponse(commit(), RPC_REPLY_OK);
222 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_after_replace.xml"));
224 verifyResponse(edit("messages/mapping/editConfigs/editConfig_replace_default.xml"), RPC_REPLY_OK);
225 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_replace_default_control.xml"));
226 verifyResponse(commit(), RPC_REPLY_OK);
228 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_replace_default_control.xml"));
235 public void testLock() throws Exception {
237 verifyResponse(lockCandidate(), RPC_REPLY_OK);
241 fail("Should have failed - locking of running datastore is not supported");
242 } catch (NetconfDocumentedException e) {
243 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
244 assertTrue(e.getErrorTag() == ErrorTag.operation_not_supported);
245 assertTrue(e.getErrorType() == ErrorType.application);
250 public void testUnlock() throws Exception {
252 verifyResponse(unlockCandidate(), RPC_REPLY_OK);
256 fail("Should have failed - unlocking of running datastore is not supported");
257 } catch (NetconfDocumentedException e) {
258 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
259 assertTrue(e.getErrorTag() == ErrorTag.operation_not_supported);
260 assertTrue(e.getErrorType() == ErrorType.application);
265 public void testEditWithCreate() throws Exception {
267 verifyResponse(edit("messages/mapping/editConfigs/editConfig_create.xml"), RPC_REPLY_OK);
268 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_create_n1_control.xml"));
272 edit("messages/mapping/editConfigs/editConfig_create.xml");
273 fail("Create should have failed - data already exists");
274 } catch (NetconfDocumentedException e) {
275 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
276 assertTrue(e.getErrorTag() == ErrorTag.data_exists);
277 assertTrue(e.getErrorType() == ErrorType.protocol);
280 verifyResponse(discardChanges(), RPC_REPLY_OK);
285 public void testDeleteNonExisting() throws Exception {
287 assertEmptyDatastore(getConfigCandidate());
288 assertEmptyDatastore(getConfigRunning());
291 edit("messages/mapping/editConfigs/editConfig_delete-top.xml");
292 fail("Delete should have failed - data is missing");
293 } catch (NetconfDocumentedException e) {
294 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
295 assertTrue(e.getErrorTag() == ErrorTag.data_missing);
296 assertTrue(e.getErrorType() == ErrorType.protocol);
302 public void testEditMissingDefaultOperation() throws Exception {
304 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_1.xml"), RPC_REPLY_OK);
305 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_2.xml"), RPC_REPLY_OK);
306 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
308 verifyResponse(commit(), RPC_REPLY_OK);
309 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
314 public static void printDocument(Document doc) throws IOException, TransformerException {
315 TransformerFactory tf = TransformerFactory.newInstance();
316 Transformer transformer = tf.newTransformer();
317 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
318 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
319 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
320 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
321 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
323 StringWriter writer = new StringWriter();
324 transformer.transform(new DOMSource(doc),
325 new StreamResult(writer));
326 LOG.warn(writer.getBuffer().toString());
330 public void testEditConfigWithMultipleOperations() throws Exception {
333 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_setup.xml"), RPC_REPLY_OK);
334 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_1.xml"), RPC_REPLY_OK);
336 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_2.xml"), RPC_REPLY_OK);
337 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_2_control.xml"));
339 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_leaf_operations.xml"), RPC_REPLY_OK);
340 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_control.xml"));
344 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_setup.xml"), RPC_REPLY_OK);
345 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_default-replace.xml"), RPC_REPLY_OK);
348 edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_create_existing.xml");
350 } catch (NetconfDocumentedException e) {
351 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
352 assertTrue(e.getErrorTag() == ErrorTag.data_exists);
353 assertTrue(e.getErrorType() == ErrorType.protocol);
356 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete_children_operations.xml"), RPC_REPLY_OK);
357 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
358 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_remove-non-existing.xml"), RPC_REPLY_OK);
361 edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete-non-existing.xml");
363 } catch (NetconfDocumentedException e) {
364 assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
365 assertTrue(e.getErrorTag() == ErrorTag.data_missing);
366 assertTrue(e.getErrorType() == ErrorType.protocol);
369 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup.xml"), RPC_REPLY_OK);
370 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup-control.xml"));
372 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2.xml"), RPC_REPLY_OK);
373 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2-control.xml"));
375 verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_delete.xml"), RPC_REPLY_OK);
376 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
382 public void testFiltering() throws Exception {
384 assertEmptyDatastore(getConfigCandidate());
385 assertEmptyDatastore(getConfigRunning());
387 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
388 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
389 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-users.xml"),
390 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
392 verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup.xml"), RPC_REPLY_OK);
393 verifyResponse(commit(), RPC_REPLY_OK);
395 //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
396 // verifyFilterIdentifier("messages/mapping/filters/get-filter-alluser.xml",
397 // YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
398 verifyFilterIdentifier("messages/mapping/filters/get-filter-company-info.xml",
399 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
400 verifyFilterIdentifier("messages/mapping/filters/get-filter-modules-and-admin.xml",
401 YangInstanceIdentifier.builder().node(TOP).build());
402 verifyFilterIdentifier("messages/mapping/filters/get-filter-only-names-types.xml",
403 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
404 // verifyFilterIdentifier("messages/mapping/filters/get-filter-specific-module-type-and-user.xml",
405 // YangInstanceIdentifier.builder().node(TOP).build());
406 // verifyFilterIdentifier("messages/mapping/filters/get-filter-superuser.xml",
407 // YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
408 verifyFilterIdentifier("messages/mapping/filters/get-filter-users.xml",
409 YangInstanceIdentifier.builder().node(TOP).node(USERS).build());
411 YangInstanceIdentifier ident = YangInstanceIdentifier.
412 builder(AUGMENTED_CONTAINER_IN_MODULES).
413 node(AUGMENTED_CONTAINER).
414 node(AUGMENTED_STRING_IN_CONT).build();
416 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-string.xml", ident);
417 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case.xml",
418 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(AUGMENTED_CASE).build());
420 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case.xml"),
421 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case.xml"));
422 verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup2.xml"), RPC_REPLY_OK);
423 verifyResponse(commit(), RPC_REPLY_OK);
425 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml",
426 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).build());
427 verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-case.xml",
428 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).node(INNER_CHOICE).node(INNER_CHOICE_TEXT).build());
430 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-string.xml"),
431 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-string.xml"));
432 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml"),
433 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
434 verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-case.xml"),
435 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
437 verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-top.xml"), RPC_REPLY_OK);
438 verifyResponse(commit(), RPC_REPLY_OK);
442 private void verifyFilterIdentifier(String resource, YangInstanceIdentifier identifier) throws Exception{
443 TestingGetConfig getConfig = new TestingGetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
444 Document request = XmlFileLoader.xmlFileToDocument(resource);
445 YangInstanceIdentifier iid = getConfig.getInstanceIdentifierFromDocument(request);
446 assertTrue(iid.equals(identifier));
449 private class TestingGetConfig extends GetConfig{
450 public TestingGetConfig(String sessionId, CurrentSchemaContext schemaContext, TransactionProvider transactionProvider) {
451 super(sessionId, schemaContext, transactionProvider);
454 public YangInstanceIdentifier getInstanceIdentifierFromDocument(Document request) throws NetconfDocumentedException {
455 XmlElement filterElement = XmlElement.fromDomDocument(request).getOnlyChildElement(GET_CONFIG).getOnlyChildElement(FILTER_NODE);
456 return getInstanceIdentifierFromFilter(filterElement);
460 private void deleteDatastore() throws Exception{
461 verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-root.xml"), RPC_REPLY_OK);
462 assertEmptyDatastore(getConfigCandidate());
464 verifyResponse(commit(), RPC_REPLY_OK);
465 assertEmptyDatastore(getConfigRunning());
468 private void verifyResponse(Document response, Document template) throws IOException, TransformerException {
469 DetailedDiff dd = new DetailedDiff(new Diff(response, template));
470 dd.overrideElementQualifier(new NetconfXmlUnitRecursiveQualifier());
472 printDocument(response);
473 printDocument(template);
475 assertTrue(dd.toString(), dd.similar());
478 private void assertEmptyDatastore(Document response) {
480 NodeList nodes = response.getChildNodes();
481 assertTrue(nodes.getLength() == 1);
483 assertEquals(nodes.item(0).getLocalName(),RPC_REPLY_ELEMENT);
485 NodeList replyNodes = nodes.item(0).getChildNodes();
486 assertTrue(replyNodes.getLength() == 1);
488 Node dataNode = replyNodes.item(0);
489 assertEquals(dataNode.getLocalName(), DATA_ELEMENT);
490 assertFalse(dataNode.hasChildNodes());
494 private Document commit() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
495 Commit commit = new Commit(sessionIdForReporting, transactionProvider);
496 return executeOperation(commit, "messages/mapping/commit.xml");
499 private Document discardChanges() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
500 DiscardChanges discardOp = new DiscardChanges(sessionIdForReporting, transactionProvider);
501 return executeOperation(discardOp, "messages/mapping/discardChanges.xml");
504 private Document edit(String resource) throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
505 EditConfig editConfig = new EditConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
506 return executeOperation(editConfig, resource);
509 private Document get() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
510 Get get = new Get(sessionIdForReporting, currentSchemaContext, transactionProvider);
511 return executeOperation(get, "messages/mapping/get.xml");
514 private Document getConfigRunning() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
515 GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
516 return executeOperation(getConfig, "messages/mapping/getConfig.xml");
519 private Document getConfigCandidate() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
520 GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
521 return executeOperation(getConfig, "messages/mapping/getConfig_candidate.xml");
524 private Document getConfigWithFilter(String resource) throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
525 GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
526 return executeOperation(getConfig, resource);
529 private Document lock() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
530 Lock lock = new Lock(sessionIdForReporting);
531 return executeOperation(lock, "messages/mapping/lock.xml");
534 private Document unlock() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
535 Unlock unlock = new Unlock(sessionIdForReporting);
536 return executeOperation(unlock, "messages/mapping/unlock.xml");
539 private Document lockCandidate() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
540 Lock lock = new Lock(sessionIdForReporting);
541 return executeOperation(lock, "messages/mapping/lock_candidate.xml");
544 private Document unlockCandidate() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
545 Unlock unlock = new Unlock(sessionIdForReporting);
546 return executeOperation(unlock, "messages/mapping/unlock_candidate.xml");
549 private Document executeOperation(NetconfOperation op, String filename) throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
550 final Document request = XmlFileLoader.xmlFileToDocument(filename);
552 HandlingPriority priority = op.canHandle(request);
553 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
555 final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
557 LOG.debug("Got response {}" , response);
562 private Collection<InputStream> getYangSchemas() {
563 final List<String> schemaPaths = Arrays.asList("/META-INF/yang/config.yang", "/yang/mdsal-netconf-mapping-test.yang");
564 final List<InputStream> schemas = new ArrayList<>();
566 for (String schemaPath : schemaPaths) {
567 InputStream resourceAsStream = getClass().getResourceAsStream(schemaPath);
568 schemas.add(resourceAsStream);
574 private SchemaContext parseSchemas(Collection<InputStream> schemas) throws IOException, YangSyntaxErrorException {
575 final YangParserImpl parser = new YangParserImpl();
576 Collection<ByteSource> sources = BuilderUtils.streamsToByteSources(schemas);
577 return parser.parseSources(sources);
580 private SchemaService createSchemaService() {
581 return new SchemaService() {
584 public void addModule(Module module) {
588 public void removeModule(Module module) {
593 public SchemaContext getSessionContext() {
594 return schemaContext;
598 public SchemaContext getGlobalContext() {
599 return schemaContext;
603 public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(final SchemaContextListener listener) {
604 listener.onGlobalContextUpdated(getGlobalContext());
605 return new ListenerRegistration<SchemaContextListener>() {
607 public void close() {
612 public SchemaContextListener getInstance() {