2 * Copyright (c) 2018 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
8 package org.opendaylight.netconf.mdsal.connector.ops;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThrows;
12 import static org.junit.Assert.assertTrue;
15 import java.io.FileInputStream;
16 import java.net.MalformedURLException;
18 import org.junit.Rule;
19 import org.junit.Test;
20 import org.junit.rules.TemporaryFolder;
21 import org.opendaylight.netconf.api.DocumentedException;
22 import org.opendaylight.netconf.api.xml.XmlUtil;
23 import org.opendaylight.netconf.test.util.XmlFileLoader;
24 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
25 import org.opendaylight.yangtools.yang.common.ErrorTag;
26 import org.opendaylight.yangtools.yang.common.ErrorType;
27 import org.w3c.dom.Document;
28 import org.xml.sax.SAXException;
30 public class CopyConfigTest extends AbstractNetconfOperationTest {
32 public TemporaryFolder tmpDir = new TemporaryFolder();
35 public void testTargetMissing() {
36 final DocumentedException e = assertThrows(DocumentedException.class,
37 () -> copyConfig("messages/mapping/copyConfigs/copyConfig_no_target.xml"));
39 assertEquals(e.getErrorSeverity(),ErrorSeverity.ERROR);
40 assertEquals(e.getErrorTag(), ErrorTag.MISSING_ATTRIBUTE);
41 assertEquals(e.getErrorType(), ErrorType.PROTOCOL);
45 public void testSourceMissing() {
46 final DocumentedException e = assertThrows(DocumentedException.class,
47 () -> copyConfig("messages/mapping/copyConfigs/copyConfig_no_source.xml"));
49 assertEquals(e.getErrorSeverity(), ErrorSeverity.ERROR);
50 assertEquals(e.getErrorTag(), ErrorTag.MISSING_ELEMENT);
51 assertEquals(e.getErrorType(), ErrorType.PROTOCOL);
55 public void testConfigMissing() {
56 final DocumentedException e = assertThrows(DocumentedException.class,
57 () -> copyConfig("messages/mapping/copyConfigs/copyConfig_no_config.xml"));
59 assertEquals(e.getErrorSeverity(), ErrorSeverity.ERROR);
60 assertEquals(e.getErrorTag(), ErrorTag.MISSING_ELEMENT);
61 assertEquals(e.getErrorType(), ErrorType.PROTOCOL);
65 public void testRunning() {
66 final DocumentedException e = assertThrows(DocumentedException.class,
67 () -> copyConfig("messages/mapping/copyConfigs/copyConfig_running.xml"));
69 assertEquals(e.getErrorSeverity(), ErrorSeverity.ERROR);
70 assertEquals(e.getErrorTag(), ErrorTag.OPERATION_NOT_SUPPORTED);
71 assertEquals(e.getErrorType(), ErrorType.PROTOCOL);
75 public void testCandidateTransaction() throws Exception {
76 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_modules.xml"), RPC_REPLY_OK);
77 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
78 "messages/mapping/copyConfigs/copyConfig_top_modules_control.xml"));
79 assertEmptyDatastore(getConfigRunning());
81 verifyResponse(discardChanges(), RPC_REPLY_OK);
82 assertEmptyDatastore(getConfigCandidate());
86 public void testWithCommit() throws Exception {
87 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_modules.xml"), RPC_REPLY_OK);
88 final Document expectedConfig = XmlFileLoader.xmlFileToDocument(
89 "messages/mapping/copyConfigs/copyConfig_top_modules_control.xml");
90 verifyResponse(getConfigCandidate(), expectedConfig);
92 verifyResponse(commit(), RPC_REPLY_OK);
93 verifyResponse(getConfigRunning(), expectedConfig);
97 public void testDeleteSubtree() throws Exception {
98 // Initialize datastore
99 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_delete_setup.xml"), RPC_REPLY_OK);
100 verifyResponse(commit(), RPC_REPLY_OK);
101 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
102 "messages/mapping/copyConfigs/copyConfig_delete_setup_control.xml"));
104 // Issue second copy-config, this time without top container
105 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_delete.xml"), RPC_REPLY_OK);
106 verifyResponse(commit(), RPC_REPLY_OK);
107 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
108 "messages/mapping/copyConfigs/copyConfig_delete_control.xml"));
112 public void testList() throws Exception {
113 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_list_setup.xml"), RPC_REPLY_OK);
114 verifyResponse(commit(), RPC_REPLY_OK);
115 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
116 "messages/mapping/copyConfigs/copyConfig_list_setup_control.xml"));
118 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_list_update.xml"), RPC_REPLY_OK);
119 verifyResponse(commit(), RPC_REPLY_OK);
120 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
121 "messages/mapping/copyConfigs/copyConfig_list_update_control.xml"));
125 public void testOrderedList() throws Exception {
126 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_ordered_list_setup.xml"),
128 verifyResponse(commit(), RPC_REPLY_OK);
129 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
130 "messages/mapping/copyConfigs/copyConfig_ordered_list_setup_control.xml"));
132 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_ordered_list_update.xml"),
134 verifyResponse(commit(), RPC_REPLY_OK);
135 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
136 "messages/mapping/copyConfigs/copyConfig_ordered_list_update_control.xml"));
140 public void testToplevelList() throws Exception {
141 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_toplevel_list_setup.xml"),
143 verifyResponse(commit(), RPC_REPLY_OK);
144 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
145 "messages/mapping/copyConfigs/copyConfig_toplevel_list_setup_control.xml"));
147 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_toplevel_list_update.xml"),
149 verifyResponse(commit(), RPC_REPLY_OK);
150 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
151 "messages/mapping/copyConfigs/copyConfig_toplevel_list_update_control.xml"));
155 public void testEmptyContainer() throws Exception {
156 // Check that empty non-presence container is removed.
157 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_empty_container.xml"),
159 verifyResponse(commit(), RPC_REPLY_OK);
160 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
161 "messages/mapping/copyConfigs/copyConfig_empty_container_control.xml"));
165 public void testEmptyPresenceContainer() throws Exception {
166 // Check that empty presence container is not removed.
167 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_empty_presence_container.xml"),
169 verifyResponse(commit(), RPC_REPLY_OK);
170 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
171 "messages/mapping/copyConfigs/copyConfig_empty_presence_container_control.xml"));
175 public void testAugmentations() throws Exception {
176 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_augmentation.xml"),
178 verifyResponse(commit(), RPC_REPLY_OK);
179 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
180 "messages/mapping/copyConfigs/copyConfig_top_augmentation_control.xml"));
184 public void testChoices() throws Exception {
185 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_choices1.xml"), RPC_REPLY_OK);
186 verifyResponse(commit(), RPC_REPLY_OK);
187 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_choices2.xml"), RPC_REPLY_OK);
188 verifyResponse(commit(), RPC_REPLY_OK);
189 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_choices3.xml"), RPC_REPLY_OK);
190 verifyResponse(commit(), RPC_REPLY_OK);
191 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_choices4.xml"), RPC_REPLY_OK);
192 verifyResponse(commit(), RPC_REPLY_OK);
193 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
194 "messages/mapping/copyConfigs/copyConfig_choices_control.xml"));
198 public void testConfigFromFile() throws Exception {
199 // Ask class loader for URI of config file and use it as <url> in <copy-config> RPC:
200 final String template = XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_from_file.xml");
201 final URI uri = getClass().getClassLoader()
202 .getResource("messages/mapping/copyConfigs/config_file_valid.xml").toURI();
203 final String copyConfig = template.replaceFirst("URL", uri.toString());
204 final Document request = XmlUtil.readXmlToDocument(copyConfig);
206 verifyResponse(copyConfig(request), RPC_REPLY_OK);
207 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
208 "messages/mapping/copyConfigs/copyConfig_from_file_control.xml"));
212 public void testConfigFromInvalidUrl() {
213 final DocumentedException e = assertThrows(DocumentedException.class,
214 () -> copyConfig("messages/mapping/copyConfigs/copyConfig_invalid_url.xml"));
216 assertEquals(e.getErrorSeverity(), ErrorSeverity.ERROR);
217 assertEquals(e.getErrorTag(), ErrorTag.INVALID_VALUE);
218 assertEquals(e.getErrorType(), ErrorType.APPLICATION);
219 assertTrue(e.getCause() instanceof MalformedURLException);
223 public void testExternalConfigInvalid() throws Exception {
224 // Ask class loader for URI of config file and use it as <url> in <copy-config> RPC:
225 final String template = XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_from_file.xml");
226 final URI uri = getClass().getClassLoader()
227 .getResource("messages/mapping/copyConfigs/config_file_invalid.xml").toURI();
228 final String copyConfig = template.replaceFirst("URL", uri.toString());
229 final Document request = XmlUtil.readXmlToDocument(copyConfig);
231 final DocumentedException e = assertThrows(DocumentedException.class,
232 () -> copyConfig(request));
234 assertEquals(e.getErrorSeverity(), ErrorSeverity.ERROR);
235 assertEquals(e.getErrorTag(), ErrorTag.OPERATION_FAILED);
236 assertEquals(e.getErrorType(), ErrorType.APPLICATION);
237 assertTrue(e.getCause() instanceof SAXException);
241 public void testCopyToFile() throws Exception {
242 // Initialize config:
243 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_modules.xml"), RPC_REPLY_OK);
244 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
245 "messages/mapping/copyConfigs/copyConfig_top_modules_control.xml"));
247 // Load copy-config template and replace URL with the URI of target file:
248 final String template = XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_to_file.xml");
249 final File outFile = new File(tmpDir.getRoot(),"test-copy-to-file.xml");
250 final String copyConfig = template.replaceFirst("URL", outFile.toURI().toString());
251 final Document request = XmlUtil.readXmlToDocument(copyConfig);
253 // Invoke copy-config RPC:
254 verifyResponse(copyConfig(request), RPC_REPLY_OK);
256 // Check if outFile was created with expected content:
257 verifyResponse(XmlUtil.readXmlToDocument(new FileInputStream(outFile)),
258 XmlFileLoader.xmlFileToDocument("messages/mapping/copyConfigs/copyConfig_to_file_control.xml"));
262 public void testUnsupportedTargetUrlProtocol() {
263 final DocumentedException e = assertThrows(DocumentedException.class,
264 () -> copyConfig("messages/mapping/copyConfigs/copyConfig_to_unsupported_url_protocol.xml"));
266 assertEquals(e.getErrorSeverity(), ErrorSeverity.ERROR);
267 assertEquals(e.getErrorTag(), ErrorTag.OPERATION_NOT_SUPPORTED);
268 assertEquals(e.getErrorType(), ErrorType.PROTOCOL);
272 public void testCopyToFileFromRunning() throws Exception {
273 // Load copy-config template and replace URL with the URI of target file:
274 final String template =
275 XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_to_file_from_running.xml");
276 final File outFile = new File(tmpDir.getRoot(),"test-copy-to-file-from-running.xml");
277 final String copyConfig = template.replaceFirst("URL", outFile.toURI().toString());
278 final Document request = XmlUtil.readXmlToDocument(copyConfig);
280 // Invoke copy-config RPC:
281 verifyResponse(copyConfig(request), RPC_REPLY_OK);
283 // Check if outFile was created with expected content:
284 verifyResponse(XmlUtil.readXmlToDocument(new FileInputStream(outFile)),
285 XmlFileLoader.xmlFileToDocument(
286 "messages/mapping/copyConfigs/copyConfig_to_file_from_running_control.xml"));
291 public void testRemoteToRemoteOperationIsNotSupported() {
292 final DocumentedException e = assertThrows(DocumentedException.class,
293 () -> copyConfig("messages/mapping/copyConfigs/copyConfig_url_remote_to_remote.xml"));
295 assertEquals(e.getErrorSeverity(), ErrorSeverity.ERROR);
296 assertEquals(e.getErrorTag(), ErrorTag.OPERATION_NOT_SUPPORTED);
297 assertEquals(e.getErrorType(), ErrorType.PROTOCOL);
300 private Document copyConfig(final String resource) throws Exception {
301 final CopyConfig copyConfig = new CopyConfig(SESSION_ID_FOR_REPORTING, getCurrentSchemaContext(),
302 getTransactionProvider());
303 return executeOperation(copyConfig, resource);
306 private Document copyConfig(final Document request) throws Exception {
307 final CopyConfig copyConfig = new CopyConfig(SESSION_ID_FOR_REPORTING, getCurrentSchemaContext(),
308 getTransactionProvider());
309 return executeOperation(copyConfig, request);