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}.
43 @Component(service = RpcImplementation.class)
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 operPath = input.path();
77 final var modelContext = operPath.databind().modelContext();
78 final var description = new StringBuilder("YANG notifications matching any of {");
79 var haveFirst = false;
80 for (var qname : qnames) {
81 final var optModule = modelContext.findModuleStatement(qname.getModule());
82 if (optModule.isEmpty()) {
83 return RestconfFuture.failed(new RestconfDocumentedException(qname + " refers to an unknown module",
84 ErrorType.APPLICATION, ErrorTag.INVALID_VALUE));
86 final var module = optModule.orElseThrow();
87 final var optStmt = module.findSchemaTreeNode(qname);
88 if (optStmt.isEmpty()) {
89 return RestconfFuture.failed(new RestconfDocumentedException(
90 qname + " refers to an unknown notification", ErrorType.APPLICATION, ErrorTag.INVALID_VALUE));
92 if (!(optStmt.orElseThrow() instanceof NotificationEffectiveStatement)) {
93 return RestconfFuture.failed(new RestconfDocumentedException(qname + " refers to a non-notification",
94 ErrorType.APPLICATION, ErrorTag.INVALID_VALUE));
98 description.append(",\n");
102 description.append("\n ")
103 .append(module.argument().getLocalName()).append(':').append(qname.getLocalName());
105 description.append("\n}");
107 return streamRegistry.createStream(restconfURI,
108 new NotificationSource(databindProvider, notificationService, qnames), description.toString())
109 .transform(stream -> new OperationsPostResult(operPath, ImmutableNodes.newContainerBuilder()
110 .withNodeIdentifier(SAL_REMOTE_OUTPUT_NODEID)
111 .withChild(ImmutableNodes.leafNode(STREAM_NAME_NODEID, stream.name()))