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