Remove required source element from Get operation for MDSAL 96/15496/4
authorMaros Marsalek <mmarsale@cisco.com>
Thu, 19 Feb 2015 12:53:38 +0000 (13:53 +0100)
committerTony Tkacik <ttkacik@cisco.com>
Mon, 23 Feb 2015 08:54:49 +0000 (08:54 +0000)
Change-Id: I2c1495dfac8ecad00605f4df98b310b3d99ac4ed
Signed-off-by: Maros Marsalek <mmarsale@cisco.com>
opendaylight/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/controller/netconf/mdsal/connector/ops/get/AbstractGet.java
opendaylight/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/controller/netconf/mdsal/connector/ops/get/Get.java
opendaylight/netconf/mdsal-netconf-connector/src/main/java/org/opendaylight/controller/netconf/mdsal/connector/ops/get/GetConfig.java

index e0c004461c116c5b1bb23f41c566537e565c05ef..8f6ff417d6499d47dc594bc5ab5fb2de03e937d0 100644 (file)
@@ -9,6 +9,7 @@
 package org.opendaylight.controller.netconf.mdsal.connector.ops.get;
 
 import com.google.common.base.Function;
+import com.google.common.base.Optional;
 import com.google.common.base.Throwables;
 import com.google.common.collect.Iterables;
 import java.io.IOException;
@@ -115,13 +116,13 @@ public abstract class AbstractGet extends AbstractLastNetconfOperation {
     }
 
     protected static final class GetConfigExecution {
-        private final Datastore datastore;
+        private final Optional<Datastore> datastore;
 
-        public GetConfigExecution(final Datastore datastore) {
+        public GetConfigExecution(final Optional<Datastore> datastore) {
             this.datastore = datastore;
         }
 
-        public Datastore getDatastore() {
+        public Optional<Datastore> getDatastore() {
             return datastore;
         }
 
@@ -132,7 +133,7 @@ public abstract class AbstractGet extends AbstractLastNetconfOperation {
                 throw new NetconfDocumentedException("Incorrect RPC: " + e.getMessage(), e.getErrorType(), e.getErrorTag(), e.getErrorSeverity(), e.getErrorInfo());
             }
 
-            final Datastore sourceDatastore;
+            final Optional<Datastore> sourceDatastore;
             try {
                 sourceDatastore = parseSource(xml);
             } catch (final NetconfDocumentedException e) {
@@ -144,14 +145,12 @@ public abstract class AbstractGet extends AbstractLastNetconfOperation {
             return new GetConfigExecution(sourceDatastore);
         }
 
-        private static Datastore parseSource(final XmlElement xml) throws NetconfDocumentedException {
-            final Datastore sourceDatastore;
-            final XmlElement sourceElement = xml.getOnlyChildElement(XmlNetconfConstants.SOURCE_KEY,
+        private static Optional<Datastore> parseSource(final XmlElement xml) throws NetconfDocumentedException {
+            final Optional<XmlElement> sourceElement = xml.getOnlyChildElementOptionally(XmlNetconfConstants.SOURCE_KEY,
                     XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
 
-            final String sourceParsed = sourceElement.getOnlyChildElement().getName();
-            sourceDatastore = Datastore.valueOf(sourceParsed);
-            return sourceDatastore;
+            return  sourceElement.isPresent() ?
+                    Optional.of(Datastore.valueOf(sourceElement.get().getOnlyChildElement().getName())) : Optional.<Datastore>absent();
         }
 
         private static void validateInputRpc(final XmlElement xml, String operationName) throws NetconfDocumentedException{
index a2b2fbb0db0304184cdc653b34a1e5f046ee3267..cebd8c88837547f0f2d2f573ca0e2e81ed03e67a 100644 (file)
@@ -52,13 +52,10 @@ public class Get extends AbstractGet {
         }
 
         final YangInstanceIdentifier dataRoot = ROOT;
-        DOMDataReadWriteTransaction rwTx = getTransaction(getConfigExecution.getDatastore());
+        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);
             return (Element) transformNormalizedNode(document, normalizedNodeOptional.get(), dataRoot);
         } catch (ReadFailedException e) {
             LOG.warn("Unable to read data: {}", dataRoot, e);
index b56bcc795caf3ad1c5adacaa98d445e1b63079a8..f2d8abbb6180f10785f7cb00c59dcb6d6fd7d5ee 100644 (file)
@@ -9,6 +9,7 @@
 package org.opendaylight.controller.netconf.mdsal.connector.ops.get;
 
 import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
 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;
@@ -52,10 +53,13 @@ public class GetConfig extends AbstractGet {
         }
 
         final YangInstanceIdentifier dataRoot = ROOT;
-        DOMDataReadWriteTransaction rwTx = getTransaction(getConfigExecution.getDatastore());
+        // Proper exception should be thrown
+        Preconditions.checkState(getConfigExecution.getDatastore().isPresent(), "Source element missing from request");
+
+        DOMDataReadWriteTransaction rwTx = getTransaction(getConfigExecution.getDatastore().get());
         try {
             final Optional<NormalizedNode<?, ?>> normalizedNodeOptional = rwTx.read(LogicalDatastoreType.CONFIGURATION, dataRoot).checkedGet();
-            if (getConfigExecution.getDatastore() == Datastore.running) {
+            if (getConfigExecution.getDatastore().get() == Datastore.running) {
                 transactionProvider.abortRunningTransaction(rwTx);
                 rwTx = null;
             }