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