10acc671f3bcfe0219836d3c6fbb8d7e72aa4f2b
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / status / StatusStatementSupport.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.status;
9
10 import com.google.common.collect.ImmutableList;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.opendaylight.yangtools.yang.model.api.Status;
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.StatusEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.StatusStatement;
18 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
22 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
23
24 public final class StatusStatementSupport
25         extends BaseStatementSupport<Status, StatusStatement, StatusEffectiveStatement> {
26     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
27         .STATUS)
28         .build();
29     private static final StatusStatementSupport INSTANCE = new StatusStatementSupport();
30
31     /*
32      * status has low argument cardinality, hence we can reuse them in case declaration does not have any
33      * substatements (which is the usual case). Yeah, we could consider an EnumMap, but this is not too bad, either.
34      */
35     private static final @NonNull EmptyStatusStatement EMPTY_CURRENT_DECL =
36             new EmptyStatusStatement(Status.CURRENT);
37     private static final @NonNull EmptyStatusStatement EMPTY_DEPRECATED_DECL =
38             new EmptyStatusStatement(Status.DEPRECATED);
39     private static final @NonNull EmptyStatusStatement EMPTY_OBSOLETE_DECL =
40             new EmptyStatusStatement(Status.OBSOLETE);
41     private static final @NonNull EmptyStatusEffectiveStatement EMPTY_CURRENT_EFF =
42             new EmptyStatusEffectiveStatement(EMPTY_CURRENT_DECL);
43     private static final @NonNull EmptyStatusEffectiveStatement EMPTY_DEPRECATED_EFF =
44             new EmptyStatusEffectiveStatement(EMPTY_DEPRECATED_DECL);
45     private static final @NonNull EmptyStatusEffectiveStatement EMPTY_OBSOLETE_EFF =
46             new EmptyStatusEffectiveStatement(EMPTY_OBSOLETE_DECL);
47
48     private StatusStatementSupport() {
49         super(YangStmtMapping.STATUS, CopyPolicy.CONTEXT_INDEPENDENT);
50     }
51
52     public static StatusStatementSupport getInstance() {
53         return INSTANCE;
54     }
55
56     @Override
57     public Status parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
58         switch (value) {
59             case "current":
60                 return Status.CURRENT;
61             case "deprecated":
62                 return Status.DEPRECATED;
63             case "obsolete":
64                 return Status.OBSOLETE;
65             default:
66                 throw new SourceException(ctx.getStatementSourceReference(),
67                     "Invalid status '%s', must be one of 'current', 'deprecated' or 'obsolete'", value);
68         }
69     }
70
71     @Override
72     public String internArgument(final String rawArgument) {
73         if ("current".equals(rawArgument)) {
74             return "current";
75         } else if ("deprecated".equals(rawArgument)) {
76             return "deprecated";
77         } else if ("obsolete".equals(rawArgument)) {
78             return "obsolete";
79         } else {
80             return rawArgument;
81         }
82     }
83
84     @Override
85     protected SubstatementValidator getSubstatementValidator() {
86         return SUBSTATEMENT_VALIDATOR;
87     }
88
89     @Override
90     protected StatusStatement createDeclared(final StmtContext<Status, StatusStatement, ?> ctx,
91             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
92         return new RegularStatusStatement(ctx.coerceStatementArgument(), substatements);
93     }
94
95     @Override
96     protected StatusStatement createEmptyDeclared(final StmtContext<Status, StatusStatement, ?> ctx) {
97         final Status argument = ctx.coerceStatementArgument();
98         switch (argument) {
99             case CURRENT:
100                 return EMPTY_CURRENT_DECL;
101             case DEPRECATED:
102                 return EMPTY_DEPRECATED_DECL;
103             case OBSOLETE:
104                 return EMPTY_OBSOLETE_DECL;
105             default:
106                 throw new IllegalStateException("Unhandled argument " + argument);
107         }
108     }
109
110     @Override
111     protected StatusEffectiveStatement createEffective(final Current<Status, StatusStatement> stmt,
112             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
113         return substatements.isEmpty() ? createEmptyEffective(stmt.declared())
114             : new RegularStatusEffectiveStatement(stmt.declared(), substatements);
115     }
116
117     private static @NonNull StatusEffectiveStatement createEmptyEffective(final StatusStatement declared) {
118         // Aggressively reuse effective instances which are backed by the corresponding empty declared instance, as this
119         // is the case unless there is a weird extension in use.
120         if (EMPTY_DEPRECATED_DECL.equals(declared)) {
121             // Most likely to be seen (as current is the default)
122             return EMPTY_DEPRECATED_EFF;
123         } else if (EMPTY_OBSOLETE_DECL.equals(declared)) {
124             // less likely
125             return EMPTY_OBSOLETE_EFF;
126         } else if (EMPTY_CURRENT_DECL.equals(declared)) {
127             // ... okay, why is this there? :)
128             return EMPTY_CURRENT_EFF;
129         } else {
130             return new EmptyStatusEffectiveStatement(declared);
131         }
132     }
133 }