2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.utils.validations;
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;
22 * Util class for validations.
25 public final class RestconfValidation {
27 private RestconfValidation() {
28 throw new UnsupportedOperationException("Util class.");
32 * Validation and parsing of revision.
36 * @return {@link Date}
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.");
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);
53 * @return {@link String}
55 public static String validateAndGetModulName(final Iterator<String> moduleName) {
56 RestconfValidationUtils.checkDocumentedError(
58 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
59 "Module name must be supplied."
62 final String name = moduleName.next();
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_"
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."
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."