Refactor ietf-restconf constants
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / RestconfImpl.java
index d40c5cc50e3208587a0966a93e8520e7f3ea1dde..bc0ecb449e308d5e3e96586d2c28220cf8cb7581 100644 (file)
@@ -13,18 +13,21 @@ import javax.ws.rs.Path;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 import org.opendaylight.restconf.nb.rfc8040.Rfc8040.IetfYangLibrary;
-import org.opendaylight.restconf.nb.rfc8040.Rfc8040.RestconfModule;
 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfService;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.rev170126.Restconf;
+import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.GroupingDefinition;
-import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
 
 @Path("/")
 public class RestconfImpl implements RestconfService {
+    private static final QName YANG_LIBRARY_VERSION = QName.create(Restconf.QNAME, "yang-library-version").intern();
+
     private final SchemaContextHandler schemaContextHandler;
 
     public RestconfImpl(final SchemaContextHandler schemaContextHandler) {
@@ -33,18 +36,23 @@ public class RestconfImpl implements RestconfService {
 
     @Override
     public NormalizedNodeContext getLibraryVersion() {
-        final EffectiveModelContext context = this.schemaContextHandler.get();
-        SchemaNode schemaNode = null;
-        for (final GroupingDefinition groupingDefinition : context
-                .findModule(RestconfModule.IETF_RESTCONF_QNAME.getModule()).get().getGroupings()) {
-            if (groupingDefinition.getQName().equals(RestconfModule.RESTCONF_GROUPING_QNAME)) {
-                schemaNode = ((ContainerSchemaNode) groupingDefinition
-                        .getDataChildByName(RestconfModule.RESTCONF_CONTAINER_QNAME))
-                                .getDataChildByName(RestconfModule.LIB_VER_LEAF_QNAME);
-            }
-        }
+        final EffectiveModelContext context = schemaContextHandler.get();
+
+        // FIXME: why are we going through a grouping here?!
+        final GroupingDefinition grouping = context
+            .findModule(Restconf.QNAME.getModule())
+            .orElseThrow(() -> new IllegalStateException("Failed to find restcibf module"))
+            .getGroupings().stream()
+            .filter(grp -> Restconf.QNAME.equals(grp.getQName()))
+            .findFirst()
+            .orElseThrow(() -> new IllegalStateException("Failed to find restconf grouping"));
+
+        final LeafSchemaNode schemaNode =
+            (LeafSchemaNode) ((ContainerSchemaNode) grouping.getDataChildByName(Restconf.QNAME))
+            .getDataChildByName(YANG_LIBRARY_VERSION);
+
         return new NormalizedNodeContext(new InstanceIdentifierContext<>(
-            YangInstanceIdentifier.of(RestconfModule.LIB_VER_LEAF_QNAME), schemaNode, null, context),
-            ImmutableNodes.leafNode(RestconfModule.LIB_VER_LEAF_QNAME, IetfYangLibrary.REVISION.toString()));
+            YangInstanceIdentifier.of(YANG_LIBRARY_VERSION), schemaNode, null, context),
+            ImmutableNodes.leafNode(YANG_LIBRARY_VERSION, IetfYangLibrary.REVISION.toString()));
     }
 }