Split Restconf implementations (draft02 and RFC) - move restconf
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / netconf / md / sal / rest / common / RestconfValidationUtils.java
1 /*
2  * Copyright (c) 2015 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.netconf.md.sal.rest.common;
10
11 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
12 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
13 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
14
15 /**
16  * sal-rest-connector
17  * org.opendaylight.controller.md.sal.rest.common
18  *
19  * <p>
20  * Utility class is centralizing all needed validation functionality for a Restconf osgi module.
21  * All methods have to throw {@link RestconfDocumentedException} only, which is a representation
22  * for all error situation followed by restconf-netconf specification.
23  * See also <a href="https://tools.ietf.org/html/draft-bierman-netconf-restconf-02">RESTCONF</a>.
24  */
25 public class RestconfValidationUtils {
26
27     private RestconfValidationUtils() {
28         throw new UnsupportedOperationException("Utility class");
29     }
30
31     /**
32      * Method returns {@link RestconfDocumentedException} for a false condition.
33      *
34      * @param condition - condition for rise {@link RestconfDocumentedException}
35      * @param type      - input {@link ErrorType} for create {@link RestconfDocumentedException}
36      * @param tag       - input {@link ErrorTag} for create {@link RestconfDocumentedException}
37      * @param message   - input error message for create {@link RestconfDocumentedException}
38      */
39     public static void checkDocumentedError(final boolean condition, final ErrorType type,
40             final ErrorTag tag, final String message) {
41         if (!condition) {
42             throw new RestconfDocumentedException(message, type, tag);
43         }
44     }
45
46     /**
47      * Method returns {@link RestconfDocumentedException} if value is NULL or same input value.
48      * {@link ErrorType} is relevant for server application layer
49      * {@link ErrorTag} is 404 data-missing
50      * See also <a href="https://tools.ietf.org/html/draft-bierman-netconf-restconf-02">RESTCONF</a>.
51      *
52      * @param value         - some value from {@link org.opendaylight.yangtools.yang.model.api.Module}
53      * @param moduleName    - name of {@link org.opendaylight.yangtools.yang.model.api.Module}
54      * @return              - T value (same input value)
55      */
56     public static <T> T checkNotNullDocumented(final T value, final String moduleName) {
57         if (value == null) {
58             final String errMsg = "Module " + moduleName + " was not found.";
59             throw new RestconfDocumentedException(errMsg, ErrorType.APPLICATION, ErrorTag.DATA_MISSING);
60         }
61         return value;
62     }
63 }