Introduce RestconfServer methods for YANG Patch
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / MdsalRestconfServer.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.nb.rfc8040.rests.services.impl;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.collect.HashBasedTable;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.ImmutableSet;
17 import com.google.common.collect.ImmutableSetMultimap;
18 import com.google.common.collect.Maps;
19 import com.google.common.util.concurrent.FutureCallback;
20 import com.google.common.util.concurrent.Futures;
21 import com.google.common.util.concurrent.MoreExecutors;
22 import java.io.IOException;
23 import java.lang.invoke.MethodHandles;
24 import java.lang.invoke.VarHandle;
25 import java.net.URI;
26 import java.util.Comparator;
27 import java.util.List;
28 import java.util.Map.Entry;
29 import java.util.concurrent.CancellationException;
30 import javax.inject.Inject;
31 import javax.inject.Singleton;
32 import org.eclipse.jdt.annotation.NonNull;
33 import org.eclipse.jdt.annotation.Nullable;
34 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
35 import org.opendaylight.mdsal.dom.api.DOMActionException;
36 import org.opendaylight.mdsal.dom.api.DOMActionResult;
37 import org.opendaylight.mdsal.dom.api.DOMActionService;
38 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
39 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
40 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
41 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
42 import org.opendaylight.mdsal.dom.api.DOMRpcService;
43 import org.opendaylight.mdsal.dom.spi.SimpleDOMActionResult;
44 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
45 import org.opendaylight.restconf.common.errors.RestconfFuture;
46 import org.opendaylight.restconf.common.errors.SettableRestconfFuture;
47 import org.opendaylight.restconf.common.patch.PatchContext;
48 import org.opendaylight.restconf.common.patch.PatchStatusContext;
49 import org.opendaylight.restconf.nb.rfc8040.ReadDataParams;
50 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindContext;
51 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindProvider;
52 import org.opendaylight.restconf.nb.rfc8040.databind.OperationInputBody;
53 import org.opendaylight.restconf.nb.rfc8040.databind.PatchBody;
54 import org.opendaylight.restconf.nb.rfc8040.databind.ResourceBody;
55 import org.opendaylight.restconf.nb.rfc8040.databind.jaxrs.QueryParams;
56 import org.opendaylight.restconf.nb.rfc8040.legacy.InstanceIdentifierContext;
57 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
58 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
59 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
60 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
61 import org.opendaylight.restconf.server.api.OperationsContent;
62 import org.opendaylight.restconf.server.api.RestconfServer;
63 import org.opendaylight.restconf.server.spi.OperationInput;
64 import org.opendaylight.restconf.server.spi.OperationOutput;
65 import org.opendaylight.restconf.server.spi.RpcImplementation;
66 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.YangApi;
67 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.restconf.Restconf;
68 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibrary;
69 import org.opendaylight.yangtools.yang.common.Empty;
70 import org.opendaylight.yangtools.yang.common.ErrorTag;
71 import org.opendaylight.yangtools.yang.common.ErrorType;
72 import org.opendaylight.yangtools.yang.common.QName;
73 import org.opendaylight.yangtools.yang.common.QNameModule;
74 import org.opendaylight.yangtools.yang.common.Revision;
75 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
76 import org.opendaylight.yangtools.yang.common.XMLNamespace;
77 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
78 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
79 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
80 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
81 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
82 import org.opendaylight.yangtools.yang.model.api.stmt.RpcEffectiveStatement;
83 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
84 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
85 import org.osgi.service.component.annotations.Activate;
86 import org.osgi.service.component.annotations.Component;
87 import org.osgi.service.component.annotations.Reference;
88 import org.slf4j.Logger;
89 import org.slf4j.LoggerFactory;
90
91 /**
92  * A RESTCONF server implemented on top of MD-SAL.
93  */
94 // FIXME: this should live in 'org.opendaylight.restconf.server.mdsal' package
95 @Singleton
96 @Component(service = { MdsalRestconfServer.class, RestconfServer.class })
97 public final class MdsalRestconfServer implements RestconfServer {
98     private static final Logger LOG = LoggerFactory.getLogger(MdsalRestconfServer.class);
99     private static final QName YANG_LIBRARY_VERSION = QName.create(Restconf.QNAME, "yang-library-version").intern();
100     private static final String YANG_LIBRARY_REVISION = YangLibrary.QNAME.getRevision().orElseThrow().toString();
101     private static final VarHandle LOCAL_STRATEGY;
102
103     static {
104         try {
105             LOCAL_STRATEGY = MethodHandles.lookup()
106                 .findVarHandle(MdsalRestconfServer.class, "localStrategy", RestconfStrategy.class);
107         } catch (NoSuchFieldException | IllegalAccessException e) {
108             throw new ExceptionInInitializerError(e);
109         }
110     }
111
112     private final @NonNull ImmutableMap<QName, RpcImplementation> localRpcs;
113     private final @NonNull DOMMountPointService mountPointService;
114     private final @NonNull DatabindProvider databindProvider;
115     private final @NonNull DOMDataBroker dataBroker;
116     private final @Nullable DOMRpcService rpcService;
117     private final @Nullable DOMActionService actionService;
118
119     @SuppressWarnings("unused")
120     private volatile RestconfStrategy localStrategy;
121
122     @Inject
123     @Activate
124     public MdsalRestconfServer(@Reference final DatabindProvider databindProvider,
125             @Reference final DOMDataBroker dataBroker, @Reference final DOMRpcService rpcService,
126             @Reference final DOMActionService actionService,
127             @Reference final DOMMountPointService mountPointService,
128             @Reference final List<RpcImplementation> localRpcs) {
129         this.databindProvider = requireNonNull(databindProvider);
130         this.dataBroker = requireNonNull(dataBroker);
131         this.rpcService = requireNonNull(rpcService);
132         this.actionService = requireNonNull(actionService);
133         this.mountPointService = requireNonNull(mountPointService);
134         this.localRpcs = Maps.uniqueIndex(localRpcs, RpcImplementation::qname);
135     }
136
137     public MdsalRestconfServer(final DatabindProvider databindProvider, final DOMDataBroker dataBroker,
138             final DOMRpcService rpcService, final DOMActionService actionService,
139             final DOMMountPointService mountPointService, final RpcImplementation... localRpcs) {
140         this(databindProvider, dataBroker, rpcService, actionService, mountPointService, List.of(localRpcs));
141     }
142
143     @Override
144     public RestconfFuture<Empty> dataDELETE(final String identifier) {
145         final var reqPath = bindRequestPath(identifier);
146         final var strategy = getRestconfStrategy(reqPath.getSchemaContext(), reqPath.getMountPoint());
147         return strategy.delete(reqPath.getInstanceIdentifier());
148     }
149
150     @Override
151     public RestconfFuture<NormalizedNodePayload> dataGET(final ReadDataParams readParams) {
152         return readData(bindRequestRoot(), readParams);
153     }
154
155     @Override
156     public RestconfFuture<NormalizedNodePayload> dataGET(final String identifier, final ReadDataParams readParams) {
157         return readData(bindRequestPath(identifier), readParams);
158     }
159
160     private @NonNull RestconfFuture<NormalizedNodePayload> readData(final InstanceIdentifierContext reqPath,
161             final ReadDataParams readParams) {
162         final var queryParams = QueryParams.newQueryParameters(readParams, reqPath);
163         final var fieldPaths = queryParams.fieldPaths();
164         final var strategy = getRestconfStrategy(reqPath.getSchemaContext(), reqPath.getMountPoint());
165         final NormalizedNode node;
166         if (fieldPaths != null && !fieldPaths.isEmpty()) {
167             node = strategy.readData(readParams.content(), reqPath.getInstanceIdentifier(),
168                 readParams.withDefaults(), fieldPaths);
169         } else {
170             node = strategy.readData(readParams.content(), reqPath.getInstanceIdentifier(),
171                 readParams.withDefaults());
172         }
173         if (node == null) {
174             return RestconfFuture.failed(new RestconfDocumentedException(
175                 "Request could not be completed because the relevant data model content does not exist",
176                 ErrorType.PROTOCOL, ErrorTag.DATA_MISSING));
177         }
178
179         return RestconfFuture.of(new NormalizedNodePayload(reqPath.inference(), node, queryParams));
180     }
181
182     @Override
183     public RestconfFuture<Empty> dataPATCH(final ResourceBody body) {
184         return dataPATCH(bindRequestRoot(), body);
185     }
186
187     @Override
188     public RestconfFuture<Empty> dataPATCH(final String identifier, final ResourceBody body) {
189         return dataPATCH(bindRequestPath(identifier), body);
190     }
191
192     private @NonNull RestconfFuture<Empty> dataPATCH(final InstanceIdentifierContext reqPath, final ResourceBody body) {
193         final var req = bindResourceRequest(reqPath, body);
194         return req.strategy().merge(req.path(), req.data());
195     }
196
197     @Override
198     public RestconfFuture<PatchStatusContext> dataPATCH(final PatchBody body) {
199         return dataPATCH(bindRequestRoot(), body);
200     }
201
202     @Override
203     public RestconfFuture<PatchStatusContext> dataPATCH(final String identifier, final PatchBody body) {
204         return dataPATCH(bindRequestPath(identifier), body);
205     }
206
207     private @NonNull RestconfFuture<PatchStatusContext> dataPATCH(final InstanceIdentifierContext reqPath,
208             final PatchBody body) {
209         final var modelContext = reqPath.getSchemaContext();
210         final PatchContext patch;
211         try {
212             patch = body.toPatchContext(modelContext, reqPath.getInstanceIdentifier());
213         } catch (IOException e) {
214             LOG.debug("Error parsing YANG Patch input", e);
215             return RestconfFuture.failed(new RestconfDocumentedException("Error parsing input: " + e.getMessage(),
216                 ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE, e));
217         }
218         return getRestconfStrategy(modelContext, reqPath.getMountPoint()).patchData(patch);
219     }
220
221     // FIXME: should follow the same pattern as operationsPOST() does
222     RestconfFuture<DOMActionResult> dataInvokePOST(final InstanceIdentifierContext reqPath,
223             final OperationInputBody body) {
224         final var yangIIdContext = reqPath.getInstanceIdentifier();
225         final var inference = reqPath.inference();
226         final ContainerNode input;
227         try {
228             input = body.toContainerNode(inference);
229         } catch (IOException e) {
230             LOG.debug("Error reading input", e);
231             return RestconfFuture.failed(new RestconfDocumentedException("Error parsing input: " + e.getMessage(),
232                 ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE, e));
233         }
234
235         final var mountPoint = reqPath.getMountPoint();
236         final var schemaPath = inference.toSchemaInferenceStack().toSchemaNodeIdentifier();
237         return mountPoint != null ? dataInvokePOST(input, schemaPath, yangIIdContext, mountPoint)
238             : dataInvokePOST(input, schemaPath, yangIIdContext, actionService);
239     }
240
241     /**
242      * Invoke Action via ActionServiceHandler.
243      *
244      * @param data input data
245      * @param yangIId invocation context
246      * @param schemaPath schema path of data
247      * @param actionService action service to invoke action
248      * @return {@link DOMActionResult}
249      */
250     private static RestconfFuture<DOMActionResult> dataInvokePOST(final ContainerNode data, final Absolute schemaPath,
251             final YangInstanceIdentifier yangIId, final DOMActionService actionService) {
252         final var ret = new SettableRestconfFuture<DOMActionResult>();
253
254         Futures.addCallback(actionService.invokeAction(schemaPath,
255             new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, yangIId.getParent()), data),
256             new FutureCallback<DOMActionResult>() {
257                 @Override
258                 public void onSuccess(final DOMActionResult result) {
259                     final var errors = result.getErrors();
260                     LOG.debug("InvokeAction Error Message {}", errors);
261                     if (errors.isEmpty()) {
262                         ret.set(result);
263                     } else {
264                         ret.setFailure(new RestconfDocumentedException("InvokeAction Error Message ", null, errors));
265                     }
266                 }
267
268                 @Override
269                 public void onFailure(final Throwable cause) {
270                     if (cause instanceof DOMActionException) {
271                         ret.set(new SimpleDOMActionResult(List.of(RpcResultBuilder.newError(
272                             ErrorType.RPC, ErrorTag.OPERATION_FAILED, cause.getMessage()))));
273                     } else if (cause instanceof RestconfDocumentedException e) {
274                         ret.setFailure(e);
275                     } else if (cause instanceof CancellationException) {
276                         ret.setFailure(new RestconfDocumentedException("Action cancelled while executing",
277                             ErrorType.RPC, ErrorTag.PARTIAL_OPERATION, cause));
278                     } else {
279                         ret.setFailure(new RestconfDocumentedException("Invocation failed", cause));
280                     }
281                 }
282             }, MoreExecutors.directExecutor());
283
284         return ret;
285     }
286
287     /**
288      * Invoking Action via mount point.
289      *
290      * @param mountPoint mount point
291      * @param data input data
292      * @param schemaPath schema path of data
293      * @return {@link DOMActionResult}
294      */
295     private static RestconfFuture<DOMActionResult> dataInvokePOST(final ContainerNode data,
296             final Absolute schemaPath, final YangInstanceIdentifier yangIId, final DOMMountPoint mountPoint) {
297         final var actionService = mountPoint.getService(DOMActionService.class);
298         return actionService.isPresent() ? dataInvokePOST(data, schemaPath, yangIId, actionService.orElseThrow())
299             : RestconfFuture.failed(new RestconfDocumentedException("DOMActionService is missing."));
300     }
301
302     @Override
303     public OperationsContent operationsGET() {
304         return operationsGET(databindProvider.currentContext().modelContext());
305     }
306
307     @Override
308     public OperationsContent operationsGET(final String operation) {
309         // get current module RPCs/actions by RPC/action name
310         final var inference = bindRequestPath(operation).inference();
311         if (inference.isEmpty()) {
312             return operationsGET(inference.getEffectiveModelContext());
313         }
314
315         final var stmt = inference.toSchemaInferenceStack().currentStatement();
316         if (stmt instanceof RpcEffectiveStatement rpc) {
317             return new OperationsContent.Leaf(inference.getEffectiveModelContext(), rpc.argument());
318         }
319         LOG.debug("Operation '{}' resulted in non-RPC {}", operation, stmt);
320         return null;
321     }
322
323     private static @NonNull OperationsContent operationsGET(final EffectiveModelContext modelContext) {
324         final var modules = modelContext.getModuleStatements();
325         if (modules.isEmpty()) {
326             // No modules, or defensive return empty content
327             return new OperationsContent.Container(modelContext, ImmutableSetMultimap.of());
328         }
329
330         // RPCs by their XMLNamespace/Revision
331         final var table = HashBasedTable.<XMLNamespace, Revision, ImmutableSet<QName>>create();
332         for (var entry : modules.entrySet()) {
333             final var module = entry.getValue();
334             final var rpcNames = module.streamEffectiveSubstatements(RpcEffectiveStatement.class)
335                 .map(RpcEffectiveStatement::argument)
336                 .collect(ImmutableSet.toImmutableSet());
337             if (!rpcNames.isEmpty()) {
338                 final var namespace = entry.getKey();
339                 table.put(namespace.getNamespace(), namespace.getRevision().orElse(null), rpcNames);
340             }
341         }
342
343         // Now pick the latest revision for each namespace
344         final var rpcs = ImmutableSetMultimap.<QNameModule, QName>builder();
345         for (var entry : table.rowMap().entrySet()) {
346             entry.getValue().entrySet().stream()
347                 .sorted(Comparator.comparing(Entry::getKey, (first, second) -> Revision.compare(second, first)))
348                 .findFirst()
349                 .ifPresent(row -> rpcs.putAll(QNameModule.create(entry.getKey(), row.getKey()), row.getValue()));
350         }
351         return new OperationsContent.Container(modelContext, rpcs.build());
352     }
353
354     @Override
355     public RestconfFuture<OperationOutput> operationsPOST(final URI restconfURI, final String apiPath,
356             final OperationInputBody body) {
357         final var currentContext = databindProvider.currentContext();
358         final var reqPath = bindRequestPath(currentContext, apiPath);
359         final var inference = reqPath.inference();
360         final ContainerNode input;
361         try {
362             input = body.toContainerNode(inference);
363         } catch (IOException e) {
364             LOG.debug("Error reading input", e);
365             return RestconfFuture.failed(new RestconfDocumentedException("Error parsing input: " + e.getMessage(),
366                 ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE, e));
367         }
368
369         return getRestconfStrategy(reqPath.getSchemaContext(), reqPath.getMountPoint())
370             .invokeRpc(restconfURI, reqPath.getSchemaNode().getQName(),
371                 new OperationInput(currentContext, inference, input));
372     }
373
374     @Override
375     public NormalizedNodePayload yangLibraryVersionGET() {
376         final var stack = SchemaInferenceStack.of(databindProvider.currentContext().modelContext());
377         stack.enterYangData(YangApi.NAME);
378         stack.enterDataTree(Restconf.QNAME);
379         stack.enterDataTree(YANG_LIBRARY_VERSION);
380         return new NormalizedNodePayload(stack.toInference(),
381             ImmutableNodes.leafNode(YANG_LIBRARY_VERSION, YANG_LIBRARY_REVISION));
382     }
383
384     @NonNull InstanceIdentifierContext bindRequestPath(final String identifier) {
385         return bindRequestPath(databindProvider.currentContext(), identifier);
386     }
387
388     @Deprecated
389     @NonNull InstanceIdentifierContext bindRequestPath(final DatabindContext databind, final String identifier) {
390         // FIXME: go through ApiPath first. That part should eventually live in callers
391         // FIXME: DatabindContext looks like it should be internal
392         return verifyNotNull(ParserIdentifier.toInstanceIdentifier(requireNonNull(identifier), databind.modelContext(),
393             mountPointService));
394     }
395
396     @NonNull InstanceIdentifierContext bindRequestRoot() {
397         return InstanceIdentifierContext.ofLocalRoot(databindProvider.currentContext().modelContext());
398     }
399
400     @NonNull ResourceRequest bindResourceRequest(final InstanceIdentifierContext reqPath, final ResourceBody body) {
401         final var inference = reqPath.inference();
402         final var path = reqPath.getInstanceIdentifier();
403         final var data = body.toNormalizedNode(path, inference, reqPath.getSchemaNode());
404
405         return new ResourceRequest(
406             getRestconfStrategy(inference.getEffectiveModelContext(), reqPath.getMountPoint()), path, data);
407     }
408
409     @VisibleForTesting
410     @NonNull RestconfStrategy getRestconfStrategy(final EffectiveModelContext modelContext,
411             final @Nullable DOMMountPoint mountPoint) {
412         if (mountPoint == null) {
413             return localStrategy(modelContext);
414         }
415
416         final var ret = RestconfStrategy.forMountPoint(modelContext, mountPoint);
417         if (ret == null) {
418             final var mountId = mountPoint.getIdentifier();
419             LOG.warn("Mount point {} does not expose a suitable access interface", mountId);
420             throw new RestconfDocumentedException("Could not find a supported access interface in mount point "
421                 + mountId);
422         }
423         return ret;
424     }
425
426     private @NonNull RestconfStrategy localStrategy(final EffectiveModelContext modelContext) {
427         final var local = (RestconfStrategy) LOCAL_STRATEGY.getAcquire(this);
428         if (local != null && modelContext.equals(local.modelContext())) {
429             return local;
430         }
431
432         final var created = new MdsalRestconfStrategy(modelContext, dataBroker, rpcService, localRpcs);
433         LOCAL_STRATEGY.setRelease(this, created);
434         return created;
435     }
436 }