df56c23ecb3c8eedae9f98a1c783dc16beea8fc7
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / utils / ParametersUtil.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.rests.utils;
9
10 import com.google.common.collect.Sets;
11 import java.util.List;
12 import java.util.Set;
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;
18
19 final class ParametersUtil {
20
21     private ParametersUtil() {
22         throw new UnsupportedOperationException("Util class.");
23     }
24
25     /**
26      * Check if URI does not contain not allowed parameters for specified operation.
27      *
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
34      */
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));
40
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);
46         }
47     }
48
49     /**
50      * Check if URI does not contain value for the same parameter more than once.
51      *
52      * @param parameterValues
53      *             URI parameter values
54      * @param parameterName
55      *             URI parameter name
56      */
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",
61                     ErrorType.PROTOCOL,
62                     ErrorTag.INVALID_VALUE);
63         }
64     }
65 }