Merge topic 'cleanup/composite-node'
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / restconf / impl / RestconfImpl.java
index 9dc299ddaf91155e62222378d8d5eafaabe6a2c4..c82cf8e0bc5050b682d9b99896f786f68381cb58 100644 (file)
@@ -218,79 +218,99 @@ public class RestconfImpl implements RestconfService {
                 null, schemaContext), moduleContainerBuilder.build());
     }
 
+    /**
+     * Valid only for mount point
+     */
     @Override
-    public StructuredData getAvailableStreams(final UriInfo uriInfo) {
-        final Set<String> availableStreams = Notificator.getStreamNames();
-
-        final List<Node<?>> streamsAsData = new ArrayList<Node<?>>();
-        final Module restconfModule = getRestconfModule();
-        final DataSchemaNode streamSchemaNode = controllerContext.getRestconfModuleRestConfSchemaNode(restconfModule,
-                Draft02.RestConfModule.STREAM_LIST_SCHEMA_NODE);
-        for (final String streamName : availableStreams) {
-            streamsAsData.add(toStreamCompositeNode(streamName, streamSchemaNode));
+    public NormalizedNodeContext getModules(final String identifier, final UriInfo uriInfo) {
+        Preconditions.checkNotNull(identifier);
+        if ( ! identifier.contains(ControllerContext.MOUNT)) {
+            final String errMsg = "URI has bad format. If modules behind mount point should be showed,"
+                    + " URI has to end with " + ControllerContext.MOUNT;
+            throw new RestconfDocumentedException(errMsg, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
         }
 
-        final DataSchemaNode streamsSchemaNode = controllerContext.getRestconfModuleRestConfSchemaNode(restconfModule,
-                Draft02.RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE);
-        final QName qName = streamsSchemaNode.getQName();
-        final CompositeNode streamsNode = NodeFactory.createImmutableCompositeNode(qName, null, streamsAsData);
-        return new StructuredData(streamsNode, streamsSchemaNode, null, parsePrettyPrintParameter(uriInfo));
-    }
-
-    @Override
-    public StructuredData getModules(final String identifier, final UriInfo uriInfo) {
-        Set<Module> modules = null;
-        DOMMountPoint mountPoint = null;
-        if (identifier.contains(ControllerContext.MOUNT)) {
-            final InstanceIdentifierContext mountPointIdentifier = controllerContext.toMountPointIdentifier(identifier);
-            mountPoint = mountPointIdentifier.getMountPoint();
-            modules = controllerContext.getAllModules(mountPoint);
-        } else {
-            throw new RestconfDocumentedException(
-                    "URI has bad format. If modules behind mount point should be showed, URI has to end with "
-                            + ControllerContext.MOUNT, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
-        }
+        final InstanceIdentifierContext mountPointIdentifier = controllerContext.toMountPointIdentifier(identifier);
+        final DOMMountPoint mountPoint = mountPointIdentifier.getMountPoint();
+        final Set<Module> modules = controllerContext.getAllModules(mountPoint);
+        final SchemaContext schemaContext = mountPoint.getSchemaContext();
+        final MapNode mountPointModulesMap = makeModuleMapNode(modules);
 
-        final List<Node<?>> modulesAsData = new ArrayList<Node<?>>();
         final Module restconfModule = getRestconfModule();
-        final DataSchemaNode moduleSchemaNode = controllerContext.getRestconfModuleRestConfSchemaNode(restconfModule,
-                Draft02.RestConfModule.MODULE_LIST_SCHEMA_NODE);
+        final DataSchemaNode modulesSchemaNode = controllerContext.getRestconfModuleRestConfSchemaNode(
+                restconfModule, Draft02.RestConfModule.MODULES_CONTAINER_SCHEMA_NODE);
+        Preconditions.checkState(modulesSchemaNode instanceof ContainerSchemaNode);
 
-        for (final Module module : modules) {
-            modulesAsData.add(toModuleCompositeNode(module, moduleSchemaNode));
-        }
+        final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> moduleContainerBuilder =
+                Builders.containerBuilder((ContainerSchemaNode) modulesSchemaNode);
+        moduleContainerBuilder.withChild(mountPointModulesMap);
 
-        final DataSchemaNode modulesSchemaNode = controllerContext.getRestconfModuleRestConfSchemaNode(restconfModule,
-                Draft02.RestConfModule.MODULES_CONTAINER_SCHEMA_NODE);
-        final QName qName = modulesSchemaNode.getQName();
-        final CompositeNode modulesNode = NodeFactory.createImmutableCompositeNode(qName, null, modulesAsData);
-        return new StructuredData(modulesNode, modulesSchemaNode, mountPoint, parsePrettyPrintParameter(uriInfo));
+        return new NormalizedNodeContext(new InstanceIdentifierContext(null, modulesSchemaNode,
+                mountPoint, schemaContext), moduleContainerBuilder.build());
     }
 
     @Override
-    public StructuredData getModule(final String identifier, final UriInfo uriInfo) {
+    public NormalizedNodeContext getModule(final String identifier, final UriInfo uriInfo) {
+        Preconditions.checkNotNull(identifier);
         final QName moduleNameAndRevision = getModuleNameAndRevision(identifier);
         Module module = null;
         DOMMountPoint mountPoint = null;
+        final SchemaContext schemaContext;
         if (identifier.contains(ControllerContext.MOUNT)) {
             final InstanceIdentifierContext mountPointIdentifier = controllerContext.toMountPointIdentifier(identifier);
             mountPoint = mountPointIdentifier.getMountPoint();
             module = controllerContext.findModuleByNameAndRevision(mountPoint, moduleNameAndRevision);
+            schemaContext = mountPoint.getSchemaContext();
         } else {
             module = controllerContext.findModuleByNameAndRevision(moduleNameAndRevision);
+            schemaContext = controllerContext.getGlobalSchema();
         }
 
         if (module == null) {
-            throw new RestconfDocumentedException("Module with name '" + moduleNameAndRevision.getLocalName()
-                    + "' and revision '" + moduleNameAndRevision.getRevision() + "' was not found.",
-                    ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
+            final String errMsg = "Module with name '" + moduleNameAndRevision.getLocalName()
+                    + "' and revision '" + moduleNameAndRevision.getRevision() + "' was not found.";
+            throw new RestconfDocumentedException(errMsg, ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ELEMENT);
         }
 
         final Module restconfModule = getRestconfModule();
-        final DataSchemaNode moduleSchemaNode = controllerContext.getRestconfModuleRestConfSchemaNode(restconfModule,
-                Draft02.RestConfModule.MODULE_LIST_SCHEMA_NODE);
-        final CompositeNode moduleNode = toModuleCompositeNode(module, moduleSchemaNode);
-        return new StructuredData(moduleNode, moduleSchemaNode, mountPoint, parsePrettyPrintParameter(uriInfo));
+        final Set<Module> modules = Collections.singleton(module);
+        final MapNode moduleMap = makeModuleMapNode(modules);
+
+        final DataSchemaNode moduleSchemaNode = controllerContext.getRestconfModuleRestConfSchemaNode(
+                restconfModule, Draft02.RestConfModule.MODULE_LIST_SCHEMA_NODE);
+        Preconditions.checkState(moduleSchemaNode instanceof ListSchemaNode);
+
+        return new NormalizedNodeContext(new InstanceIdentifierContext(null, moduleSchemaNode, mountPoint,
+                schemaContext), moduleMap);
+    }
+
+    @Override
+    public NormalizedNodeContext getAvailableStreams(final UriInfo uriInfo) {
+        final SchemaContext schemaContext = controllerContext.getGlobalSchema();
+        final Set<String> availableStreams = Notificator.getStreamNames();
+        final Module restconfModule = getRestconfModule();
+        final DataSchemaNode streamSchemaNode = controllerContext.getRestconfModuleRestConfSchemaNode(restconfModule,
+                Draft02.RestConfModule.STREAM_LIST_SCHEMA_NODE);
+        Preconditions.checkState(streamSchemaNode instanceof ListSchemaNode);
+
+        final CollectionNodeBuilder<MapEntryNode, MapNode> listStreamsBuilder = Builders
+                .mapBuilder((ListSchemaNode) streamSchemaNode);
+
+        for (final String streamName : availableStreams) {
+            listStreamsBuilder.withChild(toStreamEntryNode(streamName, streamSchemaNode));
+        }
+
+        final DataSchemaNode streamsContainerSchemaNode = controllerContext.getRestconfModuleRestConfSchemaNode(
+                restconfModule, Draft02.RestConfModule.STREAMS_CONTAINER_SCHEMA_NODE);
+        Preconditions.checkState(streamsContainerSchemaNode instanceof ContainerSchemaNode);
+
+        final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> streamsContainerBuilder =
+                Builders.containerBuilder((ContainerSchemaNode) streamsContainerSchemaNode);
+        streamsContainerBuilder.withChild(listStreamsBuilder.build());
+
+
+        return new NormalizedNodeContext(new InstanceIdentifierContext(null, streamsContainerSchemaNode, null,
+                schemaContext), streamsContainerBuilder.build());
     }
 
     @Override
@@ -1653,4 +1673,49 @@ public class RestconfImpl implements RestconfService {
         Preconditions.checkArgument(moduleSchemaNode instanceof ListSchemaNode,
                 "moduleSchemaNode has to be of type ListSchemaNode");\r        final ListSchemaNode listModuleSchemaNode = (ListSchemaNode) moduleSchemaNode;\r        final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> moduleNodeValues = Builders\r                .mapEntryBuilder(listModuleSchemaNode);\r\r        List<DataSchemaNode> instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName(\r                (listModuleSchemaNode), "name");\r        final DataSchemaNode nameSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);\r        Preconditions.checkState(nameSchemaNode instanceof LeafSchemaNode);\r        moduleNodeValues.withChild(Builders.leafBuilder((LeafSchemaNode) nameSchemaNode).withValue(module.getName())\r                .build());\r\r        instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName(\r                (listModuleSchemaNode), "revision");\r        final DataSchemaNode revisionSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);\r        Preconditions.checkState(revisionSchemaNode instanceof LeafSchemaNode);\r        final String revision = REVISION_FORMAT.format(module.getRevision());\r        moduleNodeValues.withChild(Builders.leafBuilder((LeafSchemaNode) revisionSchemaNode).withValue(revision)\r                .build());\r\r        instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName(\r                (listModuleSchemaNode), "namespace");\r        final DataSchemaNode namespaceSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);\r        Preconditions.checkState(namespaceSchemaNode instanceof LeafSchemaNode);\r        moduleNodeValues.withChild(Builders.leafBuilder((LeafSchemaNode) namespaceSchemaNode)\r                .withValue(module.getNamespace().toString()).build());\r\r        instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName(\r                (listModuleSchemaNode), "feature");\r        final DataSchemaNode featureSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);\r        Preconditions.checkState(featureSchemaNode instanceof LeafListSchemaNode);\r        final ListNodeBuilder<Object, LeafSetEntryNode<Object>> featuresBuilder = Builders\r                .leafSetBuilder((LeafListSchemaNode) featureSchemaNode);\r        for (final FeatureDefinition feature : module.getFeatures()) {\r            featuresBuilder.withChild(Builders.leafSetEntryBuilder(((LeafListSchemaNode) featureSchemaNode))\r                    .withValue(feature.getQName().getLocalName()).build());\r        }\r        moduleNodeValues.withChild(featuresBuilder.build());
 \r        return moduleNodeValues.build();\r    }
+
+    protected MapEntryNode toStreamEntryNode(final String streamName, final DataSchemaNode streamSchemaNode) {
+        Preconditions.checkArgument(streamSchemaNode instanceof ListSchemaNode,
+                "streamSchemaNode has to be of type ListSchemaNode");
+        final ListSchemaNode listStreamSchemaNode = (ListSchemaNode) streamSchemaNode;
+        final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> streamNodeValues = Builders
+                .mapEntryBuilder(listStreamSchemaNode);
+
+        List<DataSchemaNode> instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName(
+                (listStreamSchemaNode), "name");
+        final DataSchemaNode nameSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);
+        Preconditions.checkState(nameSchemaNode instanceof LeafSchemaNode);
+        streamNodeValues.withChild(Builders.leafBuilder((LeafSchemaNode) nameSchemaNode).withValue(streamName)
+                .build());
+
+        instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName(
+                (listStreamSchemaNode), "description");
+        final DataSchemaNode descriptionSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);
+        Preconditions.checkState(descriptionSchemaNode instanceof LeafSchemaNode);
+        streamNodeValues.withChild(Builders.leafBuilder((LeafSchemaNode) nameSchemaNode)
+                .withValue("DESCRIPTION_PLACEHOLDER").build());
+
+        instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName(
+                (listStreamSchemaNode), "replay-support");
+        final DataSchemaNode replaySupportSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);
+        Preconditions.checkState(replaySupportSchemaNode instanceof LeafSchemaNode);
+        streamNodeValues.withChild(Builders.leafBuilder((LeafSchemaNode) replaySupportSchemaNode)
+                .withValue(Boolean.valueOf(true)).build());
+
+        instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName(
+                (listStreamSchemaNode), "replay-log-creation-time");
+        final DataSchemaNode replayLogCreationTimeSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);
+        Preconditions.checkState(replayLogCreationTimeSchemaNode instanceof LeafSchemaNode);
+        streamNodeValues.withChild(Builders.leafBuilder((LeafSchemaNode) replayLogCreationTimeSchemaNode)
+                .withValue("").build());
+
+        instanceDataChildrenByName = ControllerContext.findInstanceDataChildrenByName(
+                (listStreamSchemaNode), "events");
+        final DataSchemaNode eventsSchemaNode = Iterables.getFirst(instanceDataChildrenByName, null);
+        Preconditions.checkState(eventsSchemaNode instanceof LeafSchemaNode);
+        streamNodeValues.withChild(Builders.leafBuilder((LeafSchemaNode) eventsSchemaNode)
+                .withValue("").build());
+
+        return streamNodeValues.build();
+    }
 }