Unit Test for md-sal netconf northbound mapping.
[controller.git] / opendaylight / netconf / mdsal-netconf-connector / src / test / java / org / opendaylight / controller / netconf / mdsal / connector / ops / NetconfMDSalMappingTest.java
1 /*
2  * Copyright (c) 2015 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.controller.netconf.mdsal.connector.ops;
10
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;
15
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.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collection;
23 import java.util.EnumMap;
24 import java.util.List;
25 import java.util.concurrent.ExecutorService;
26 import javax.xml.parsers.ParserConfigurationException;
27 import org.custommonkey.xmlunit.DetailedDiff;
28 import org.custommonkey.xmlunit.Diff;
29 import org.custommonkey.xmlunit.XMLUnit;
30 import org.custommonkey.xmlunit.examples.RecursiveElementNameAndTextQualifier;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.opendaylight.controller.cluster.datastore.ConcurrentDOMDataBroker;
34 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
35 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStoreFactory;
36 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
37 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
38 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
39 import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
40 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
41 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
42 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
43 import org.opendaylight.controller.netconf.mdsal.connector.CurrentSchemaContext;
44 import org.opendaylight.controller.netconf.mdsal.connector.TransactionProvider;
45 import org.opendaylight.controller.netconf.mdsal.connector.ops.get.Get;
46 import org.opendaylight.controller.netconf.mdsal.connector.ops.get.GetConfig;
47 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
48 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
49 import org.opendaylight.controller.sal.core.api.model.SchemaService;
50 import org.opendaylight.controller.sal.core.spi.data.DOMStore;
51 import org.opendaylight.yangtools.concepts.ListenerRegistration;
52 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
53 import org.opendaylight.yangtools.yang.model.api.Module;
54 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
55 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
56 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
57 import org.opendaylight.yangtools.yang.parser.builder.impl.BuilderUtils;
58 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
61 import org.w3c.dom.Document;
62 import org.w3c.dom.Node;
63 import org.w3c.dom.NodeList;
64 import org.xml.sax.SAXException;
65
66 public class NetconfMDSalMappingTest {
67
68     private static final Logger LOG = LoggerFactory.getLogger(NetconfMDSalMappingTest.class);
69
70     private static final String RPC_REPLY_ELEMENT = "rpc-reply";
71     private static final String DATA_ELEMENT = "data";
72
73     private static Document RPC_REPLY_OK = null;
74
75     static {
76         try {
77             RPC_REPLY_OK = XmlFileLoader.xmlFileToDocument("messages/mapping/rpc-reply_ok.xml");
78         } catch (Exception e) {
79             LOG.debug("unable to load rpc reply ok.", e);
80             RPC_REPLY_OK = XmlUtil.newDocument();
81         }
82     }
83
84     private CurrentSchemaContext currentSchemaContext = null;
85     private SchemaContext schemaContext = null;
86     private String sessionIdForReporting = "netconf-test-session1";
87
88     private TransactionProvider transactionProvider = null;
89
90
91     @Before
92     public void setUp() throws Exception {
93
94         XMLUnit.setIgnoreWhitespace(true);
95         XMLUnit.setIgnoreAttributeOrder(true);
96
97         this.schemaContext = parseSchemas(getYangSchemas());
98         schemaContext.getModules();
99         SchemaService schemaService = createSchemaService();
100
101         final DOMStore operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", schemaService);
102         final DOMStore configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", schemaService);
103
104         final EnumMap<LogicalDatastoreType, DOMStore> datastores = new EnumMap<>(LogicalDatastoreType.class);
105         datastores.put(LogicalDatastoreType.CONFIGURATION, configStore);
106         datastores.put(LogicalDatastoreType.OPERATIONAL, operStore);
107
108         ExecutorService listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
109                 16, 16, "CommitFutures");
110
111         ConcurrentDOMDataBroker cdb = new ConcurrentDOMDataBroker(datastores, listenableFutureExecutor);
112         this.transactionProvider = new TransactionProvider(cdb, sessionIdForReporting);
113         this.currentSchemaContext = new CurrentSchemaContext(schemaService);
114
115     }
116
117     @Test
118     public void testEmptyDatastore() throws Exception {
119
120         Document response = getConfigRunning();
121         assertEmptyDatastore(response);
122
123         response = getConfigCandidate();
124         assertEmptyDatastore(response);
125
126         response = get();
127         assertEmptyDatastore(response);
128
129     }
130
131     @Test
132     public void testEditRunning() throws Exception {
133
134         try {
135             edit("messages/mapping/editConfig_running.xml");
136             fail("Should have failed - edit config on running datastore is not supported");
137         } catch (NetconfDocumentedException e) {
138             assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
139             assertTrue(e.getErrorTag() == ErrorTag.operation_not_supported);
140             assertTrue(e.getErrorType() == ErrorType.protocol);
141         }
142
143     }
144
145     @Test
146     public void testCandidateTransaction() throws Exception {
147
148         verifyResponse(edit("messages/mapping/editConfig_merge_n1.xml"), RPC_REPLY_OK);
149         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_merge_n1_control.xml"));
150         assertEmptyDatastore(getConfigRunning());
151
152         verifyResponse(discardChanges(), RPC_REPLY_OK);
153         assertEmptyDatastore(getConfigCandidate());
154
155     }
156
157     @Test
158     public void testEditWithCommit() throws Exception {
159
160         verifyResponse(edit("messages/mapping/editConfig_merge_n1.xml"), RPC_REPLY_OK);
161         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_merge_n1_control.xml"));
162
163         verifyResponse(commit(), RPC_REPLY_OK);
164         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_merge_n1_control.xml"));
165
166         deleteDatastore();
167
168     }
169
170     @Test
171     public void testMultipleEditsWithMerge() throws Exception {
172
173         verifyResponse(edit("messages/mapping/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
174         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_merge_multiple_control_1.xml"));
175         verifyResponse(edit("messages/mapping/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
176         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_merge_multiple_control_2.xml"));
177         assertEmptyDatastore(getConfigRunning());
178
179         verifyResponse(commit(), RPC_REPLY_OK);
180         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_merge_multiple_control_2.xml"));
181
182         deleteDatastore();
183
184     }
185
186     @Test
187     public void testMoreComplexEditConfigs() throws Exception {
188
189         verifyResponse(edit("messages/mapping/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
190         verifyResponse(edit("messages/mapping/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
191
192         verifyResponse(edit("messages/mapping/editConfig_merge_multiple_2.xml"), RPC_REPLY_OK);
193         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_merge_multiple_after_more_complex_merge.xml"));
194
195         verifyResponse(edit("messages/mapping/editConfig_merge_multiple_3.xml"), RPC_REPLY_OK);
196         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_merge_multiple_after_more_complex_merge_2.xml"));
197
198         verifyResponse(edit("messages/mapping/editConfig_merge_multiple_4_replace.xml"), RPC_REPLY_OK);
199         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_merge_multiple_after_replace.xml"));
200         verifyResponse(commit(), RPC_REPLY_OK);
201
202         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_merge_multiple_after_replace.xml"));
203
204         verifyResponse(edit("messages/mapping/editConfig_replace_default.xml"), RPC_REPLY_OK);
205         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_replace_default_control.xml"));
206         verifyResponse(commit(), RPC_REPLY_OK);
207
208         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_replace_default_control.xml"));
209
210         deleteDatastore();
211
212     }
213
214     @Test
215     public void testLock() throws Exception {
216
217         verifyResponse(lockCandidate(), RPC_REPLY_OK);
218
219         try {
220             lock();
221             fail("Should have failed - locking of running datastore is not supported");
222         } catch (NetconfDocumentedException e) {
223             assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
224             assertTrue(e.getErrorTag() == ErrorTag.operation_not_supported);
225             assertTrue(e.getErrorType() == ErrorType.application);
226         }
227     }
228
229     @Test
230     public void testUnlock() throws Exception {
231
232         verifyResponse(unlockCandidate(), RPC_REPLY_OK);
233
234         try {
235             unlock();
236             fail("Should have failed - unlocking of running datastore is not supported");
237         } catch (NetconfDocumentedException e) {
238             assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
239             assertTrue(e.getErrorTag() == ErrorTag.operation_not_supported);
240             assertTrue(e.getErrorType() == ErrorType.application);
241         }
242     }
243
244     @Test
245     public void testEditWithCreate() throws Exception {
246
247         verifyResponse(edit("messages/mapping/editConfig_create.xml"), RPC_REPLY_OK);
248         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_merge_n1_control.xml"));
249
250         try {
251             edit("messages/mapping/editConfig_create.xml");
252             fail("Create should have failed - data already exists");
253         } catch (NetconfDocumentedException e) {
254             assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
255             assertTrue(e.getErrorTag() == ErrorTag.data_exists);
256             assertTrue(e.getErrorType() == ErrorType.protocol);
257         }
258
259         verifyResponse(discardChanges(), RPC_REPLY_OK);
260
261     }
262
263     @Test
264     public void testDeleteNonExisting() throws Exception {
265
266         assertEmptyDatastore(getConfigCandidate());
267         assertEmptyDatastore(getConfigRunning());
268
269         try {
270             edit("messages/mapping/editConfig_delete-root.xml");
271             fail("Delete should have failed - data is missing");
272         } catch (NetconfDocumentedException e) {
273             assertTrue(e.getErrorSeverity() == ErrorSeverity.error);
274             assertTrue(e.getErrorTag() == ErrorTag.data_missing);
275             assertTrue(e.getErrorType() == ErrorType.protocol);
276         }
277
278     }
279
280     @Test
281     public void testEditMissingDefaultOperation() throws Exception {
282
283         verifyResponse(edit("messages/mapping/editConfig_merge_missing_default-operation_1.xml"), RPC_REPLY_OK);
284         verifyResponse(edit("messages/mapping/editConfig_merge_missing_default-operation_2.xml"), RPC_REPLY_OK);
285         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_merge_missing_default-operation_control.xml"));
286
287         verifyResponse(commit(), RPC_REPLY_OK);
288         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_merge_missing_default-operation_control.xml"));
289
290         deleteDatastore();
291     }
292
293     private void deleteDatastore() throws Exception{
294         verifyResponse(edit("messages/mapping/editConfig_delete-root.xml"), RPC_REPLY_OK);
295         assertEmptyDatastore(getConfigCandidate());
296
297         verifyResponse(commit(), RPC_REPLY_OK);
298         assertEmptyDatastore(getConfigRunning());
299     }
300
301     private void verifyResponse(Document response, Document template) {
302         DetailedDiff dd = new DetailedDiff(new Diff(response, template));
303         dd.overrideElementQualifier(new RecursiveElementNameAndTextQualifier());
304         assertTrue(dd.similar());
305     }
306
307     private void assertEmptyDatastore(Document response) {
308
309         NodeList nodes = response.getChildNodes();
310         assertTrue(nodes.getLength() == 1);
311
312         assertEquals(nodes.item(0).getLocalName(),RPC_REPLY_ELEMENT);
313
314         NodeList replyNodes = nodes.item(0).getChildNodes();
315         assertTrue(replyNodes.getLength() == 1);
316
317         Node dataNode = replyNodes.item(0);
318         assertEquals(dataNode.getLocalName(), DATA_ELEMENT);
319         assertFalse(dataNode.hasChildNodes());
320
321     }
322
323     private Document commit() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
324         Commit commit = new Commit(sessionIdForReporting, transactionProvider);
325         return executeOperation(commit, "messages/mapping/commit.xml");
326     }
327
328     private Document discardChanges() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
329         DiscardChanges discardOp = new DiscardChanges(sessionIdForReporting, transactionProvider);
330         return executeOperation(discardOp, "messages/mapping/discardChanges.xml");
331     }
332
333     private Document edit(String resource) throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
334         EditConfig editConfig = new EditConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
335         return executeOperation(editConfig, resource);
336     }
337
338     private Document get() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
339         Get get = new Get(sessionIdForReporting, currentSchemaContext, transactionProvider);
340         return executeOperation(get, "messages/mapping/get.xml");
341     }
342
343     private Document getConfigRunning() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
344         GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
345         return executeOperation(getConfig, "messages/mapping/getConfig.xml");
346     }
347
348     private Document getConfigCandidate() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
349         GetConfig getConfig = new GetConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
350         return executeOperation(getConfig, "messages/mapping/getConfig_candidate.xml");
351     }
352
353     private Document lock() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
354         Lock lock = new Lock(sessionIdForReporting);
355         return executeOperation(lock, "messages/mapping/lock.xml");
356     }
357
358     private Document unlock() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
359         Unlock unlock = new Unlock(sessionIdForReporting);
360         return executeOperation(unlock, "messages/mapping/unlock.xml");
361     }
362
363     private Document lockCandidate() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
364         Lock lock = new Lock(sessionIdForReporting);
365         return executeOperation(lock, "messages/mapping/lock_candidate.xml");
366     }
367
368     private Document unlockCandidate() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
369         Unlock unlock = new Unlock(sessionIdForReporting);
370         return executeOperation(unlock, "messages/mapping/unlock_candidate.xml");
371     }
372
373     private Document executeOperation(NetconfOperation op, String filename) throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
374         final Document request = XmlFileLoader.xmlFileToDocument(filename);
375
376         HandlingPriority priority = op.canHandle(request);
377         Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
378
379         final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
380
381         LOG.debug("Got response {}" , response);
382
383         return response;
384     }
385
386     private Collection<InputStream> getYangSchemas() {
387         final List<String> schemaPaths = Arrays.asList("/META-INF/yang/config.yang", "/yang/mdsal-netconf-mapping-test.yang");
388         final List<InputStream> schemas = new ArrayList<>();
389
390         for (String schemaPath : schemaPaths) {
391             InputStream resourceAsStream = getClass().getResourceAsStream(schemaPath);
392             schemas.add(resourceAsStream);
393         }
394
395         return schemas;
396     }
397
398     private SchemaContext parseSchemas(Collection<InputStream> schemas) throws IOException, YangSyntaxErrorException {
399         final YangParserImpl parser = new YangParserImpl();
400         Collection<ByteSource> sources = BuilderUtils.streamsToByteSources(schemas);
401         return parser.parseSources(sources);
402     }
403
404     private SchemaService createSchemaService() {
405         return new SchemaService() {
406
407             @Override
408             public void addModule(Module module) {
409             }
410
411             @Override
412             public void removeModule(Module module) {
413
414             }
415
416             @Override
417             public SchemaContext getSessionContext() {
418                 return schemaContext;
419             }
420
421             @Override
422             public SchemaContext getGlobalContext() {
423                 return schemaContext;
424             }
425
426             @Override
427             public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(final SchemaContextListener listener) {
428                 listener.onGlobalContextUpdated(getGlobalContext());
429                 return new ListenerRegistration<SchemaContextListener>() {
430                     @Override
431                     public void close() {
432
433                     }
434
435                     @Override
436                     public SchemaContextListener getInstance() {
437                         return listener;
438                     }
439                 };
440             }
441         };
442     }
443 }