2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.rest.services.impl;
10 import com.google.common.base.Preconditions;
12 import javax.ws.rs.core.UriInfo;
13 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
14 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
15 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
16 import org.opendaylight.restconf.Draft11;
17 import org.opendaylight.restconf.common.references.SchemaContextRef;
18 import org.opendaylight.restconf.handlers.SchemaContextHandler;
19 import org.opendaylight.restconf.rest.services.api.RestconfStreamsService;
20 import org.opendaylight.restconf.utils.mapping.RestconfMappingNodeUtil;
21 import org.opendaylight.restconf.utils.schema.context.RestconfSchemaUtil;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
26 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
28 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
29 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 * Implementation of {@link RestconfStreamsService}
38 public class RestconfStreamsServiceImpl implements RestconfStreamsService {
40 private final SchemaContextHandler schemaContextHandler;
43 * Set {@link SchemaContextHandler} for getting actual {@link SchemaContext}
46 * @param schemaContextHandler
47 * - handling schema context
49 public RestconfStreamsServiceImpl(final SchemaContextHandler schemaContextHandler) {
50 this.schemaContextHandler = schemaContextHandler;
54 public NormalizedNodeContext getAvailableStreams(final UriInfo uriInfo) {
55 final SchemaContextRef schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
56 final Set<String> availableStreams = Notificator.getStreamNames();
58 final DataSchemaNode streamListSchemaNode = RestconfSchemaUtil.getRestconfSchemaNode(
59 schemaContextRef.getRestconfModule(), Draft11.MonitoringModule.STREAM_LIST_SCHEMA_NODE);
60 Preconditions.checkState(streamListSchemaNode instanceof ListSchemaNode);
61 final CollectionNodeBuilder<MapEntryNode, MapNode> listStreamBuilder = Builders
62 .mapBuilder((ListSchemaNode) streamListSchemaNode);
64 for (final String streamValue : availableStreams) {
65 listStreamBuilder.withChild(RestconfMappingNodeUtil.toStreamEntryNode(streamValue, streamListSchemaNode));
68 final DataSchemaNode streamContSchemaNode = RestconfSchemaUtil.getRestconfSchemaNode(
69 schemaContextRef.getRestconfModule(), Draft11.MonitoringModule.STREAMS_CONTAINER_SCHEMA_NODE);
70 Preconditions.checkState(streamContSchemaNode instanceof ContainerSchemaNode);
71 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> streamsContainerBuilder = Builders
72 .containerBuilder((ContainerSchemaNode) streamContSchemaNode);
74 streamsContainerBuilder.withChild(listStreamBuilder.build());
76 return new NormalizedNodeContext(
77 new InstanceIdentifierContext<>(null, streamContSchemaNode, null, schemaContextRef.get()),
78 streamsContainerBuilder.build());