07924d7cb703785b1f0082d7a07fdcae741276f3
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / revision_date / RevisionDateStatementSupport.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.yangtools.yang.parser.rfc7950.stmt.revision_date;
9
10 import java.time.format.DateTimeParseException;
11 import org.opendaylight.yangtools.yang.common.Revision;
12 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
13 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateEffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateStatement;
15 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
18 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
19
20 public final class RevisionDateStatementSupport
21         extends AbstractStatementSupport<Revision, RevisionDateStatement, RevisionDateEffectiveStatement> {
22     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
23             SubstatementValidator.builder(YangStmtMapping.REVISION_DATE).build();
24     private static final RevisionDateStatementSupport INSTANCE = new RevisionDateStatementSupport();
25
26     private RevisionDateStatementSupport() {
27         super(YangStmtMapping.REVISION_DATE, CopyPolicy.CONTEXT_INDEPENDENT);
28     }
29
30     public static RevisionDateStatementSupport getInstance() {
31         return INSTANCE;
32     }
33
34     @Override
35     public Revision parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
36         try {
37             return Revision.of(value);
38         } catch (DateTimeParseException e) {
39             throw new SourceException(ctx.getStatementSourceReference(), e,
40                 "Revision value %s is not in required format yyyy-MM-dd", value);
41         }
42     }
43
44     @Override
45     public RevisionDateStatement createDeclared(final StmtContext<Revision, RevisionDateStatement, ?> ctx) {
46         return new RevisionDateStatementImpl(ctx);
47     }
48
49     @Override
50     public RevisionDateEffectiveStatement createEffective(
51             final StmtContext<Revision, RevisionDateStatement, RevisionDateEffectiveStatement> ctx) {
52         return new RevisionDateEffectiveStatementImpl(ctx);
53     }
54
55     @Override
56     protected SubstatementValidator getSubstatementValidator() {
57         return SUBSTATEMENT_VALIDATOR;
58     }
59 }