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.ErrorSeverity;
22 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;
23 import org.opendaylight.netconf.api.DocumentedException.ErrorType;
24 import org.opendaylight.netconf.api.xml.XmlUtil;
25 import org.opendaylight.netconf.util.test.XmlFileLoader;
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 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
40 assertTrue(e.getErrorTag() == ErrorTag.MISSING_ATTRIBUTE);
41 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
46 public void testSourceMissing() throws Exception {
48 copyConfig("messages/mapping/copyConfigs/copyConfig_no_source.xml");
49 fail("Should have fanode1iled - <source> element is missing");
50 } catch (final DocumentedException e) {
51 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
52 assertTrue(e.getErrorTag() == ErrorTag.MISSING_ELEMENT);
53 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
58 public void testConfigMissing() throws Exception {
60 copyConfig("messages/mapping/copyConfigs/copyConfig_no_config.xml");
61 fail("Should have failed - neither <config> nor <url> element is present");
62 } catch (final DocumentedException e) {
63 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
64 assertTrue(e.getErrorTag() == ErrorTag.MISSING_ELEMENT);
65 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
70 public void testRunning() throws Exception {
72 copyConfig("messages/mapping/copyConfigs/copyConfig_running.xml");
73 fail("Should have failed - copy config on running datastore is not supported");
74 } catch (final DocumentedException e) {
75 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
76 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
77 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
82 public void testCandidateTransaction() throws Exception {
83 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_modules.xml"), RPC_REPLY_OK);
84 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
85 "messages/mapping/copyConfigs/copyConfig_top_modules_control.xml"));
86 assertEmptyDatastore(getConfigRunning());
88 verifyResponse(discardChanges(), RPC_REPLY_OK);
89 assertEmptyDatastore(getConfigCandidate());
93 public void testWithCommit() throws Exception {
94 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_modules.xml"), RPC_REPLY_OK);
95 final Document expectedConfig = XmlFileLoader.xmlFileToDocument(
96 "messages/mapping/copyConfigs/copyConfig_top_modules_control.xml");
97 verifyResponse(getConfigCandidate(), expectedConfig);
99 verifyResponse(commit(), RPC_REPLY_OK);
100 verifyResponse(getConfigRunning(), expectedConfig);
104 public void testDeleteSubtree() throws Exception {
105 // Initialize datastore
106 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_delete_setup.xml"), RPC_REPLY_OK);
107 verifyResponse(commit(), RPC_REPLY_OK);
108 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
109 "messages/mapping/copyConfigs/copyConfig_delete_setup_control.xml"));
111 // Issue second copy-config, this time without top container
112 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_delete.xml"), RPC_REPLY_OK);
113 verifyResponse(commit(), RPC_REPLY_OK);
114 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
115 "messages/mapping/copyConfigs/copyConfig_delete_control.xml"));
119 public void testList() throws Exception {
120 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_list_setup.xml"), RPC_REPLY_OK);
121 verifyResponse(commit(), RPC_REPLY_OK);
122 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
123 "messages/mapping/copyConfigs/copyConfig_list_setup_control.xml"));
125 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_list_update.xml"), RPC_REPLY_OK);
126 verifyResponse(commit(), RPC_REPLY_OK);
127 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
128 "messages/mapping/copyConfigs/copyConfig_list_update_control.xml"));
132 public void testOrderedList() throws Exception {
133 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_ordered_list_setup.xml"),
135 verifyResponse(commit(), RPC_REPLY_OK);
136 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
137 "messages/mapping/copyConfigs/copyConfig_ordered_list_setup_control.xml"));
139 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_ordered_list_update.xml"),
141 verifyResponse(commit(), RPC_REPLY_OK);
142 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
143 "messages/mapping/copyConfigs/copyConfig_ordered_list_update_control.xml"));
147 public void testToplevelList() throws Exception {
148 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_toplevel_list_setup.xml"),
150 verifyResponse(commit(), RPC_REPLY_OK);
151 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
152 "messages/mapping/copyConfigs/copyConfig_toplevel_list_setup_control.xml"));
154 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_toplevel_list_update.xml"),
156 verifyResponse(commit(), RPC_REPLY_OK);
157 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
158 "messages/mapping/copyConfigs/copyConfig_toplevel_list_update_control.xml"));
162 public void testEmptyContainer() throws Exception {
163 // Check that empty non-presence container is removed.
164 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_empty_container.xml"),
166 verifyResponse(commit(), RPC_REPLY_OK);
167 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
168 "messages/mapping/copyConfigs/copyConfig_empty_container_control.xml"));
172 public void testEmptyPresenceContainer() throws Exception {
173 // Check that empty presence container is not removed.
174 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_empty_presence_container.xml"),
176 verifyResponse(commit(), RPC_REPLY_OK);
177 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
178 "messages/mapping/copyConfigs/copyConfig_empty_presence_container_control.xml"));
182 public void testAugmentations() throws Exception {
183 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_augmentation.xml"),
185 verifyResponse(commit(), RPC_REPLY_OK);
186 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
187 "messages/mapping/copyConfigs/copyConfig_top_augmentation_control.xml"));
191 public void testChoices() throws Exception {
192 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_choices1.xml"), RPC_REPLY_OK);
193 verifyResponse(commit(), RPC_REPLY_OK);
194 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_choices2.xml"), RPC_REPLY_OK);
195 verifyResponse(commit(), RPC_REPLY_OK);
196 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_choices3.xml"), RPC_REPLY_OK);
197 verifyResponse(commit(), RPC_REPLY_OK);
198 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_choices4.xml"), RPC_REPLY_OK);
199 verifyResponse(commit(), RPC_REPLY_OK);
200 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
201 "messages/mapping/copyConfigs/copyConfig_choices_control.xml"));
205 public void testConfigFromFile() throws Exception {
206 // Ask class loader for URI of config file and use it as <url> in <copy-config> RPC:
207 final String template = XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_from_file.xml");
208 final URI uri = getClass().getClassLoader()
209 .getResource("messages/mapping/copyConfigs/config_file_valid.xml").toURI();
210 final String copyConfig = template.replaceFirst("URL", uri.toString());
211 final Document request = XmlUtil.readXmlToDocument(copyConfig);
213 verifyResponse(copyConfig(request), RPC_REPLY_OK);
214 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
215 "messages/mapping/copyConfigs/copyConfig_from_file_control.xml"));
219 public void testConfigFromInvalidUrl() throws Exception {
221 copyConfig("messages/mapping/copyConfigs/copyConfig_invalid_url.xml");
222 fail("Should have failed - provided <url> is not valid");
223 } catch (final DocumentedException e) {
224 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
225 assertTrue(e.getErrorTag() == ErrorTag.INVALID_VALUE);
226 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
227 assertTrue(e.getCause() instanceof MalformedURLException);
232 public void testExternalConfigInvalid() throws Exception {
234 // Ask class loader for URI of config file and use it as <url> in <copy-config> RPC:
235 final String template = XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_from_file.xml");
236 final URI uri = getClass().getClassLoader()
237 .getResource("messages/mapping/copyConfigs/config_file_invalid.xml").toURI();
238 final String copyConfig = template.replaceFirst("URL", uri.toString());
239 final Document request = XmlUtil.readXmlToDocument(copyConfig);
241 fail("Should have failed - provided config is not valid XML");
242 } catch (final DocumentedException e) {
243 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
244 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_FAILED);
245 assertTrue(e.getErrorType() == ErrorType.APPLICATION);
246 assertTrue(e.getCause() instanceof SAXException);
251 public void testCopyToFile() throws Exception {
252 // Initialize config:
253 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_modules.xml"), RPC_REPLY_OK);
254 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
255 "messages/mapping/copyConfigs/copyConfig_top_modules_control.xml"));
257 // Load copy-config template and replace URL with the URI of target file:
258 final String template = XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_to_file.xml");
259 final File outFile = new File(tmpDir.getRoot(),"test-copy-to-file.xml");
260 final String copyConfig = template.replaceFirst("URL", outFile.toURI().toString());
261 final Document request = XmlUtil.readXmlToDocument(copyConfig);
263 // Invoke copy-config RPC:
264 verifyResponse(copyConfig(request), RPC_REPLY_OK);
266 // Check if outFile was created with expected content:
267 verifyResponse(XmlUtil.readXmlToDocument(new FileInputStream(outFile)),
268 XmlFileLoader.xmlFileToDocument("messages/mapping/copyConfigs/copyConfig_to_file_control.xml"));
272 public void testUnsupportedTargetUrlProtocol() throws Exception {
274 copyConfig("messages/mapping/copyConfigs/copyConfig_to_unsupported_url_protocol.xml");
275 fail("Should have failed - exporting config to http server is not supported");
276 } catch (final DocumentedException e) {
277 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
278 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
279 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
284 public void testCopyToFileFromRunning() throws Exception {
285 // Load copy-config template and replace URL with the URI of target file:
286 final String template =
287 XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_to_file_from_running.xml");
288 final File outFile = new File(tmpDir.getRoot(),"test-copy-to-file-from-running.xml");
289 final String copyConfig = template.replaceFirst("URL", outFile.toURI().toString());
290 final Document request = XmlUtil.readXmlToDocument(copyConfig);
292 // Invoke copy-config RPC:
293 verifyResponse(copyConfig(request), RPC_REPLY_OK);
295 // Check if outFile was created with expected content:
296 verifyResponse(XmlUtil.readXmlToDocument(new FileInputStream(outFile)),
297 XmlFileLoader.xmlFileToDocument(
298 "messages/mapping/copyConfigs/copyConfig_to_file_from_running_control.xml"));
303 public void testRemoteToRemoteOperationIsNotSupported() throws Exception {
305 copyConfig("messages/mapping/copyConfigs/copyConfig_url_remote_to_remote.xml");
306 fail("Should have failed - remote to remote operations are not supported");
307 } catch (final DocumentedException e) {
308 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
309 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
310 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
314 private Document copyConfig(final String resource) throws Exception {
315 final CopyConfig copyConfig = new CopyConfig(SESSION_ID_FOR_REPORTING, getCurrentSchemaContext(),
316 getTransactionProvider());
317 return executeOperation(copyConfig, resource);
320 private Document copyConfig(final Document request) throws Exception {
321 final CopyConfig copyConfig = new CopyConfig(SESSION_ID_FOR_REPORTING, getCurrentSchemaContext(),
322 getTransactionProvider());
323 return executeOperation(copyConfig, request);