Reduce the use of AttrBuilders
[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.annotation.Nonnull;
18 import javax.ws.rs.Path;
19 import javax.ws.rs.core.UriInfo;
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;
41
42 /**
43  * Implementation of {@link RestconfStreamsSubscriptionService}.
44  *
45  */
46 @Path("/")
47 public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSubscriptionService {
48
49     private static final Logger LOG = LoggerFactory.getLogger(RestconfStreamsSubscriptionServiceImpl.class);
50
51     private HandlersHolder handlersHolder;
52
53     /**
54      * Initialize holder of handlers with holders as parameters.
55      *
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}
64      */
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);
70     }
71
72     @Override
73     public synchronized void updateHandlers(final Object... handlers) {
74         for (final Object object : handlers) {
75             if (object instanceof HandlersHolder) {
76                 handlersHolder = (HandlersHolder) object;
77             }
78         }
79     }
80
81     @Override
82     public NormalizedNodeContext subscribeToStream(final String identifier, final UriInfo uriInfo) {
83         final NotificationQueryParams notificationQueryParams = NotificationQueryParams.fromUriInfo(uriInfo);
84
85         URI response = null;
86         if (identifier.contains(RestconfStreamsConstants.DATA_SUBSCR)) {
87             response = SubscribeToStreamUtil.notifiDataStream(identifier, uriInfo, notificationQueryParams,
88                     this.handlersHolder);
89         } else if (identifier.contains(RestconfStreamsConstants.NOTIFICATION_STREAM)) {
90             response = SubscribeToStreamUtil.notifYangStream(identifier, uriInfo, notificationQueryParams,
91                     this.handlersHolder);
92         }
93
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")));
102
103             // prepare new header with location
104             final Map<String, Object> headers = new HashMap<>();
105             headers.put("Location", response);
106
107             return new NormalizedNodeContext(iid, builder.build(), headers);
108         }
109
110         final String msg = "Bad type of notification of sal-remote";
111         LOG.warn(msg);
112         throw new RestconfDocumentedException(msg);
113     }
114
115     /**
116      * Holder of all handlers for notifications.
117      */
118     public static final class HandlersHolder {
119
120         private final DOMDataBrokerHandler domDataBrokerHandler;
121         private final NotificationServiceHandler notificationServiceHandler;
122         private final TransactionChainHandler transactionChainHandler;
123         private final SchemaContextHandler schemaHandler;
124
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;
132         }
133
134         /**
135          * Get {@link DOMDataBrokerHandler}.
136          *
137          * @return the domDataBrokerHandler
138          */
139         public DOMDataBrokerHandler getDomDataBrokerHandler() {
140             return this.domDataBrokerHandler;
141         }
142
143         /**
144          * Get {@link NotificationServiceHandler}.
145          *
146          * @return the notificationServiceHandler
147          */
148         public NotificationServiceHandler getNotificationServiceHandler() {
149             return this.notificationServiceHandler;
150         }
151
152         /**
153          * Get {@link TransactionChainHandler}.
154          *
155          * @return the transactionChainHandler
156          */
157         public TransactionChainHandler getTransactionChainHandler() {
158             return this.transactionChainHandler;
159         }
160
161         /**
162          * Get {@link SchemaContextHandler}.
163          *
164          * @return the schemaHandler
165          */
166         public SchemaContextHandler getSchemaHandler() {
167             return this.schemaHandler;
168         }
169     }
170
171     /**
172      * Parser and holder of query paramteres from uriInfo for notifications.
173      *
174      */
175     public static final class NotificationQueryParams {
176
177         private final Instant start;
178         private final Instant stop;
179         private final String filter;
180
181         private NotificationQueryParams(final Instant start, final Instant stop, final String filter) {
182             this.start = start == null ? Instant.now() : start;
183             this.stop = stop;
184             this.filter = filter;
185         }
186
187         static NotificationQueryParams fromUriInfo(final UriInfo uriInfo) {
188             Instant start = null;
189             boolean startTimeUsed = false;
190             Instant stop = null;
191             boolean stopTimeUsed = false;
192             String filter = null;
193             boolean filterUsed = false;
194
195             for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
196                 switch (entry.getKey()) {
197                     case "start-time":
198                         if (!startTimeUsed) {
199                             startTimeUsed = true;
200                             start = SubscribeToStreamUtil.parseDateFromQueryParam(entry);
201                         } else {
202                             throw new RestconfDocumentedException("Start-time parameter can be used only once.");
203                         }
204                         break;
205                     case "stop-time":
206                         if (!stopTimeUsed) {
207                             stopTimeUsed = true;
208                             stop = SubscribeToStreamUtil.parseDateFromQueryParam(entry);
209                         } else {
210                             throw new RestconfDocumentedException("Stop-time parameter can be used only once.");
211                         }
212                         break;
213                     case "filter":
214                         if (!filterUsed) {
215                             filterUsed = true;
216                             filter = entry.getValue().iterator().next();
217                         }
218                         break;
219                     default:
220                         throw new RestconfDocumentedException(
221                                 "Bad parameter used with notifications: " + entry.getKey());
222                 }
223             }
224             if (!startTimeUsed && stopTimeUsed) {
225                 throw new RestconfDocumentedException("Stop-time parameter has to be used with start-time parameter.");
226             }
227
228             return new NotificationQueryParams(start, stop, filter);
229         }
230
231         /**
232          * Get start-time query parameter.
233          *
234          * @return start-time
235          */
236         @Nonnull
237         public Instant getStart() {
238             return start;
239         }
240
241         /**
242          * Get stop-time query parameter.
243          *
244          * @return stop-time
245          */
246         public Optional<Instant> getStop() {
247             return Optional.ofNullable(stop);
248         }
249
250         /**
251          * Get filter query parameter.
252          *
253          * @return filter
254          */
255         public Optional<String> getFilter() {
256             return Optional.ofNullable(filter);
257         }
258     }
259
260 }