2 * Copyright (c) 2022 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.server.mdsal;
10 import static java.util.Objects.requireNonNull;
11 import static org.opendaylight.yang.svc.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.monitoring.rev170126.YangModuleInfoImpl.qnameOf;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import javax.annotation.PreDestroy;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.checkerframework.checker.lock.qual.Holding;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.mdsal.common.api.CommitInfo;
22 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
23 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
24 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
25 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
26 import org.opendaylight.restconf.api.query.AbstractReplayParam;
27 import org.opendaylight.restconf.api.query.ChangedLeafNodesOnlyParam;
28 import org.opendaylight.restconf.api.query.ChildNodesOnlyParam;
29 import org.opendaylight.restconf.api.query.DepthParam;
30 import org.opendaylight.restconf.api.query.FieldsParam;
31 import org.opendaylight.restconf.api.query.FilterParam;
32 import org.opendaylight.restconf.api.query.LeafNodesOnlyParam;
33 import org.opendaylight.restconf.api.query.PrettyPrintParam;
34 import org.opendaylight.restconf.api.query.SkipNotificationDataParam;
35 import org.opendaylight.restconf.api.query.WithDefaultsParam;
36 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.monitoring.rev170126.RestconfState;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.monitoring.rev170126.restconf.state.Capabilities;
38 import org.opendaylight.yangtools.concepts.Registration;
39 import org.opendaylight.yangtools.yang.common.Empty;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
43 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
44 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
45 import org.osgi.service.component.annotations.Activate;
46 import org.osgi.service.component.annotations.Component;
47 import org.osgi.service.component.annotations.Deactivate;
48 import org.osgi.service.component.annotations.Reference;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
53 * A simple component which maintains {@link Capabilities} in the operational datastore.
56 @Component(service = { })
57 public final class CapabilitiesWriter implements AutoCloseable, FutureCallback<Empty> {
58 private static final Logger LOG = LoggerFactory.getLogger(CapabilitiesWriter.class);
61 static final @NonNull NodeIdentifier CAPABILITY = NodeIdentifier.create(qnameOf("capability"));
63 private static final YangInstanceIdentifier PATH = YangInstanceIdentifier.of(
64 NodeIdentifier.create(RestconfState.QNAME), NodeIdentifier.create(Capabilities.QNAME), CAPABILITY);
66 private final DOMDataBroker dataBroker;
68 private DOMTransactionChain txChain;
69 private Registration reg;
71 private boolean written;
75 public CapabilitiesWriter(@Reference final DOMDataBroker dataBroker,
76 @Reference final DOMSchemaService schemaService) {
77 this.dataBroker = requireNonNull(dataBroker);
78 reg = schemaService.registerSchemaContextListener(this::onModelContextUpdated);
84 public synchronized void close() {
90 deleteRestconfState();
91 if (txChain != null) {
97 public synchronized void onFailure(final Throwable cause) {
98 LOG.warn("Transaction chain failed, updates may not have been propagated", cause);
103 public synchronized void onSuccess(final Empty result) {
104 LOG.debug("Transaction chain closed successfully");
108 private synchronized void onModelContextUpdated(final EffectiveModelContext newModelContext) {
110 LOG.debug("Ignoring model context update");
114 if (newModelContext.findModuleStatement(RestconfState.QNAME.getModule()).isPresent()) {
115 writeRestconfState();
117 deleteRestconfState();
122 private void deleteRestconfState() {
124 LOG.debug("No state recorded as written, not attempting removal");
128 LOG.debug("Removing ietf-restconf-monitoring state");
129 if (txChain == null) {
130 txChain = dataBroker.createMergingTransactionChain();
131 txChain.addCallback(this);
134 final var tx = txChain.newWriteOnlyTransaction();
135 tx.delete(LogicalDatastoreType.OPERATIONAL, PATH);
136 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
138 public void onSuccess(final CommitInfo result) {
143 public void onFailure(final Throwable cause) {
144 // Ignored, will be reported on the transaction chain
146 }, MoreExecutors.directExecutor());
150 private void writeRestconfState() {
152 LOG.debug("State recorded as written, not updating it");
156 LOG.debug("Updating state of ietf-restconf-monitoring");
157 if (txChain == null) {
158 txChain = dataBroker.createMergingTransactionChain();
159 txChain.addCallback(this);
162 final var tx = txChain.newWriteOnlyTransaction();
163 tx.put(LogicalDatastoreType.OPERATIONAL, PATH, mapCapabilities());
164 tx.commit().addCallback(new FutureCallback<CommitInfo>() {
166 public void onSuccess(final CommitInfo result) {
171 public void onFailure(final Throwable cause) {
172 // Ignored, will be reported on the transaction chain
174 }, MoreExecutors.directExecutor());
177 private synchronized void markWritten() {
178 LOG.debug("State of ietf-restconf-monitoring updated");
182 private synchronized void markUnwritten() {
183 LOG.debug("State of ietf-restconf-monitoring removed");
188 * Create a {@code restconf-state} container.
190 * @return A container holding capabilities
193 static @NonNull LeafSetNode<String> mapCapabilities() {
194 return ImmutableNodes.<String>newSystemLeafSetBuilder()
195 .withNodeIdentifier(CAPABILITY)
196 .withChildValue(DepthParam.capabilityUri().toString())
197 .withChildValue(FieldsParam.capabilityUri().toString())
198 .withChildValue(FilterParam.capabilityUri().toString())
199 .withChildValue(AbstractReplayParam.capabilityUri().toString())
200 .withChildValue(WithDefaultsParam.capabilityUri().toString())
201 .withChildValue(PrettyPrintParam.capabilityUri().toString())
202 .withChildValue(LeafNodesOnlyParam.capabilityUri().toString())
203 .withChildValue(ChangedLeafNodesOnlyParam.capabilityUri().toString())
204 .withChildValue(SkipNotificationDataParam.capabilityUri().toString())
205 .withChildValue(ChildNodesOnlyParam.capabilityUri().toString())