Refactor pretty printing
[netconf.git] / restconf / restconf-nb / src / main / java / org / opendaylight / restconf / nb / rfc8040 / databind / jaxrs / QueryParams.java
1 /*
2  * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.restconf.nb.rfc8040.databind.jaxrs;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.annotations.VisibleForTesting;
14 import com.google.common.collect.ImmutableMap;
15 import java.util.List;
16 import java.util.Set;
17 import java.util.function.Function;
18 import javax.ws.rs.core.UriInfo;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.restconf.api.query.ChangedLeafNodesOnlyParam;
22 import org.opendaylight.restconf.api.query.ChildNodesOnlyParam;
23 import org.opendaylight.restconf.api.query.ContentParam;
24 import org.opendaylight.restconf.api.query.DepthParam;
25 import org.opendaylight.restconf.api.query.FieldsParam;
26 import org.opendaylight.restconf.api.query.FilterParam;
27 import org.opendaylight.restconf.api.query.InsertParam;
28 import org.opendaylight.restconf.api.query.LeafNodesOnlyParam;
29 import org.opendaylight.restconf.api.query.PointParam;
30 import org.opendaylight.restconf.api.query.PrettyPrintParam;
31 import org.opendaylight.restconf.api.query.SkipNotificationDataParam;
32 import org.opendaylight.restconf.api.query.StartTimeParam;
33 import org.opendaylight.restconf.api.query.StopTimeParam;
34 import org.opendaylight.restconf.api.query.WithDefaultsParam;
35 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
36 import org.opendaylight.restconf.common.errors.RestconfError;
37 import org.opendaylight.restconf.server.api.DataGetParams;
38 import org.opendaylight.yangtools.yang.common.ErrorTag;
39 import org.opendaylight.yangtools.yang.common.ErrorType;
40
41 @Beta
42 public final class QueryParams {
43     private static final Set<String> KNOWN_PARAMS = Set.of(
44         // Read data
45         ContentParam.uriName, DepthParam.uriName, FieldsParam.uriName, WithDefaultsParam.uriName,
46         // Modify data
47         InsertParam.uriName, PointParam.uriName,
48         // Notifications
49         FilterParam.uriName, StartTimeParam.uriName, StopTimeParam.uriName,
50         // ODL extensions
51         ChangedLeafNodesOnlyParam.uriName, ChildNodesOnlyParam.uriName, PrettyPrintParam.uriName,
52         LeafNodesOnlyParam.uriName, SkipNotificationDataParam.uriName);
53
54     private QueryParams() {
55         // Utility class
56     }
57
58     /**
59      * Normalize query parameters from an {@link UriInfo}.
60      *
61      * @param uriInfo An {@link UriInfo}
62      * @return Normalized query parameters
63      * @throws NullPointerException if {@code uriInfo} is {@code null}
64      * @throws IllegalArgumentException if there are multiple values for a parameter
65      */
66     public static @NonNull ImmutableMap<String, String> normalize(final UriInfo uriInfo) {
67         final var builder = ImmutableMap.<String, String>builder();
68         for (var entry : uriInfo.getQueryParameters().entrySet()) {
69             final var values = entry.getValue();
70             switch (values.size()) {
71                 case 0:
72                     // No-op
73                     break;
74                 case 1:
75                     builder.put(entry.getKey(), values.get(0));
76                     break;
77                 default:
78                     throw new IllegalArgumentException(
79                         "Parameter " + entry.getKey() + " can appear at most once in request URI");
80             }
81         }
82         return builder.build();
83     }
84
85     /**
86      * Parse parameters from URI request and check their types and values.
87      *
88      * @param uriInfo URI info
89      * @return {@link DataGetParams}
90      */
91     public static @NonNull DataGetParams newDataGetParams(final UriInfo uriInfo) {
92         ContentParam content = ContentParam.ALL;
93         DepthParam depth = null;
94         FieldsParam fields = null;
95         WithDefaultsParam withDefaults = null;
96         PrettyPrintParam prettyPrint = PrettyPrintParam.FALSE;
97
98         for (var entry : uriInfo.getQueryParameters().entrySet()) {
99             final var paramName = entry.getKey();
100             final var paramValues = entry.getValue();
101
102             try {
103                 switch (paramName) {
104                     case ContentParam.uriName:
105                         content = optionalParam(ContentParam::forUriValue, paramName, paramValues);
106                         break;
107                     case DepthParam.uriName:
108                         final String depthStr = optionalParam(paramName, paramValues);
109                         try {
110                             depth = DepthParam.forUriValue(depthStr);
111                         } catch (IllegalArgumentException e) {
112                             throw new RestconfDocumentedException(e, new RestconfError(ErrorType.PROTOCOL,
113                                 ErrorTag.INVALID_VALUE, "Invalid depth parameter: " + depthStr, null,
114                                 "The depth parameter must be an integer between 1 and 65535 or \"unbounded\""));
115                         }
116                         break;
117                     case FieldsParam.uriName:
118                         fields = optionalParam(FieldsParam::forUriValue, paramName, paramValues);
119                         break;
120                     case WithDefaultsParam.uriName:
121                         withDefaults = optionalParam(WithDefaultsParam::forUriValue, paramName, paramValues);
122                         break;
123                     case PrettyPrintParam.uriName:
124                         prettyPrint = optionalParam(PrettyPrintParam::forUriValue, paramName, paramValues);
125                         break;
126                     default:
127                         throw unhandledParam("read", paramName);
128                 }
129             } catch (IllegalArgumentException e) {
130                 throw new RestconfDocumentedException("Invalid " + paramName + " value: " + e.getMessage(),
131                     ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE, e);
132             }
133         }
134
135         return new DataGetParams(content, depth, fields, withDefaults, prettyPrint);
136     }
137
138     private static RestconfDocumentedException unhandledParam(final String operation, final String name) {
139         return KNOWN_PARAMS.contains(name)
140             ? new RestconfDocumentedException("Invalid parameter in " + operation + ": " + name,
141                 ErrorType.PROTOCOL, ErrorTag.MALFORMED_MESSAGE)
142             : new RestconfDocumentedException("Unknown parameter in " + operation + ": " + name,
143                 ErrorType.PROTOCOL, ErrorTag.UNKNOWN_ATTRIBUTE);
144     }
145
146     @VisibleForTesting
147     static @Nullable String optionalParam(final String name, final List<String> values) {
148         return switch (values.size()) {
149             case 0 -> null;
150             case 1 -> requireNonNull(values.get(0));
151             default -> throw new RestconfDocumentedException(
152                 "Parameter " + name + " can appear at most once in request URI",
153                 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
154         };
155     }
156
157     private static <T> @Nullable T optionalParam(final Function<String, @NonNull T> factory, final String name,
158             final List<String> values) {
159         final String str = optionalParam(name, values);
160         return str == null ? null : factory.apply(str);
161     }
162 }