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 DOMDataBrokerHandler domDataBrokerHandler;
42 public Response subscribeToStream(final String identifier, final UriInfo uriInfo) {
43 final Map<String, String> mapOfValues = SubscribeToStreamUtil.mapValuesFromUri(identifier);
45 final LogicalDatastoreType ds = SubscribeToStreamUtil.parseURIEnum(LogicalDatastoreType.class,
46 mapOfValues.get(RestconfStreamsConstants.DATASTORE_PARAM_NAME));
48 final String msg = "Stream name doesn't contains datastore value (pattern /datastore=)";
50 throw new RestconfDocumentedException(msg, ErrorType.APPLICATION, ErrorTag.MISSING_ATTRIBUTE);
53 final DataChangeScope scope = SubscribeToStreamUtil.parseURIEnum(DataChangeScope.class,
54 mapOfValues.get(RestconfStreamsConstants.SCOPE_PARAM_NAME));
56 final String msg = "Stream name doesn't contains datastore value (pattern /scope=)";
58 throw new RestconfDocumentedException(msg, ErrorType.APPLICATION, ErrorTag.MISSING_ATTRIBUTE);
61 final String streamName = Notificator.createStreamNameFromUri(identifier);
62 if (Strings.isNullOrEmpty(streamName)) {
63 final String msg = "Stream name is empty.";
65 throw new RestconfDocumentedException(msg, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
68 final ListenerAdapter listener = Notificator.getListenerFor(streamName);
69 SubscribeToStreamUtil.registration(ds, scope, listener, this.domDataBrokerHandler.get());
71 final int port = SubscribeToStreamUtil.prepareNotificationPort();
73 final UriBuilder uriBuilder = uriInfo.getAbsolutePathBuilder();
74 final UriBuilder uriToWebSocketServer = uriBuilder.port(port).scheme(RestconfStreamsConstants.SCHEMA_SUBSCIBRE_URI);
75 final URI uri = uriToWebSocketServer.replacePath(streamName).build();
77 return Response.status(Status.OK).location(uri).build();