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.notif;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableSet;
14 import javax.inject.Inject;
15 import javax.inject.Singleton;
16 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
17 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
18 import org.opendaylight.restconf.common.errors.RestconfFuture;
19 import org.opendaylight.restconf.server.api.OperationsPostResult;
20 import org.opendaylight.restconf.server.spi.DatabindProvider;
21 import org.opendaylight.restconf.server.spi.OperationInput;
22 import org.opendaylight.restconf.server.spi.RestconfStream;
23 import org.opendaylight.restconf.server.spi.RpcImplementation;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.CreateDataChangeEventSubscriptionOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.CreateNotificationStream;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.remote.rev140114.CreateNotificationStreamInput;
27 import org.opendaylight.yangtools.yang.common.ErrorTag;
28 import org.opendaylight.yangtools.yang.common.ErrorType;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
33 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
34 import org.opendaylight.yangtools.yang.model.api.stmt.NotificationEffectiveStatement;
35 import org.osgi.service.component.annotations.Activate;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Reference;
40 * RESTCONF implementation of {@link CreateNotificationStream}.
44 public final class CreateNotificationStreamRpc extends RpcImplementation {
45 private static final NodeIdentifier SAL_REMOTE_OUTPUT_NODEID =
46 NodeIdentifier.create(CreateDataChangeEventSubscriptionOutput.QNAME);
47 private static final NodeIdentifier NOTIFICATIONS =
48 NodeIdentifier.create(QName.create(CreateNotificationStreamInput.QNAME, "notifications").intern());
49 private static final NodeIdentifier STREAM_NAME_NODEID =
50 NodeIdentifier.create(QName.create(CreateDataChangeEventSubscriptionOutput.QNAME, "stream-name").intern());
52 private final DatabindProvider databindProvider;
53 private final DOMNotificationService notificationService;
54 private final RestconfStream.Registry streamRegistry;
58 public CreateNotificationStreamRpc(@Reference final RestconfStream.Registry streamRegistry,
59 @Reference final DatabindProvider databindProvider,
60 @Reference final DOMNotificationService notificationService) {
61 super(CreateNotificationStream.QNAME);
62 this.databindProvider = requireNonNull(databindProvider);
63 this.notificationService = requireNonNull(notificationService);
64 this.streamRegistry = requireNonNull(streamRegistry);
68 public RestconfFuture<OperationsPostResult> invoke(final URI restconfURI, final OperationInput input) {
69 final var body = input.input();
70 final var qnames = ((LeafSetNode<String>) body.getChildByArg(NOTIFICATIONS)).body().stream()
71 .map(LeafSetEntryNode::body)
74 .collect(ImmutableSet.toImmutableSet());
76 final var modelContext = input.databind().modelContext();
77 final var description = new StringBuilder("YANG notifications matching any of {");
78 var haveFirst = false;
79 for (var qname : qnames) {
80 final var optModule = modelContext.findModuleStatement(qname.getModule());
81 if (optModule.isEmpty()) {
82 return RestconfFuture.failed(new RestconfDocumentedException(qname + " refers to an unknown module",
83 ErrorType.APPLICATION, ErrorTag.INVALID_VALUE));
85 final var module = optModule.orElseThrow();
86 final var optStmt = module.findSchemaTreeNode(qname);
87 if (optStmt.isEmpty()) {
88 return RestconfFuture.failed(new RestconfDocumentedException(
89 qname + " refers to an unknown notification", ErrorType.APPLICATION, ErrorTag.INVALID_VALUE));
91 if (!(optStmt.orElseThrow() instanceof NotificationEffectiveStatement)) {
92 return RestconfFuture.failed(new RestconfDocumentedException(qname + " refers to a non-notification",
93 ErrorType.APPLICATION, ErrorTag.INVALID_VALUE));
97 description.append(",\n");
101 description.append("\n ")
102 .append(module.argument().getLocalName()).append(':').append(qname.getLocalName());
104 description.append("\n}");
106 return streamRegistry.createStream(restconfURI,
107 new NotificationSource(databindProvider, notificationService, qnames), description.toString())
108 .transform(stream -> input.newOperationOutput(ImmutableNodes.newContainerBuilder()
109 .withNodeIdentifier(SAL_REMOTE_OUTPUT_NODEID)
110 .withChild(ImmutableNodes.leafNode(STREAM_NAME_NODEID, stream.name()))