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.common.QName;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
36 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeBuilder;
37 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
43 * Implementation of {@link RestconfStreamsSubscriptionService}.
47 public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSubscriptionService {
49 private static final Logger LOG = LoggerFactory.getLogger(RestconfStreamsSubscriptionServiceImpl.class);
51 private HandlersHolder handlersHolder;
54 * Initialize holder of handlers with holders as parameters.
56 * @param domDataBrokerHandler
57 * handler of {@link DOMDataBroker}
58 * @param notificationServiceHandler
59 * handler of {@link DOMNotificationService}
60 * @param schemaHandler
61 * handler of {@link SchemaContext}
62 * @param transactionChainHandler
63 * handler of {@link DOMTransactionChain}
65 public RestconfStreamsSubscriptionServiceImpl(final DOMDataBrokerHandler domDataBrokerHandler,
66 final NotificationServiceHandler notificationServiceHandler, final SchemaContextHandler schemaHandler,
67 final TransactionChainHandler transactionChainHandler) {
68 this.handlersHolder = new HandlersHolder(domDataBrokerHandler, notificationServiceHandler,
69 transactionChainHandler, schemaHandler);
73 public synchronized void updateHandlers(final Object... handlers) {
74 for (final Object object : handlers) {
75 if (object instanceof HandlersHolder) {
76 handlersHolder = (HandlersHolder) object;
82 public NormalizedNodeContext subscribeToStream(final String identifier, final UriInfo uriInfo) {
83 final NotificationQueryParams notificationQueryParams = NotificationQueryParams.fromUriInfo(uriInfo);
86 if (identifier.contains(RestconfStreamsConstants.DATA_SUBSCR)) {
87 response = SubscribeToStreamUtil.notifiDataStream(identifier, uriInfo, notificationQueryParams,
89 } else if (identifier.contains(RestconfStreamsConstants.NOTIFICATION_STREAM)) {
90 response = SubscribeToStreamUtil.notifYangStream(identifier, uriInfo, notificationQueryParams,
94 if (response != null) {
95 // prepare node with value of location
96 final InstanceIdentifierContext<?> iid =
97 SubscribeToStreamUtil.prepareIIDSubsStreamOutput(this.handlersHolder.getSchemaHandler());
98 final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> builder =
99 ImmutableLeafNodeBuilder.create().withValue(response.toString());
100 builder.withNodeIdentifier(
101 NodeIdentifier.create(QName.create("subscribe:to:notification", "2016-10-28", "location")));
103 // prepare new header with location
104 final Map<String, Object> headers = new HashMap<>();
105 headers.put("Location", response);
107 return new NormalizedNodeContext(iid, builder.build(), headers);
110 final String msg = "Bad type of notification of sal-remote";
112 throw new RestconfDocumentedException(msg);
116 * Holder of all handlers for notifications.
118 public static final class HandlersHolder {
120 private final DOMDataBrokerHandler domDataBrokerHandler;
121 private final NotificationServiceHandler notificationServiceHandler;
122 private final TransactionChainHandler transactionChainHandler;
123 private final SchemaContextHandler schemaHandler;
125 private HandlersHolder(final DOMDataBrokerHandler domDataBrokerHandler,
126 final NotificationServiceHandler notificationServiceHandler,
127 final TransactionChainHandler transactionChainHandler, final SchemaContextHandler schemaHandler) {
128 this.domDataBrokerHandler = domDataBrokerHandler;
129 this.notificationServiceHandler = notificationServiceHandler;
130 this.transactionChainHandler = transactionChainHandler;
131 this.schemaHandler = schemaHandler;
135 * Get {@link DOMDataBrokerHandler}.
137 * @return the domDataBrokerHandler
139 public DOMDataBrokerHandler getDomDataBrokerHandler() {
140 return this.domDataBrokerHandler;
144 * Get {@link NotificationServiceHandler}.
146 * @return the notificationServiceHandler
148 public NotificationServiceHandler getNotificationServiceHandler() {
149 return this.notificationServiceHandler;
153 * Get {@link TransactionChainHandler}.
155 * @return the transactionChainHandler
157 public TransactionChainHandler getTransactionChainHandler() {
158 return this.transactionChainHandler;
162 * Get {@link SchemaContextHandler}.
164 * @return the schemaHandler
166 public SchemaContextHandler getSchemaHandler() {
167 return this.schemaHandler;
172 * Parser and holder of query paramteres from uriInfo for notifications.
175 public static final class NotificationQueryParams {
177 private final Instant start;
178 private final Instant stop;
179 private final String filter;
181 private NotificationQueryParams(final Instant start, final Instant stop, final String filter) {
182 this.start = start == null ? Instant.now() : start;
184 this.filter = filter;
187 static NotificationQueryParams fromUriInfo(final UriInfo uriInfo) {
188 Instant start = null;
189 boolean startTimeUsed = false;
191 boolean stopTimeUsed = false;
192 String filter = null;
193 boolean filterUsed = false;
195 for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
196 switch (entry.getKey()) {
198 if (!startTimeUsed) {
199 startTimeUsed = true;
200 start = SubscribeToStreamUtil.parseDateFromQueryParam(entry);
202 throw new RestconfDocumentedException("Start-time parameter can be used only once.");
208 stop = SubscribeToStreamUtil.parseDateFromQueryParam(entry);
210 throw new RestconfDocumentedException("Stop-time parameter can be used only once.");
216 filter = entry.getValue().iterator().next();
220 throw new RestconfDocumentedException(
221 "Bad parameter used with notifications: " + entry.getKey());
224 if (!startTimeUsed && stopTimeUsed) {
225 throw new RestconfDocumentedException("Stop-time parameter has to be used with start-time parameter.");
228 return new NotificationQueryParams(start, stop, filter);
232 * Get start-time query parameter.
236 public @NonNull Instant getStart() {
241 * Get stop-time query parameter.
245 public Optional<Instant> getStop() {
246 return Optional.ofNullable(stop);
250 * Get filter query parameter.
254 public Optional<String> getFilter() {
255 return Optional.ofNullable(filter);