18216d438f0efc9d0c5000df49a8cee42a27aa26
[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 com.google.common.collect.ImmutableList;
11 import java.time.format.DateTimeParseException;
12 import org.opendaylight.yangtools.yang.common.Revision;
13 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
14 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateStatement;
18 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
22 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
23
24 public final class RevisionDateStatementSupport
25         extends BaseStatementSupport<Revision, RevisionDateStatement, RevisionDateEffectiveStatement> {
26     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
27             SubstatementValidator.builder(YangStmtMapping.REVISION_DATE).build();
28     private static final RevisionDateStatementSupport INSTANCE = new RevisionDateStatementSupport();
29
30     private RevisionDateStatementSupport() {
31         super(YangStmtMapping.REVISION_DATE, CopyPolicy.CONTEXT_INDEPENDENT);
32     }
33
34     public static RevisionDateStatementSupport getInstance() {
35         return INSTANCE;
36     }
37
38     @Override
39     public Revision parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
40         try {
41             return Revision.of(value);
42         } catch (DateTimeParseException e) {
43             throw new SourceException(ctx.getStatementSourceReference(), e,
44                 "Revision value %s is not in required format yyyy-MM-dd", value);
45         }
46     }
47
48     @Override
49     protected SubstatementValidator getSubstatementValidator() {
50         return SUBSTATEMENT_VALIDATOR;
51     }
52
53     @Override
54     protected RevisionDateStatement createDeclared(final StmtContext<Revision, RevisionDateStatement, ?> ctx,
55             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
56         return new RegularRevisionDateStatement(ctx.coerceStatementArgument(), substatements);
57     }
58
59     @Override
60     protected RevisionDateStatement createEmptyDeclared(final StmtContext<Revision, RevisionDateStatement, ?> ctx) {
61         return new EmptyRevisionDateStatement(ctx.coerceStatementArgument());
62     }
63
64     @Override
65     protected RevisionDateEffectiveStatement createEffective(final Current<Revision, RevisionDateStatement> stmt,
66             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
67         return substatements.isEmpty() ? new EmptyRevisionDateEffectiveStatement(stmt.declared())
68             : new RegularRevisionDateEffectiveStatement(stmt.declared(), substatements);
69     }
70 }