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.if_feature;
10 import static com.google.common.base.Verify.verifyNotNull;
12 import com.google.common.collect.ImmutableList;
13 import java.util.Collection;
14 import java.util.HashMap;
15 import java.util.HashSet;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.YangVersion;
20 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
21 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.IfFeatureEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.IfFeatureExpr;
25 import org.opendaylight.yangtools.yang.model.api.stmt.IfFeatureStatement;
26 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
27 import org.opendaylight.yangtools.yang.parser.spi.FeatureNamespace;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceAction;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.InferenceContext;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public final class IfFeatureStatementSupport
42 extends BaseStatementSupport<IfFeatureExpr, IfFeatureStatement, IfFeatureEffectiveStatement> {
43 private static final Logger LOG = LoggerFactory.getLogger(IfFeatureStatementSupport.class);
44 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
45 YangStmtMapping.IF_FEATURE)
47 private static final IfFeatureStatementSupport INSTANCE = new IfFeatureStatementSupport();
49 private IfFeatureStatementSupport() {
50 super(YangStmtMapping.IF_FEATURE, CopyPolicy.CONTEXT_INDEPENDENT);
53 public static IfFeatureStatementSupport getInstance() {
58 public IfFeatureExpr parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
59 if (YangVersion.VERSION_1_1.equals(ctx.getRootVersion())) {
60 return IfFeaturePredicateVisitor.parseIfFeatureExpression(ctx, value);
62 return IfFeatureExpr.isPresent(StmtContextUtils.parseNodeIdentifier(ctx, value));
66 public void onFullDefinitionDeclared(
67 final Mutable<IfFeatureExpr, IfFeatureStatement, IfFeatureEffectiveStatement> stmt) {
68 super.onFullDefinitionDeclared(stmt);
70 final ModelActionBuilder verifyFeatures = stmt.newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
71 final Map<Prerequisite<?>, QName> backRef = new HashMap<>();
72 for (QName feature : stmt.coerceStatementArgument().getReferencedFeatures()) {
73 backRef.put(verifyFeatures.requiresCtx(stmt, FeatureNamespace.class, feature,
74 ModelProcessingPhase.EFFECTIVE_MODEL), feature);
77 verifyFeatures.apply(new InferenceAction() {
79 public void apply(final InferenceContext ctx) {
80 LOG.debug("Resolved all feature references in {}", backRef.values());
84 public void prerequisiteFailed(final Collection<? extends Prerequisite<?>> failed) {
85 final Set<QName> unresolvedFeatures = new HashSet<>();
86 for (Prerequisite<?> prereq : failed) {
87 unresolvedFeatures.add(verifyNotNull(backRef.get(prereq)));
90 throw new InferenceException(stmt.getStatementSourceReference(),
91 "Failed to resolve feature references %s in \"%s\"", unresolvedFeatures,
92 stmt.rawStatementArgument());
98 protected SubstatementValidator getSubstatementValidator() {
99 return SUBSTATEMENT_VALIDATOR;
103 protected IfFeatureStatement createDeclared(final StmtContext<IfFeatureExpr, IfFeatureStatement, ?> ctx,
104 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
105 return new RegularIfFeatureStatement(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument(),
110 protected IfFeatureStatement createEmptyDeclared(final StmtContext<IfFeatureExpr, IfFeatureStatement, ?> ctx) {
111 return new EmptyIfFeatureStatement(ctx.coerceRawStatementArgument(), ctx.coerceStatementArgument());
115 protected IfFeatureEffectiveStatement createEffective(
116 final StmtContext<IfFeatureExpr, IfFeatureStatement, IfFeatureEffectiveStatement> ctx,
117 final IfFeatureStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
118 return new RegularIfFeatureEffectiveStatement(declared, substatements);
122 protected IfFeatureEffectiveStatement createEmptyEffective(
123 final StmtContext<IfFeatureExpr, IfFeatureStatement, IfFeatureEffectiveStatement> ctx,
124 final IfFeatureStatement declared) {
125 return new EmptyIfFeatureEffectiveStatement(declared);