2 * Copyright (c) 2016 Cisco Systems, Inc. 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.nb.rfc8040.rests.services.impl;
11 import java.time.Instant;
12 import java.util.HashMap;
13 import java.util.List;
15 import java.util.Map.Entry;
16 import java.util.Optional;
17 import javax.ws.rs.Path;
18 import javax.ws.rs.core.UriInfo;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
21 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
22 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
23 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
24 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
25 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
26 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMDataBrokerHandler;
27 import org.opendaylight.restconf.nb.rfc8040.handlers.NotificationServiceHandler;
28 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
29 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
30 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfStreamsSubscriptionService;
31 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants;
32 import org.opendaylight.restconf.nb.rfc8040.rests.utils.SubscribeToStreamUtil;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
35 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeBuilder;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * Implementation of {@link RestconfStreamsSubscriptionService}.
45 public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSubscriptionService {
47 private static final Logger LOG = LoggerFactory.getLogger(RestconfStreamsSubscriptionServiceImpl.class);
49 private HandlersHolder handlersHolder;
52 * Initialize holder of handlers with holders as parameters.
54 * @param domDataBrokerHandler
55 * handler of {@link DOMDataBroker}
56 * @param notificationServiceHandler
57 * handler of {@link DOMNotificationService}
58 * @param schemaHandler
59 * handler of {@link SchemaContext}
60 * @param transactionChainHandler
61 * handler of {@link DOMTransactionChain}
63 public RestconfStreamsSubscriptionServiceImpl(final DOMDataBrokerHandler domDataBrokerHandler,
64 final NotificationServiceHandler notificationServiceHandler, final SchemaContextHandler schemaHandler,
65 final TransactionChainHandler transactionChainHandler) {
66 this.handlersHolder = new HandlersHolder(domDataBrokerHandler, notificationServiceHandler,
67 transactionChainHandler, schemaHandler);
71 public synchronized void updateHandlers(final Object... handlers) {
72 for (final Object object : handlers) {
73 if (object instanceof HandlersHolder) {
74 handlersHolder = (HandlersHolder) object;
80 public NormalizedNodeContext subscribeToStream(final String identifier, final UriInfo uriInfo) {
81 final NotificationQueryParams notificationQueryParams = NotificationQueryParams.fromUriInfo(uriInfo);
84 if (identifier.contains(RestconfStreamsConstants.DATA_SUBSCRIPTION)) {
85 response = SubscribeToStreamUtil.subscribeToDataStream(identifier, uriInfo, notificationQueryParams,
87 } else if (identifier.contains(RestconfStreamsConstants.NOTIFICATION_STREAM)) {
88 response = SubscribeToStreamUtil.subscribeToYangStream(identifier, uriInfo, notificationQueryParams,
92 if (response != null) {
93 // prepare node with value of location
94 final InstanceIdentifierContext<?> iid =
95 SubscribeToStreamUtil.prepareIIDSubsStreamOutput(this.handlersHolder.getSchemaHandler());
96 final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> builder =
97 ImmutableLeafNodeBuilder.create().withValue(response.toString());
98 builder.withNodeIdentifier(NodeIdentifier.create(RestconfStreamsConstants.LOCATION_QNAME));
100 // prepare new header with location
101 final Map<String, Object> headers = new HashMap<>();
102 headers.put("Location", response);
104 return new NormalizedNodeContext(iid, builder.build(), headers);
107 final String msg = "Bad type of notification of sal-remote";
109 throw new RestconfDocumentedException(msg);
113 * Holder of all handlers for notifications.
115 public static final class HandlersHolder {
117 private final DOMDataBrokerHandler domDataBrokerHandler;
118 private final NotificationServiceHandler notificationServiceHandler;
119 private final TransactionChainHandler transactionChainHandler;
120 private final SchemaContextHandler schemaHandler;
122 private HandlersHolder(final DOMDataBrokerHandler domDataBrokerHandler,
123 final NotificationServiceHandler notificationServiceHandler,
124 final TransactionChainHandler transactionChainHandler, final SchemaContextHandler schemaHandler) {
125 this.domDataBrokerHandler = domDataBrokerHandler;
126 this.notificationServiceHandler = notificationServiceHandler;
127 this.transactionChainHandler = transactionChainHandler;
128 this.schemaHandler = schemaHandler;
132 * Get {@link DOMDataBrokerHandler}.
134 * @return the domDataBrokerHandler
136 public DOMDataBrokerHandler getDomDataBrokerHandler() {
137 return this.domDataBrokerHandler;
141 * Get {@link NotificationServiceHandler}.
143 * @return the notificationServiceHandler
145 public NotificationServiceHandler getNotificationServiceHandler() {
146 return this.notificationServiceHandler;
150 * Get {@link TransactionChainHandler}.
152 * @return the transactionChainHandler
154 public TransactionChainHandler getTransactionChainHandler() {
155 return this.transactionChainHandler;
159 * Get {@link SchemaContextHandler}.
161 * @return the schemaHandler
163 public SchemaContextHandler getSchemaHandler() {
164 return this.schemaHandler;
169 * Parser and holder of query paramteres from uriInfo for notifications.
171 public static final class NotificationQueryParams {
173 private final Instant start;
174 private final Instant stop;
175 private final String filter;
177 private NotificationQueryParams(final Instant start, final Instant stop, final String filter) {
178 this.start = start == null ? Instant.now() : start;
180 this.filter = filter;
183 static NotificationQueryParams fromUriInfo(final UriInfo uriInfo) {
184 Instant start = null;
185 boolean startTimeUsed = false;
187 boolean stopTimeUsed = false;
188 String filter = null;
189 boolean filterUsed = false;
191 for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
192 switch (entry.getKey()) {
194 if (!startTimeUsed) {
195 startTimeUsed = true;
196 start = SubscribeToStreamUtil.parseDateFromQueryParam(entry);
198 throw new RestconfDocumentedException("Start-time parameter can be used only once.");
204 stop = SubscribeToStreamUtil.parseDateFromQueryParam(entry);
206 throw new RestconfDocumentedException("Stop-time parameter can be used only once.");
212 filter = entry.getValue().iterator().next();
216 throw new RestconfDocumentedException(
217 "Bad parameter used with notifications: " + entry.getKey());
220 if (!startTimeUsed && stopTimeUsed) {
221 throw new RestconfDocumentedException("Stop-time parameter has to be used with start-time parameter.");
224 return new NotificationQueryParams(start, stop, filter);
228 * Get start-time query parameter.
232 public @NonNull Instant getStart() {
237 * Get stop-time query parameter.
241 public Optional<Instant> getStop() {
242 return Optional.ofNullable(stop);
246 * Get filter query parameter.
250 public Optional<String> getFilter() {
251 return Optional.ofNullable(filter);