da89d99bcfd227666b1387888963513b9707e810
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / services / simple / 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.services.simple.impl;
9
10 import javax.ws.rs.Path;
11 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
12 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
13 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.IetfYangLibrary;
14 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.RestconfModule;
15 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
16 import org.opendaylight.restconf.nb.rfc8040.services.simple.api.RestconfService;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
20 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
21 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
23 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
26
27 @Path("/")
28 public class RestconfImpl implements RestconfService {
29
30     private volatile SchemaContextHandler schemaContextHandler;
31
32     public RestconfImpl(final SchemaContextHandler schemaContextHandler) {
33         this.schemaContextHandler = schemaContextHandler;
34     }
35
36     @Override
37     public NormalizedNodeContext getLibraryVersion() {
38         final SchemaContext context = this.schemaContextHandler.get();
39         SchemaNode schemaNode = null;
40         for (final GroupingDefinition groupingDefinition : context
41                 .findModule(RestconfModule.IETF_RESTCONF_QNAME.getModule()).get().getGroupings()) {
42             if (groupingDefinition.getQName().equals(RestconfModule.RESTCONF_GROUPING_QNAME)) {
43                 schemaNode = ((ContainerSchemaNode) groupingDefinition
44                         .getDataChildByName(RestconfModule.RESTCONF_CONTAINER_QNAME))
45                                 .getDataChildByName(RestconfModule.LIB_VER_LEAF_QNAME);
46             }
47         }
48         final YangInstanceIdentifier yangIId = YangInstanceIdentifier.of(
49                 QName.create(RestconfModule.IETF_RESTCONF_QNAME, RestconfModule.LIB_VER_LEAF_SCHEMA_NODE));
50         final InstanceIdentifierContext<? extends SchemaNode> iid =
51                 new InstanceIdentifierContext<>(yangIId, schemaNode, null, context);
52         final NormalizedNode<?, ?> data =
53                 Builders.leafBuilder((LeafSchemaNode) schemaNode).withValue(IetfYangLibrary.REVISION).build();
54         return new NormalizedNodeContext(iid, data);
55     }
56
57     @Override
58     public synchronized void updateHandlers(final Object... handlers) {
59         for (final Object object : handlers) {
60             if (object instanceof SchemaContextHandler) {
61                 schemaContextHandler = (SchemaContextHandler) object;
62             }
63         }
64     }
65 }