Refactor NormalizedNodePayload
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / RestconfImpl.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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 java.util.Objects.requireNonNull;
11
12 import javax.ws.rs.GET;
13 import javax.ws.rs.Path;
14 import javax.ws.rs.Produces;
15 import javax.ws.rs.core.MediaType;
16 import org.opendaylight.restconf.nb.rfc8040.MediaTypes;
17 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindProvider;
18 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.Restconf;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.library.rev190104.YangLibrary;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
25
26 /**
27  * Service for getting the revision of {@code ietf-yang-library}.
28  */
29 @Path("/")
30 public final class RestconfImpl {
31     private static final QName YANG_LIBRARY_VERSION = QName.create(Restconf.QNAME, "yang-library-version").intern();
32     private static final String YANG_LIBRARY_REVISION = YangLibrary.QNAME.getRevision().orElseThrow().toString();
33
34     private final DatabindProvider databindProvider;
35
36     public RestconfImpl(final DatabindProvider databindProvider) {
37         this.databindProvider = requireNonNull(databindProvider);
38     }
39
40     /**
41      * Get revision of IETF YANG Library module.
42      *
43      * @return {@link NormalizedNodePayload}
44      */
45     @GET
46     @Path("/yang-library-version")
47     @Produces({
48         MediaTypes.APPLICATION_YANG_DATA_JSON,
49         MediaTypes.APPLICATION_YANG_DATA_XML,
50         MediaType.APPLICATION_JSON,
51         MediaType.APPLICATION_XML,
52         MediaType.TEXT_XML
53     })
54     public NormalizedNodePayload getLibraryVersion() {
55         final EffectiveModelContext context = databindProvider.currentContext().modelContext();
56
57         final SchemaInferenceStack stack = SchemaInferenceStack.of(context);
58         // FIXME: use rc:data instantiation once the stack supports it
59         stack.enterGrouping(Restconf.QNAME);
60         stack.enterDataTree(Restconf.QNAME);
61         stack.enterDataTree(YANG_LIBRARY_VERSION);
62
63         return new NormalizedNodePayload(stack.toInference(),
64             ImmutableNodes.leafNode(YANG_LIBRARY_VERSION, YANG_LIBRARY_REVISION));
65     }
66 }