Revert "Fix nested YANG 1.1 Action invocation"
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / RestconfStreamsSubscriptionServiceImpl.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.nb.rfc8040.rests.services.impl;
9
10 import java.net.URI;
11 import java.time.Instant;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
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;
40
41 /**
42  * Implementation of {@link RestconfStreamsSubscriptionService}.
43  */
44 @Path("/")
45 public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSubscriptionService {
46
47     private static final Logger LOG = LoggerFactory.getLogger(RestconfStreamsSubscriptionServiceImpl.class);
48
49     private HandlersHolder handlersHolder;
50
51     /**
52      * Initialize holder of handlers with holders as parameters.
53      *
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}
62      */
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);
68     }
69
70     @Override
71     public synchronized void updateHandlers(final Object... handlers) {
72         for (final Object object : handlers) {
73             if (object instanceof HandlersHolder) {
74                 handlersHolder = (HandlersHolder) object;
75             }
76         }
77     }
78
79     @Override
80     public NormalizedNodeContext subscribeToStream(final String identifier, final UriInfo uriInfo) {
81         final NotificationQueryParams notificationQueryParams = NotificationQueryParams.fromUriInfo(uriInfo);
82
83         URI response = null;
84         if (identifier.contains(RestconfStreamsConstants.DATA_SUBSCRIPTION)) {
85             response = SubscribeToStreamUtil.subscribeToDataStream(identifier, uriInfo, notificationQueryParams,
86                     this.handlersHolder);
87         } else if (identifier.contains(RestconfStreamsConstants.NOTIFICATION_STREAM)) {
88             response = SubscribeToStreamUtil.subscribeToYangStream(identifier, uriInfo, notificationQueryParams,
89                     this.handlersHolder);
90         }
91
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));
99
100             // prepare new header with location
101             final Map<String, Object> headers = new HashMap<>();
102             headers.put("Location", response);
103
104             return new NormalizedNodeContext(iid, builder.build(), headers);
105         }
106
107         final String msg = "Bad type of notification of sal-remote";
108         LOG.warn(msg);
109         throw new RestconfDocumentedException(msg);
110     }
111
112     /**
113      * Holder of all handlers for notifications.
114      */
115     public static final class HandlersHolder {
116
117         private final DOMDataBrokerHandler domDataBrokerHandler;
118         private final NotificationServiceHandler notificationServiceHandler;
119         private final TransactionChainHandler transactionChainHandler;
120         private final SchemaContextHandler schemaHandler;
121
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;
129         }
130
131         /**
132          * Get {@link DOMDataBrokerHandler}.
133          *
134          * @return the domDataBrokerHandler
135          */
136         public DOMDataBrokerHandler getDomDataBrokerHandler() {
137             return this.domDataBrokerHandler;
138         }
139
140         /**
141          * Get {@link NotificationServiceHandler}.
142          *
143          * @return the notificationServiceHandler
144          */
145         public NotificationServiceHandler getNotificationServiceHandler() {
146             return this.notificationServiceHandler;
147         }
148
149         /**
150          * Get {@link TransactionChainHandler}.
151          *
152          * @return the transactionChainHandler
153          */
154         public TransactionChainHandler getTransactionChainHandler() {
155             return this.transactionChainHandler;
156         }
157
158         /**
159          * Get {@link SchemaContextHandler}.
160          *
161          * @return the schemaHandler
162          */
163         public SchemaContextHandler getSchemaHandler() {
164             return this.schemaHandler;
165         }
166     }
167
168     /**
169      * Parser and holder of query paramteres from uriInfo for notifications.
170      */
171     public static final class NotificationQueryParams {
172
173         private final Instant start;
174         private final Instant stop;
175         private final String filter;
176
177         private NotificationQueryParams(final Instant start, final Instant stop, final String filter) {
178             this.start = start == null ? Instant.now() : start;
179             this.stop = stop;
180             this.filter = filter;
181         }
182
183         static NotificationQueryParams fromUriInfo(final UriInfo uriInfo) {
184             Instant start = null;
185             boolean startTimeUsed = false;
186             Instant stop = null;
187             boolean stopTimeUsed = false;
188             String filter = null;
189             boolean filterUsed = false;
190
191             for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
192                 switch (entry.getKey()) {
193                     case "start-time":
194                         if (!startTimeUsed) {
195                             startTimeUsed = true;
196                             start = SubscribeToStreamUtil.parseDateFromQueryParam(entry);
197                         } else {
198                             throw new RestconfDocumentedException("Start-time parameter can be used only once.");
199                         }
200                         break;
201                     case "stop-time":
202                         if (!stopTimeUsed) {
203                             stopTimeUsed = true;
204                             stop = SubscribeToStreamUtil.parseDateFromQueryParam(entry);
205                         } else {
206                             throw new RestconfDocumentedException("Stop-time parameter can be used only once.");
207                         }
208                         break;
209                     case "filter":
210                         if (!filterUsed) {
211                             filterUsed = true;
212                             filter = entry.getValue().iterator().next();
213                         }
214                         break;
215                     default:
216                         throw new RestconfDocumentedException(
217                                 "Bad parameter used with notifications: " + entry.getKey());
218                 }
219             }
220             if (!startTimeUsed && stopTimeUsed) {
221                 throw new RestconfDocumentedException("Stop-time parameter has to be used with start-time parameter.");
222             }
223
224             return new NotificationQueryParams(start, stop, filter);
225         }
226
227         /**
228          * Get start-time query parameter.
229          *
230          * @return start-time
231          */
232         public @NonNull Instant getStart() {
233             return start;
234         }
235
236         /**
237          * Get stop-time query parameter.
238          *
239          * @return stop-time
240          */
241         public Optional<Instant> getStop() {
242             return Optional.ofNullable(stop);
243         }
244
245         /**
246          * Get filter query parameter.
247          *
248          * @return filter
249          */
250         public Optional<String> getFilter() {
251             return Optional.ofNullable(filter);
252         }
253     }
254 }