Move streams support classes
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / NotificationQueryParams.java
index 45e2c82cad2d9d3b1e43f7d81a4547ddf1c864a9..680057d2b53656f0e7d606a3e236df9abc9db6dc 100644 (file)
@@ -8,96 +8,39 @@
  */
 package org.opendaylight.restconf.nb.rfc8040;
 
+import static com.google.common.base.Preconditions.checkArgument;
+
 import com.google.common.base.MoreObjects;
-import java.util.List;
-import java.util.Map.Entry;
-import javax.ws.rs.core.UriInfo;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
-import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.yangtools.concepts.Immutable;
 
 /**
- * Parser and holder of query paramteres from uriInfo for notifications.
+ * Parser and holder of query parameters from uriInfo for notifications.
  */
 public final class NotificationQueryParams implements Immutable {
-    private final StartTimeParameter startTime;
-    private final StopTimeParameter stopTime;
-    private final FilterParameter filter;
-    private final boolean skipNotificationData;
+    private final SkipNotificationDataParam skipNotificationData;
+    private final LeafNodesOnlyParam leafNodesOnly;
+    private final StartTimeParam startTime;
+    private final StopTimeParam stopTime;
+    private final FilterParam filter;
 
-    private NotificationQueryParams(final StartTimeParameter startTime, final StopTimeParameter stopTime,
-            final FilterParameter filter, final boolean skipNotificationData) {
+    private NotificationQueryParams(final StartTimeParam startTime, final StopTimeParam stopTime,
+            final FilterParam filter, final LeafNodesOnlyParam leafNodesOnly,
+            final SkipNotificationDataParam skipNotificationData) {
         this.startTime = startTime;
         this.stopTime = stopTime;
         this.filter = filter;
+        this.leafNodesOnly = leafNodesOnly;
         this.skipNotificationData = skipNotificationData;
     }
 
-    // FIXME: this is JAX-RS specific
-    public static @NonNull NotificationQueryParams fromUriInfo(final UriInfo uriInfo) {
-        StartTimeParameter startTime = null;
-        StopTimeParameter stopTime = null;
-        FilterParameter filter = null;
-        boolean skipNotificationData = false;
-
-        for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
-            final String paramName = entry.getKey();
-            final List<String> paramValues = entry.getValue();
-            if (paramName.equals(StartTimeParameter.uriName())) {
-                switch (paramValues.size()) {
-                    case 0:
-                        break;
-                    case 1:
-                        final String str = paramValues.get(0);
-                        try {
-                            startTime = StartTimeParameter.forUriValue(str);
-                        } catch (IllegalArgumentException e) {
-                            throw new RestconfDocumentedException("Invalid start-time date: " + str, e);
-                        }
-                        break;
-                    default:
-                        throw new RestconfDocumentedException("Start-time parameter can be used only once.");
-                }
-            } else if (paramName.equals(StopTimeParameter.uriName())) {
-                switch (paramValues.size()) {
-                    case 0:
-                        break;
-                    case 1:
-                        final String str = paramValues.get(0);
-                        try {
-                            stopTime = StopTimeParameter.forUriValue(str);
-                        } catch (IllegalArgumentException e) {
-                            throw new RestconfDocumentedException("Invalid stop-time date: " + str, e);
-                        }
-                        break;
-                    default:
-                        throw new RestconfDocumentedException("Stop-time parameter can be used only once.");
-                }
-            } else if (paramName.equals(FilterParameter.uriName())) {
-                if (!paramValues.isEmpty()) {
-                    filter = FilterParameter.forUriValue(paramValues.get(0));
-                }
-            } else if (paramName.equals("odl-skip-notification-data")) {
-                switch (paramValues.size()) {
-                    case 0:
-                        break;
-                    case 1:
-                        skipNotificationData = Boolean.parseBoolean(paramValues.get(0));
-                        break;
-                    default:
-                        throw new RestconfDocumentedException(
-                            "Odl-skip-notification-data parameter can be used only once.");
-                }
-            } else {
-                throw new RestconfDocumentedException("Bad parameter used with notifications: " + paramName);
-            }
-        }
-        if (startTime == null && stopTime != null) {
-            throw new RestconfDocumentedException("Stop-time parameter has to be used with start-time parameter.");
-        }
-
-        return new NotificationQueryParams(startTime, stopTime, filter, skipNotificationData);
+    public static @NonNull NotificationQueryParams of(final StartTimeParam startTime, final StopTimeParam stopTime,
+            final FilterParam filter, final LeafNodesOnlyParam leafNodesOnly,
+            final SkipNotificationDataParam skipNotificationData) {
+        checkArgument(stopTime == null || startTime != null,
+            "Stop-time parameter has to be used with start-time parameter.");
+        return new NotificationQueryParams(startTime, stopTime, filter, leafNodesOnly, skipNotificationData);
     }
 
     /**
@@ -105,7 +48,7 @@ public final class NotificationQueryParams implements Immutable {
      *
      * @return start-time
      */
-    public @Nullable StartTimeParameter startTime() {
+    public @Nullable StartTimeParam startTime() {
         return startTime;
     }
 
@@ -114,7 +57,7 @@ public final class NotificationQueryParams implements Immutable {
      *
      * @return stop-time
      */
-    public @Nullable StopTimeParameter stopTime() {
+    public @Nullable StopTimeParam stopTime() {
         return stopTime;
     }
 
@@ -123,16 +66,25 @@ public final class NotificationQueryParams implements Immutable {
      *
      * @return filter
      */
-    public @Nullable FilterParameter filter() {
+    public @Nullable FilterParam filter() {
         return filter;
     }
 
     /**
-     * Check whether this query should notify changes without data.
+     * Get odl-leaf-nodes-only query parameter.
+     *
+     * @return odl-leaf-nodes-only
+     */
+    public @Nullable LeafNodesOnlyParam leafNodesOnly() {
+        return leafNodesOnly;
+    }
+
+    /**
+     * Get odl-skip-notification-data query parameter.
      *
-     * @return true if this query should notify about changes with  data
+     * @return odl-skip-notification-data
      */
-    public boolean isSkipNotificationData() {
+    public @Nullable SkipNotificationDataParam skipNotificationData() {
         return skipNotificationData;
     }
 
@@ -140,14 +92,20 @@ public final class NotificationQueryParams implements Immutable {
     public String toString() {
         final var helper = MoreObjects.toStringHelper(this);
         if (startTime != null) {
-            helper.add("startTime", startTime.uriValue());
+            helper.add("startTime", startTime.paramValue());
         }
         if (stopTime != null) {
-            helper.add("stopTime", stopTime.uriValue());
+            helper.add("stopTime", stopTime.paramValue());
         }
         if (filter != null) {
-            helper.add("filter", filter.uriValue());
+            helper.add("filter", filter.paramValue());
+        }
+        if (leafNodesOnly != null) {
+            helper.add("leafNodesOnly", leafNodesOnly.value());
+        }
+        if (skipNotificationData != null) {
+            helper.add("skipNotificationData", skipNotificationData.value());
         }
-        return helper.add("skipNotificationData", skipNotificationData).toString();
+        return helper.toString();
     }
 }