Bug 5528 - Subscribing to stream impl
[netconf.git] / restconf / sal-rest-connector / src / main / java / org / opendaylight / restconf / restful / 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.restful.services.impl;
9
10 import com.google.common.base.Strings;
11 import java.net.URI;
12 import java.util.Map;
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;
30
31 /**
32  * Implementation of {@link RestconfStreamsSubscriptionService}
33  *
34  */
35 public class RestconfStreamsSubscriptionServiceImpl implements RestconfStreamsSubscriptionService {
36
37     private static final Logger LOG = LoggerFactory.getLogger(RestconfStreamsSubscriptionServiceImpl.class);
38
39     private DOMDataBrokerHandler domDataBrokerHandler;
40
41     @Override
42     public Response subscribeToStream(final String identifier, final UriInfo uriInfo) {
43         final Map<String, String> mapOfValues = SubscribeToStreamUtil.mapValuesFromUri(identifier);
44
45         final LogicalDatastoreType ds = SubscribeToStreamUtil.parseURIEnum(LogicalDatastoreType.class,
46                 mapOfValues.get(RestconfStreamsConstants.DATASTORE_PARAM_NAME));
47         if (ds == null) {
48             final String msg = "Stream name doesn't contains datastore value (pattern /datastore=)";
49             LOG.debug(msg);
50             throw new RestconfDocumentedException(msg, ErrorType.APPLICATION, ErrorTag.MISSING_ATTRIBUTE);
51         }
52
53         final DataChangeScope scope = SubscribeToStreamUtil.parseURIEnum(DataChangeScope.class,
54                 mapOfValues.get(RestconfStreamsConstants.SCOPE_PARAM_NAME));
55         if (scope == null) {
56             final String msg = "Stream name doesn't contains datastore value (pattern /scope=)";
57             LOG.warn(msg);
58             throw new RestconfDocumentedException(msg, ErrorType.APPLICATION, ErrorTag.MISSING_ATTRIBUTE);
59         }
60
61         final String streamName = Notificator.createStreamNameFromUri(identifier);
62         if (Strings.isNullOrEmpty(streamName)) {
63             final String msg = "Stream name is empty.";
64             LOG.warn(msg);
65             throw new RestconfDocumentedException(msg, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
66         }
67
68         final ListenerAdapter listener = Notificator.getListenerFor(streamName);
69         SubscribeToStreamUtil.registration(ds, scope, listener, this.domDataBrokerHandler.get());
70
71         final int port = SubscribeToStreamUtil.prepareNotificationPort();
72
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();
76
77         return Response.status(Status.OK).location(uri).build();
78     }
79 }