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}.
45 public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSubscriptionService {
47 private static final Logger LOG = LoggerFactory.getLogger(RestconfStreamsSubscriptionServiceImpl.class);
49 private final 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 NormalizedNodeContext subscribeToStream(final String identifier, final UriInfo uriInfo) {
72 final NotificationQueryParams notificationQueryParams = NotificationQueryParams.fromUriInfo(uriInfo);
75 if (identifier.contains(RestconfStreamsConstants.DATA_SUBSCR)) {
76 response = SubscribeToStreamUtil.notifiDataStream(identifier, uriInfo, notificationQueryParams,
78 } else if (identifier.contains(RestconfStreamsConstants.NOTIFICATION_STREAM)) {
79 response = SubscribeToStreamUtil.notifYangStream(identifier, uriInfo, notificationQueryParams,
83 if (response != null) {
84 // prepare node with value of location
85 final InstanceIdentifierContext<?> iid =
86 SubscribeToStreamUtil.prepareIIDSubsStreamOutput(this.handlersHolder.getSchemaHandler());
87 final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> builder =
88 ImmutableLeafNodeBuilder.create().withValue(response.toString());
89 builder.withNodeIdentifier(
90 NodeIdentifier.create(QName.create("subscribe:to:notification", "2016-10-28", "location")));
92 // prepare new header with location
93 final Map<String, Object> headers = new HashMap<>();
94 headers.put("Location", response);
96 return new NormalizedNodeContext(iid, builder.build(), headers);
99 final String msg = "Bad type of notification of sal-remote";
101 throw new RestconfDocumentedException(msg);
105 * Holder of all handlers for notifications.
107 public final class HandlersHolder {
109 private final DOMDataBrokerHandler domDataBrokerHandler;
110 private final NotificationServiceHandler notificationServiceHandler;
111 private final TransactionChainHandler transactionChainHandler;
112 private final SchemaContextHandler schemaHandler;
114 private HandlersHolder(final DOMDataBrokerHandler domDataBrokerHandler,
115 final NotificationServiceHandler notificationServiceHandler,
116 final TransactionChainHandler transactionChainHandler, final SchemaContextHandler schemaHandler) {
117 this.domDataBrokerHandler = domDataBrokerHandler;
118 this.notificationServiceHandler = notificationServiceHandler;
119 this.transactionChainHandler = transactionChainHandler;
120 this.schemaHandler = schemaHandler;
124 * Get {@link DOMDataBrokerHandler}.
126 * @return the domDataBrokerHandler
128 public DOMDataBrokerHandler getDomDataBrokerHandler() {
129 return this.domDataBrokerHandler;
133 * Get {@link NotificationServiceHandler}.
135 * @return the notificationServiceHandler
137 public NotificationServiceHandler getNotificationServiceHandler() {
138 return this.notificationServiceHandler;
142 * Get {@link TransactionChainHandler}.
144 * @return the transactionChainHandler
146 public TransactionChainHandler getTransactionChainHandler() {
147 return this.transactionChainHandler;
151 * Get {@link SchemaContextHandler}.
153 * @return the schemaHandler
155 public SchemaContextHandler getSchemaHandler() {
156 return this.schemaHandler;
161 * Parser and holder of query paramteres from uriInfo for notifications.
164 public static final class NotificationQueryParams {
166 private final Instant start;
167 private final Instant stop;
168 private final String filter;
170 private NotificationQueryParams(final Instant start, final Instant stop, final String filter) {
171 this.start = start == null ? Instant.now() : start;
173 this.filter = filter;
176 static NotificationQueryParams fromUriInfo(final UriInfo uriInfo) {
177 Instant start = null;
178 boolean startTimeUsed = false;
180 boolean stopTimeUsed = false;
181 String filter = null;
182 boolean filterUsed = false;
184 for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
185 switch (entry.getKey()) {
187 if (!startTimeUsed) {
188 startTimeUsed = true;
189 start = SubscribeToStreamUtil.parseDateFromQueryParam(entry);
191 throw new RestconfDocumentedException("Start-time parameter can be used only once.");
197 stop = SubscribeToStreamUtil.parseDateFromQueryParam(entry);
199 throw new RestconfDocumentedException("Stop-time parameter can be used only once.");
205 filter = entry.getValue().iterator().next();
209 throw new RestconfDocumentedException(
210 "Bad parameter used with notifications: " + entry.getKey());
213 if (!startTimeUsed && stopTimeUsed) {
214 throw new RestconfDocumentedException("Stop-time parameter has to be used with start-time parameter.");
217 return new NotificationQueryParams(start, stop, filter);
221 * Get start-time query parameter.
226 public Instant getStart() {
231 * Get stop-time query parameter.
235 public Optional<Instant> getStop() {
236 return Optional.ofNullable(stop);
240 * Get filter query parameter.
244 public Optional<String> getFilter() {
245 return Optional.ofNullable(filter);