6a2d471f1ccad4f49e7d93c9a938b0065097b23f
[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                 throws SourceException {
94             SUBSTATEMENT_VALIDATOR.validate(augmentNode);
95
96             if (StmtContextUtils.isInExtensionBody(augmentNode)) {
97                 return;
98             }
99
100             final ModelActionBuilder augmentAction = augmentNode.newInferenceAction(
101                 ModelProcessingPhase.EFFECTIVE_MODEL);
102             final ModelActionBuilder.Prerequisite<StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>>> sourceCtxPrereq =
103                     augmentAction.requiresCtx(augmentNode, ModelProcessingPhase.EFFECTIVE_MODEL);
104             final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target =
105                     augmentAction.mutatesEffectiveCtx(getSearchRoot(augmentNode), SchemaNodeIdentifierBuildNamespace.class, augmentNode.getStatementArgument());
106             augmentAction.apply(new ModelActionBuilder.InferenceAction() {
107
108                 @Override
109                 public void apply() {
110                     final StatementContextBase<?, ?, ?> augmentTargetCtx = (StatementContextBase<?, ?, ?>) target.get();
111                     if (!AugmentUtils.isSupportedAugmentTarget(augmentTargetCtx) || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
112                         augmentNode.setIsSupportedToBuildEffective(false);
113                         return;
114                     }
115
116                     // FIXME: this is a workaround for models which augment a node which is added via an extension
117                     //        which we do not handle. This needs to be reworked in terms of unknown schema nodes.
118                     final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) augmentNode;
119                     try {
120                         AugmentUtils.copyFromSourceToTarget(augmentSourceCtx, augmentTargetCtx);
121                         augmentTargetCtx.addEffectiveSubstatement(augmentSourceCtx);
122                         updateAugmentOrder(augmentSourceCtx);
123                     } catch (SourceException e) {
124                         LOG.warn("Failed to add augmentation %s defined at {}",
125                             augmentSourceCtx.getStatementSourceReference(), e);
126                     }
127                 }
128
129                 private void updateAugmentOrder(final StatementContextBase<?, ?, ?> augmentSourceCtx) {
130                     Integer currentOrder = augmentSourceCtx.getFromNamespace(StmtOrderingNamespace.class,
131                         Rfc6020Mapping.AUGMENT);
132                     if (currentOrder == null) {
133                         currentOrder = 1;
134                     } else {
135                         currentOrder++;
136                     }
137
138                     augmentSourceCtx.setOrder(currentOrder);
139                     augmentSourceCtx.addToNs(StmtOrderingNamespace.class, Rfc6020Mapping.AUGMENT, currentOrder);
140                 }
141
142                 @Override
143                 public void prerequisiteFailed(final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed) {
144                     throw new InferenceException(augmentNode.getStatementSourceReference(),
145                         "Augment target '%s' not found", augmentNode.getStatementArgument());
146                 }
147             });
148         }
149
150         private static Mutable<?, ?, ?> getSearchRoot(final Mutable<?, ?, ?> augmentContext) {
151             Mutable<?, ?, ?> parent = augmentContext.getParentContext();
152             // Augment is in uses - we need to augment instantiated nodes in parent.
153             if (Rfc6020Mapping.USES.equals(parent.getPublicDefinition())) {
154                 return parent.getParentContext();
155             }
156             return parent;
157         }
158     }
159
160     @Nonnull
161     @Override
162     public SchemaNodeIdentifier getTargetNode() {
163         return argument();
164     }
165
166     @Override
167     public Collection<? extends DataDefinitionStatement> getDataDefinitions() {
168         return allDeclared(DataDefinitionStatement.class);
169     }
170 }