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.assertTrue;
11 import static org.junit.Assert.fail;
14 import java.io.FileInputStream;
15 import java.net.MalformedURLException;
17 import org.junit.Rule;
18 import org.junit.Test;
19 import org.junit.rules.TemporaryFolder;
20 import org.opendaylight.netconf.api.DocumentedException;
21 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;
22 import org.opendaylight.netconf.api.xml.XmlUtil;
23 import org.opendaylight.netconf.util.test.XmlFileLoader;
24 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
25 import org.opendaylight.yangtools.yang.common.ErrorType;
26 import org.w3c.dom.Document;
27 import org.xml.sax.SAXException;
29 public class CopyConfigTest extends AbstractNetconfOperationTest {
31 public TemporaryFolder tmpDir = new TemporaryFolder();
34 public void testTargetMissing() throws Exception {
36 copyConfig("messages/mapping/copyConfigs/copyConfig_no_target.xml");
37 fail("Should have failed - <target> element is missing");
38 } catch (final DocumentedException e) {
39 // FIXME: use assertThrows() and assertEquals()
40 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
41 assertTrue(e.getErrorTag() == ErrorTag.MISSING_ATTRIBUTE);
42 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
47 public void testSourceMissing() throws Exception {
49 copyConfig("messages/mapping/copyConfigs/copyConfig_no_source.xml");
50 fail("Should have fanode1iled - <source> element is missing");
51 } catch (final DocumentedException e) {
52 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
53 assertTrue(e.getErrorTag() == ErrorTag.MISSING_ELEMENT);
54 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
59 public void testConfigMissing() throws Exception {
61 copyConfig("messages/mapping/copyConfigs/copyConfig_no_config.xml");
62 fail("Should have failed - neither <config> nor <url> element is present");
63 } catch (final DocumentedException e) {
64 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
65 assertTrue(e.getErrorTag() == ErrorTag.MISSING_ELEMENT);
66 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
71 public void testRunning() throws Exception {
73 copyConfig("messages/mapping/copyConfigs/copyConfig_running.xml");
74 fail("Should have failed - copy config on running datastore is not supported");
75 } catch (final DocumentedException e) {
76 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
77 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
78 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
83 public void testCandidateTransaction() throws Exception {
84 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_modules.xml"), RPC_REPLY_OK);
85 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
86 "messages/mapping/copyConfigs/copyConfig_top_modules_control.xml"));
87 assertEmptyDatastore(getConfigRunning());
89 verifyResponse(discardChanges(), RPC_REPLY_OK);
90 assertEmptyDatastore(getConfigCandidate());
94 public void testWithCommit() throws Exception {
95 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_modules.xml"), RPC_REPLY_OK);
96 final Document expectedConfig = XmlFileLoader.xmlFileToDocument(
97 "messages/mapping/copyConfigs/copyConfig_top_modules_control.xml");
98 verifyResponse(getConfigCandidate(), expectedConfig);
100 verifyResponse(commit(), RPC_REPLY_OK);
101 verifyResponse(getConfigRunning(), expectedConfig);
105 public void testDeleteSubtree() throws Exception {
106 // Initialize datastore
107 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_delete_setup.xml"), RPC_REPLY_OK);
108 verifyResponse(commit(), RPC_REPLY_OK);
109 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
110 "messages/mapping/copyConfigs/copyConfig_delete_setup_control.xml"));
112 // Issue second copy-config, this time without top container
113 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_delete.xml"), RPC_REPLY_OK);
114 verifyResponse(commit(), RPC_REPLY_OK);
115 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
116 "messages/mapping/copyConfigs/copyConfig_delete_control.xml"));
120 public void testList() throws Exception {
121 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_list_setup.xml"), RPC_REPLY_OK);
122 verifyResponse(commit(), RPC_REPLY_OK);
123 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
124 "messages/mapping/copyConfigs/copyConfig_list_setup_control.xml"));
126 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_list_update.xml"), RPC_REPLY_OK);
127 verifyResponse(commit(), RPC_REPLY_OK);
128 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
129 "messages/mapping/copyConfigs/copyConfig_list_update_control.xml"));
133 public void testOrderedList() throws Exception {
134 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_ordered_list_setup.xml"),
136 verifyResponse(commit(), RPC_REPLY_OK);
137 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
138 "messages/mapping/copyConfigs/copyConfig_ordered_list_setup_control.xml"));
140 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_ordered_list_update.xml"),
142 verifyResponse(commit(), RPC_REPLY_OK);
143 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
144 "messages/mapping/copyConfigs/copyConfig_ordered_list_update_control.xml"));
148 public void testToplevelList() throws Exception {
149 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_toplevel_list_setup.xml"),
151 verifyResponse(commit(), RPC_REPLY_OK);
152 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
153 "messages/mapping/copyConfigs/copyConfig_toplevel_list_setup_control.xml"));
155 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_toplevel_list_update.xml"),
157 verifyResponse(commit(), RPC_REPLY_OK);
158 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
159 "messages/mapping/copyConfigs/copyConfig_toplevel_list_update_control.xml"));
163 public void testEmptyContainer() throws Exception {
164 // Check that empty non-presence container is removed.
165 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_empty_container.xml"),
167 verifyResponse(commit(), RPC_REPLY_OK);
168 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
169 "messages/mapping/copyConfigs/copyConfig_empty_container_control.xml"));
173 public void testEmptyPresenceContainer() throws Exception {
174 // Check that empty presence container is not removed.
175 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_empty_presence_container.xml"),
177 verifyResponse(commit(), RPC_REPLY_OK);
178 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
179 "messages/mapping/copyConfigs/copyConfig_empty_presence_container_control.xml"));
183 public void testAugmentations() throws Exception {
184 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_augmentation.xml"),
186 verifyResponse(commit(), RPC_REPLY_OK);
187 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
188 "messages/mapping/copyConfigs/copyConfig_top_augmentation_control.xml"));
192 public void testChoices() throws Exception {
193 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_choices1.xml"), RPC_REPLY_OK);
194 verifyResponse(commit(), RPC_REPLY_OK);
195 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_choices2.xml"), RPC_REPLY_OK);
196 verifyResponse(commit(), RPC_REPLY_OK);
197 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_choices3.xml"), RPC_REPLY_OK);
198 verifyResponse(commit(), RPC_REPLY_OK);
199 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_choices4.xml"), RPC_REPLY_OK);
200 verifyResponse(commit(), RPC_REPLY_OK);
201 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
202 "messages/mapping/copyConfigs/copyConfig_choices_control.xml"));
206 public void testConfigFromFile() throws Exception {
207 // Ask class loader for URI of config file and use it as <url> in <copy-config> RPC:
208 final String template = XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_from_file.xml");
209 final URI uri = getClass().getClassLoader()
210 .getResource("messages/mapping/copyConfigs/config_file_valid.xml").toURI();
211 final String copyConfig = template.replaceFirst("URL", uri.toString());
212 final Document request = XmlUtil.readXmlToDocument(copyConfig);
214 verifyResponse(copyConfig(request), RPC_REPLY_OK);
215 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
216 "messages/mapping/copyConfigs/copyConfig_from_file_control.xml"));
220 public void testConfigFromInvalidUrl() throws Exception {
222 copyConfig("messages/mapping/copyConfigs/copyConfig_invalid_url.xml");
223 fail("Should have failed - provided <url> is not valid");
224 } catch (final DocumentedException e) {
225 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
226 assertTrue(e.getErrorTag() == ErrorTag.INVALID_VALUE);
227 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
228 assertTrue(e.getCause() instanceof MalformedURLException);
233 public void testExternalConfigInvalid() throws Exception {
235 // Ask class loader for URI of config file and use it as <url> in <copy-config> RPC:
236 final String template = XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_from_file.xml");
237 final URI uri = getClass().getClassLoader()
238 .getResource("messages/mapping/copyConfigs/config_file_invalid.xml").toURI();
239 final String copyConfig = template.replaceFirst("URL", uri.toString());
240 final Document request = XmlUtil.readXmlToDocument(copyConfig);
242 fail("Should have failed - provided config is not valid XML");
243 } catch (final DocumentedException e) {
244 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
245 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_FAILED);
246 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
247 assertTrue(e.getCause() instanceof SAXException);
252 public void testCopyToFile() throws Exception {
253 // Initialize config:
254 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_modules.xml"), RPC_REPLY_OK);
255 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
256 "messages/mapping/copyConfigs/copyConfig_top_modules_control.xml"));
258 // Load copy-config template and replace URL with the URI of target file:
259 final String template = XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_to_file.xml");
260 final File outFile = new File(tmpDir.getRoot(),"test-copy-to-file.xml");
261 final String copyConfig = template.replaceFirst("URL", outFile.toURI().toString());
262 final Document request = XmlUtil.readXmlToDocument(copyConfig);
264 // Invoke copy-config RPC:
265 verifyResponse(copyConfig(request), RPC_REPLY_OK);
267 // Check if outFile was created with expected content:
268 verifyResponse(XmlUtil.readXmlToDocument(new FileInputStream(outFile)),
269 XmlFileLoader.xmlFileToDocument("messages/mapping/copyConfigs/copyConfig_to_file_control.xml"));
273 public void testUnsupportedTargetUrlProtocol() throws Exception {
275 copyConfig("messages/mapping/copyConfigs/copyConfig_to_unsupported_url_protocol.xml");
276 fail("Should have failed - exporting config to http server is not supported");
277 } catch (final DocumentedException e) {
278 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
279 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
280 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
285 public void testCopyToFileFromRunning() throws Exception {
286 // Load copy-config template and replace URL with the URI of target file:
287 final String template =
288 XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_to_file_from_running.xml");
289 final File outFile = new File(tmpDir.getRoot(),"test-copy-to-file-from-running.xml");
290 final String copyConfig = template.replaceFirst("URL", outFile.toURI().toString());
291 final Document request = XmlUtil.readXmlToDocument(copyConfig);
293 // Invoke copy-config RPC:
294 verifyResponse(copyConfig(request), RPC_REPLY_OK);
296 // Check if outFile was created with expected content:
297 verifyResponse(XmlUtil.readXmlToDocument(new FileInputStream(outFile)),
298 XmlFileLoader.xmlFileToDocument(
299 "messages/mapping/copyConfigs/copyConfig_to_file_from_running_control.xml"));
304 public void testRemoteToRemoteOperationIsNotSupported() throws Exception {
306 copyConfig("messages/mapping/copyConfigs/copyConfig_url_remote_to_remote.xml");
307 fail("Should have failed - remote to remote operations are not supported");
308 } catch (final DocumentedException e) {
309 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
310 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
311 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
315 private Document copyConfig(final String resource) throws Exception {
316 final CopyConfig copyConfig = new CopyConfig(SESSION_ID_FOR_REPORTING, getCurrentSchemaContext(),
317 getTransactionProvider());
318 return executeOperation(copyConfig, resource);
321 private Document copyConfig(final Document request) throws Exception {
322 final CopyConfig copyConfig = new CopyConfig(SESSION_ID_FOR_REPORTING, getCurrentSchemaContext(),
323 getTransactionProvider());
324 return executeOperation(copyConfig, request);