Centralize RestconfStrategy allocation 52/93052/5
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 14 Oct 2020 13:27:27 +0000 (15:27 +0200)
committerRobert Varga <nite@hq.sk>
Wed, 14 Oct 2020 16:16:44 +0000 (16:16 +0000)
Move dispatch logic for various implementations to RestconfStrategy,
so we have explicit control over it.

Change-Id: I68f3d5ea9eb4f2c9ba512d3d07a8e4adcb5c7cf7
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImpl.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/MdsalRestconfStrategy.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/RestconfStrategy.java

index e5da72e67fd92154f81cb7291b9cc0ff74d1750f..1a383475f48ec8878b5772be9dae32b71b569f8e 100644 (file)
@@ -29,13 +29,10 @@ import javax.ws.rs.Path;
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
-import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.mdsal.dom.api.DOMActionResult;
-import org.opendaylight.mdsal.dom.api.DOMDataBroker;
 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
-import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 import org.opendaylight.restconf.common.context.WriterParameters;
@@ -53,7 +50,6 @@ import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfDataService;
 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfStreamsSubscriptionService;
 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
-import org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy;
 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
 import org.opendaylight.restconf.nb.rfc8040.rests.utils.DeleteDataTransactionUtil;
 import org.opendaylight.restconf.nb.rfc8040.rests.utils.PatchDataTransactionUtil;
@@ -397,34 +393,15 @@ public class RestconfDataServiceImpl implements RestconfDataService {
 
     // FIXME: why is this synchronized?
     public synchronized RestconfStrategy getRestconfStrategy(final DOMMountPoint mountPoint) {
-        final TransactionChainHandler transactionChain;
-        if (mountPoint != null) {
-            final Optional<NetconfDataTreeService> service = mountPoint.getService(NetconfDataTreeService.class);
-            if (service.isPresent()) {
-                return new NetconfRestconfStrategy(service.get());
-            }
-            transactionChain = transactionChainOfMountPoint(mountPoint);
-        } else {
-            transactionChain = transactionChainHandler;
-        }
-        return new MdsalRestconfStrategy(transactionChain);
-    }
-
-    /**
-     * Prepare transaction chain to access data of mount point.
-     *
-     * @param mountPoint mount point reference
-     * @return {@link TransactionChainHandler}
-     */
-    private static TransactionChainHandler transactionChainOfMountPoint(final @NonNull DOMMountPoint mountPoint) {
-        final Optional<DOMDataBroker> domDataBrokerService = mountPoint.getService(DOMDataBroker.class);
-        if (domDataBrokerService.isPresent()) {
-            return new TransactionChainHandler(domDataBrokerService.get());
+        if (mountPoint == null) {
+            return new MdsalRestconfStrategy(transactionChainHandler);
         }
 
-        final String errMsg = "DOM data broker service isn't available for mount point " + mountPoint.getIdentifier();
-        LOG.warn(errMsg);
-        throw new RestconfDocumentedException(errMsg);
+        return RestconfStrategy.forMountPoint(mountPoint).orElseThrow(() -> {
+            LOG.warn("Mount point {} does not expose a suitable access interface", mountPoint.getIdentifier());
+            return new RestconfDocumentedException("Could not find a supported access interface in mount point "
+                + mountPoint.getIdentifier());
+        });
     }
 
     /**
index 1c090a61600e5517d9b554c24b0216ba54bbc083..c2eb2fbd9e6cb785f70fb135c648357a344d9adc 100644 (file)
@@ -15,6 +15,7 @@ import java.util.Optional;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.common.api.CommitInfo;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.mdsal.dom.api.DOMDataBroker;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
@@ -34,6 +35,10 @@ public final class MdsalRestconfStrategy extends RestconfStrategy {
 
     private DOMDataTreeReadWriteTransaction rwTx;
 
+    public MdsalRestconfStrategy(final DOMDataBroker dataBroker) {
+        this(new TransactionChainHandler(dataBroker));
+    }
+
     public MdsalRestconfStrategy(final TransactionChainHandler transactionChainHandler) {
         this.transactionChainHandler = requireNonNull(transactionChainHandler);
         transactionChain = transactionChainHandler.get();
index 1795d9ea234f406ae0fea015b4c169f355b29e12..0c01e6784f9644121d42a8f9bc86f0b2ffd6afaf 100644 (file)
@@ -14,7 +14,10 @@ import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.common.api.CommitInfo;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.mdsal.dom.api.DOMDataBroker;
+import org.opendaylight.mdsal.dom.api.DOMMountPoint;
 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
+import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
@@ -28,6 +31,27 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 // FIXME: it seems the first three operations deal with lifecycle of a transaction, while others invoke various
 //        operations. This should be handled through proper allocation indirection.
 public abstract class RestconfStrategy {
+    RestconfStrategy() {
+        // Hidden on purpose
+    }
+
+    /**
+     * Look up the appropriate strategy for a particular mount point.
+     *
+     * @param mountPoint Target mount point
+     * @return A strategy, or null if the mount point does not expose a supported interface
+     * @throws NullPointerException if {@code mountPoint} is null
+     */
+    public static Optional<RestconfStrategy> forMountPoint(final DOMMountPoint mountPoint) {
+        final Optional<RestconfStrategy> netconf = mountPoint.getService(NetconfDataTreeService.class)
+            .map(NetconfRestconfStrategy::new);
+        if (netconf.isPresent()) {
+            return netconf;
+        }
+
+        return mountPoint.getService(DOMDataBroker.class).map(MdsalRestconfStrategy::new);
+    }
+
     /**
      * Lock the entire datastore.
      */