Merge "Add subscribeToStream to JSONRestConfService"
authorTom Pantelis <tompantelis@gmail.com>
Tue, 30 Oct 2018 12:47:36 +0000 (12:47 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Tue, 30 Oct 2018 12:47:36 +0000 (12:47 +0000)
restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/rest/impl/NormalizedNodeJsonBodyWriter.java
restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/api/JSONRestconfService.java
restconf/restconf-nb-bierman02/src/main/java/org/opendaylight/netconf/sal/restconf/impl/JSONRestconfServiceImpl.java

index e4f00aa145aa4797bb6a5879f49c0733275f80d1..80493da094272957beb58082dba7220b90ee44a5 100644 (file)
@@ -73,8 +73,10 @@ public class NormalizedNodeJsonBodyWriter implements MessageBodyWriter<Normalize
     public void writeTo(final NormalizedNodeContext context, final Class<?> type, final Type genericType,
             final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders,
             final OutputStream entityStream) throws IOException, WebApplicationException {
-        for (final Entry<String, Object> entry : context.getNewHeaders().entrySet()) {
-            httpHeaders.add(entry.getKey(), entry.getValue());
+        if (httpHeaders != null) {
+            for (final Entry<String, Object> entry : context.getNewHeaders().entrySet()) {
+                httpHeaders.add(entry.getKey(), entry.getValue());
+            }
         }
         final NormalizedNode<?, ?> data = context.getData();
         if (data == null) {
index 5e080da064ff6ebfb08a7974badd63f49e596698..17651af566da338dbca6fa5846dac8330bb78430 100644 (file)
@@ -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<String> 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<String> subscribeToStream(@Nonnull String identifier, MultivaluedMap<String, String> params)
+                                                                                    throws OperationFailedException;
 }
index 50b23594daf75b983e768956f872967a448011f4..64c482bf0e980bee8fe9035f94d825c3cd69c060 100644 (file)
@@ -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<String> subscribeToStream(@Nonnull String identifier,
+                                      MultivaluedMap<String, String> 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<String, String>();
+        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() {
     }