Eliminate NormalizedNodePayload
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / spi / YangLibraryVersionResource.java
1 /*
2  * Copyright (c) 2024 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.server.spi;
9
10 import static java.util.Objects.requireNonNull;
11
12 import org.eclipse.jdt.annotation.NonNullByDefault;
13 import org.opendaylight.restconf.api.ApiPath;
14 import org.opendaylight.restconf.api.FormattableBody;
15 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
16 import org.opendaylight.restconf.common.errors.RestconfFuture;
17 import org.opendaylight.restconf.server.api.DatabindContext;
18 import org.opendaylight.restconf.server.api.ServerRequest;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.YangApi;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.restconf.Restconf;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
23 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
24 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
25 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * RESTCONF {@code /yang-library-version} content for a {@code GET} operation as per
31  * <a href="https://www.rfc-editor.org/rfc/rfc8040#section-3.3.3">RFC8040, section 3.3.3</a>.
32  */
33 @NonNullByDefault
34 public record YangLibraryVersionResource(DatabindContext databind, Inference restconf, LeafNode<String> leaf)
35         implements HttpGetResource {
36     private static final Logger LOG = LoggerFactory.getLogger(YangLibraryVersionResource.class);
37     private static final QName YANG_LIBRARY_VERSION = QName.create(Restconf.QNAME, "yang-library-version").intern();
38
39     public YangLibraryVersionResource {
40         requireNonNull(databind);
41         requireNonNull(restconf);
42         requireNonNull(leaf);
43     }
44
45     public static HttpGetResource of(final DatabindContext databind) {
46         final var modelContext = databind.modelContext();
47
48         final Inference leafInference;
49         try {
50             final var stack = SchemaInferenceStack.of(modelContext);
51             stack.enterYangData(YangApi.NAME);
52             stack.enterDataTree(Restconf.QNAME);
53             stack.enterDataTree(YANG_LIBRARY_VERSION);
54             stack.exitToDataTree();
55             leafInference = stack.toInference();
56         } catch (IllegalArgumentException e) {
57             LOG.debug("Cannot find yang-library-version", e);
58             return new FailedHttpGetResource(new RestconfDocumentedException(
59                 "yang-library-version is not available", e));
60         }
61
62         final var it = modelContext.findModuleStatements("ietf-yang-library").iterator();
63         if (!it.hasNext()) {
64             LOG.debug("Cannot find ietf-yang-library");
65             return new FailedHttpGetResource(new RestconfDocumentedException("No ietf-yang-library present"));
66         }
67
68         return new YangLibraryVersionResource(databind, leafInference,
69             ImmutableNodes.leafNode(YANG_LIBRARY_VERSION, it.next().localQNameModule().revisionUnion().unionString()));
70     }
71
72     @Override
73     public RestconfFuture<FormattableBody> httpGET(final ServerRequest request) {
74         return RestconfFuture.of(new DataFormattableBody<>(databind, restconf, leaf));
75     }
76
77     @Override
78     public RestconfFuture<FormattableBody> httpGET(final ServerRequest request, final ApiPath apiPath) {
79         throw new UnsupportedOperationException();
80     }
81 }