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