2 * Copyright (c) 2023 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.services.impl;
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.collect.ImmutableMap;
15 import com.google.common.collect.Maps;
16 import java.io.IOException;
17 import java.lang.invoke.MethodHandles;
18 import java.lang.invoke.VarHandle;
20 import java.util.List;
21 import javax.inject.Inject;
22 import javax.inject.Singleton;
23 import org.eclipse.jdt.annotation.NonNull;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
26 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
27 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
28 import org.opendaylight.mdsal.dom.api.DOMRpcService;
29 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
30 import org.opendaylight.restconf.common.errors.RestconfFuture;
31 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindContext;
32 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindProvider;
33 import org.opendaylight.restconf.nb.rfc8040.databind.OperationInputBody;
34 import org.opendaylight.restconf.nb.rfc8040.legacy.InstanceIdentifierContext;
35 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
36 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
37 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
38 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
39 import org.opendaylight.restconf.server.api.RestconfServer;
40 import org.opendaylight.restconf.server.spi.OperationInput;
41 import org.opendaylight.restconf.server.spi.OperationOutput;
42 import org.opendaylight.restconf.server.spi.RpcImplementation;
43 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.YangApi;
44 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.restconf.Restconf;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibrary;
46 import org.opendaylight.yangtools.yang.common.ErrorTag;
47 import org.opendaylight.yangtools.yang.common.ErrorType;
48 import org.opendaylight.yangtools.yang.common.QName;
49 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
50 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
51 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
52 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
53 import org.osgi.service.component.annotations.Activate;
54 import org.osgi.service.component.annotations.Component;
55 import org.osgi.service.component.annotations.Reference;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
60 * A RESTCONF server implemented on top of MD-SAL.
62 // FIXME: this should live in 'org.opendaylight.restconf.server.mdsal' package
64 @Component(service = { MdsalRestconfServer.class, RestconfServer.class })
65 public final class MdsalRestconfServer implements RestconfServer {
66 private static final Logger LOG = LoggerFactory.getLogger(MdsalRestconfServer.class);
67 private static final QName YANG_LIBRARY_VERSION = QName.create(Restconf.QNAME, "yang-library-version").intern();
68 private static final String YANG_LIBRARY_REVISION = YangLibrary.QNAME.getRevision().orElseThrow().toString();
69 private static final VarHandle LOCAL_STRATEGY;
73 LOCAL_STRATEGY = MethodHandles.lookup()
74 .findVarHandle(MdsalRestconfServer.class, "localStrategy", RestconfStrategy.class);
75 } catch (NoSuchFieldException | IllegalAccessException e) {
76 throw new ExceptionInInitializerError(e);
80 private final @NonNull ImmutableMap<QName, RpcImplementation> localRpcs;
81 private final @NonNull DOMMountPointService mountPointService;
82 private final @NonNull DatabindProvider databindProvider;
83 private final @NonNull DOMDataBroker dataBroker;
84 private final @Nullable DOMRpcService rpcService;
86 @SuppressWarnings("unused")
87 private volatile RestconfStrategy localStrategy;
91 public MdsalRestconfServer(@Reference final DatabindProvider databindProvider,
92 @Reference final DOMDataBroker dataBroker, @Reference final DOMRpcService rpcService,
93 @Reference final DOMMountPointService mountPointService,
94 @Reference final List<RpcImplementation> localRpcs) {
95 this.databindProvider = requireNonNull(databindProvider);
96 this.dataBroker = requireNonNull(dataBroker);
97 this.rpcService = requireNonNull(rpcService);
98 this.mountPointService = requireNonNull(mountPointService);
99 this.localRpcs = Maps.uniqueIndex(localRpcs, RpcImplementation::qname);
102 public MdsalRestconfServer(final DatabindProvider databind, final DOMDataBroker dataBroker,
103 final DOMRpcService rpcService, final DOMMountPointService mountPointService,
104 final RpcImplementation... localRpcs) {
105 this(databind, dataBroker, rpcService, mountPointService, List.of(localRpcs));
108 @NonNull InstanceIdentifierContext bindRequestPath(final String identifier) {
109 return bindRequestPath(databindProvider.currentContext(), identifier);
113 @NonNull InstanceIdentifierContext bindRequestPath(final DatabindContext databind, final String identifier) {
114 // FIXME: go through ApiPath first. That part should eventually live in callers
115 // FIXME: DatabindContext looks like it should be internal
116 return verifyNotNull(ParserIdentifier.toInstanceIdentifier(requireNonNull(identifier), databind.modelContext(),
120 public @NonNull NormalizedNodePayload yangLibraryVersionGET() {
121 final var stack = SchemaInferenceStack.of(databindProvider.currentContext().modelContext());
122 stack.enterYangData(YangApi.NAME);
123 stack.enterDataTree(Restconf.QNAME);
124 stack.enterDataTree(YANG_LIBRARY_VERSION);
125 return new NormalizedNodePayload(stack.toInference(),
126 ImmutableNodes.leafNode(YANG_LIBRARY_VERSION, YANG_LIBRARY_REVISION));
130 public RestconfFuture<OperationOutput> invokeRpc(final URI restconfURI, final String apiPath,
131 final OperationInputBody body) {
132 final var currentContext = databindProvider.currentContext();
133 final var reqPath = bindRequestPath(currentContext, apiPath);
134 final var inference = reqPath.inference();
135 final ContainerNode input;
137 input = body.toContainerNode(inference);
138 } catch (IOException e) {
139 LOG.debug("Error reading input", e);
140 return RestconfFuture.failed(new RestconfDocumentedException("Error parsing input: " + e.getMessage(),
141 ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE, e));
144 return getRestconfStrategy(reqPath.getSchemaContext(), reqPath.getMountPoint())
145 .invokeRpc(restconfURI, reqPath.getSchemaNode().getQName(),
146 new OperationInput(currentContext, inference, input));
149 @NonNull InstanceIdentifierContext bindRequestRoot() {
150 return InstanceIdentifierContext.ofLocalRoot(databindProvider.currentContext().modelContext());
154 @NonNull RestconfStrategy getRestconfStrategy(final EffectiveModelContext modelContext,
155 final @Nullable DOMMountPoint mountPoint) {
156 if (mountPoint == null) {
157 return localStrategy(modelContext);
160 final var ret = RestconfStrategy.forMountPoint(modelContext, mountPoint);
162 final var mountId = mountPoint.getIdentifier();
163 LOG.warn("Mount point {} does not expose a suitable access interface", mountId);
164 throw new RestconfDocumentedException("Could not find a supported access interface in mount point "
170 private @NonNull RestconfStrategy localStrategy(final EffectiveModelContext modelContext) {
171 final var local = (RestconfStrategy) LOCAL_STRATEGY.getAcquire(this);
172 if (local != null && modelContext.equals(local.modelContext())) {
176 final var created = new MdsalRestconfStrategy(modelContext, dataBroker, rpcService, localRpcs);
177 LOCAL_STRATEGY.setRelease(this, created);