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