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