Split out RFC8040-only constructs from yang-common
[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.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
22 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
25 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
26
27 @Path("/")
28 public class RestconfImpl implements RestconfService {
29     private static final QName YANG_LIBRARY_VERSION = QName.create(Restconf.QNAME, "yang-library-version").intern();
30
31     private final SchemaContextHandler schemaContextHandler;
32
33     public RestconfImpl(final SchemaContextHandler schemaContextHandler) {
34         this.schemaContextHandler = requireNonNull(schemaContextHandler);
35     }
36
37     @Override
38     public NormalizedNodePayload getLibraryVersion() {
39         final EffectiveModelContext context = schemaContextHandler.get();
40
41         // FIXME: why are we going through a grouping here?!
42         final GroupingDefinition grouping = context
43             .findModule(Restconf.QNAME.getModule())
44             .orElseThrow(() -> new IllegalStateException("Failed to find restcibf module"))
45             .getGroupings().stream()
46             .filter(grp -> Restconf.QNAME.equals(grp.getQName()))
47             .findFirst()
48             .orElseThrow(() -> new IllegalStateException("Failed to find restconf grouping"));
49
50         final LeafSchemaNode schemaNode =
51             (LeafSchemaNode) ((ContainerSchemaNode) grouping.getDataChildByName(Restconf.QNAME))
52             .getDataChildByName(YANG_LIBRARY_VERSION);
53
54         return NormalizedNodePayload.of(new InstanceIdentifierContext<>(
55             YangInstanceIdentifier.of(YANG_LIBRARY_VERSION), schemaNode, null, context),
56             ImmutableNodes.leafNode(YANG_LIBRARY_VERSION, IetfYangLibrary.REVISION.toString()));
57     }
58 }