Split Restconf implementations (draft02 and RFC) - Application
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / restconf / restful / 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.restful.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.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;
40
41 /**
42  * Implementation of {@link RestconfStreamsSubscriptionService}.
43  *
44  * @deprecated move to splitted module restconf-nb-rfc8040
45  */
46 @Deprecated
47 public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSubscriptionService {
48
49     private static final Logger LOG = LoggerFactory.getLogger(RestconfStreamsSubscriptionServiceImpl.class);
50
51     private final 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 NormalizedNodeContext subscribeToStream(final String identifier, final UriInfo uriInfo) {
74         final NotificationQueryParams notificationQueryParams = NotificationQueryParams.fromUriInfo(uriInfo);
75
76         URI response = null;
77         if (identifier.contains(RestconfStreamsConstants.DATA_SUBSCR)) {
78             response = SubscribeToStreamUtil.notifiDataStream(identifier, uriInfo, notificationQueryParams,
79                     this.handlersHolder);
80         } else if (identifier.contains(RestconfStreamsConstants.NOTIFICATION_STREAM)) {
81             response = SubscribeToStreamUtil.notifYangStream(identifier, uriInfo, notificationQueryParams,
82                     this.handlersHolder);
83         }
84
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")));
93
94             // prepare new header with location
95             final Map<String, Object> headers = new HashMap<>();
96             headers.put("Location", response);
97
98             return new NormalizedNodeContext(iid, builder.build(), headers);
99         }
100
101         final String msg = "Bad type of notification of sal-remote";
102         LOG.warn(msg);
103         throw new RestconfDocumentedException(msg);
104     }
105
106     /**
107      * Holder of all handlers for notifications.
108      */
109     public final class HandlersHolder {
110
111         private final DOMDataBrokerHandler domDataBrokerHandler;
112         private final NotificationServiceHandler notificationServiceHandler;
113         private final TransactionChainHandler transactionChainHandler;
114         private final SchemaContextHandler schemaHandler;
115
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;
123         }
124
125         /**
126          * Get {@link DOMDataBrokerHandler}.
127          *
128          * @return the domDataBrokerHandler
129          */
130         public DOMDataBrokerHandler getDomDataBrokerHandler() {
131             return this.domDataBrokerHandler;
132         }
133
134         /**
135          * Get {@link NotificationServiceHandler}.
136          *
137          * @return the notificationServiceHandler
138          */
139         public NotificationServiceHandler getNotificationServiceHandler() {
140             return this.notificationServiceHandler;
141         }
142
143         /**
144          * Get {@link TransactionChainHandler}.
145          *
146          * @return the transactionChainHandler
147          */
148         public TransactionChainHandler getTransactionChainHandler() {
149             return this.transactionChainHandler;
150         }
151
152         /**
153          * Get {@link SchemaContextHandler}.
154          *
155          * @return the schemaHandler
156          */
157         public SchemaContextHandler getSchemaHandler() {
158             return this.schemaHandler;
159         }
160     }
161
162     /**
163      * Parser and holder of query paramteres from uriInfo for notifications.
164      *
165      */
166     public static final class NotificationQueryParams {
167
168         private final Instant start;
169         private final Instant stop;
170         private final String filter;
171
172         private NotificationQueryParams(final Instant start, final Instant stop, final String filter) {
173             this.start = start == null ? Instant.now() : start;
174             this.stop = stop;
175             this.filter = filter;
176         }
177
178         static NotificationQueryParams fromUriInfo(final UriInfo uriInfo) {
179             Instant start = null;
180             boolean startTimeUsed = false;
181             Instant stop = null;
182             boolean stopTimeUsed = false;
183             String filter = null;
184             boolean filterUsed = false;
185
186             for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
187                 switch (entry.getKey()) {
188                     case "start-time":
189                         if (!startTimeUsed) {
190                             startTimeUsed = true;
191                             start = SubscribeToStreamUtil.parseDateFromQueryParam(entry);
192                         } else {
193                             throw new RestconfDocumentedException("Start-time parameter can be used only once.");
194                         }
195                         break;
196                     case "stop-time":
197                         if (!stopTimeUsed) {
198                             stopTimeUsed = true;
199                             stop = SubscribeToStreamUtil.parseDateFromQueryParam(entry);
200                         } else {
201                             throw new RestconfDocumentedException("Stop-time parameter can be used only once.");
202                         }
203                         break;
204                     case "filter":
205                         if (!filterUsed) {
206                             filterUsed = true;
207                             filter = entry.getValue().iterator().next();
208                         }
209                         break;
210                     default:
211                         throw new RestconfDocumentedException(
212                                 "Bad parameter used with notifications: " + entry.getKey());
213                 }
214             }
215             if (!startTimeUsed && stopTimeUsed) {
216                 throw new RestconfDocumentedException("Stop-time parameter has to be used with start-time parameter.");
217             }
218
219             return new NotificationQueryParams(start, stop, filter);
220         }
221
222         /**
223          * Get start-time query parameter.
224          *
225          * @return start-time
226          */
227         @Nonnull
228         public Instant getStart() {
229             return start;
230         }
231
232         /**
233          * Get stop-time query parameter.
234          *
235          * @return stop-time
236          */
237         public Optional<Instant> getStop() {
238             return Optional.ofNullable(stop);
239         }
240
241         /**
242          * Get filter query parameter.
243          *
244          * @return filter
245          */
246         public Optional<String> getFilter() {
247             return Optional.ofNullable(filter);
248         }
249     }
250
251 }