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.utils.validation;
10 import java.text.ParseException;
11 import java.util.Date;
12 import java.util.Iterator;
13 import org.opendaylight.netconf.md.sal.rest.common.RestconfValidationUtils;
14 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
15 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
16 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
17 import org.opendaylight.restconf.utils.parser.builder.ParserBuilderConstants;
18 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
21 * Util class for validations
24 public final class RestconfValidation {
26 private RestconfValidation() {
27 throw new UnsupportedOperationException("Util class.");
31 * Validation and parsing of revision.
35 * @return {@link Date}
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.");
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);
52 * @return {@link String}
54 public static String validateAndGetModulName(final Iterator<String> moduleName) {
55 RestconfValidationUtils.checkDocumentedError(
57 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
58 "Module name must be supplied."
61 final String name = moduleName.next();
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_"
69 RestconfValidationUtils.checkDocumentedError(
70 !name.toUpperCase().startsWith("XML"),
71 ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE,
72 "Identifier must NOT start with XML ignore case."
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."