Do not pass outer model context
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / CreateStreamUtil.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.rests.services.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableSet;
13 import java.util.Set;
14 import java.util.stream.Collectors;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
18 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
19 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
20 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
21 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
22 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
23 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
24 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
25 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants;
26 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.DeviceNotificationListenerAdaptor;
27 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenersBroker;
28 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.NotificationListenerAdapter;
29 import org.opendaylight.restconf.nb.rfc8040.utils.parser.IdentifierCodec;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.device.notification.rev221106.SubscribeDeviceNotificationInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.device.notification.rev221106.SubscribeDeviceNotificationOutput;
32 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.CreateDataChangeEventSubscriptionInput1.Scope;
33 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping;
34 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
35 import org.opendaylight.yangtools.yang.common.ErrorTag;
36 import org.opendaylight.yangtools.yang.common.ErrorType;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.common.QNameModule;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
43 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
46 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
47 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
48 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
49 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
50 import org.opendaylight.yangtools.yang.model.api.Module;
51 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
52 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement;
53 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 /**
58  * Utility class for creation of data-change-event or YANG notification streams.
59  */
60 final class CreateStreamUtil {
61     private static final Logger LOG = LoggerFactory.getLogger(CreateStreamUtil.class);
62     private static final QNameModule SAL_REMOTE_AUGMENT = NotificationOutputTypeGrouping.QNAME.getModule();
63
64     private static final QNameModule DEVICE_NOTIFICATION_MODULE = SubscribeDeviceNotificationInput.QNAME.getModule();
65     private static final QName DATASTORE_QNAME =
66         QName.create(SAL_REMOTE_AUGMENT, RestconfStreamsConstants.DATASTORE_PARAM_NAME).intern();
67     private static final QName SCOPE_QNAME =
68         QName.create(SAL_REMOTE_AUGMENT, RestconfStreamsConstants.SCOPE_PARAM_NAME).intern();
69     private static final QName OUTPUT_TYPE_QNAME =
70         QName.create(SAL_REMOTE_AUGMENT, "notification-output-type").intern();
71     private static final QName DEVICE_NOTIFICATION_PATH_QNAME =
72         QName.create(DEVICE_NOTIFICATION_MODULE, "path").intern();
73     private static final QName DEVICE_NOTIFICATION_STREAM_PATH =
74         QName.create(DEVICE_NOTIFICATION_PATH_QNAME, "stream-path").intern();
75     private static final NodeIdentifier DATASTORE_NODEID = NodeIdentifier.create(DATASTORE_QNAME);
76     private static final NodeIdentifier SCOPE_NODEID = NodeIdentifier.create(SCOPE_QNAME);
77     private static final NodeIdentifier OUTPUT_TYPE_NODEID = NodeIdentifier.create(OUTPUT_TYPE_QNAME);
78     private static final NodeIdentifier DEVICE_NOTIFICATION_PATH_NODEID =
79         NodeIdentifier.create(DEVICE_NOTIFICATION_PATH_QNAME);
80     private static final AugmentationIdentifier SAL_REMOTE_AUG_IDENTIFIER = new AugmentationIdentifier(
81         ImmutableSet.of(SCOPE_QNAME, DATASTORE_QNAME, OUTPUT_TYPE_QNAME));
82
83     private CreateStreamUtil() {
84         // Hidden on purpose
85     }
86
87     /**
88      * Create data-change-event or notification stream with POST operation via RPC.
89      *
90      * @param payload      Input of RPC - example in JSON (data-change-event stream):
91      *                     <pre>
92      *                     {@code
93      *                         {
94      *                             "input": {
95      *                                 "path": "/toaster:toaster/toaster:toasterStatus",
96      *                                 "sal-remote-augment:datastore": "OPERATIONAL",
97      *                                 "sal-remote-augment:scope": "ONE"
98      *                             }
99      *                         }
100      *                     }
101      *                     </pre>
102      * @param refSchemaCtx Reference to {@link EffectiveModelContext}.
103      * @return {@link DOMRpcResult} - Output of RPC - example in JSON:
104      *     <pre>
105      *     {@code
106      *         {
107      *             "output": {
108      *                 "stream-name": "toaster:toaster/toaster:toasterStatus/datastore=OPERATIONAL/scope=ONE"
109      *             }
110      *         }
111      *     }
112      *     </pre>
113      */
114     static DOMRpcResult createDataChangeNotifiStream(final NormalizedNodePayload payload,
115             final EffectiveModelContext refSchemaCtx) {
116         // parsing out of container with settings and path
117         final ContainerNode data = (ContainerNode) requireNonNull(payload).getData();
118         final QName qname = payload.getInstanceIdentifierContext().getSchemaNode().getQName();
119         final YangInstanceIdentifier path = preparePath(data, qname);
120
121         // building of stream name
122         final StringBuilder streamNameBuilder = new StringBuilder(
123                 prepareDataChangeNotifiStreamName(path, requireNonNull(refSchemaCtx), data));
124         final NotificationOutputType outputType = prepareOutputType(data);
125         if (outputType.equals(NotificationOutputType.JSON)) {
126             streamNameBuilder.append('/').append(outputType.getName());
127         }
128         final String streamName = streamNameBuilder.toString();
129
130         // registration of the listener
131         ListenersBroker.getInstance().registerDataChangeListener(path, streamName, outputType);
132
133         // building of output
134         final QName outputQname = QName.create(qname, "output");
135         final QName streamNameQname = QName.create(qname, "stream-name");
136
137         return new DefaultDOMRpcResult(Builders.containerBuilder()
138             .withNodeIdentifier(new NodeIdentifier(outputQname))
139             .withChild(ImmutableNodes.leafNode(streamNameQname, streamName))
140             .build());
141     }
142
143     /**
144      * Create device notification stream.
145      *
146      * @param baseUrl base Url
147      * @param payload data
148      * @param streamUtil stream utility
149      * @param mountPointService dom mount point service
150      * @return {@link DOMRpcResult} - Output of RPC - example in JSON
151      */
152     static DOMRpcResult createDeviceNotificationListener(final String baseUrl, final NormalizedNodePayload payload,
153             final SubscribeToStreamUtil streamUtil, final DOMMountPointService mountPointService) {
154         // parsing out of container with settings and path
155         // FIXME: ugly cast
156         final ContainerNode data = (ContainerNode) requireNonNull(payload).getData();
157         // FIXME: ugly cast
158         final YangInstanceIdentifier path =
159             (YangInstanceIdentifier) data.findChildByArg(DEVICE_NOTIFICATION_PATH_NODEID)
160                 .map(DataContainerChild::body)
161                 .orElseThrow(() -> new RestconfDocumentedException("No path specified", ErrorType.APPLICATION,
162                     ErrorTag.DATA_MISSING));
163
164         if (!(path.getLastPathArgument() instanceof NodeIdentifierWithPredicates listId)) {
165             throw new RestconfDocumentedException("Path does not refer to a list item", ErrorType.APPLICATION,
166                 ErrorTag.INVALID_VALUE);
167         }
168         if (listId.size() != 1) {
169             throw new RestconfDocumentedException("Target list uses multiple keys", ErrorType.APPLICATION,
170                 ErrorTag.INVALID_VALUE);
171         }
172         final String deviceName = listId.values().iterator().next().toString();
173
174         final DOMMountPoint mountPoint = mountPointService.getMountPoint(path)
175             .orElseThrow(() -> new RestconfDocumentedException("Mount point not available", ErrorType.APPLICATION,
176                 ErrorTag.OPERATION_FAILED));
177
178         final DOMNotificationService mountNotifService = mountPoint.getService(DOMNotificationService.class)
179             .orElseThrow(() -> new RestconfDocumentedException("Mount point does not support notifications",
180                 ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED));
181
182         final EffectiveModelContext mountModelContext = mountPoint.getService(DOMSchemaService.class)
183             .orElseThrow(() -> new RestconfDocumentedException("Mount point schema not available",
184                 ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED))
185             .getGlobalContext();
186         final Set<Absolute> notificationPaths = mountModelContext.getModuleStatements().values().stream()
187             .flatMap(module -> module.streamEffectiveSubstatements(NotificationEffectiveStatement.class))
188             .map(notification -> Absolute.of(notification.argument()))
189             .collect(Collectors.toUnmodifiableSet());
190         if (notificationPaths.isEmpty()) {
191             throw new RestconfDocumentedException("Device does not support notification", ErrorType.APPLICATION,
192                 ErrorTag.OPERATION_FAILED);
193         }
194
195         final DeviceNotificationListenerAdaptor notificationListenerAdapter = ListenersBroker.getInstance()
196             .registerDeviceNotificationListener(deviceName, prepareOutputType(data), mountModelContext,
197                 mountPointService, mountPoint.getIdentifier());
198         notificationListenerAdapter.listen(mountNotifService, notificationPaths);
199
200         // building of output
201         return new DefaultDOMRpcResult(Builders.containerBuilder()
202             .withNodeIdentifier(new NodeIdentifier(SubscribeDeviceNotificationOutput.QNAME))
203             .withChild(ImmutableNodes.leafNode(DEVICE_NOTIFICATION_STREAM_PATH, baseUrl + deviceName
204                 + "?" + RestconfStreamsConstants.NOTIFICATION_TYPE + "=" + RestconfStreamsConstants.DEVICE))
205             .build());
206     }
207
208     /**
209      * Prepare {@link NotificationOutputType}.
210      *
211      * @param data Container with stream settings (RPC create-stream).
212      * @return Parsed {@link NotificationOutputType}.
213      */
214     private static NotificationOutputType prepareOutputType(final ContainerNode data) {
215         final String outputName = extractStringLeaf(data, OUTPUT_TYPE_NODEID);
216         return outputName != null ? NotificationOutputType.valueOf(outputName) : NotificationOutputType.XML;
217     }
218
219     /**
220      * Prepare stream name.
221      *
222      * @param path          Path of element from which data-change-event notifications are going to be generated.
223      * @param schemaContext Schema context.
224      * @param data          Container with stream settings (RPC create-stream).
225      * @return Parsed stream name.
226      */
227     private static String prepareDataChangeNotifiStreamName(final YangInstanceIdentifier path,
228             final EffectiveModelContext schemaContext, final ContainerNode data) {
229         final String datastoreName = extractStringLeaf(data, DATASTORE_NODEID);
230         final LogicalDatastoreType datastoreType = datastoreName != null ? LogicalDatastoreType.valueOf(datastoreName)
231             : LogicalDatastoreType.CONFIGURATION;
232
233         final String scopeName = extractStringLeaf(data, SCOPE_NODEID);
234         // FIXME: this is not really used
235         final Scope scope = scopeName != null ? Scope.ofName(scopeName) : Scope.BASE;
236
237         return RestconfStreamsConstants.DATA_SUBSCRIPTION
238             + "/" + ListenersBroker.createStreamNameFromUri(IdentifierCodec.serialize(path, schemaContext)
239                 + "/" + RestconfStreamsConstants.DATASTORE_PARAM_NAME + "=" + datastoreType
240                 + "/" + RestconfStreamsConstants.SCOPE_PARAM_NAME + "=" + scope);
241     }
242
243     /**
244      * Prepare {@link YangInstanceIdentifier} of stream source.
245      *
246      * @param data          Container with stream settings (RPC create-stream).
247      * @param qualifiedName QName of the input RPC context (used only in debugging).
248      * @return Parsed {@link YangInstanceIdentifier} of data element from which the data-change-event notifications
249      *     are going to be generated.
250      */
251     private static YangInstanceIdentifier preparePath(final ContainerNode data, final QName qualifiedName) {
252         final Object pathValue = data.findChildByArg(new NodeIdentifier(QName.create(qualifiedName, "path")))
253             .map(DataContainerChild::body)
254             .orElse(null);
255         if (!(pathValue instanceof YangInstanceIdentifier)) {
256             LOG.debug("Instance identifier {} was not normalized correctly", qualifiedName);
257             throw new RestconfDocumentedException(
258                     "Instance identifier was not normalized correctly",
259                     ErrorType.APPLICATION,
260                     ErrorTag.OPERATION_FAILED);
261         }
262         return (YangInstanceIdentifier) pathValue;
263     }
264
265     private static @Nullable String extractStringLeaf(final ContainerNode data, final NodeIdentifier childName) {
266         final DataContainerChild augNode = data.childByArg(SAL_REMOTE_AUG_IDENTIFIER);
267         if (augNode instanceof AugmentationNode) {
268             final DataContainerChild enumNode = ((AugmentationNode) augNode).childByArg(childName);
269             if (enumNode instanceof LeafNode) {
270                 final Object value = enumNode.body();
271                 if (value instanceof String) {
272                     return (String) value;
273                 }
274             }
275         }
276         return null;
277     }
278
279     /**
280      * Create YANG notification stream using notification definition in YANG schema.
281      *
282      * @param notificationDefinition YANG notification definition.
283      * @param refSchemaCtx           Reference to {@link EffectiveModelContext}
284      * @param outputType             Output type (XML or JSON).
285      * @return {@link NotificationListenerAdapter}
286      */
287     static NotificationListenerAdapter createYangNotifiStream(final NotificationDefinition notificationDefinition,
288             final EffectiveModelContext refSchemaCtx, final NotificationOutputType outputType) {
289         final var streamName = parseNotificationStreamName(requireNonNull(notificationDefinition),
290                 requireNonNull(refSchemaCtx), requireNonNull(outputType.getName()));
291         final var listenersBroker = ListenersBroker.getInstance();
292
293         final var existing = listenersBroker.notificationListenerFor(streamName);
294         return existing != null ? existing
295             : listenersBroker.registerNotificationListener(
296                 Absolute.of(notificationDefinition.getQName()), streamName, outputType);
297     }
298
299     private static String parseNotificationStreamName(final NotificationDefinition notificationDefinition,
300             final EffectiveModelContext refSchemaCtx, final String outputType) {
301         final QName notificationDefinitionQName = notificationDefinition.getQName();
302         final Module module = refSchemaCtx.findModule(
303                 notificationDefinitionQName.getModule().getNamespace(),
304                 notificationDefinitionQName.getModule().getRevision()).orElse(null);
305         requireNonNull(module, String.format("Module for namespace %s does not exist.",
306                 notificationDefinitionQName.getModule().getNamespace()));
307
308         final StringBuilder streamNameBuilder = new StringBuilder();
309         streamNameBuilder.append(RestconfStreamsConstants.NOTIFICATION_STREAM)
310                 .append('/')
311                 .append(module.getName())
312                 .append(':')
313                 .append(notificationDefinitionQName.getLocalName());
314         if (outputType.equals(NotificationOutputType.JSON.getName())) {
315             streamNameBuilder.append('/').append(NotificationOutputType.JSON.getName());
316         }
317         return streamNameBuilder.toString();
318     }
319 }