0e6ef322fb9d7a97273e253cc542d510aa5119f7
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / RevisionDateStatementImpl.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 org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
13 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
14 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.RevisionDateStatement;
16 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
20 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.RevisionDateEffectiveStatementImpl;
22
23 public class RevisionDateStatementImpl extends
24         AbstractDeclaredStatement<Date> implements RevisionDateStatement {
25     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(Rfc6020Mapping
26             .REVISION_DATE)
27             .build();
28
29     protected RevisionDateStatementImpl(
30             StmtContext<Date, RevisionDateStatement, ?> context) {
31         super(context);
32     }
33
34     public static class Definition
35             extends
36             AbstractStatementSupport<Date, RevisionDateStatement, EffectiveStatement<Date, RevisionDateStatement>> {
37
38         public Definition() {
39             super(Rfc6020Mapping.REVISION_DATE);
40         }
41
42         @Override
43         public Date parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) throws SourceException {
44             Date revision;
45             try {
46                 revision = SimpleDateFormatUtil.getRevisionFormat()
47                         .parse(value);
48             } catch (ParseException e) {
49                 throw new SourceException(String.format("Revision value %s is not in required format yyyy-MM-dd",
50                         value), ctx.getStatementSourceReference(), e);
51             }
52
53             return revision;
54         }
55
56         @Override
57         public RevisionDateStatement createDeclared(
58                 StmtContext<Date, RevisionDateStatement, ?> ctx) {
59             return new RevisionDateStatementImpl(ctx);
60         }
61
62         @Override
63         public EffectiveStatement<Date, RevisionDateStatement> createEffective(
64                 StmtContext<Date, RevisionDateStatement, EffectiveStatement<Date, RevisionDateStatement>> ctx) {
65             return new RevisionDateEffectiveStatementImpl(ctx);
66         }
67
68         @Override
69         public void onFullDefinitionDeclared(StmtContext.Mutable<Date, RevisionDateStatement,
70                 EffectiveStatement<Date, RevisionDateStatement>> stmt) throws SourceException {
71             super.onFullDefinitionDeclared(stmt);
72             SUBSTATEMENT_VALIDATOR.validate(stmt);
73         }
74     }
75
76     @Override
77     public Date getDate() {
78         return argument();
79     }
80
81 }