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.nb.rfc8040.streams;
10 import static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.monitoring.rev170126.$YangModuleInfoImpl.qnameOf;
12 import com.google.common.annotations.VisibleForTesting;
15 import java.util.stream.Collectors;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.monitoring.rev170126.RestconfState;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.monitoring.rev170126.restconf.state.Streams;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.monitoring.rev170126.restconf.state.streams.Stream;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.monitoring.rev170126.restconf.state.streams.stream.Access;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
25 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
27 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
28 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
29 import org.opendaylight.yangtools.yang.data.util.DataSchemaContext;
30 import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
31 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
35 * Utilities for creating the content of {@code /ietf-restconf-monitoring:restconf-state/streams}.
37 public final class RestconfStateStreams {
38 private static final YangInstanceIdentifier RESTCONF_STATE_STREAMS = YangInstanceIdentifier.of(
39 NodeIdentifier.create(RestconfState.QNAME), NodeIdentifier.create(Streams.QNAME),
40 NodeIdentifier.create(Stream.QNAME));
43 static final QName DESCRIPTION_QNAME = qnameOf("description");
45 static final QName ENCODING_QNAME = qnameOf("encoding");
47 static final QName LOCATION_QNAME = qnameOf("location");
49 static final QName NAME_QNAME = qnameOf("name");
51 private RestconfStateStreams() {
55 public static @NonNull YangInstanceIdentifier restconfStateStreamPath(final String streamName) {
56 return restconfStateStreamPath(NodeIdentifierWithPredicates.of(Stream.QNAME, NAME_QNAME, streamName));
59 public static @NonNull YangInstanceIdentifier restconfStateStreamPath(final NodeIdentifierWithPredicates arg) {
60 return RESTCONF_STATE_STREAMS.node(arg);
64 * Map data of yang notification to normalized node according to ietf-restconf-monitoring.
66 * @param streamName stream name
67 * @param qnames Notification QNames to listen on
68 * @param outputType output type of notification
69 * @param uri location of registered listener for sending data of notification
70 * @return mapped data of notification - map entry node if parent exists,
71 * container streams with list and map entry node if not
73 public static MapEntryNode notificationStreamEntry(final String streamName, final Set<QName> qnames,
74 final String outputType, final URI uri) {
75 return Builders.mapEntryBuilder()
76 .withNodeIdentifier(NodeIdentifierWithPredicates.of(Stream.QNAME, NAME_QNAME, streamName))
77 .withChild(ImmutableNodes.leafNode(NAME_QNAME, streamName))
78 .withChild(ImmutableNodes.leafNode(DESCRIPTION_QNAME, qnames.stream()
80 .collect(Collectors.joining(","))))
81 .withChild(createAccessList(outputType, uri))
86 * Map data of data change notification to normalized node according to ietf-restconf-monitoring.
88 * @param path path of data to listen on
89 * @param outputType output type of notification
90 * @param uri location of registered listener for sending data of notification
91 * @param schemaContext schemaContext for parsing instance identifier to get schema node of data
92 * @return mapped data of notification - map entry node if parent exists,
93 * container streams with list and map entry node if not
95 public static MapEntryNode dataChangeStreamEntry(final YangInstanceIdentifier path,
96 final String outputType, final URI uri, final EffectiveModelContext schemaContext,
97 final String streamName) {
98 final var streamEntry = Builders.mapEntryBuilder()
99 .withNodeIdentifier(NodeIdentifierWithPredicates.of(Stream.QNAME, NAME_QNAME, streamName))
100 .withChild(ImmutableNodes.leafNode(NAME_QNAME, streamName));
102 DataSchemaContextTree.from(schemaContext).findChild(path)
103 .map(DataSchemaContext::dataSchemaNode)
104 .flatMap(DataSchemaNode::getDescription)
105 .ifPresent(desc -> streamEntry.withChild(ImmutableNodes.leafNode(DESCRIPTION_QNAME, desc)));
108 .withChild(createAccessList(outputType, uri))
112 private static MapNode createAccessList(final String outputType, final URI uriToWebsocketServer) {
113 return Builders.mapBuilder()
114 .withNodeIdentifier(new NodeIdentifier(Access.QNAME))
115 .withChild(Builders.mapEntryBuilder()
116 .withNodeIdentifier(NodeIdentifierWithPredicates.of(Access.QNAME, ENCODING_QNAME, outputType))
117 .withChild(ImmutableNodes.leafNode(ENCODING_QNAME, outputType))
118 .withChild(ImmutableNodes.leafNode(LOCATION_QNAME, uriToWebsocketServer.toString()))