package org.opendaylight.netconf.mdsal.connector.ops;
+import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import java.net.URI;
import java.net.URISyntaxException;
}
private static Datastore extractTargetParameter(final XmlElement operationElement) throws DocumentedException {
- final NodeList elementsByTagName = operationElement.getDomElement().getElementsByTagName(TARGET_KEY);
+ final NodeList elementsByTagName = getElementsByTagName(operationElement, TARGET_KEY);
// Direct lookup instead of using XmlElement class due to performance
if (elementsByTagName.getLength() == 0) {
final Map<String, String> errorInfo = ImmutableMap.of("bad-attribute", TARGET_KEY, "bad-element",
}
private static ModifyAction getDefaultOperation(final XmlElement operationElement) throws DocumentedException {
- final NodeList elementsByTagName = operationElement.getDomElement().getElementsByTagName(DEFAULT_OPERATION_KEY);
+ final NodeList elementsByTagName = getElementsByTagName(operationElement, DEFAULT_OPERATION_KEY);
if (elementsByTagName.getLength() == 0) {
return ModifyAction.MERGE;
} else if (elementsByTagName.getLength() > 1) {
return childNode.get();
}
+ @VisibleForTesting
+ static NodeList getElementsByTagName(final XmlElement operationElement, final String key) throws
+ DocumentedException {
+ final Element element = operationElement.getDomElement();
+ final NodeList elementsByTagName;
+
+ if (Strings.isNullOrEmpty(element.getPrefix())) {
+ elementsByTagName = element.getElementsByTagName(key);
+ } else {
+ elementsByTagName = element.getElementsByTagNameNS(operationElement.getNamespace(), key);
+ }
+
+ return elementsByTagName;
+ }
+
@Override
protected String getOperationName() {
return OPERATION_NAME;
import org.custommonkey.xmlunit.DetailedDiff;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.XMLUnit;
+import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
+import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.opendaylight.controller.cluster.databroker.ConcurrentDOMDataBroker;
import org.opendaylight.controller.config.util.xml.DocumentedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
+import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class NetconfMDSalMappingTest {
private static final Logger LOG = LoggerFactory.getLogger(NetconfMDSalMappingTest.class);
+ private static final String TARGET_KEY = "target";
private static final String RPC_REPLY_ELEMENT = "rpc-reply";
private static final String DATA_ELEMENT = "data";
private static final String FILTER_NODE = "filter";
@Test
public void testAugmentedContainerReplace() throws Exception {
verifyResponse(edit("messages/mapping/editConfigs/editConfig_empty_modules_create.xml"),
- RPC_REPLY_OK);
+ RPC_REPLY_OK);
verifyResponse(commit(), RPC_REPLY_OK);
verifyResponse(edit("messages/mapping/editConfigs/editConfig_augmented_container_replace.xml"),
- RPC_REPLY_OK);
+ RPC_REPLY_OK);
verifyResponse(commit(), RPC_REPLY_OK);
deleteDatastore();
@Test
public void testLeafFromAugmentReplace() throws Exception {
verifyResponse(edit("messages/mapping/editConfigs/editConfig_empty_modules_create.xml"),
- RPC_REPLY_OK);
+ RPC_REPLY_OK);
verifyResponse(commit(), RPC_REPLY_OK);
verifyResponse(edit("messages/mapping/editConfigs/editConfig_leaf_from_augment_replace.xml"),
- RPC_REPLY_OK);
+ RPC_REPLY_OK);
verifyResponse(commit(), RPC_REPLY_OK);
deleteDatastore();
deleteDatastore();
}
+ @Test
+ public void testEditConfigGetElementByTagName() throws Exception {
+ EditConfig editConfig = new EditConfig("test_edit-config", Mockito.mock(CurrentSchemaContext.class),
+ Mockito.mock(TransactionProvider.class));
+
+ String stringWithoutPrefix =
+ "<rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n"
+ + " <edit-config>\n"
+ + " <target>\n"
+ + " <candidate/>\n"
+ + " </target>\n"
+ + " </edit-config>\n"
+ + "</rpc>";
+ XmlElement xe = getXmlElement(stringWithoutPrefix);
+ NodeList nodeList = editConfig.getElementsByTagName(xe, TARGET_KEY);
+ Assert.assertEquals(1, nodeList.getLength());
+
+ String stringWithPrefix =
+ "<nc:rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n"
+ + " <nc:edit-config>\n"
+ + " <nc:target>\n"
+ + " <nc:candidate/>\n"
+ + " </nc:target>\n"
+ + " </nc:edit-config>\n"
+ + "</nc:rpc>";
+
+ xe = getXmlElement(stringWithPrefix);
+ nodeList = editConfig.getElementsByTagName(xe, TARGET_KEY);
+ Assert.assertEquals(1, nodeList.getLength());
+
+ String stringWithoutTarget =
+ "<nc:rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n"
+ + " <nc:edit-config>\n"
+ + " <nc:target>\n"
+ + " </nc:target>\n"
+ + " </nc:edit-config>\n"
+ + "</nc:rpc>";
+ xe = getXmlElement(stringWithoutTarget);
+
+ try {
+ nodeList = editConfig.getElementsByTagName(xe, TARGET_KEY);
+ XmlElement.fromDomElement((Element) nodeList.item(0)).getOnlyChildElement();
+ Assert.fail("Not specified target, we should fail");
+ } catch (DocumentedException documentedException) {
+ // Ignore
+ }
+
+ }
+
+ private XmlElement getXmlElement(final String elementAsString) throws Exception {
+ Document document = XmlUtil.readXmlToDocument(elementAsString);
+ Element element = document.getDocumentElement();
+ return XmlElement.fromDomElement(element);
+ }
+
@Test
public void testReplaceMapEntry() throws Exception {
verifyResponse(edit("messages/mapping/editConfigs/edit-config-replace-map-entry.xml"), RPC_REPLY_OK);