From 8d582bb9618c04444f75dc834ee6b2e9c5a422f7 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Wed, 14 Oct 2020 15:27:27 +0200 Subject: [PATCH] Centralize RestconfStrategy allocation Move dispatch logic for various implementations to RestconfStrategy, so we have explicit control over it. Change-Id: I68f3d5ea9eb4f2c9ba512d3d07a8e4adcb5c7cf7 Signed-off-by: Robert Varga --- .../impl/RestconfDataServiceImpl.java | 37 ++++--------------- .../transactions/MdsalRestconfStrategy.java | 5 +++ .../rests/transactions/RestconfStrategy.java | 24 ++++++++++++ 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImpl.java b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImpl.java index e5da72e67f..1a383475f4 100644 --- a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImpl.java +++ b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImpl.java @@ -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 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 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()); + }); } /** diff --git a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/MdsalRestconfStrategy.java b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/MdsalRestconfStrategy.java index 1c090a6160..c2eb2fbd9e 100644 --- a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/MdsalRestconfStrategy.java +++ b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/MdsalRestconfStrategy.java @@ -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(); diff --git a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/RestconfStrategy.java b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/RestconfStrategy.java index 1795d9ea23..0c01e6784f 100644 --- a/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/RestconfStrategy.java +++ b/restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/RestconfStrategy.java @@ -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 forMountPoint(final DOMMountPoint mountPoint) { + final Optional 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. */ -- 2.36.6