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.restful.services.impl;
10 import com.google.common.base.Strings;
13 import javax.ws.rs.core.Response;
14 import javax.ws.rs.core.Response.Status;
15 import javax.ws.rs.core.UriBuilder;
16 import javax.ws.rs.core.UriInfo;
17 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
18 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
19 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
20 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
21 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
22 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
23 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
24 import org.opendaylight.restconf.handlers.DOMDataBrokerHandler;
25 import org.opendaylight.restconf.restful.services.api.RestconfStreamsSubscriptionService;
26 import org.opendaylight.restconf.restful.utils.RestconfStreamsConstants;
27 import org.opendaylight.restconf.restful.utils.SubscribeToStreamUtil;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Implementation of {@link RestconfStreamsSubscriptionService}
35 public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSubscriptionService {
37 private static final Logger LOG = LoggerFactory.getLogger(RestconfStreamsSubscriptionServiceImpl.class);
39 private final DOMDataBrokerHandler domDataBrokerHandler;
41 public RestconfStreamsSubscriptionServiceImpl(final DOMDataBrokerHandler domDataBrokerHandler) {
42 this.domDataBrokerHandler = domDataBrokerHandler;
46 public Response subscribeToStream(final String identifier, final UriInfo uriInfo) {
47 final Map<String, String> mapOfValues = SubscribeToStreamUtil.mapValuesFromUri(identifier);
49 final LogicalDatastoreType ds = SubscribeToStreamUtil.parseURIEnum(LogicalDatastoreType.class,
50 mapOfValues.get(RestconfStreamsConstants.DATASTORE_PARAM_NAME));
52 final String msg = "Stream name doesn't contains datastore value (pattern /datastore=)";
54 throw new RestconfDocumentedException(msg, ErrorType.APPLICATION, ErrorTag.MISSING_ATTRIBUTE);
57 final DataChangeScope scope = SubscribeToStreamUtil.parseURIEnum(DataChangeScope.class,
58 mapOfValues.get(RestconfStreamsConstants.SCOPE_PARAM_NAME));
60 final String msg = "Stream name doesn't contains datastore value (pattern /scope=)";
62 throw new RestconfDocumentedException(msg, ErrorType.APPLICATION, ErrorTag.MISSING_ATTRIBUTE);
65 final String streamName = Notificator.createStreamNameFromUri(identifier);
66 if (Strings.isNullOrEmpty(streamName)) {
67 final String msg = "Stream name is empty.";
69 throw new RestconfDocumentedException(msg, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
72 final ListenerAdapter listener = Notificator.getListenerFor(streamName);
73 SubscribeToStreamUtil.registration(ds, scope, listener, this.domDataBrokerHandler.get());
75 final int port = SubscribeToStreamUtil.prepareNotificationPort();
77 final UriBuilder uriBuilder = uriInfo.getAbsolutePathBuilder();
78 final UriBuilder uriToWebSocketServer = uriBuilder.port(port).scheme(RestconfStreamsConstants.SCHEMA_SUBSCIBRE_URI);
79 final URI uri = uriToWebSocketServer.replacePath(streamName).build();
81 return Response.status(Status.OK).location(uri).build();