2 * Copyright (c) 2023 PANTHEON.tech, s.r.o. 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.server.mdsal.streams.devnotif;
10 import static java.util.Objects.requireNonNull;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
16 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
17 import org.opendaylight.restconf.common.errors.RestconfFuture;
18 import org.opendaylight.restconf.nb.rfc8040.utils.parser.YangInstanceIdentifierSerializer;
19 import org.opendaylight.restconf.server.api.OperationsPostResult;
20 import org.opendaylight.restconf.server.spi.OperationInput;
21 import org.opendaylight.restconf.server.spi.RestconfStream;
22 import org.opendaylight.restconf.server.spi.RpcImplementation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.device.notification.rev240218.SubscribeDeviceNotification;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.device.notification.rev240218.SubscribeDeviceNotificationInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.device.notification.rev240218.SubscribeDeviceNotificationOutput;
26 import org.opendaylight.yangtools.yang.common.ErrorTag;
27 import org.opendaylight.yangtools.yang.common.ErrorType;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
32 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
33 import org.osgi.service.component.annotations.Activate;
34 import org.osgi.service.component.annotations.Component;
35 import org.osgi.service.component.annotations.Reference;
38 * RESTCONF implementation of {@link SubscribeDeviceNotification}.
42 public final class SubscribeDeviceNotificationRpc extends RpcImplementation {
43 private static final NodeIdentifier DEVICE_NOTIFICATION_PATH_NODEID =
44 NodeIdentifier.create(QName.create(SubscribeDeviceNotificationInput.QNAME, "path").intern());
45 private static final NodeIdentifier DEVICE_NOTIFICATION_STREAM_NAME_NODEID =
46 NodeIdentifier.create(QName.create(SubscribeDeviceNotificationInput.QNAME, "stream-name").intern());
48 private final DOMMountPointService mountPointService;
49 private final RestconfStream.Registry streamRegistry;
53 public SubscribeDeviceNotificationRpc(@Reference final RestconfStream.Registry streamRegistry,
54 @Reference final DOMMountPointService mountPointService) {
55 super(SubscribeDeviceNotification.QNAME);
56 this.mountPointService = requireNonNull(mountPointService);
57 this.streamRegistry = requireNonNull(streamRegistry);
61 public RestconfFuture<OperationsPostResult> invoke(final URI restconfURI, final OperationInput input) {
62 final var body = input.input();
63 final var pathLeaf = body.childByArg(DEVICE_NOTIFICATION_PATH_NODEID);
64 if (pathLeaf == null) {
65 return RestconfFuture.failed(new RestconfDocumentedException("No path specified", ErrorType.APPLICATION,
66 ErrorTag.MISSING_ELEMENT));
68 final var pathLeafBody = pathLeaf.body();
69 if (!(pathLeafBody instanceof YangInstanceIdentifier path)) {
70 return RestconfFuture.failed(new RestconfDocumentedException("Unexpected path " + pathLeafBody,
71 ErrorType.APPLICATION, ErrorTag.BAD_ELEMENT));
73 if (!(path.getLastPathArgument() instanceof NodeIdentifierWithPredicates listId)) {
74 return RestconfFuture.failed(new RestconfDocumentedException(path + " does not refer to a list item",
75 ErrorType.APPLICATION, ErrorTag.BAD_ELEMENT));
77 if (listId.size() != 1) {
78 return RestconfFuture.failed(new RestconfDocumentedException(path + " uses multiple keys",
79 ErrorType.APPLICATION, ErrorTag.INVALID_VALUE));
82 return streamRegistry.createStream(restconfURI, new DeviceNotificationSource(mountPointService, path),
83 "All YANG notifications occuring on mount point /"
84 + new YangInstanceIdentifierSerializer(input.databind()).serializePath(path))
85 .transform(stream -> input.newOperationOutput(ImmutableNodes.newContainerBuilder()
86 .withNodeIdentifier(new NodeIdentifier(SubscribeDeviceNotificationOutput.QNAME))
87 .withChild(ImmutableNodes.leafNode(DEVICE_NOTIFICATION_STREAM_NAME_NODEID, stream.name()))