Purge AbstractNotificationsData
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / streams / RestconfStateStreams.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.nb.rfc8040.streams;
9
10 import static org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.restconf.monitoring.rev170126.$YangModuleInfoImpl.qnameOf;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import java.net.URI;
14 import java.util.Set;
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;
33
34 /**
35  * Utilities for creating the content of {@code /ietf-restconf-monitoring:restconf-state/streams}.
36  */
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));
41
42     @VisibleForTesting
43     static final QName DESCRIPTION_QNAME = qnameOf("description");
44     @VisibleForTesting
45     static final QName ENCODING_QNAME = qnameOf("encoding");
46     @VisibleForTesting
47     static final QName LOCATION_QNAME = qnameOf("location");
48     @VisibleForTesting
49     static final QName NAME_QNAME = qnameOf("name");
50
51     private RestconfStateStreams() {
52         // Hidden on purpose
53     }
54
55     public static @NonNull YangInstanceIdentifier restconfStateStreamPath(final String streamName) {
56         return restconfStateStreamPath(NodeIdentifierWithPredicates.of(Stream.QNAME, NAME_QNAME, streamName));
57     }
58
59     public static @NonNull YangInstanceIdentifier restconfStateStreamPath(final NodeIdentifierWithPredicates arg) {
60         return RESTCONF_STATE_STREAMS.node(arg);
61     }
62
63     /**
64      * Map data of yang notification to normalized node according to ietf-restconf-monitoring.
65      *
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
72      */
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()
79                 .map(QName::toString)
80                 .collect(Collectors.joining(","))))
81             .withChild(createAccessList(outputType, uri))
82             .build();
83     }
84
85     /**
86      * Map data of data change notification to normalized node according to ietf-restconf-monitoring.
87      *
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
94      */
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));
101
102         DataSchemaContextTree.from(schemaContext).findChild(path)
103             .map(DataSchemaContext::dataSchemaNode)
104             .flatMap(DataSchemaNode::getDescription)
105             .ifPresent(desc -> streamEntry.withChild(ImmutableNodes.leafNode(DESCRIPTION_QNAME, desc)));
106
107         return streamEntry
108             .withChild(createAccessList(outputType, uri))
109             .build();
110     }
111
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()))
119                 .build())
120             .build();
121     }
122 }