Improve xml lookup in edit-config rpc for md-sal 89/18589/2
authorMaros Marsalek <mmarsale@cisco.com>
Sat, 18 Apr 2015 21:10:38 +0000 (23:10 +0200)
committerMaros Marsalek <mmarsale@cisco.com>
Mon, 20 Apr 2015 06:43:04 +0000 (08:43 +0200)
Lookup the attributes of rpc in xml payload directly to improve lookup
performance instead of using utility XmlElement class

Change-Id: I47e8f47f279ccbd271e5ed903a352ebbc307d0f9
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
opendaylight/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/controller/netconf/mdsal/connector/ops/EditConfig.java

index fbefb5c56d818ffd3e039cea449202530a113e42..a2aa0e20679473951cacd1191097f1368e76bac6 100644 (file)
@@ -23,8 +23,6 @@ import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorT
 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
 import org.opendaylight.controller.netconf.mdsal.connector.CurrentSchemaContext;
 import org.opendaylight.controller.netconf.mdsal.connector.TransactionProvider;
-import org.opendaylight.controller.netconf.util.exception.MissingNameSpaceException;
-import org.opendaylight.controller.netconf.util.exception.UnexpectedNamespaceException;
 import org.opendaylight.controller.netconf.util.mapping.AbstractSingletonNetconfOperation;
 import org.opendaylight.controller.netconf.util.xml.XmlElement;
 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
@@ -47,6 +45,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
 
 public class EditConfig extends AbstractSingletonNetconfOperation {
 
@@ -182,35 +181,32 @@ public class EditConfig extends AbstractSingletonNetconfOperation {
             LOG.debug("DataNode from module is not ContainerSchemaNode nor ListSchemaNode, aborting..");
             return Optional.absent();
         }
-
     }
 
     private Datastore extractTargetParameter(final XmlElement operationElement) throws NetconfDocumentedException {
-        final XmlElement targetChildNode;
-        try {
-            final XmlElement targetElement = operationElement.getOnlyChildElementWithSameNamespace(TARGET_KEY);
-            targetChildNode = targetElement.getOnlyChildElementWithSameNamespace();
-        } catch (final MissingNameSpaceException | UnexpectedNamespaceException e) {
-            LOG.trace("Can't get only child element with same namespace", e);
-            throw NetconfDocumentedException.wrap(e);
+        final NodeList elementsByTagName = operationElement.getDomElement().getElementsByTagName(TARGET_KEY);
+        // Direct lookup instead of using XmlElement class due to performance
+        if (elementsByTagName.getLength() == 0) {
+            throw new NetconfDocumentedException("Missing target element", ErrorType.rpc, ErrorTag.missing_attribute, ErrorSeverity.error);
+        } else if (elementsByTagName.getLength() > 1) {
+            throw new NetconfDocumentedException("Multiple target elements", ErrorType.rpc, ErrorTag.unknown_attribute, ErrorSeverity.error);
+        } else {
+            final XmlElement targetChildNode = XmlElement.fromDomElement((Element) elementsByTagName.item(0)).getOnlyChildElement();
+            return Datastore.valueOf(targetChildNode.getName());
         }
-
-        return Datastore.valueOf(targetChildNode.getName());
     }
 
-    private ModifyAction getDefaultOperation(final XmlElement operationElement) throws NetconfDocumentedException{
-        try {
-            return ModifyAction.fromXmlValue(getElement(operationElement, DEFAULT_OPERATION_KEY).getTextContent());
-        } catch (NetconfDocumentedException e) {
-            if (e.getErrorType() == ErrorType.protocol
-                    && e.getErrorSeverity() == ErrorSeverity.error
-                    && e.getErrorTag() == ErrorTag.missing_element) {
-                return ModifyAction.MERGE;
-            }
-            else {
-                throw e;
-            }
+    private ModifyAction getDefaultOperation(final XmlElement operationElement) throws NetconfDocumentedException {
+        final NodeList elementsByTagName = operationElement.getDomElement().getElementsByTagName(DEFAULT_OPERATION_KEY);
+        if(elementsByTagName.getLength() == 0) {
+            return ModifyAction.MERGE;
+        } else if(elementsByTagName.getLength() > 1) {
+            throw new NetconfDocumentedException("Multiple " + DEFAULT_OPERATION_KEY + " elements",
+                    ErrorType.rpc, ErrorTag.unknown_attribute, ErrorSeverity.error);
+        } else {
+            return ModifyAction.fromXmlValue(elementsByTagName.item(0).getTextContent());
         }
+
     }
 
     private XmlElement getElement(final XmlElement operationElement, String elementName) throws NetconfDocumentedException {