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
9 package org.opendaylight.restconf.restful.utils;
11 import com.google.common.collect.Sets;
12 import java.util.List;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
16 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
17 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
18 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
20 class ParametersUtil {
22 private ParametersUtil() {
23 throw new UnsupportedOperationException("Util class.");
27 * Check if URI does not contain not allowed parameters for specified operation
29 * @param operationType
30 * - type of operation (READ, POST, PUT, DELETE...)
31 * @param usedParameters
32 * - parameters used in URI request
33 * @param allowedParameters
34 * - allowed parameters for operation
36 static void checkParametersTypes(@Nonnull final String operationType,
37 @Nonnull final Set<String> usedParameters,
38 @Nonnull final String... allowedParameters) {
39 final Set<String> notAllowedParameters = Sets.newHashSet(usedParameters);
40 notAllowedParameters.removeAll(Sets.newHashSet(allowedParameters));
42 if (!notAllowedParameters.isEmpty()) {
43 throw new RestconfDocumentedException(
44 "Not allowed parameters for " + operationType + " operation: " + notAllowedParameters,
45 RestconfError.ErrorType.PROTOCOL,
46 RestconfError.ErrorTag.INVALID_VALUE);
51 * Check if URI does not contain value for the same parameter more than once
53 * @param parameterValues
54 * - URI parameter values
55 * @param parameterName
56 * - URI parameter name
58 static void checkParameterCount(@Nonnull final List<String> parameterValues, @Nonnull final String parameterName) {
59 if (parameterValues.size() > 1) {
60 throw new RestconfDocumentedException(
61 "Parameter " + parameterName + " can appear at most once in request URI",
63 ErrorTag.INVALID_VALUE);