2 * Copyright (c) 2015 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
9 package org.opendaylight.controller.netconf.mdsal.connector.ops;
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;
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;
66 public class NetconfMDSalMappingTest {
68 private static final Logger LOG = LoggerFactory.getLogger(NetconfMDSalMappingTest.class);
70 private static final String RPC_REPLY_ELEMENT = "rpc-reply";
71 private static final String DATA_ELEMENT = "data";
73 private static Document RPC_REPLY_OK = null;
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();
84 private CurrentSchemaContext currentSchemaContext = null;
85 private SchemaContext schemaContext = null;
86 private String sessionIdForReporting = "netconf-test-session1";
88 private TransactionProvider transactionProvider = null;
92 public void setUp() throws Exception {
94 XMLUnit.setIgnoreWhitespace(true);
95 XMLUnit.setIgnoreAttributeOrder(true);
97 this.schemaContext = parseSchemas(getYangSchemas());
98 schemaContext.getModules();
99 SchemaService schemaService = createSchemaService();
101 final DOMStore operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", schemaService);
102 final DOMStore configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", schemaService);
104 final EnumMap<LogicalDatastoreType, DOMStore> datastores = new EnumMap<>(LogicalDatastoreType.class);
105 datastores.put(LogicalDatastoreType.CONFIGURATION, configStore);
106 datastores.put(LogicalDatastoreType.OPERATIONAL, operStore);
108 ExecutorService listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
109 16, 16, "CommitFutures");
111 ConcurrentDOMDataBroker cdb = new ConcurrentDOMDataBroker(datastores, listenableFutureExecutor);
112 this.transactionProvider = new TransactionProvider(cdb, sessionIdForReporting);
113 this.currentSchemaContext = new CurrentSchemaContext(schemaService);
118 public void testEmptyDatastore() throws Exception {
120 Document response = getConfigRunning();
121 assertEmptyDatastore(response);
123 response = getConfigCandidate();
124 assertEmptyDatastore(response);
127 assertEmptyDatastore(response);
132 public void testEditRunning() throws Exception {
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);
146 public void testCandidateTransaction() throws Exception {
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());
152 verifyResponse(discardChanges(), RPC_REPLY_OK);
153 assertEmptyDatastore(getConfigCandidate());
158 public void testEditWithCommit() throws Exception {
160 verifyResponse(edit("messages/mapping/editConfig_merge_n1.xml"), RPC_REPLY_OK);
161 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_merge_n1_control.xml"));
163 verifyResponse(commit(), RPC_REPLY_OK);
164 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_merge_n1_control.xml"));
171 public void testMultipleEditsWithMerge() throws Exception {
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());
179 verifyResponse(commit(), RPC_REPLY_OK);
180 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_merge_multiple_control_2.xml"));
187 public void testMoreComplexEditConfigs() throws Exception {
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);
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"));
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"));
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);
202 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_merge_multiple_after_replace.xml"));
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);
208 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_replace_default_control.xml"));
215 public void testLock() throws Exception {
217 verifyResponse(lockCandidate(), RPC_REPLY_OK);
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);
230 public void testUnlock() throws Exception {
232 verifyResponse(unlockCandidate(), RPC_REPLY_OK);
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);
245 public void testEditWithCreate() throws Exception {
247 verifyResponse(edit("messages/mapping/editConfig_create.xml"), RPC_REPLY_OK);
248 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_merge_n1_control.xml"));
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);
259 verifyResponse(discardChanges(), RPC_REPLY_OK);
264 public void testDeleteNonExisting() throws Exception {
266 assertEmptyDatastore(getConfigCandidate());
267 assertEmptyDatastore(getConfigRunning());
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);
281 public void testEditMissingDefaultOperation() throws Exception {
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"));
287 verifyResponse(commit(), RPC_REPLY_OK);
288 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/editConfig_merge_missing_default-operation_control.xml"));
293 private void deleteDatastore() throws Exception{
294 verifyResponse(edit("messages/mapping/editConfig_delete-root.xml"), RPC_REPLY_OK);
295 assertEmptyDatastore(getConfigCandidate());
297 verifyResponse(commit(), RPC_REPLY_OK);
298 assertEmptyDatastore(getConfigRunning());
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());
307 private void assertEmptyDatastore(Document response) {
309 NodeList nodes = response.getChildNodes();
310 assertTrue(nodes.getLength() == 1);
312 assertEquals(nodes.item(0).getLocalName(),RPC_REPLY_ELEMENT);
314 NodeList replyNodes = nodes.item(0).getChildNodes();
315 assertTrue(replyNodes.getLength() == 1);
317 Node dataNode = replyNodes.item(0);
318 assertEquals(dataNode.getLocalName(), DATA_ELEMENT);
319 assertFalse(dataNode.hasChildNodes());
323 private Document commit() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
324 Commit commit = new Commit(sessionIdForReporting, transactionProvider);
325 return executeOperation(commit, "messages/mapping/commit.xml");
328 private Document discardChanges() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
329 DiscardChanges discardOp = new DiscardChanges(sessionIdForReporting, transactionProvider);
330 return executeOperation(discardOp, "messages/mapping/discardChanges.xml");
333 private Document edit(String resource) throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
334 EditConfig editConfig = new EditConfig(sessionIdForReporting, currentSchemaContext, transactionProvider);
335 return executeOperation(editConfig, resource);
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");
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");
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");
353 private Document lock() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
354 Lock lock = new Lock(sessionIdForReporting);
355 return executeOperation(lock, "messages/mapping/lock.xml");
358 private Document unlock() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
359 Unlock unlock = new Unlock(sessionIdForReporting);
360 return executeOperation(unlock, "messages/mapping/unlock.xml");
363 private Document lockCandidate() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
364 Lock lock = new Lock(sessionIdForReporting);
365 return executeOperation(lock, "messages/mapping/lock_candidate.xml");
368 private Document unlockCandidate() throws NetconfDocumentedException, ParserConfigurationException, SAXException, IOException {
369 Unlock unlock = new Unlock(sessionIdForReporting);
370 return executeOperation(unlock, "messages/mapping/unlock_candidate.xml");
373 private Document executeOperation(NetconfOperation op, String filename) throws ParserConfigurationException, SAXException, IOException, NetconfDocumentedException {
374 final Document request = XmlFileLoader.xmlFileToDocument(filename);
376 HandlingPriority priority = op.canHandle(request);
377 Preconditions.checkState(priority != HandlingPriority.CANNOT_HANDLE);
379 final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
381 LOG.debug("Got response {}" , response);
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<>();
390 for (String schemaPath : schemaPaths) {
391 InputStream resourceAsStream = getClass().getResourceAsStream(schemaPath);
392 schemas.add(resourceAsStream);
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);
404 private SchemaService createSchemaService() {
405 return new SchemaService() {
408 public void addModule(Module module) {
412 public void removeModule(Module module) {
417 public SchemaContext getSessionContext() {
418 return schemaContext;
422 public SchemaContext getGlobalContext() {
423 return schemaContext;
427 public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(final SchemaContextListener listener) {
428 listener.onGlobalContextUpdated(getGlobalContext());
429 return new ListenerRegistration<SchemaContextListener>() {
431 public void close() {
436 public SchemaContextListener getInstance() {