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