b2aa6593110386e24f6a227611ffbdf4411f7069
[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.Nonnull;
13 import javax.annotation.Nullable;
14 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
15 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionStatement;
20 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.RevisionEffectiveStatementImpl;
26
27 public class RevisionStatementImpl extends AbstractDeclaredStatement<Date>
28         implements RevisionStatement {
29     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
30         YangStmtMapping.REVISION)
31             .addOptional(YangStmtMapping.DESCRIPTION)
32             .addOptional(YangStmtMapping.REFERENCE)
33             .build();
34
35     protected RevisionStatementImpl(final 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(YangStmtMapping.REVISION);
45         }
46
47         @Override
48         public Date parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
49             try {
50                 return SimpleDateFormatUtil.getRevisionFormat().parse(value);
51             } catch (ParseException e) {
52                 throw new SourceException(ctx.getStatementSourceReference(), e,
53                     "Revision value %s is not in required format yyyy-MM-dd", value);
54             }
55         }
56
57         @Override
58         public RevisionStatement createDeclared(final StmtContext<Date, RevisionStatement, ?> ctx) {
59             return new RevisionStatementImpl(ctx);
60         }
61
62         @Override
63         public EffectiveStatement<Date, RevisionStatement> createEffective(
64                 final StmtContext<Date, RevisionStatement, EffectiveStatement<Date, RevisionStatement>> ctx) {
65             return new RevisionEffectiveStatementImpl(ctx);
66         }
67
68         @Override
69         public void onFullDefinitionDeclared(
70                 final StmtContext.Mutable<Date, RevisionStatement, EffectiveStatement<Date, RevisionStatement>> stmt) {
71             super.onFullDefinitionDeclared(stmt);
72             SUBSTATEMENT_VALIDATOR.validate(stmt);
73         }
74     }
75
76     @Nonnull
77     @Override
78     public Date getDate() {
79         return argument();
80     }
81
82     @Nullable
83     @Override
84     public DescriptionStatement getDescription() {
85         return firstDeclared(DescriptionStatement.class);
86     }
87
88     @Nullable
89     @Override
90     public ReferenceStatement getReference() {
91         return firstDeclared(ReferenceStatement.class);
92     }
93 }