2 * Copyright (c) 2015 Cisco Systems, Inc. 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.stmt.rfc6020;
10 import static org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator.MAX;
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;
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)
60 protected AugmentStatementImpl(final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
64 public static class Definition extends
65 AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
68 super(Rfc6020Mapping.AUGMENT);
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");
77 return Utils.nodeIdentifierFromPath(ctx, value);
81 public AugmentStatement createDeclared(
82 final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
83 return new AugmentStatementImpl(ctx);
87 public EffectiveStatement<SchemaNodeIdentifier, AugmentStatement> createEffective(
88 final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
89 return new AugmentEffectiveStatementImpl(ctx);
93 public void onFullDefinitionDeclared(
94 final StmtContext.Mutable<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode) {
95 if (!StmtContextUtils.areFeaturesSupported(augmentNode)) {
99 SUBSTATEMENT_VALIDATOR.validate(augmentNode);
101 if (StmtContextUtils.isInExtensionBody(augmentNode)) {
105 final ModelActionBuilder augmentAction = augmentNode.newInferenceAction(
106 ModelProcessingPhase.EFFECTIVE_MODEL);
107 final ModelActionBuilder.Prerequisite<StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>>> sourceCtxPrereq =
108 augmentAction.requiresCtx(augmentNode, ModelProcessingPhase.EFFECTIVE_MODEL);
109 final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target =
110 augmentAction.mutatesEffectiveCtx(getSearchRoot(augmentNode), SchemaNodeIdentifierBuildNamespace.class, augmentNode.getStatementArgument());
111 augmentAction.apply(new ModelActionBuilder.InferenceAction() {
114 public void apply() {
115 final StatementContextBase<?, ?, ?> augmentTargetCtx = (StatementContextBase<?, ?, ?>) target.get();
116 if (!AugmentUtils.isSupportedAugmentTarget(augmentTargetCtx)
117 || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
118 augmentNode.setIsSupportedToBuildEffective(false);
122 * Marks case short hand in augment
124 if (augmentTargetCtx.getPublicDefinition() == Rfc6020Mapping.CHOICE) {
125 augmentNode.addToNs(AugmentToChoiceNamespace.class, augmentNode, true);
128 // FIXME: this is a workaround for models which augment a node which is added via an extension
129 // which we do not handle. This needs to be reworked in terms of unknown schema nodes.
130 final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) augmentNode;
132 AugmentUtils.copyFromSourceToTarget(augmentSourceCtx, augmentTargetCtx);
133 augmentTargetCtx.addEffectiveSubstatement(augmentSourceCtx);
134 updateAugmentOrder(augmentSourceCtx);
135 } catch (SourceException e) {
136 LOG.debug("Failed to add augmentation {} defined at {}",
137 augmentTargetCtx.getStatementSourceReference(),
138 augmentSourceCtx.getStatementSourceReference(), e);
142 private void updateAugmentOrder(final StatementContextBase<?, ?, ?> augmentSourceCtx) {
143 Integer currentOrder = augmentSourceCtx.getFromNamespace(StmtOrderingNamespace.class,
144 Rfc6020Mapping.AUGMENT);
145 if (currentOrder == null) {
151 augmentSourceCtx.setOrder(currentOrder);
152 augmentSourceCtx.addToNs(StmtOrderingNamespace.class, Rfc6020Mapping.AUGMENT, currentOrder);
156 public void prerequisiteFailed(final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed) {
157 throw new InferenceException(augmentNode.getStatementSourceReference(),
158 "Augment target '%s' not found", augmentNode.getStatementArgument());
163 private static Mutable<?, ?, ?> getSearchRoot(final Mutable<?, ?, ?> augmentContext) {
164 Mutable<?, ?, ?> parent = augmentContext.getParentContext();
165 // Augment is in uses - we need to augment instantiated nodes in parent.
166 if (Rfc6020Mapping.USES.equals(parent.getPublicDefinition())) {
167 return parent.getParentContext();
175 public SchemaNodeIdentifier getTargetNode() {
180 public Collection<? extends DataDefinitionStatement> getDataDefinitions() {
181 return allDeclared(DataDefinitionStatement.class);