Fix findbugs violations in restconf-nb-rfc8040
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / utils / validations / RestconfValidation.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.utils.validations;
9
10 import java.time.format.DateTimeParseException;
11 import java.util.Date;
12 import java.util.Iterator;
13 import java.util.Locale;
14 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
15 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
16 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
17 import org.opendaylight.restconf.common.validation.RestconfValidationUtils;
18 import org.opendaylight.restconf.nb.rfc8040.utils.parser.builder.ParserBuilderConstants;
19 import org.opendaylight.yangtools.yang.common.Revision;
20
21 /**
22  * Util class for validations.
23  *
24  */
25 public final class RestconfValidation {
26
27     private RestconfValidation() {
28         throw new UnsupportedOperationException("Util class.");
29     }
30
31     /**
32      * Validation and parsing of revision.
33      *
34      * @param revisionDate
35      *             iterator
36      * @return {@link Date}
37      */
38     public static Revision validateAndGetRevision(final Iterator<String> revisionDate) {
39         RestconfValidationUtils.checkDocumentedError(revisionDate.hasNext(), ErrorType.PROTOCOL,
40                 ErrorTag.INVALID_VALUE, "Revision date must be supplied.");
41         try {
42             return Revision.of(revisionDate.next());
43         } catch (final DateTimeParseException e) {
44             throw new RestconfDocumentedException("Supplied revision is not in expected date format YYYY-mm-dd", e);
45         }
46     }
47
48     /**
49      * Validation of name.
50      *
51      * @param moduleName
52      *             iterator
53      * @return {@link String}
54      */
55     public static String validateAndGetModulName(final Iterator<String> moduleName) {
56         RestconfValidationUtils.checkDocumentedError(
57                 moduleName.hasNext(),
58                 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
59                 "Module name must be supplied."
60         );
61
62         final String name = moduleName.next();
63
64         RestconfValidationUtils.checkDocumentedError(
65                 !name.isEmpty() && ParserBuilderConstants.Deserializer.IDENTIFIER_FIRST_CHAR.matches(name.charAt(0)),
66                 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
67                 "Identifier must start with character from set 'a-zA-Z_"
68         );
69
70         RestconfValidationUtils.checkDocumentedError(
71                 !name.toUpperCase(Locale.ROOT).startsWith("XML"),
72                 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
73                 "Identifier must NOT start with XML ignore case."
74         );
75
76         RestconfValidationUtils.checkDocumentedError(
77                 ParserBuilderConstants.Deserializer.IDENTIFIER.matchesAllOf(name.substring(1)),
78                 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
79                 "Supplied name has not expected identifier format."
80         );
81
82         return name;
83     }
84
85 }