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.rev221106.SubscribeDeviceNotification;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.device.notification.rev221106.SubscribeDeviceNotificationInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.device.notification.rev221106.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.impl.schema.Builders;
33 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Reference;
39 * RESTCONF implementation of {@link SubscribeDeviceNotification}.
43 public final class SubscribeDeviceNotificationRpc extends RpcImplementation {
44 private static final NodeIdentifier DEVICE_NOTIFICATION_PATH_NODEID =
45 NodeIdentifier.create(QName.create(SubscribeDeviceNotificationInput.QNAME, "path").intern());
46 // FIXME: NETCONF-1102: this should be 'stream-name'
47 private static final NodeIdentifier DEVICE_NOTIFICATION_STREAM_PATH_NODEID =
48 NodeIdentifier.create(QName.create(SubscribeDeviceNotificationInput.QNAME, "stream-path").intern());
50 private final DOMMountPointService mountPointService;
51 private final RestconfStream.Registry streamRegistry;
55 public SubscribeDeviceNotificationRpc(@Reference final RestconfStream.Registry streamRegistry,
56 @Reference final DOMMountPointService mountPointService) {
57 super(SubscribeDeviceNotification.QNAME);
58 this.mountPointService = requireNonNull(mountPointService);
59 this.streamRegistry = requireNonNull(streamRegistry);
63 public RestconfFuture<OperationsPostResult> invoke(final URI restconfURI, final OperationInput input) {
64 final var body = input.input();
65 final var pathLeaf = body.childByArg(DEVICE_NOTIFICATION_PATH_NODEID);
66 if (pathLeaf == null) {
67 return RestconfFuture.failed(new RestconfDocumentedException("No path specified", ErrorType.APPLICATION,
68 ErrorTag.MISSING_ELEMENT));
70 final var pathLeafBody = pathLeaf.body();
71 if (!(pathLeafBody instanceof YangInstanceIdentifier path)) {
72 return RestconfFuture.failed(new RestconfDocumentedException("Unexpected path " + pathLeafBody,
73 ErrorType.APPLICATION, ErrorTag.BAD_ELEMENT));
75 if (!(path.getLastPathArgument() instanceof NodeIdentifierWithPredicates listId)) {
76 return RestconfFuture.failed(new RestconfDocumentedException(path + " does not refer to a list item",
77 ErrorType.APPLICATION, ErrorTag.BAD_ELEMENT));
79 if (listId.size() != 1) {
80 return RestconfFuture.failed(new RestconfDocumentedException(path + " uses multiple keys",
81 ErrorType.APPLICATION, ErrorTag.INVALID_VALUE));
84 return streamRegistry.createStream(restconfURI, new DeviceNotificationSource(mountPointService, path),
85 "All YANG notifications occuring on mount point /"
86 + new YangInstanceIdentifierSerializer(input.databind()).serializePath(path))
87 .transform(stream -> input.newOperationOutput(Builders.containerBuilder()
88 .withNodeIdentifier(new NodeIdentifier(SubscribeDeviceNotificationOutput.QNAME))
89 .withChild(ImmutableNodes.leafNode(DEVICE_NOTIFICATION_STREAM_PATH_NODEID, stream.name()))