16ef33e27b3c4e35f0a65aeffee0e851b63ecccd
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / RevisionStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.stmt.rfc6020;
9
10 import java.text.ParseException;
11 import java.util.Date;
12 import javax.annotation.Nullable;
13 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
14 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionStatement;
19 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.RevisionEffectiveStatementImpl;
25
26 public class RevisionStatementImpl extends AbstractDeclaredStatement<Date>
27         implements RevisionStatement {
28     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(Rfc6020Mapping
29             .REVISION)
30             .add(Rfc6020Mapping.DESCRIPTION, 0, 1)
31             .add(Rfc6020Mapping.REFERENCE, 0, 1)
32             .build();
33
34     protected RevisionStatementImpl(
35             StmtContext<Date, RevisionStatement, ?> context) {
36         super(context);
37     }
38
39     public static class Definition
40             extends
41             AbstractStatementSupport<Date, RevisionStatement, EffectiveStatement<Date, RevisionStatement>> {
42
43         public Definition() {
44             super(Rfc6020Mapping.REVISION);
45         }
46
47         @Override
48         public Date parseArgumentValue(StmtContext<?, ?, ?> ctx, String value)
49                 throws SourceException {
50             Date revision;
51             try {
52                 revision = SimpleDateFormatUtil.getRevisionFormat()
53                         .parse(value);
54             } catch (ParseException e) {
55                 throw new SourceException(String.format("Revision value %s is not in required format yyyy-MM-dd",
56                         value), ctx.getStatementSourceReference(), e);
57             }
58
59             return revision;
60         }
61
62         @Override
63         public RevisionStatement createDeclared(
64                 StmtContext<Date, RevisionStatement, ?> ctx) {
65             return new RevisionStatementImpl(ctx);
66         }
67
68         @Override
69         public EffectiveStatement<Date, RevisionStatement> createEffective(
70                 StmtContext<Date, RevisionStatement, EffectiveStatement<Date, RevisionStatement>> ctx) {
71             return new RevisionEffectiveStatementImpl(ctx);
72         }
73
74         @Override
75         public void onFullDefinitionDeclared(StmtContext.Mutable<Date, RevisionStatement,
76                 EffectiveStatement<Date, RevisionStatement>> stmt) throws SourceException {
77             super.onFullDefinitionDeclared(stmt);
78             SUBSTATEMENT_VALIDATOR.validate(stmt);
79         }
80     }
81
82     @Override
83     public Date getDate() {
84         return argument();
85     }
86
87     @Nullable
88     @Override
89     public DescriptionStatement getDescription() {
90         return firstDeclared(DescriptionStatement.class);
91     }
92
93     @Nullable
94     @Override
95     public ReferenceStatement getReference() {
96         return firstDeclared(ReferenceStatement.class);
97     }
98 }