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 com.google.common.base.Preconditions;
11 import java.util.Collection;
12 import java.util.regex.Pattern;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.DataDefinitionStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
19 import org.opendaylight.yangtools.yang.parser.spi.SubstatementValidator;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractDeclaredStatement;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
29 import org.opendaylight.yangtools.yang.parser.spi.source.AugmentToChoiceNamespace;
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;
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 .addAny(Rfc6020Mapping.ANYXML)
44 .addAny(Rfc6020Mapping.CASE)
45 .addAny(Rfc6020Mapping.CHOICE)
46 .addAny(Rfc6020Mapping.CONTAINER)
47 .addOptional(Rfc6020Mapping.DESCRIPTION)
48 .addAny(Rfc6020Mapping.IF_FEATURE)
49 .addAny(Rfc6020Mapping.LEAF)
50 .addAny(Rfc6020Mapping.LEAF_LIST)
51 .addAny(Rfc6020Mapping.LIST)
52 .addOptional(Rfc6020Mapping.REFERENCE)
53 .addOptional(Rfc6020Mapping.STATUS)
54 .addAny(Rfc6020Mapping.USES)
55 .addOptional(Rfc6020Mapping.WHEN)
58 protected AugmentStatementImpl(final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
62 public static class Definition extends
63 AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
66 super(Rfc6020Mapping.AUGMENT);
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");
75 return Utils.nodeIdentifierFromPath(ctx, value);
79 public AugmentStatement createDeclared(
80 final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
81 return new AugmentStatementImpl(ctx);
85 public EffectiveStatement<SchemaNodeIdentifier, AugmentStatement> createEffective(
86 final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
87 return new AugmentEffectiveStatementImpl(ctx);
91 public void onFullDefinitionDeclared(
92 final StmtContext.Mutable<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode) {
93 if (!StmtContextUtils.areFeaturesSupported(augmentNode)) {
97 SUBSTATEMENT_VALIDATOR.validate(augmentNode);
99 if (StmtContextUtils.isInExtensionBody(augmentNode)) {
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() {
112 public void apply() {
113 final StatementContextBase<?, ?, ?> augmentTargetCtx = (StatementContextBase<?, ?, ?>) target.get();
114 if (!AugmentUtils.isSupportedAugmentTarget(augmentTargetCtx)
115 || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
116 augmentNode.setIsSupportedToBuildEffective(false);
120 * Marks case short hand in augment
122 if (augmentTargetCtx.getPublicDefinition() == Rfc6020Mapping.CHOICE) {
123 augmentNode.addToNs(AugmentToChoiceNamespace.class, augmentNode, true);
126 // FIXME: this is a workaround for models which augment a node which is added via an extension
127 // which we do not handle. This needs to be reworked in terms of unknown schema nodes.
128 final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) augmentNode;
130 AugmentUtils.copyFromSourceToTarget(augmentSourceCtx, augmentTargetCtx);
131 augmentTargetCtx.addEffectiveSubstatement(augmentSourceCtx);
132 updateAugmentOrder(augmentSourceCtx);
133 } catch (SourceException e) {
134 LOG.debug("Failed to add augmentation {} defined at {}",
135 augmentTargetCtx.getStatementSourceReference(),
136 augmentSourceCtx.getStatementSourceReference(), e);
140 private void updateAugmentOrder(final StatementContextBase<?, ?, ?> augmentSourceCtx) {
141 Integer currentOrder = augmentSourceCtx.getFromNamespace(StmtOrderingNamespace.class,
142 Rfc6020Mapping.AUGMENT);
143 if (currentOrder == null) {
149 augmentSourceCtx.setOrder(currentOrder);
150 augmentSourceCtx.addToNs(StmtOrderingNamespace.class, Rfc6020Mapping.AUGMENT, currentOrder);
154 public void prerequisiteFailed(final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed) {
155 throw new InferenceException(augmentNode.getStatementSourceReference(),
156 "Augment target '%s' not found", augmentNode.getStatementArgument());
161 private static Mutable<?, ?, ?> getSearchRoot(final Mutable<?, ?, ?> augmentContext) {
162 Mutable<?, ?, ?> parent = augmentContext.getParentContext();
163 // Augment is in uses - we need to augment instantiated nodes in parent.
164 if (Rfc6020Mapping.USES.equals(parent.getPublicDefinition())) {
165 return parent.getParentContext();
173 public SchemaNodeIdentifier getTargetNode() {
178 public Collection<? extends DataDefinitionStatement> getDataDefinitions() {
179 return allDeclared(DataDefinitionStatement.class);