Add subscribeToStream to JSONRestConfService
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / netconf / sal / restconf / impl / JSONRestconfServiceImpl.java
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() {
     }