Bug 4506: Honor if-feature during SchemaContext assembly
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / AugmentStatementImpl.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 static org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator.MAX;
11 import com.google.common.base.Preconditions;
12 import java.util.Collection;
13 import java.util.regex.Pattern;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.DataDefinitionStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
20 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
30 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
31 import org.opendaylight.yangtools.yang.parser.spi.source.StmtOrderingNamespace;
32 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
33 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AugmentEffectiveStatementImpl;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class AugmentStatementImpl extends AbstractDeclaredStatement<SchemaNodeIdentifier> implements AugmentStatement {
38     private static final Logger LOG = LoggerFactory.getLogger(AugmentStatementImpl.class);
39     private static final Pattern PATH_REL_PATTERN1 = Pattern.compile("\\.\\.?\\s*/(.+)");
40     private static final Pattern PATH_REL_PATTERN2 = Pattern.compile("//.*");
41     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator
42             .builder(Rfc6020Mapping.AUGMENT)
43             .add(Rfc6020Mapping.ANYXML, 0, MAX)
44             .add(Rfc6020Mapping.CASE, 0, MAX)
45             .add(Rfc6020Mapping.CHOICE, 0, MAX)
46             .add(Rfc6020Mapping.CONTAINER, 0, MAX)
47             .add(Rfc6020Mapping.DESCRIPTION, 0, 1)
48             .add(Rfc6020Mapping.IF_FEATURE, 0, MAX)
49             .add(Rfc6020Mapping.LEAF, 0, MAX)
50             .add(Rfc6020Mapping.LEAF_LIST, 0, MAX)
51             .add(Rfc6020Mapping.LIST, 0, MAX)
52             .add(Rfc6020Mapping.REFERENCE, 0, 1)
53             .add(Rfc6020Mapping.STATUS, 0, 1)
54             .add(Rfc6020Mapping.USES, 0, MAX)
55             .add(Rfc6020Mapping.WHEN, 0, 1)
56             .build();
57
58     protected AugmentStatementImpl(final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
59         super(context);
60     }
61
62     public static class Definition extends
63             AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
64
65         public Definition() {
66             super(Rfc6020Mapping.AUGMENT);
67         }
68
69         @Override
70         public SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
71             Preconditions.checkArgument(!PATH_REL_PATTERN1.matcher(value).matches()
72                 && !PATH_REL_PATTERN2.matcher(value).matches(),
73                 "An argument for augment can be only absolute path; or descendant if used in uses");
74
75             return Utils.nodeIdentifierFromPath(ctx, value);
76         }
77
78         @Override
79         public AugmentStatement createDeclared(
80                 final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
81             return new AugmentStatementImpl(ctx);
82         }
83
84         @Override
85         public EffectiveStatement<SchemaNodeIdentifier, AugmentStatement> createEffective(
86                 final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
87             return new AugmentEffectiveStatementImpl(ctx);
88         }
89
90         @Override
91         public void onFullDefinitionDeclared(
92                 final StmtContext.Mutable<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode) {
93             if (!StmtContextUtils.areFeaturesSupported(augmentNode)) {
94                 return;
95             }
96
97             SUBSTATEMENT_VALIDATOR.validate(augmentNode);
98
99             if (StmtContextUtils.isInExtensionBody(augmentNode)) {
100                 return;
101             }
102
103             final ModelActionBuilder augmentAction = augmentNode.newInferenceAction(
104                 ModelProcessingPhase.EFFECTIVE_MODEL);
105             final ModelActionBuilder.Prerequisite<StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>>> sourceCtxPrereq =
106                     augmentAction.requiresCtx(augmentNode, ModelProcessingPhase.EFFECTIVE_MODEL);
107             final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target =
108                     augmentAction.mutatesEffectiveCtx(getSearchRoot(augmentNode), SchemaNodeIdentifierBuildNamespace.class, augmentNode.getStatementArgument());
109             augmentAction.apply(new ModelActionBuilder.InferenceAction() {
110
111                 @Override
112                 public void apply() {
113                     final StatementContextBase<?, ?, ?> augmentTargetCtx = (StatementContextBase<?, ?, ?>) target.get();
114                     if (!AugmentUtils.isSupportedAugmentTarget(augmentTargetCtx)
115                             || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
116                         augmentNode.setIsSupportedToBuildEffective(false);
117                         return;
118                     }
119
120                     // FIXME: this is a workaround for models which augment a node which is added via an extension
121                     //        which we do not handle. This needs to be reworked in terms of unknown schema nodes.
122                     final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) augmentNode;
123                     try {
124                         AugmentUtils.copyFromSourceToTarget(augmentSourceCtx, augmentTargetCtx);
125                         augmentTargetCtx.addEffectiveSubstatement(augmentSourceCtx);
126                         updateAugmentOrder(augmentSourceCtx);
127                     } catch (SourceException e) {
128                         LOG.debug("Failed to add augmentation {} defined at {}",
129                             augmentTargetCtx.getStatementSourceReference(),
130                                 augmentSourceCtx.getStatementSourceReference(), e);
131                     }
132                 }
133
134                 private void updateAugmentOrder(final StatementContextBase<?, ?, ?> augmentSourceCtx) {
135                     Integer currentOrder = augmentSourceCtx.getFromNamespace(StmtOrderingNamespace.class,
136                         Rfc6020Mapping.AUGMENT);
137                     if (currentOrder == null) {
138                         currentOrder = 1;
139                     } else {
140                         currentOrder++;
141                     }
142
143                     augmentSourceCtx.setOrder(currentOrder);
144                     augmentSourceCtx.addToNs(StmtOrderingNamespace.class, Rfc6020Mapping.AUGMENT, currentOrder);
145                 }
146
147                 @Override
148                 public void prerequisiteFailed(final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed) {
149                     throw new InferenceException(augmentNode.getStatementSourceReference(),
150                         "Augment target '%s' not found", augmentNode.getStatementArgument());
151                 }
152             });
153         }
154
155         private static Mutable<?, ?, ?> getSearchRoot(final Mutable<?, ?, ?> augmentContext) {
156             Mutable<?, ?, ?> parent = augmentContext.getParentContext();
157             // Augment is in uses - we need to augment instantiated nodes in parent.
158             if (Rfc6020Mapping.USES.equals(parent.getPublicDefinition())) {
159                 return parent.getParentContext();
160             }
161             return parent;
162         }
163     }
164
165     @Nonnull
166     @Override
167     public SchemaNodeIdentifier getTargetNode() {
168         return argument();
169     }
170
171     @Override
172     public Collection<? extends DataDefinitionStatement> getDataDefinitions() {
173         return allDeclared(DataDefinitionStatement.class);
174     }
175 }