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.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
15 import com.google.common.io.ByteSource;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.MoreExecutors;
18 import java.io.StringWriter;
19 import java.util.EnumMap;
20 import java.util.concurrent.ExecutorService;
21 import javax.xml.transform.OutputKeys;
22 import javax.xml.transform.Transformer;
23 import javax.xml.transform.TransformerFactory;
24 import javax.xml.transform.dom.DOMSource;
25 import javax.xml.transform.stream.StreamResult;
26 import org.custommonkey.xmlunit.DetailedDiff;
27 import org.custommonkey.xmlunit.Diff;
28 import org.custommonkey.xmlunit.XMLUnit;
29 import org.junit.Before;
30 import org.mockito.MockitoAnnotations;
31 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
32 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
33 import org.opendaylight.mdsal.dom.broker.SerializedDOMDataBroker;
34 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
35 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStoreFactory;
36 import org.opendaylight.netconf.api.xml.XmlUtil;
37 import org.opendaylight.netconf.mapping.api.NetconfOperation;
38 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
39 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
40 import org.opendaylight.netconf.mdsal.connector.TransactionProvider;
41 import org.opendaylight.netconf.mdsal.connector.ops.get.Get;
42 import org.opendaylight.netconf.mdsal.connector.ops.get.GetConfig;
43 import org.opendaylight.netconf.util.test.NetconfXmlUnitRecursiveQualifier;
44 import org.opendaylight.netconf.util.test.XmlFileLoader;
45 import org.opendaylight.yangtools.util.concurrent.SpecialExecutors;
46 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
47 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.w3c.dom.Document;
51 import org.w3c.dom.Node;
52 import org.w3c.dom.NodeList;
54 public abstract class AbstractNetconfOperationTest {
55 private static final Logger LOG = LoggerFactory.getLogger(AbstractNetconfOperationTest.class);
56 protected static final String SESSION_ID_FOR_REPORTING = "netconf-test-session1";
57 private static final String RPC_REPLY_ELEMENT = "rpc-reply";
58 private static final String DATA_ELEMENT = "data";
59 protected static final Document RPC_REPLY_OK = getReplyOk();
61 private CurrentSchemaContext currentSchemaContext;
62 private TransactionProvider transactionProvider;
65 public void setUp() throws Exception {
66 MockitoAnnotations.initMocks(this);
68 XMLUnit.setIgnoreWhitespace(true);
69 XMLUnit.setIgnoreAttributeOrder(true);
71 final SchemaContext schemaContext = getSchemaContext();
72 final DOMSchemaService schemaService = new SchemaServiceStub(schemaContext);
73 final DOMStore operStore = InMemoryDOMDataStoreFactory.create("DOM-OPER", schemaService);
74 final DOMStore configStore = InMemoryDOMDataStoreFactory.create("DOM-CFG", schemaService);
76 currentSchemaContext = new CurrentSchemaContext(schemaService, sourceIdentifier -> {
77 final YangTextSchemaSource yangTextSchemaSource =
78 YangTextSchemaSource.delegateForByteSource(sourceIdentifier, ByteSource.wrap("module test".getBytes()));
79 return Futures.immediateFuture(yangTextSchemaSource);
82 final EnumMap<LogicalDatastoreType, DOMStore> datastores = new EnumMap<>(LogicalDatastoreType.class);
83 datastores.put(LogicalDatastoreType.CONFIGURATION, configStore);
84 datastores.put(LogicalDatastoreType.OPERATIONAL, operStore);
86 final ExecutorService listenableFutureExecutor = SpecialExecutors.newBlockingBoundedCachedThreadPool(
87 16, 16, "CommitFutures", CopyConfigTest.class);
89 final SerializedDOMDataBroker sdb = new SerializedDOMDataBroker(datastores,
90 MoreExecutors.listeningDecorator(listenableFutureExecutor));
91 this.transactionProvider = new TransactionProvider(sdb, SESSION_ID_FOR_REPORTING);
94 protected abstract SchemaContext getSchemaContext();
96 protected CurrentSchemaContext getCurrentSchemaContext() {
97 return currentSchemaContext;
100 protected TransactionProvider getTransactionProvider() {
101 return transactionProvider;
104 @SuppressWarnings("illegalCatch")
105 private static Document getReplyOk() {
108 doc = XmlFileLoader.xmlFileToDocument("messages/mapping/rpc-reply_ok.xml");
109 } catch (final Exception e) {
110 LOG.debug("unable to load rpc reply ok.", e);
111 doc = XmlUtil.newDocument();
116 protected Document commit() throws Exception {
117 final Commit commit = new Commit(SESSION_ID_FOR_REPORTING, transactionProvider);
118 return executeOperation(commit, "messages/mapping/commit.xml");
121 protected Document discardChanges() throws Exception {
122 final DiscardChanges discardOp = new DiscardChanges(SESSION_ID_FOR_REPORTING, transactionProvider);
123 return executeOperation(discardOp, "messages/mapping/discardChanges.xml");
126 protected Document edit(final String resource) throws Exception {
127 final EditConfig editConfig = new EditConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext,
128 transactionProvider);
129 return executeOperation(editConfig, resource);
132 protected Document edit(final Document request) throws Exception {
133 final EditConfig editConfig = new EditConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext,
134 transactionProvider);
135 return executeOperation(editConfig, request);
138 protected Document get() throws Exception {
139 final Get get = new Get(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
140 return executeOperation(get, "messages/mapping/get.xml");
143 protected Document getWithFilter(final String resource) throws Exception {
144 final Get get = new Get(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
145 return executeOperation(get, resource);
148 protected Document getConfigRunning() throws Exception {
149 final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
150 return executeOperation(getConfig, "messages/mapping/getConfig.xml");
153 protected Document getConfigCandidate() throws Exception {
154 final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
155 return executeOperation(getConfig, "messages/mapping/getConfig_candidate.xml");
158 protected Document getConfigWithFilter(final String resource) throws Exception {
159 final GetConfig getConfig = new GetConfig(SESSION_ID_FOR_REPORTING, currentSchemaContext, transactionProvider);
160 return executeOperation(getConfig, resource);
163 protected static Document lock() throws Exception {
164 final Lock lock = new Lock(SESSION_ID_FOR_REPORTING);
165 return executeOperation(lock, "messages/mapping/lock.xml");
168 protected static Document unlock() throws Exception {
169 final Unlock unlock = new Unlock(SESSION_ID_FOR_REPORTING);
170 return executeOperation(unlock, "messages/mapping/unlock.xml");
173 protected static Document lockWithoutTarget() throws Exception {
174 final Lock lock = new Lock(SESSION_ID_FOR_REPORTING);
175 return executeOperation(lock, "messages/mapping/lock_notarget.xml");
178 protected static Document unlockWithoutTarget() throws Exception {
179 final Unlock unlock = new Unlock(SESSION_ID_FOR_REPORTING);
180 return executeOperation(unlock, "messages/mapping/unlock_notarget.xml");
183 protected static Document lockCandidate() throws Exception {
184 final Lock lock = new Lock(SESSION_ID_FOR_REPORTING);
185 return executeOperation(lock, "messages/mapping/lock_candidate.xml");
188 protected static Document unlockCandidate() throws Exception {
189 final Unlock unlock = new Unlock(SESSION_ID_FOR_REPORTING);
190 return executeOperation(unlock, "messages/mapping/unlock_candidate.xml");
193 protected static Document executeOperation(final NetconfOperation op, final String filename) throws Exception {
194 final Document request = XmlFileLoader.xmlFileToDocument(filename);
195 return executeOperation(op, request);
198 protected static Document executeOperation(final NetconfOperation op, final Document request) throws Exception {
199 final Document response = op.handle(request, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
200 LOG.debug("Got response {}", response);
204 protected static void assertEmptyDatastore(final Document response) {
205 final NodeList nodes = response.getChildNodes();
206 assertTrue(nodes.getLength() == 1);
208 assertEquals(nodes.item(0).getLocalName(), RPC_REPLY_ELEMENT);
210 final NodeList replyNodes = nodes.item(0).getChildNodes();
211 assertTrue(replyNodes.getLength() == 1);
213 final Node dataNode = replyNodes.item(0);
214 assertEquals(dataNode.getLocalName(), DATA_ELEMENT);
215 assertFalse(dataNode.hasChildNodes());
218 protected static void verifyResponse(final Document response, final Document template) throws Exception {
219 final DetailedDiff dd = new DetailedDiff(new Diff(response, template));
220 dd.overrideElementQualifier(new NetconfXmlUnitRecursiveQualifier());
222 LOG.warn("Actual response:");
223 printDocument(response);
224 LOG.warn("Expected response:");
225 printDocument(template);
226 fail("Differences found: " + dd.toString());
230 private static void printDocument(final Document doc) throws Exception {
231 final TransformerFactory tf = TransformerFactory.newInstance();
232 final Transformer transformer = tf.newTransformer();
233 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
234 transformer.setOutputProperty(OutputKeys.METHOD, "xml");
235 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
236 transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
237 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
239 final StringWriter writer = new StringWriter();
240 transformer.transform(new DOMSource(doc),
241 new StreamResult(writer));
242 LOG.warn(writer.getBuffer().toString());