Reconstruct inference stack during normalization
[netconf.git] / restconf / restconf-nb-rfc8040 / 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.Path;
13 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
14 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.IetfYangLibrary;
15 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
16 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
17 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfService;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.Restconf;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
21 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
24 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
25
26 @Path("/")
27 public class RestconfImpl implements RestconfService {
28     private static final QName YANG_LIBRARY_VERSION = QName.create(Restconf.QNAME, "yang-library-version").intern();
29
30     private final SchemaContextHandler schemaContextHandler;
31
32     public RestconfImpl(final SchemaContextHandler schemaContextHandler) {
33         this.schemaContextHandler = requireNonNull(schemaContextHandler);
34     }
35
36     @Override
37     public NormalizedNodePayload getLibraryVersion() {
38         final EffectiveModelContext context = schemaContextHandler.get();
39
40         // FIXME: why are we going through a grouping here?!
41         final GroupingDefinition grouping = context
42             .findModule(Restconf.QNAME.getModule())
43             .orElseThrow(() -> new IllegalStateException("Failed to find restcibf module"))
44             .getGroupings().stream()
45             .filter(grp -> Restconf.QNAME.equals(grp.getQName()))
46             .findFirst()
47             .orElseThrow(() -> new IllegalStateException("Failed to find restconf grouping"));
48
49         final LeafSchemaNode schemaNode =
50             (LeafSchemaNode) ((ContainerSchemaNode) grouping.getDataChildByName(Restconf.QNAME))
51             .getDataChildByName(YANG_LIBRARY_VERSION);
52
53         return NormalizedNodePayload.of(InstanceIdentifierContext.ofDataSchemaNode(context, schemaNode, null),
54             ImmutableNodes.leafNode(YANG_LIBRARY_VERSION, IetfYangLibrary.REVISION.toString()));
55     }
56 }