2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.status;
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;
24 public final class StatusStatementSupport
25 extends BaseStatementSupport<Status, StatusStatement, StatusEffectiveStatement> {
26 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
29 private static final StatusStatementSupport INSTANCE = new StatusStatementSupport();
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.
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);
48 private StatusStatementSupport() {
49 super(YangStmtMapping.STATUS, CopyPolicy.CONTEXT_INDEPENDENT);
52 public static StatusStatementSupport getInstance() {
57 public Status parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
60 return Status.CURRENT;
62 return Status.DEPRECATED;
64 return Status.OBSOLETE;
66 throw new SourceException(ctx.sourceReference(),
67 "Invalid status '%s', must be one of 'current', 'deprecated' or 'obsolete'", value);
72 public String internArgument(final String rawArgument) {
73 if ("current".equals(rawArgument)) {
75 } else if ("deprecated".equals(rawArgument)) {
77 } else if ("obsolete".equals(rawArgument)) {
85 protected SubstatementValidator getSubstatementValidator() {
86 return SUBSTATEMENT_VALIDATOR;
90 protected StatusStatement createDeclared(final StmtContext<Status, StatusStatement, ?> ctx,
91 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
92 return new RegularStatusStatement(ctx.getArgument(), substatements);
96 protected StatusStatement createEmptyDeclared(final StmtContext<Status, StatusStatement, ?> ctx) {
97 final Status argument = ctx.getArgument();
100 return EMPTY_CURRENT_DECL;
102 return EMPTY_DEPRECATED_DECL;
104 return EMPTY_OBSOLETE_DECL;
106 throw new IllegalStateException("Unhandled argument " + argument);
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);
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)) {
125 return EMPTY_OBSOLETE_EFF;
126 } else if (EMPTY_CURRENT_DECL.equals(declared)) {
127 // ... okay, why is this there? :)
128 return EMPTY_CURRENT_EFF;
130 return new EmptyStatusEffectiveStatement(declared);