Decouple config and netconf subsystems.
[controller.git] / opendaylight / netconf / mdsal-netconf-connector / src / main / java / org / opendaylight / controller / netconf / mdsal / connector / ops / get / Get.java
index a2b2fbb0db0304184cdc653b34a1e5f046ee3267..41d488aee9a9a668705472e6dbeb1a3d8d86d88a 100644 (file)
@@ -9,17 +9,19 @@
 package org.opendaylight.controller.netconf.mdsal.connector.ops.get;
 
 import com.google.common.base.Optional;
+import org.opendaylight.controller.config.util.xml.DocumentedException;
+import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorSeverity;
+import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorTag;
+import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorType;
+import org.opendaylight.controller.config.util.xml.XmlElement;
+import org.opendaylight.controller.config.util.xml.XmlUtil;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
-import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
-import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorSeverity;
-import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorTag;
-import org.opendaylight.controller.netconf.api.NetconfDocumentedException.ErrorType;
+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.mdsal.connector.ops.Datastore;
-import org.opendaylight.controller.netconf.util.xml.XmlElement;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.slf4j.Logger;
@@ -32,7 +34,6 @@ public class Get extends AbstractGet {
     private static final Logger LOG = LoggerFactory.getLogger(Get.class);
 
     private static final String OPERATION_NAME = "get";
-
     private final TransactionProvider transactionProvider;
 
     public Get(final String netconfSessionIdForReporting, final CurrentSchemaContext schemaContext, final TransactionProvider transactionProvider) {
@@ -41,38 +42,38 @@ public class Get extends AbstractGet {
     }
 
     @Override
-    protected Element handleWithNoSubsequentOperations(Document document, XmlElement operationElement) throws NetconfDocumentedException {
-        GetConfigExecution getConfigExecution = null;
-        try {
-            getConfigExecution = GetConfigExecution.fromXml(operationElement, OPERATION_NAME);
+    protected Element handleWithNoSubsequentOperations(Document document, XmlElement operationElement) throws DocumentedException {
 
-        } catch (final NetconfDocumentedException e) {
-            LOG.warn("Get request processing failed on session: {}", getNetconfSessionIdForReporting(), e);
-            throw e;
+        final Optional<YangInstanceIdentifier> dataRootOptional = getDataRootFromFilter(operationElement);
+        if (!dataRootOptional.isPresent()) {
+            return XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.<String>absent());
         }
 
-        final YangInstanceIdentifier dataRoot = ROOT;
-        DOMDataReadWriteTransaction rwTx = getTransaction(getConfigExecution.getDatastore());
+        final YangInstanceIdentifier dataRoot = dataRootOptional.get();
+
+        DOMDataReadWriteTransaction rwTx = getTransaction(Datastore.running);
         try {
             final Optional<NormalizedNode<?, ?>> normalizedNodeOptional = rwTx.read(LogicalDatastoreType.OPERATIONAL, dataRoot).checkedGet();
-            if (getConfigExecution.getDatastore() == Datastore.running) {
-                transactionProvider.abortRunningTransaction(rwTx);
-                rwTx = null;
+            transactionProvider.abortRunningTransaction(rwTx);
+
+            if (!normalizedNodeOptional.isPresent()) {
+                return XmlUtil.createElement(document, XmlNetconfConstants.DATA_KEY, Optional.<String>absent());
             }
-            return (Element) transformNormalizedNode(document, normalizedNodeOptional.get(), dataRoot);
+
+            return serializeNodeWithParentStructure(document, dataRoot, normalizedNodeOptional.get());
         } catch (ReadFailedException e) {
             LOG.warn("Unable to read data: {}", dataRoot, e);
             throw new IllegalStateException("Unable to read data " + dataRoot, e);
         }
     }
 
-    private DOMDataReadWriteTransaction getTransaction(Datastore datastore) throws NetconfDocumentedException{
+    private DOMDataReadWriteTransaction getTransaction(Datastore datastore) throws DocumentedException{
         if (datastore == Datastore.candidate) {
             return transactionProvider.getOrCreateTransaction();
         } else if (datastore == Datastore.running) {
             return transactionProvider.createRunningTransaction();
         }
-        throw new NetconfDocumentedException("Incorrect Datastore: ", ErrorType.protocol, ErrorTag.bad_element, ErrorSeverity.error);
+        throw new DocumentedException("Incorrect Datastore: ", ErrorType.protocol, ErrorTag.bad_element, ErrorSeverity.error);
     }
 
     @Override