987ce42c84f559b9e924cdd90ff029244f6152c3
[netconf.git] / restconf / restconf-common / src / main / java / org / opendaylight / restconf / common / validation / 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 package org.opendaylight.restconf.common.validation;
9
10 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
11 import org.opendaylight.yangtools.yang.common.ErrorTag;
12 import org.opendaylight.yangtools.yang.common.ErrorType;
13
14 /**
15  * sal-rest-connector
16  * org.opendaylight.controller.md.sal.rest.common
17  *
18  * <p>
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 also <a href="https://tools.ietf.org/html/draft-bierman-netconf-restconf-02">RESTCONF</a>.
23  */
24 public final class RestconfValidationUtils {
25     private RestconfValidationUtils() {
26         // Hidden on purpose
27     }
28
29     /**
30      * Method returns {@link RestconfDocumentedException} if value is NULL or same input value.
31      * {@link ErrorType} is relevant for server application layer
32      * {@link ErrorTag} is 404 data-missing
33      * See also <a href="https://tools.ietf.org/html/draft-bierman-netconf-restconf-02">RESTCONF</a>.
34      *
35      * @param value         - some value from {@link org.opendaylight.yangtools.yang.model.api.Module}
36      * @param moduleName    - name of {@link org.opendaylight.yangtools.yang.model.api.Module}
37      * @return              - T value (same input value)
38      */
39     public static <T> T checkNotNullDocumented(final T value, final String moduleName) {
40         if (value == null) {
41             final String errMsg = "Module " + moduleName + " was not found.";
42             throw new RestconfDocumentedException(errMsg, ErrorType.APPLICATION, ErrorTag.DATA_MISSING);
43         }
44         return value;
45     }
46 }