Refactor Revision statement implementations
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / revision / RevisionStatementSupport.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;
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.RevisionEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionStatement;
18 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
21 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
22
23 public final class RevisionStatementSupport
24         extends BaseStatementSupport<Revision, RevisionStatement, RevisionEffectiveStatement> {
25     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
26         YangStmtMapping.REVISION)
27             .addOptional(YangStmtMapping.DESCRIPTION)
28             .addOptional(YangStmtMapping.REFERENCE)
29             .build();
30     private static final RevisionStatementSupport INSTANCE = new RevisionStatementSupport();
31
32     private RevisionStatementSupport() {
33         super(YangStmtMapping.REVISION);
34     }
35
36     public static RevisionStatementSupport getInstance() {
37         return INSTANCE;
38     }
39
40     @Override
41     public Revision parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
42         try {
43             return Revision.of(value);
44         } catch (DateTimeParseException e) {
45             throw new SourceException(ctx.getStatementSourceReference(), e,
46                 "Revision value %s is not in required format yyyy-MM-dd", value);
47         }
48     }
49
50     @Override
51     protected SubstatementValidator getSubstatementValidator() {
52         return SUBSTATEMENT_VALIDATOR;
53     }
54
55     @Override
56     protected RevisionStatement createDeclared(final StmtContext<Revision, RevisionStatement, ?> ctx,
57             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
58         return new RegularRevisionStatement(ctx.coerceStatementArgument(), substatements);
59     }
60
61     @Override
62     protected RevisionStatement createEmptyDeclared(final StmtContext<Revision, RevisionStatement, ?> ctx) {
63         return new EmptyRevisionStatement(ctx.coerceStatementArgument());
64     }
65
66     @Override
67     protected RevisionEffectiveStatement createEffective(
68             final StmtContext<Revision, RevisionStatement, RevisionEffectiveStatement> ctx,
69             final RevisionStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
70         return new RegularRevisionEffectiveStatement(declared, substatements);
71     }
72
73     @Override
74     protected RevisionEffectiveStatement createEmptyEffective(
75             final StmtContext<Revision, RevisionStatement, RevisionEffectiveStatement> ctx,
76             final RevisionStatement declared) {
77         return new EmptyRevisionEffectiveStatement(declared);
78     }
79 }