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.nb.rfc8040.rests.services.impl;
11 import javax.ws.rs.Path;
12 import javax.ws.rs.core.UriInfo;
13 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
14 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
15 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
16 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
17 import org.opendaylight.restconf.nb.rfc8040.NotificationQueryParams;
18 import org.opendaylight.restconf.nb.rfc8040.databind.jaxrs.QueryParams;
19 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
20 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
21 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfStreamsSubscriptionService;
22 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants;
23 import org.opendaylight.restconf.nb.rfc8040.streams.Configuration;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Implementation of {@link RestconfStreamsSubscriptionService}.
35 public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSubscriptionService {
36 private static final Logger LOG = LoggerFactory.getLogger(RestconfStreamsSubscriptionServiceImpl.class);
37 private static final QName LOCATION_QNAME =
38 QName.create("subscribe:to:notification", "2016-10-28", "location").intern();
39 private static final NodeIdentifier LOCATION_NODEID = NodeIdentifier.create(LOCATION_QNAME);
40 private static final QName NOTIFI_QNAME = QName.create(LOCATION_QNAME, "notifi").intern();
42 private final SubscribeToStreamUtil streamUtils;
43 private final HandlersHolder handlersHolder;
46 * Initialize holder of handlers with holders as parameters.
48 * @param dataBroker {@link DOMDataBroker}
49 * @param notificationService {@link DOMNotificationService}
50 * @param schemaHandler
51 * handler of {@link SchemaContext}
52 * @param configuration
53 * configuration for restconf {@link Configuration}}
55 public RestconfStreamsSubscriptionServiceImpl(final DOMDataBroker dataBroker,
56 final DOMNotificationService notificationService, final SchemaContextHandler schemaHandler,
57 final Configuration configuration) {
58 handlersHolder = new HandlersHolder(dataBroker, notificationService, schemaHandler);
59 streamUtils = configuration.isUseSSE() ? SubscribeToStreamUtil.serverSentEvents()
60 : SubscribeToStreamUtil.webSockets();
64 public NormalizedNodePayload subscribeToStream(final String identifier, final UriInfo uriInfo) {
65 final NotificationQueryParams params = QueryParams.newNotificationQueryParams(uriInfo);
68 if (identifier.contains(RestconfStreamsConstants.DATA_SUBSCRIPTION)) {
69 response = streamUtils.subscribeToDataStream(identifier, uriInfo, params, handlersHolder);
70 } else if (identifier.contains(RestconfStreamsConstants.NOTIFICATION_STREAM)) {
71 response = streamUtils.subscribeToYangStream(identifier, uriInfo, params, handlersHolder);
73 final String msg = "Bad type of notification of sal-remote";
75 throw new RestconfDocumentedException(msg);
78 // prepare node with value of location
79 return NormalizedNodePayload.ofLocation(prepareIIDSubsStreamOutput(handlersHolder.getSchemaHandler()),
80 LOCATION_NODEID, response);
84 * Prepare InstanceIdentifierContext for Location leaf.
86 * @param schemaHandler Schema context handler.
87 * @return InstanceIdentifier of Location leaf.
89 private static InstanceIdentifierContext prepareIIDSubsStreamOutput(final SchemaContextHandler schemaHandler) {
90 return InstanceIdentifierContext.ofStack(
91 SchemaInferenceStack.ofDataTreePath(schemaHandler.get(), NOTIFI_QNAME, LOCATION_QNAME));
95 * Holder of all handlers for notifications.
97 // FIXME: why do we even need this class?!
98 public static final class HandlersHolder {
99 private final DOMDataBroker dataBroker;
100 private final DOMNotificationService notificationService;
101 private final SchemaContextHandler schemaHandler;
103 private HandlersHolder(final DOMDataBroker dataBroker, final DOMNotificationService notificationService,
104 final SchemaContextHandler schemaHandler) {
105 this.dataBroker = dataBroker;
106 this.notificationService = notificationService;
107 this.schemaHandler = schemaHandler;
111 * Get {@link DOMDataBroker}.
113 * @return the dataBroker
115 public DOMDataBroker getDataBroker() {
120 * Get {@link DOMNotificationService}.
122 * @return the notificationService
124 public DOMNotificationService getNotificationServiceHandler() {
125 return notificationService;
129 * Get {@link SchemaContextHandler}.
131 * @return the schemaHandler
133 public SchemaContextHandler getSchemaHandler() {
134 return schemaHandler;