Bug 5528 - Connecting RESTful part to restconf + fix bugs
[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 final DOMDataBrokerHandler domDataBrokerHandler;
40
41     public RestconfStreamsSubscriptionServiceImpl(final DOMDataBrokerHandler domDataBrokerHandler) {
42         this.domDataBrokerHandler = domDataBrokerHandler;
43     }
44
45     @Override
46     public Response subscribeToStream(final String identifier, final UriInfo uriInfo) {
47         final Map<String, String> mapOfValues = SubscribeToStreamUtil.mapValuesFromUri(identifier);
48
49         final LogicalDatastoreType ds = SubscribeToStreamUtil.parseURIEnum(LogicalDatastoreType.class,
50                 mapOfValues.get(RestconfStreamsConstants.DATASTORE_PARAM_NAME));
51         if (ds == null) {
52             final String msg = "Stream name doesn't contains datastore value (pattern /datastore=)";
53             LOG.debug(msg);
54             throw new RestconfDocumentedException(msg, ErrorType.APPLICATION, ErrorTag.MISSING_ATTRIBUTE);
55         }
56
57         final DataChangeScope scope = SubscribeToStreamUtil.parseURIEnum(DataChangeScope.class,
58                 mapOfValues.get(RestconfStreamsConstants.SCOPE_PARAM_NAME));
59         if (scope == null) {
60             final String msg = "Stream name doesn't contains datastore value (pattern /scope=)";
61             LOG.warn(msg);
62             throw new RestconfDocumentedException(msg, ErrorType.APPLICATION, ErrorTag.MISSING_ATTRIBUTE);
63         }
64
65         final String streamName = Notificator.createStreamNameFromUri(identifier);
66         if (Strings.isNullOrEmpty(streamName)) {
67             final String msg = "Stream name is empty.";
68             LOG.warn(msg);
69             throw new RestconfDocumentedException(msg, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
70         }
71
72         final ListenerAdapter listener = Notificator.getListenerFor(streamName);
73         SubscribeToStreamUtil.registration(ds, scope, listener, this.domDataBrokerHandler.get());
74
75         final int port = SubscribeToStreamUtil.prepareNotificationPort();
76
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();
80
81         return Response.status(Status.OK).location(uri).build();
82     }
83 }