2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.rests.utils;
10 import com.google.common.collect.Sets;
11 import java.util.List;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
15 import org.opendaylight.restconf.common.errors.RestconfError;
16 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
17 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
19 final class ParametersUtil {
21 private ParametersUtil() {
22 throw new UnsupportedOperationException("Util class.");
26 * Check if URI does not contain not allowed parameters for specified operation.
28 * @param operationType
29 * type of operation (READ, POST, PUT, DELETE...)
30 * @param usedParameters
31 * parameters used in URI request
32 * @param allowedParameters
33 * allowed parameters for operation
35 static void checkParametersTypes(final @NonNull String operationType,
36 final @NonNull Set<String> usedParameters,
37 final @NonNull String... allowedParameters) {
38 final Set<String> notAllowedParameters = Sets.newHashSet(usedParameters);
39 notAllowedParameters.removeAll(Sets.newHashSet(allowedParameters));
41 if (!notAllowedParameters.isEmpty()) {
42 throw new RestconfDocumentedException(
43 "Not allowed parameters for " + operationType + " operation: " + notAllowedParameters,
44 RestconfError.ErrorType.PROTOCOL,
45 RestconfError.ErrorTag.INVALID_VALUE);
50 * Check if URI does not contain value for the same parameter more than once.
52 * @param parameterValues
53 * URI parameter values
54 * @param parameterName
57 static void checkParameterCount(final @NonNull List<String> parameterValues, final @NonNull String parameterName) {
58 if (parameterValues.size() > 1) {
59 throw new RestconfDocumentedException(
60 "Parameter " + parameterName + " can appear at most once in request URI",
62 ErrorTag.INVALID_VALUE);