YANGTOOLS-706: Split up base utility classes into rfc6020.util
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / StatusStatementImpl.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 javax.annotation.Nonnull;
11 import org.opendaylight.yangtools.yang.model.api.Status;
12 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
13 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.StatusStatement;
15 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
16 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
17 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
19 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
20 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.StatusEffectiveStatementImpl;
21
22 public class StatusStatementImpl extends AbstractDeclaredStatement<Status>
23         implements StatusStatement {
24     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
25             .STATUS)
26             .build();
27
28     protected StatusStatementImpl(
29             final StmtContext<Status, StatusStatement, ?> context) {
30         super(context);
31     }
32
33     public static class Definition
34             extends
35             AbstractStatementSupport<Status, StatusStatement, EffectiveStatement<Status, StatusStatement>> {
36
37         public Definition() {
38             super(YangStmtMapping.STATUS);
39         }
40
41         @Override
42         public Status parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
43             switch (value) {
44                 case "current":
45                     return Status.CURRENT;
46                 case "deprecated":
47                     return Status.DEPRECATED;
48                 case "obsolete":
49                     return Status.OBSOLETE;
50                 default:
51                     throw new SourceException(ctx.getStatementSourceReference(),
52                         "Invalid status '%s', must be one of 'current', 'deprecated' or 'obsolete'", value);
53             }
54         }
55
56         @Override
57         public StatusStatement createDeclared(
58                 final StmtContext<Status, StatusStatement, ?> ctx) {
59             return new StatusStatementImpl(ctx);
60         }
61
62         @Override
63         public EffectiveStatement<Status, StatusStatement> createEffective(
64                 final StmtContext<Status, StatusStatement, EffectiveStatement<Status, StatusStatement>> ctx) {
65             return new StatusEffectiveStatementImpl(ctx);
66         }
67
68         @Override
69         public String internArgument(final String rawArgument) {
70             if ("current".equals(rawArgument)) {
71                 return "current";
72             } else if ("deprecated".equals(rawArgument)) {
73                 return "deprecated";
74             } else if ("obsolete".equals(rawArgument)) {
75                 return "obsolete";
76             } else {
77                 return rawArgument;
78             }
79         }
80
81         @Override
82         protected SubstatementValidator getSubstatementValidator() {
83             return SUBSTATEMENT_VALIDATOR;
84         }
85     }
86
87     @Nonnull
88     @Override
89     public Status getValue() {
90         return argument();
91     }
92
93 }