Fixing of useless message 'Unknown key : content' (read data) 59/83359/5
authorJaroslav Tóth <jtoth@frinx.io>
Thu, 1 Aug 2019 15:17:40 +0000 (17:17 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 15 Aug 2019 09:08:44 +0000 (11:08 +0200)
- After reading of data in RFC-8040 with set content query
  parameter, the INFO log with unknown query parameter was sent
  (but it was recognized correctly in another part of code).
- Refactoring of the readData(..) method, so the parsing of
  withDefaults query parameter is moved to ReadDataTransactionUtil
  that contains other parsing logic.
- Adding of withDefaults query paramameter to WriteParameters
  data class.
- Added unit tests.

Change-Id: I25a01c62a995facce24f29a0dc48048aa1e49f56
Signed-off-by: Jaroslav Tóth <jtoth@frinx.io>
restconf/restconf-common/src/main/java/org/opendaylight/restconf/common/context/WriterParameters.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/services/impl/RestconfDataServiceImpl.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtil.java
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/RestconfDataServiceConstant.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/ReadDataTransactionUtilTest.java

index 8941f9751ccd1a692c426c83a9ef8d30cb6f51cc..79bfa12ab2ce161810992b550b72780abe8a6a74 100644 (file)
@@ -18,6 +18,7 @@ public final class WriterParameters {
     private final List<Set<QName>> fields;
     private final boolean prettyPrint;
     private final boolean tagged;
+    private final String withDefault;
 
     private WriterParameters(final WriterParametersBuilder builder) {
         this.content = builder.content;
@@ -25,6 +26,7 @@ public final class WriterParameters {
         this.fields = builder.fields;
         this.prettyPrint = builder.prettyPrint;
         this.tagged = builder.tagged;
+        this.withDefault = builder.withDefault;
     }
 
     public String getContent() {
@@ -47,12 +49,17 @@ public final class WriterParameters {
         return this.tagged;
     }
 
+    public String getWithDefault() {
+        return withDefault;
+    }
+
     public static class WriterParametersBuilder {
         private String content;
         private Integer depth;
         private List<Set<QName>> fields;
         private boolean prettyPrint;
         private boolean tagged;
+        private String withDefault;
 
         public WriterParametersBuilder() {}
 
@@ -76,6 +83,11 @@ public final class WriterParameters {
             return this;
         }
 
+        public WriterParametersBuilder setWithDefault(final String withDefault) {
+            this.withDefault = withDefault;
+            return this;
+        }
+
         public WriterParameters build() {
             return new WriterParameters(this);
         }
index 55f3cd8ee1495a620974f53ac2aac6270c175574..b0dcebd66ab55ffcf51046329791b76f4b98364c 100644 (file)
@@ -103,38 +103,7 @@ public class RestconfDataServiceImpl implements RestconfDataService {
         final SchemaContextRef schemaContextRef = new SchemaContextRef(this.schemaContextHandler.get());
         final InstanceIdentifierContext<?> instanceIdentifier = ParserIdentifier.toInstanceIdentifier(
                 identifier, schemaContextRef.get(), Optional.of(this.mountPointServiceHandler.get()));
-
-        boolean withDefaUsed = false;
-        String withDefa = null;
-
-        for (final Entry<String, List<String>> entry : uriInfo.getQueryParameters().entrySet()) {
-            switch (entry.getKey()) {
-                case "with-defaults":
-                    if (!withDefaUsed) {
-                        withDefaUsed = true;
-                        withDefa = entry.getValue().iterator().next();
-                    } else {
-                        throw new RestconfDocumentedException("With-defaults parameter can be used only once.");
-                    }
-                    break;
-                default:
-                    LOG.info("Unknown key : {}.", entry.getKey());
-                    break;
-            }
-        }
-        boolean tagged = false;
-        if (withDefaUsed) {
-            if ("report-all-tagged".equals(withDefa)) {
-                tagged = true;
-                withDefa = null;
-            }
-            if ("report-all".equals(withDefa)) {
-                withDefa = null;
-            }
-        }
-
-        final WriterParameters parameters = ReadDataTransactionUtil.parseUriParameters(
-                instanceIdentifier, uriInfo, tagged);
+        final WriterParameters parameters = ReadDataTransactionUtil.parseUriParameters(instanceIdentifier, uriInfo);
 
         final DOMMountPoint mountPoint = instanceIdentifier.getMountPoint();
         final TransactionChainHandler localTransactionChainHandler;
@@ -146,9 +115,8 @@ public class RestconfDataServiceImpl implements RestconfDataService {
 
         final TransactionVarsWrapper transactionNode = new TransactionVarsWrapper(
                 instanceIdentifier, mountPoint, localTransactionChainHandler);
-        final NormalizedNode<?, ?> node =
-                ReadDataTransactionUtil.readData(identifier, parameters.getContent(), transactionNode, withDefa,
-                        schemaContextRef, uriInfo);
+        final NormalizedNode<?, ?> node = ReadDataTransactionUtil.readData(identifier, parameters.getContent(),
+                transactionNode, parameters.getWithDefault(), schemaContextRef, uriInfo);
         if (identifier != null && identifier.contains(STREAM_PATH) && identifier.contains(STREAM_ACCESS_PATH_PART)
                 && identifier.contains(STREAM_LOCATION_PATH_PART)) {
             final String value = (String) node.getValue();
index 014da282754f43338e1f5cfe3936659ce0fbdce3..509562bb284fa8df2b2cc26ac859880212f1d869 100644 (file)
@@ -92,39 +92,15 @@ public final class ReadDataTransactionUtil {
     /**
      * Parse parameters from URI request and check their types and values.
      *
-     *
      * @param identifier
      *             {@link InstanceIdentifierContext}
      * @param uriInfo
      *             URI info
-     * @param tagged
-     *             set tagged for {@link WriterParameters}
      * @return {@link WriterParameters}
      */
-    public static @NonNull WriterParameters parseUriParameters(final @NonNull InstanceIdentifierContext<?> identifier,
-            final @Nullable UriInfo uriInfo, final boolean tagged) {
-        return parseParams(identifier, uriInfo, tagged);
-    }
-
-    /**
-     * Parse parameters from URI request and check their types and values.
-     *
-     *
-     * @param identifier
-     *             {@link InstanceIdentifierContext}
-     * @param uriInfo
-     *             URI info
-     * @return {@link WriterParameters}
-     */
-    public static @NonNull WriterParameters parseUriParameters(final @NonNull InstanceIdentifierContext<?> identifier,
-                                                               final @Nullable UriInfo uriInfo) {
-        return parseParams(identifier, uriInfo, false);
-    }
-
-    private static WriterParameters parseParams(final InstanceIdentifierContext<?> identifier, final UriInfo uriInfo,
-            final boolean tagged) {
+    public static WriterParameters parseUriParameters(final InstanceIdentifierContext<?> identifier,
+                                                      final UriInfo uriInfo) {
         final WriterParametersBuilder builder = new WriterParametersBuilder();
-        builder.setTagged(tagged);
 
         if (uriInfo == null) {
             return builder.build();
@@ -145,6 +121,9 @@ public final class ReadDataTransactionUtil {
         final List<String> depth = uriInfo.getQueryParameters().getOrDefault(
                 RestconfDataServiceConstant.ReadData.DEPTH,
                 Collections.singletonList(RestconfDataServiceConstant.ReadData.UNBOUNDED));
+        final List<String> withDefaults = uriInfo.getQueryParameters().getOrDefault(
+                RestconfDataServiceConstant.ReadData.WITH_DEFAULTS,
+                Collections.emptyList());
         // fields
         final List<String> fields = uriInfo.getQueryParameters().getOrDefault(
                 RestconfDataServiceConstant.ReadData.FIELDS,
@@ -154,6 +133,7 @@ public final class ReadDataTransactionUtil {
         ParametersUtil.checkParameterCount(content, RestconfDataServiceConstant.ReadData.CONTENT);
         ParametersUtil.checkParameterCount(depth, RestconfDataServiceConstant.ReadData.DEPTH);
         ParametersUtil.checkParameterCount(fields, RestconfDataServiceConstant.ReadData.FIELDS);
+        ParametersUtil.checkParameterCount(fields, RestconfDataServiceConstant.ReadData.WITH_DEFAULTS);
 
         // check and set content
         final String contentValue = content.get(0);
@@ -190,6 +170,19 @@ public final class ReadDataTransactionUtil {
             builder.setFields(ParserFieldsParameter.parseFieldsParameter(identifier, fields.get(0)));
         }
 
+        // check and set withDefaults parameter
+        if (!withDefaults.isEmpty()) {
+            switch (withDefaults.get(0)) {
+                case RestconfDataServiceConstant.ReadData.REPORT_ALL_TAGGED_DEFAULT_VALUE:
+                    builder.setTagged(true);
+                    break;
+                case RestconfDataServiceConstant.ReadData.REPORT_ALL_DEFAULT_VALUE:
+                    break;
+                default:
+                    builder.setWithDefault(withDefaults.get(0));
+            }
+        }
+
         return builder.build();
     }
 
index a39ec65bc08ece3f8654cbb72db0be183579b08f..7cc4d31bcfba830a8def45233b60e46b86fcbef5 100644 (file)
@@ -46,6 +46,8 @@ public final class RestconfDataServiceConstant {
 
         public static final String READ_TYPE_TX = "READ";
         public static final String WITH_DEFAULTS = "with-defaults";
+        public static final String REPORT_ALL_DEFAULT_VALUE = "report-all";
+        public static final String REPORT_ALL_TAGGED_DEFAULT_VALUE = "report-all-tagged";
 
         private ReadData() {
             throw new UnsupportedOperationException("Util class.");
index 709fc73effc3c581dab760889dc163002742351b..bb4d19f62f4a137fc0901a632949e6ddbd9ad1ca 100644 (file)
@@ -8,8 +8,10 @@
 package org.opendaylight.restconf.nb.rfc8040.rests.utils;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.when;
@@ -36,6 +38,7 @@ import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWrapper;
+import org.opendaylight.restconf.nb.rfc8040.rests.utils.RestconfDataServiceConstant.ReadData;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
@@ -437,4 +440,59 @@ public class ReadDataTransactionUtilTest {
             assertEquals("Error status code is not correct", 400, e.getErrors().get(0).getErrorTag().getStatusCode());
         }
     }
+
+    /**
+     * Testing parsing of with-defaults parameter which value doesn't match report-all or report-all-tagged patterns
+     * - non-reporting setting.
+     */
+    @Test
+    public void parseUriParametersWithDefaultAndNonTaggedTest() {
+        // preparation of input data
+        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
+        final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
+        final String preparedDefaultValue = "sample-default";
+        parameters.put(RestconfDataServiceConstant.ReadData.WITH_DEFAULTS,
+                Collections.singletonList(preparedDefaultValue));
+        when(uriInfo.getQueryParameters()).thenReturn(parameters);
+
+        final WriterParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
+        assertEquals(preparedDefaultValue, writerParameters.getWithDefault());
+        assertFalse(writerParameters.isTagged());
+    }
+
+    /**
+     * Testing parsing of with-defaults parameter which value matches 'report-all-tagged' setting - default value should
+     * be set to {@code null} and tagged flag should be set to {@code true}.
+     */
+    @Test
+    public void parseUriParametersWithDefaultAndTaggedTest() {
+        // preparation of input data
+        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
+        final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
+        parameters.put(RestconfDataServiceConstant.ReadData.WITH_DEFAULTS,
+                Collections.singletonList(ReadData.REPORT_ALL_TAGGED_DEFAULT_VALUE));
+        when(uriInfo.getQueryParameters()).thenReturn(parameters);
+
+        final WriterParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
+        assertNull(writerParameters.getWithDefault());
+        assertTrue(writerParameters.isTagged());
+    }
+
+    /**
+     * Testing parsing of with-defaults parameter which value matches 'report-all' setting - default value should
+     * be set to {@code null} and tagged flag should be set to {@code false}.
+     */
+    @Test
+    public void parseUriParametersWithDefaultAndReportAllTest() {
+        // preparation of input data
+        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
+        final MultivaluedHashMap<String, String> parameters = new MultivaluedHashMap<>();
+        parameters.put(RestconfDataServiceConstant.ReadData.WITH_DEFAULTS,
+                Collections.singletonList(ReadData.REPORT_ALL_DEFAULT_VALUE));
+        when(uriInfo.getQueryParameters()).thenReturn(parameters);
+
+        final WriterParameters writerParameters = ReadDataTransactionUtil.parseUriParameters(context, uriInfo);
+        assertNull(writerParameters.getWithDefault());
+        assertFalse(writerParameters.isTagged());
+    }
 }