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.restful.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.annotation.Nonnull;
18 import javax.ws.rs.core.UriInfo;
19 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
20 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
21 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
22 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
23 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
24 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
25 import org.opendaylight.restconf.handlers.DOMDataBrokerHandler;
26 import org.opendaylight.restconf.handlers.NotificationServiceHandler;
27 import org.opendaylight.restconf.handlers.SchemaContextHandler;
28 import org.opendaylight.restconf.handlers.TransactionChainHandler;
29 import org.opendaylight.restconf.restful.services.api.RestconfStreamsSubscriptionService;
30 import org.opendaylight.restconf.restful.utils.RestconfStreamsConstants;
31 import org.opendaylight.restconf.restful.utils.SubscribeToStreamUtil;
32 import org.opendaylight.yangtools.yang.common.QName;
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.NormalizedNodeAttrBuilder;
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}.
44 * @deprecated move to splitted module restconf-nb-rfc8040
47 public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSubscriptionService {
49 private static final Logger LOG = LoggerFactory.getLogger(RestconfStreamsSubscriptionServiceImpl.class);
51 private final 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 NormalizedNodeContext subscribeToStream(final String identifier, final UriInfo uriInfo) {
74 final NotificationQueryParams notificationQueryParams = NotificationQueryParams.fromUriInfo(uriInfo);
77 if (identifier.contains(RestconfStreamsConstants.DATA_SUBSCR)) {
78 response = SubscribeToStreamUtil.notifiDataStream(identifier, uriInfo, notificationQueryParams,
80 } else if (identifier.contains(RestconfStreamsConstants.NOTIFICATION_STREAM)) {
81 response = SubscribeToStreamUtil.notifYangStream(identifier, uriInfo, notificationQueryParams,
85 if (response != null) {
86 // prepare node with value of location
87 final InstanceIdentifierContext<?> iid =
88 SubscribeToStreamUtil.prepareIIDSubsStreamOutput(this.handlersHolder.getSchemaHandler());
89 final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> builder =
90 ImmutableLeafNodeBuilder.create().withValue(response.toString());
91 builder.withNodeIdentifier(
92 NodeIdentifier.create(QName.create("subscribe:to:notification", "2016-10-28", "location")));
94 // prepare new header with location
95 final Map<String, Object> headers = new HashMap<>();
96 headers.put("Location", response);
98 return new NormalizedNodeContext(iid, builder.build(), headers);
101 final String msg = "Bad type of notification of sal-remote";
103 throw new RestconfDocumentedException(msg);
107 * Holder of all handlers for notifications.
109 public final class HandlersHolder {
111 private final DOMDataBrokerHandler domDataBrokerHandler;
112 private final NotificationServiceHandler notificationServiceHandler;
113 private final TransactionChainHandler transactionChainHandler;
114 private final SchemaContextHandler schemaHandler;
116 private HandlersHolder(final DOMDataBrokerHandler domDataBrokerHandler,
117 final NotificationServiceHandler notificationServiceHandler,
118 final TransactionChainHandler transactionChainHandler, final SchemaContextHandler schemaHandler) {
119 this.domDataBrokerHandler = domDataBrokerHandler;
120 this.notificationServiceHandler = notificationServiceHandler;
121 this.transactionChainHandler = transactionChainHandler;
122 this.schemaHandler = schemaHandler;
126 * Get {@link DOMDataBrokerHandler}.
128 * @return the domDataBrokerHandler
130 public DOMDataBrokerHandler getDomDataBrokerHandler() {
131 return this.domDataBrokerHandler;
135 * Get {@link NotificationServiceHandler}.
137 * @return the notificationServiceHandler
139 public NotificationServiceHandler getNotificationServiceHandler() {
140 return this.notificationServiceHandler;
144 * Get {@link TransactionChainHandler}.
146 * @return the transactionChainHandler
148 public TransactionChainHandler getTransactionChainHandler() {
149 return this.transactionChainHandler;
153 * Get {@link SchemaContextHandler}.
155 * @return the schemaHandler
157 public SchemaContextHandler getSchemaHandler() {
158 return this.schemaHandler;
163 * Parser and holder of query paramteres from uriInfo for notifications.
166 public static final class NotificationQueryParams {
168 private final Instant start;
169 private final Instant stop;
170 private final String filter;
172 private NotificationQueryParams(final Instant start, final Instant stop, final String filter) {
173 this.start = start == null ? Instant.now() : start;
175 this.filter = filter;
178 static NotificationQueryParams fromUriInfo(final UriInfo uriInfo) {
179 Instant start = null;
180 boolean startTimeUsed = false;
182 boolean stopTimeUsed = false;
183 String filter = null;
184 boolean filterUsed = false;
186 for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
187 switch (entry.getKey()) {
189 if (!startTimeUsed) {
190 startTimeUsed = true;
191 start = SubscribeToStreamUtil.parseDateFromQueryParam(entry);
193 throw new RestconfDocumentedException("Start-time parameter can be used only once.");
199 stop = SubscribeToStreamUtil.parseDateFromQueryParam(entry);
201 throw new RestconfDocumentedException("Stop-time parameter can be used only once.");
207 filter = entry.getValue().iterator().next();
211 throw new RestconfDocumentedException(
212 "Bad parameter used with notifications: " + entry.getKey());
215 if (!startTimeUsed && stopTimeUsed) {
216 throw new RestconfDocumentedException("Stop-time parameter has to be used with start-time parameter.");
219 return new NotificationQueryParams(start, stop, filter);
223 * Get start-time query parameter.
228 public Instant getStart() {
233 * Get stop-time query parameter.
237 public Optional<Instant> getStop() {
238 return Optional.ofNullable(stop);
242 * Get filter query parameter.
246 public Optional<String> getFilter() {
247 return Optional.ofNullable(filter);