Improve content checking 37/96837/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 8 Jul 2021 12:44:04 +0000 (14:44 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 8 Jul 2021 12:44:04 +0000 (14:44 +0200)
We have neat nested ifs to check for the content value, which makes
it rather hard to understand what is going on. Use a simple switch
expression to validate the values instead.

Change-Id: I7799fafd45ec393541a5f7f2ede92d8e6a178375
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtil.java

index 1aa29a256253f2fba62d99fa2a0ce44b49d7e534..d6bad685dbfac071c85869a071bbbd3fddccf05f 100644 (file)
@@ -132,18 +132,20 @@ public final class ReadDataTransactionUtil {
 
         // check and set content
         final String contentValue = content.get(0);
-        if (!contentValue.equals(RestconfDataServiceConstant.ReadData.ALL)) {
-            if (!contentValue.equals(RestconfDataServiceConstant.ReadData.CONFIG)
-                    && !contentValue.equals(RestconfDataServiceConstant.ReadData.NONCONFIG)) {
+        switch (contentValue) {
+            case RestconfDataServiceConstant.ReadData.ALL:
+            case RestconfDataServiceConstant.ReadData.CONFIG:
+            case RestconfDataServiceConstant.ReadData.NONCONFIG:
+                // FIXME: we really want to have a proper enumeration for this field
+                builder.setContent(contentValue);
+                break;
+            default:
                 throw new RestconfDocumentedException(
-                        new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
-                                "Invalid content parameter: " + contentValue, null,
-                                "The content parameter value must be either config, nonconfig or all (default)"));
-            }
+                    new RestconfError(RestconfError.ErrorType.PROTOCOL, RestconfError.ErrorTag.INVALID_VALUE,
+                        "Invalid content parameter: " + contentValue, null,
+                        "The content parameter value must be either config, nonconfig or all (default)"));
         }
 
-        builder.setContent(content.get(0));
-
         // check and set depth
         if (!depth.get(0).equals(RestconfDataServiceConstant.ReadData.UNBOUNDED)) {
             final Integer value = Ints.tryParse(depth.get(0));