Bug 5528 - Handlers
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / restconf / rest / services / impl / RestconfStreamsServiceImpl.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.rest.services.impl;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Set;
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;
33
34 /**
35  * Implementation of {@link RestconfStreamsService}
36  *
37  */
38 public class RestconfStreamsServiceImpl implements RestconfStreamsService {
39
40     private final SchemaContextHandler schemaContextHandler;
41
42     /**
43      * Set {@link SchemaContextHandler} for getting actual {@link SchemaContext}
44      * .
45      *
46      * @param schemaContextHandler
47      *            - handling schema context
48      */
49     public RestconfStreamsServiceImpl(final SchemaContextHandler schemaContextHandler) {
50         this.schemaContextHandler = schemaContextHandler;
51     }
52
53     @Override
54     public NormalizedNodeContext getAvailableStreams(final UriInfo uriInfo) {
55         final SchemaContextRef schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
56         final Set<String> availableStreams = Notificator.getStreamNames();
57
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);
63
64         for (final String streamValue : availableStreams) {
65             listStreamBuilder.withChild(RestconfMappingNodeUtil.toStreamEntryNode(streamValue, streamListSchemaNode));
66         }
67
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);
73
74         streamsContainerBuilder.withChild(listStreamBuilder.build());
75
76         return new NormalizedNodeContext(
77                 new InstanceIdentifierContext<>(null, streamContSchemaNode, null, schemaContextRef.get()),
78                 streamsContainerBuilder.build());
79     }
80 }