d1918f2e097507542ddc1b6e050a1e0dfd127b85
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / server / mdsal / streams / devnotif / SubscribeDeviceNotificationRpc.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech, s.r.o. 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.server.mdsal.streams.devnotif;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.net.URI;
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.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;
36
37 /**
38  * RESTCONF implementation of {@link SubscribeDeviceNotification}.
39  */
40 @Singleton
41 @Component
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     // FIXME: NETCONF-1102: this should be 'stream-name'
46     private static final NodeIdentifier DEVICE_NOTIFICATION_STREAM_PATH_NODEID =
47         NodeIdentifier.create(QName.create(SubscribeDeviceNotificationInput.QNAME, "stream-path").intern());
48
49     private final DOMMountPointService mountPointService;
50     private final RestconfStream.Registry streamRegistry;
51
52     @Inject
53     @Activate
54     public SubscribeDeviceNotificationRpc(@Reference final RestconfStream.Registry streamRegistry,
55             @Reference final DOMMountPointService mountPointService) {
56         super(SubscribeDeviceNotification.QNAME);
57         this.mountPointService = requireNonNull(mountPointService);
58         this.streamRegistry = requireNonNull(streamRegistry);
59     }
60
61     @Override
62     public RestconfFuture<OperationsPostResult> invoke(final URI restconfURI, final OperationInput input) {
63         final var body = input.input();
64         final var pathLeaf = body.childByArg(DEVICE_NOTIFICATION_PATH_NODEID);
65         if (pathLeaf == null) {
66             return RestconfFuture.failed(new RestconfDocumentedException("No path specified", ErrorType.APPLICATION,
67                 ErrorTag.MISSING_ELEMENT));
68         }
69         final var pathLeafBody = pathLeaf.body();
70         if (!(pathLeafBody instanceof YangInstanceIdentifier path)) {
71             return RestconfFuture.failed(new RestconfDocumentedException("Unexpected path " + pathLeafBody,
72                 ErrorType.APPLICATION, ErrorTag.BAD_ELEMENT));
73         }
74         if (!(path.getLastPathArgument() instanceof NodeIdentifierWithPredicates listId)) {
75             return RestconfFuture.failed(new RestconfDocumentedException(path + " does not refer to a list item",
76                 ErrorType.APPLICATION, ErrorTag.BAD_ELEMENT));
77         }
78         if (listId.size() != 1) {
79             return RestconfFuture.failed(new RestconfDocumentedException(path + " uses multiple keys",
80                 ErrorType.APPLICATION, ErrorTag.INVALID_VALUE));
81         }
82
83         return streamRegistry.createStream(restconfURI, new DeviceNotificationSource(mountPointService, path),
84             "All YANG notifications occuring on mount point /"
85                 + new YangInstanceIdentifierSerializer(input.databind()).serializePath(path))
86             .transform(stream -> input.newOperationOutput(ImmutableNodes.newContainerBuilder()
87                 .withNodeIdentifier(new NodeIdentifier(SubscribeDeviceNotificationOutput.QNAME))
88                 .withChild(ImmutableNodes.leafNode(DEVICE_NOTIFICATION_STREAM_PATH_NODEID, stream.name()))
89                 .build()));
90     }
91 }