2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.restconf.nb.rfc8040.rests.transactions;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.List;
14 import java.util.Optional;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
18 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
19 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
20 import org.opendaylight.restconf.common.errors.RestconfFuture;
21 import org.opendaylight.restconf.common.errors.SettableRestconfFuture;
22 import org.opendaylight.yangtools.yang.common.Empty;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 * Baseline execution strategy for various RESTCONF operations.
29 * @see NetconfRestconfStrategy
30 * @see MdsalRestconfStrategy
32 // FIXME: it seems the first three operations deal with lifecycle of a transaction, while others invoke various
33 // operations. This should be handled through proper allocation indirection.
34 public abstract class RestconfStrategy {
40 * Look up the appropriate strategy for a particular mount point.
42 * @param mountPoint Target mount point
43 * @return A strategy, or null if the mount point does not expose a supported interface
44 * @throws NullPointerException if {@code mountPoint} is null
46 public static Optional<RestconfStrategy> forMountPoint(final DOMMountPoint mountPoint) {
47 final Optional<RestconfStrategy> netconf = mountPoint.getService(NetconfDataTreeService.class)
48 .map(NetconfRestconfStrategy::new);
49 if (netconf.isPresent()) {
53 return mountPoint.getService(DOMDataBroker.class).map(MdsalRestconfStrategy::new);
57 * Lock the entire datastore.
59 * @return A {@link RestconfTransaction}. This transaction needs to be either committed or canceled before doing
62 public abstract RestconfTransaction prepareWriteExecution();
65 * Read data from the datastore.
67 * @param store the logical data store which should be modified
68 * @param path the data object path
69 * @return a ListenableFuture containing the result of the read
71 public abstract ListenableFuture<Optional<NormalizedNode>> read(LogicalDatastoreType store,
72 YangInstanceIdentifier path);
75 * Read data selected using fields from the datastore.
77 * @param store the logical data store which should be modified
78 * @param path the parent data object path
79 * @param fields paths to selected fields relative to parent path
80 * @return a ListenableFuture containing the result of the read
82 public abstract ListenableFuture<Optional<NormalizedNode>> read(LogicalDatastoreType store,
83 YangInstanceIdentifier path, List<YangInstanceIdentifier> fields);
86 * Check if data already exists in the datastore.
88 * @param store the logical data store which should be modified
89 * @param path the data object path
90 * @return a FluentFuture containing the result of the check
92 public abstract ListenableFuture<Boolean> exists(LogicalDatastoreType store, YangInstanceIdentifier path);
95 * Delete data from the configuration datastore. If the data does not exist, this operation will fail, as outlined
96 * in <a href="https://www.rfc-editor.org/rfc/rfc8040#section-4.7">RFC8040 section 4.7</a>
98 * @param path Path to delete
99 * @return A {@link RestconfFuture}
100 * @throws NullPointerException if {@code path} is {@code null}
102 public final @NonNull RestconfFuture<Empty> delete(final YangInstanceIdentifier path) {
103 final var ret = new SettableRestconfFuture<Empty>();
104 delete(ret, requireNonNull(path));
108 protected abstract void delete(@NonNull SettableRestconfFuture<Empty> future, @NonNull YangInstanceIdentifier path);