From b31e56f1104a15bee56d1dce9d2829707cd12b5e Mon Sep 17 00:00:00 2001 From: Josh Date: Sun, 28 Oct 2018 14:34:50 +0200 Subject: [PATCH] Add subscribeToStream to JSONRestConfService The idea here is to allow applications to pre-register websocket listeners. Change-Id: Ic7e55f19251e0b139256435ad215b44a596b84d9 Signed-off-by: Josh --- .../impl/NormalizedNodeJsonBodyWriter.java | 6 +++-- .../sal/restconf/api/JSONRestconfService.java | 14 +++++++++++ .../impl/JSONRestconfServiceImpl.java | 25 +++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/NormalizedNodeJsonBodyWriter.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/NormalizedNodeJsonBodyWriter.java index e4f00aa145..80493da094 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/NormalizedNodeJsonBodyWriter.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/NormalizedNodeJsonBodyWriter.java @@ -73,8 +73,10 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap httpHeaders, final OutputStream entityStream) throws IOException, WebApplicationException { - for (final Entry entry : context.getNewHeaders().entrySet()) { - httpHeaders.add(entry.getKey(), entry.getValue()); + if (httpHeaders != null) { + for (final Entry entry : context.getNewHeaders().entrySet()) { + httpHeaders.add(entry.getKey(), entry.getValue()); + } } final NormalizedNode data = context.getData(); if (data == null) { diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/api/JSONRestconfService.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/api/JSONRestconfService.java index 5e080da064..17651af566 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/api/JSONRestconfService.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/api/JSONRestconfService.java @@ -9,9 +9,12 @@ package org.opendaylight.netconf.sal.restconf.api; import com.google.common.base.Optional; import javax.annotation.Nonnull; +import javax.ws.rs.core.MultivaluedMap; + import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.yangtools.yang.common.OperationFailedException; + /** * Provides restconf CRUD operations via code with input/output data in JSON format. * @@ -84,4 +87,15 @@ public interface JSONRestconfService { * @throws OperationFailedException if the request fails. */ Optional patch(@Nonnull String uriPath, @Nonnull String payload) throws OperationFailedException; + + /** + * Subscribe to a stream. + * @param identifier the identifier of the stream, e.g., "data-change-event-subscription/neutron:neutron/... + * ...neutron:ports/datastore=OPERATIONAL/scope=SUBTREE". + * @param params HTTP query parameters or null. + * @return On optional containing the JSON response. + * @throws OperationFailedException if the requests fails. + */ + Optional subscribeToStream(@Nonnull String identifier, MultivaluedMap params) + throws OperationFailedException; } diff --git a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/JSONRestconfServiceImpl.java b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/JSONRestconfServiceImpl.java index 50b23594da..64c482bf0e 100644 --- a/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/JSONRestconfServiceImpl.java +++ b/restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/JSONRestconfServiceImpl.java @@ -17,7 +17,11 @@ import java.io.InputStream; import java.lang.annotation.Annotation; import java.nio.charset.StandardCharsets; import java.util.List; +import javax.annotation.Nonnull; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.UriInfo; + import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; import org.opendaylight.netconf.sal.rest.api.RestconfService; import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader; @@ -31,6 +35,7 @@ import org.opendaylight.restconf.common.errors.RestconfError; import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag; import org.opendaylight.restconf.common.patch.PatchContext; import org.opendaylight.restconf.common.patch.PatchStatusContext; +import org.opendaylight.restconf.common.util.MultivaluedHashMap; import org.opendaylight.restconf.common.util.SimpleUriInfo; import org.opendaylight.yangtools.yang.common.OperationFailedException; import org.opendaylight.yangtools.yang.common.RpcError; @@ -210,6 +215,26 @@ public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseab return Optional.fromNullable(output); } + @SuppressWarnings("checkstyle:IllegalCatch") + @Override + public Optional subscribeToStream(@Nonnull String identifier, + MultivaluedMap params) throws OperationFailedException { + //Note: We use http://127.0.0.1 because the Uri parser requires something there though it does nothing + String uri = new StringBuilder("http://127.0.0.1:8081/restconf/streams/stream/").append(identifier).toString(); + MultivaluedMap queryParams = (params != null) ? params : new MultivaluedHashMap(); + UriInfo uriInfo = new SimpleUriInfo(uri, queryParams); + + String jsonRes = null; + try { + NormalizedNodeContext res = restconfService.subscribeToStream(identifier, uriInfo); + jsonRes = toJson(res); + } catch (final Exception e) { + propagateExceptionAs(identifier, e, "RPC"); + } + + return Optional.fromNullable(jsonRes); + } + @Override public void close() { } -- 2.36.6