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.impl.services;
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.rest.api.schema.context.SchemaContextHandler;
18 import org.opendaylight.restconf.rest.api.services.RestconfStreamsService;
19 import org.opendaylight.restconf.utils.mapping.RestconfMappingNodeUtil;
20 import org.opendaylight.restconf.utils.schema.context.RestconfSchemaUtil;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
22 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
25 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.CollectionNodeBuilder;
27 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
28 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 * Implementation of {@link RestconfStreamsService}
37 public class RestconfStreamsServiceImpl implements RestconfStreamsService {
39 private final SchemaContextHandler schemaContextHandler;
42 * Set {@link SchemaContextHandler} for getting actual {@link SchemaContext}
45 * @param schemaContextHandler
46 * - handling schema context
48 public RestconfStreamsServiceImpl(final SchemaContextHandler schemaContextHandler) {
49 this.schemaContextHandler = schemaContextHandler;
53 public NormalizedNodeContext getAvailableStreams(final UriInfo uriInfo) {
54 final SchemaContextRef schemaContextRef = new SchemaContextRef(this.schemaContextHandler.getSchemaContext());
55 final Set<String> availableStreams = Notificator.getStreamNames();
57 final DataSchemaNode streamListSchemaNode = RestconfSchemaUtil.getRestconfSchemaNode(
58 schemaContextRef.getRestconfModule(), Draft11.MonitoringModule.STREAM_LIST_SCHEMA_NODE);
59 Preconditions.checkState(streamListSchemaNode instanceof ListSchemaNode);
60 final CollectionNodeBuilder<MapEntryNode, MapNode> listStreamBuilder = Builders
61 .mapBuilder((ListSchemaNode) streamListSchemaNode);
63 for (final String streamValue : availableStreams) {
64 listStreamBuilder.withChild(RestconfMappingNodeUtil.toStreamEntryNode(streamValue, streamListSchemaNode));
67 final DataSchemaNode streamContSchemaNode = RestconfSchemaUtil.getRestconfSchemaNode(
68 schemaContextRef.getRestconfModule(), Draft11.MonitoringModule.STREAMS_CONTAINER_SCHEMA_NODE);
69 Preconditions.checkState(streamContSchemaNode instanceof ContainerSchemaNode);
70 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> streamsContainerBuilder = Builders
71 .containerBuilder((ContainerSchemaNode) streamContSchemaNode);
73 streamsContainerBuilder.withChild(listStreamBuilder.build());
75 return new NormalizedNodeContext(
76 new InstanceIdentifierContext<>(null, streamContSchemaNode, null, schemaContextRef.get()),
77 streamsContainerBuilder.build());