a6d5dca51d09ceceaffef65adf3059fe72324103
[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 static com.google.common.base.Preconditions.checkState;
11
12 import java.net.URI;
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;
36
37 /**
38  * Implementation of {@link RestconfStreamsSubscriptionService}.
39  */
40 @Path("/")
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);
49
50     private final SubscribeToStreamUtil streamUtils;
51     private final HandlersHolder handlersHolder;
52
53     /**
54      * Initialize holder of handlers with holders as parameters.
55      *
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}}
62      */
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();
69     }
70
71     @Override
72     public NormalizedNodePayload subscribeToStream(final String identifier, final UriInfo uriInfo) {
73         final NotificationQueryParams notificationQueryParams = NotificationQueryParams.fromUriInfo(uriInfo);
74
75         final URI response;
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);
80         } else {
81             final String msg = "Bad type of notification of sal-remote";
82             LOG.warn(msg);
83             throw new RestconfDocumentedException(msg);
84         }
85
86         // prepare node with value of location
87         return NormalizedNodePayload.ofLocation(prepareIIDSubsStreamOutput(handlersHolder.getSchemaHandler()),
88             LOCATION_NODEID, response);
89     }
90
91     /**
92      * Prepare InstanceIdentifierContext for Location leaf.
93      *
94      * @param schemaHandler Schema context handler.
95      * @return InstanceIdentifier of Location leaf.
96      */
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");
104
105         return new InstanceIdentifierContext<SchemaNode>(LOCATION_PATH, location, null, schemaHandler.get());
106     }
107
108     /**
109      * Holder of all handlers for notifications.
110      */
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;
116
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;
122         }
123
124         /**
125          * Get {@link DOMDataBroker}.
126          *
127          * @return the dataBroker
128          */
129         public DOMDataBroker getDataBroker() {
130             return dataBroker;
131         }
132
133         /**
134          * Get {@link DOMNotificationService}.
135          *
136          * @return the notificationService
137          */
138         public DOMNotificationService getNotificationServiceHandler() {
139             return notificationService;
140         }
141
142         /**
143          * Get {@link SchemaContextHandler}.
144          *
145          * @return the schemaHandler
146          */
147         public SchemaContextHandler getSchemaHandler() {
148             return schemaHandler;
149         }
150     }
151 }