d262b982ef449faea3b0a5ec11376652fa0e86b7
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / restconf / restful / 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
9 package org.opendaylight.restconf.restful.utils;
10
11 import com.google.common.collect.Sets;
12 import java.util.List;
13 import java.util.Set;
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;
19
20 class ParametersUtil {
21
22     private ParametersUtil() {
23         throw new UnsupportedOperationException("Util class.");
24     }
25
26     /**
27      * Check if URI does not contain not allowed parameters for specified operation.
28      *
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
35      */
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));
41
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);
47         }
48     }
49
50     /**
51      * Check if URI does not contain value for the same parameter more than once.
52      *
53      * @param parameterValues
54      *             URI parameter values
55      * @param parameterName
56      *             URI parameter name
57      */
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",
62                     ErrorType.PROTOCOL,
63                     ErrorTag.INVALID_VALUE);
64         }
65     }
66 }