14c177b80616848cf8946c6703249f67eecf8b36
[netconf.git] / netconf / mdsal-netconf-connector / src / test / java / org / opendaylight / netconf / mdsal / connector / ops / CopyConfigTest.java
1 /*
2  * Copyright (c) 2018 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.mdsal.connector.ops;
10
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13 import static org.opendaylight.yangtools.yang.test.util.YangParserTestUtils.parseYangResources;
14
15 import org.junit.Test;
16 import org.opendaylight.netconf.api.DocumentedException;
17 import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;
18 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;
19 import org.opendaylight.netconf.api.DocumentedException.ErrorType;
20 import org.opendaylight.netconf.util.test.XmlFileLoader;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.w3c.dom.Document;
23
24 public class CopyConfigTest extends AbstractNetconfOperationTest {
25
26     @Override
27     protected SchemaContext getSchemaContext() {
28         return parseYangResources(CopyConfigTest.class,
29             "/yang/mdsal-netconf-mapping-test.yang");
30     }
31
32
33     @Test
34     public void testTargetMissing() throws Exception {
35         try {
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);
42         }
43     }
44
45     @Test
46     public void testSourceMissing() throws Exception {
47         try {
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);
54         }
55     }
56
57     @Test
58     public void testConfigMissing() throws Exception {
59         try {
60             copyConfig("messages/mapping/copyConfigs/copyConfig_no_config.xml");
61             fail("Should have failed - <config> element is missing");
62         } catch (final DocumentedException e) {
63             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
64             assertTrue(e.getErrorTag() == ErrorTag.MISSING_ELEMENT);
65             assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
66         }
67     }
68
69     @Test
70     public void testRunning() throws Exception {
71         try {
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);
78         }
79     }
80
81     @Test
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());
87
88         verifyResponse(discardChanges(), RPC_REPLY_OK);
89         assertEmptyDatastore(getConfigCandidate());
90     }
91
92     @Test
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);
98
99         verifyResponse(commit(), RPC_REPLY_OK);
100         verifyResponse(getConfigRunning(), expectedConfig);
101     }
102
103     @Test
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"));
110
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"));
116     }
117
118     @Test
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"));
124
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"));
129     }
130
131     @Test
132     public void testOrderedList() throws Exception {
133         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_ordered_list_setup.xml"),
134             RPC_REPLY_OK);
135         verifyResponse(commit(), RPC_REPLY_OK);
136         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
137             "messages/mapping/copyConfigs/copyConfig_ordered_list_setup_control.xml"));
138
139         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_ordered_list_update.xml"),
140             RPC_REPLY_OK);
141         verifyResponse(commit(), RPC_REPLY_OK);
142         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
143             "messages/mapping/copyConfigs/copyConfig_ordered_list_update_control.xml"));
144     }
145
146     @Test
147     public void testToplevelList() throws Exception {
148         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_toplevel_list_setup.xml"),
149             RPC_REPLY_OK);
150         verifyResponse(commit(), RPC_REPLY_OK);
151         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
152             "messages/mapping/copyConfigs/copyConfig_toplevel_list_setup_control.xml"));
153
154         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_toplevel_list_update.xml"),
155             RPC_REPLY_OK);
156         verifyResponse(commit(), RPC_REPLY_OK);
157         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
158             "messages/mapping/copyConfigs/copyConfig_toplevel_list_update_control.xml"));
159     }
160
161     @Test
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"),
165             RPC_REPLY_OK);
166         verifyResponse(commit(), RPC_REPLY_OK);
167         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
168             "messages/mapping/copyConfigs/copyConfig_empty_container_control.xml"));
169     }
170
171     @Test
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"),
175             RPC_REPLY_OK);
176         verifyResponse(commit(), RPC_REPLY_OK);
177         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
178             "messages/mapping/copyConfigs/copyConfig_empty_presence_container_control.xml"));
179     }
180
181     @Test
182     public void testAugmentations() throws Exception {
183         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_augmentation.xml"),
184             RPC_REPLY_OK);
185         verifyResponse(commit(), RPC_REPLY_OK);
186         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
187             "messages/mapping/copyConfigs/copyConfig_top_augmentation_control.xml"));
188     }
189
190     @Test
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"));
202     }
203
204     private Document copyConfig(final String resource) throws Exception {
205         final CopyConfig copyConfig = new CopyConfig(SESSION_ID_FOR_REPORTING, getCurrentSchemaContext(),
206             getTransactionProvider());
207         return executeOperation(copyConfig, resource);
208     }
209 }