Move KeyEffectiveStatement implementations
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / meta / FeatureStatementSupport.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.meta;
9
10 import com.google.common.collect.ImmutableList;
11 import org.opendaylight.yangtools.yang.common.QName;
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.FeatureEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
20 import org.opendaylight.yangtools.yang.model.ri.stmt.EffectiveStatements;
21 import org.opendaylight.yangtools.yang.model.spi.meta.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
22 import org.opendaylight.yangtools.yang.parser.spi.FeatureNamespace;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractQNameStatementSupport;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
29
30 public final class FeatureStatementSupport
31         extends AbstractQNameStatementSupport<FeatureStatement, FeatureEffectiveStatement> {
32     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
33         YangStmtMapping.FEATURE)
34         .addOptional(YangStmtMapping.DESCRIPTION)
35         .addAny(YangStmtMapping.IF_FEATURE)
36         .addOptional(YangStmtMapping.STATUS)
37         .addOptional(YangStmtMapping.REFERENCE)
38         .build();
39     private static final FeatureStatementSupport INSTANCE = new FeatureStatementSupport();
40
41     private FeatureStatementSupport() {
42         super(YangStmtMapping.FEATURE, StatementPolicy.reject());
43     }
44
45     public static FeatureStatementSupport getInstance() {
46         return INSTANCE;
47     }
48
49     @Override
50     public QName parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
51         return StmtContextUtils.parseIdentifier(ctx, value);
52     }
53
54     @Override
55     public void onFullDefinitionDeclared(final Mutable<QName, FeatureStatement, FeatureEffectiveStatement> stmt) {
56         super.onFullDefinitionDeclared(stmt);
57         stmt.addContext(FeatureNamespace.class, stmt.getArgument(), stmt);
58     }
59
60     @Override
61     protected SubstatementValidator getSubstatementValidator() {
62         return SUBSTATEMENT_VALIDATOR;
63     }
64
65     @Override
66     protected FeatureStatement createDeclared(final StmtContext<QName, FeatureStatement, ?> ctx,
67             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
68         return DeclaredStatements.createFeature(ctx.getArgument(), substatements);
69     }
70
71     @Override
72     protected FeatureEffectiveStatement createEffective(final Current<QName, FeatureStatement> stmt,
73             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
74         return EffectiveStatements.createFeature(stmt.declared(), stmt.effectivePath(), computeFlags(substatements),
75             substatements);
76     }
77
78     private static int computeFlags(final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
79         return new FlagsBuilder()
80                 .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
81                 .toFlags();
82     }
83 }