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;
10 import static com.google.common.base.Preconditions.checkState;
13 import java.util.Optional;
14 import javax.ws.rs.Path;
15 import javax.ws.rs.core.UriInfo;
16 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
17 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
18 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
19 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
20 import org.opendaylight.restconf.nb.rfc8040.NotificationQueryParams;
21 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
22 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
23 import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfStreamsSubscriptionService;
24 import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfStreamsConstants;
25 import org.opendaylight.restconf.nb.rfc8040.streams.Configuration;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
29 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Implementation of {@link RestconfStreamsSubscriptionService}.
41 public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSubscriptionService {
42 private static final Logger LOG = LoggerFactory.getLogger(RestconfStreamsSubscriptionServiceImpl.class);
43 private static final QName LOCATION_QNAME =
44 QName.create("subscribe:to:notification", "2016-10-28", "location").intern();
45 private static final NodeIdentifier LOCATION_NODEID = NodeIdentifier.create(LOCATION_QNAME);
46 private static final QName NOTIFI_QNAME = QName.create(LOCATION_QNAME, "notifi").intern();
47 private static final YangInstanceIdentifier LOCATION_PATH =
48 YangInstanceIdentifier.create(NodeIdentifier.create(NOTIFI_QNAME), LOCATION_NODEID);
50 private final SubscribeToStreamUtil streamUtils;
51 private final HandlersHolder handlersHolder;
54 * Initialize holder of handlers with holders as parameters.
56 * @param dataBroker {@link DOMDataBroker}
57 * @param notificationService {@link DOMNotificationService}
58 * @param schemaHandler
59 * handler of {@link SchemaContext}
60 * @param configuration
61 * configuration for restconf {@link Configuration}}
63 public RestconfStreamsSubscriptionServiceImpl(final DOMDataBroker dataBroker,
64 final DOMNotificationService notificationService, final SchemaContextHandler schemaHandler,
65 final Configuration configuration) {
66 handlersHolder = new HandlersHolder(dataBroker, notificationService, schemaHandler);
67 streamUtils = configuration.isUseSSE() ? SubscribeToStreamUtil.serverSentEvents()
68 : SubscribeToStreamUtil.webSockets();
72 public NormalizedNodePayload subscribeToStream(final String identifier, final UriInfo uriInfo) {
73 final NotificationQueryParams notificationQueryParams = NotificationQueryParams.fromUriInfo(uriInfo);
76 if (identifier.contains(RestconfStreamsConstants.DATA_SUBSCRIPTION)) {
77 response = streamUtils.subscribeToDataStream(identifier, uriInfo, notificationQueryParams, handlersHolder);
78 } else if (identifier.contains(RestconfStreamsConstants.NOTIFICATION_STREAM)) {
79 response = streamUtils.subscribeToYangStream(identifier, uriInfo, notificationQueryParams, handlersHolder);
81 final String msg = "Bad type of notification of sal-remote";
83 throw new RestconfDocumentedException(msg);
86 // prepare node with value of location
87 return NormalizedNodePayload.ofLocation(prepareIIDSubsStreamOutput(handlersHolder.getSchemaHandler()),
88 LOCATION_NODEID, response);
92 * Prepare InstanceIdentifierContext for Location leaf.
94 * @param schemaHandler Schema context handler.
95 * @return InstanceIdentifier of Location leaf.
97 private static InstanceIdentifierContext<?> prepareIIDSubsStreamOutput(final SchemaContextHandler schemaHandler) {
98 final Optional<Module> module = schemaHandler.get().findModule(NOTIFI_QNAME.getModule());
99 checkState(module.isPresent());
100 final DataSchemaNode notify = module.get().dataChildByName(NOTIFI_QNAME);
101 checkState(notify instanceof ContainerSchemaNode, "Unexpected non-container %s", notify);
102 final DataSchemaNode location = ((ContainerSchemaNode) notify).dataChildByName(LOCATION_QNAME);
103 checkState(location != null, "Missing location");
105 return new InstanceIdentifierContext<SchemaNode>(LOCATION_PATH, location, null, schemaHandler.get());
109 * Holder of all handlers for notifications.
111 // FIXME: why do we even need this class?!
112 public static final class HandlersHolder {
113 private final DOMDataBroker dataBroker;
114 private final DOMNotificationService notificationService;
115 private final SchemaContextHandler schemaHandler;
117 private HandlersHolder(final DOMDataBroker dataBroker, final DOMNotificationService notificationService,
118 final SchemaContextHandler schemaHandler) {
119 this.dataBroker = dataBroker;
120 this.notificationService = notificationService;
121 this.schemaHandler = schemaHandler;
125 * Get {@link DOMDataBroker}.
127 * @return the dataBroker
129 public DOMDataBroker getDataBroker() {
134 * Get {@link DOMNotificationService}.
136 * @return the notificationService
138 public DOMNotificationService getNotificationServiceHandler() {
139 return notificationService;
143 * Get {@link SchemaContextHandler}.
145 * @return the schemaHandler
147 public SchemaContextHandler getSchemaHandler() {
148 return schemaHandler;