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.SourceException;
32 import org.opendaylight.yangtools.yang.parser.spi.source.StmtOrderingNamespace;
33 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AugmentEffectiveStatementImpl;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class AugmentStatementImpl extends AbstractDeclaredStatement<SchemaNodeIdentifier> implements AugmentStatement {
39 private static final Logger LOG = LoggerFactory.getLogger(AugmentStatementImpl.class);
40 private static final Pattern PATH_REL_PATTERN1 = Pattern.compile("\\.\\.?\\s*/(.+)");
41 private static final Pattern PATH_REL_PATTERN2 = Pattern.compile("//.*");
42 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator
43 .builder(Rfc6020Mapping.AUGMENT)
44 .add(Rfc6020Mapping.ANYXML, 0, MAX)
45 .add(Rfc6020Mapping.CASE, 0, MAX)
46 .add(Rfc6020Mapping.CHOICE, 0, MAX)
47 .add(Rfc6020Mapping.CONTAINER, 0, MAX)
48 .add(Rfc6020Mapping.DESCRIPTION, 0, 1)
49 .add(Rfc6020Mapping.IF_FEATURE, 0, MAX)
50 .add(Rfc6020Mapping.LEAF, 0, MAX)
51 .add(Rfc6020Mapping.LEAF_LIST, 0, MAX)
52 .add(Rfc6020Mapping.LIST, 0, MAX)
53 .add(Rfc6020Mapping.REFERENCE, 0, 1)
54 .add(Rfc6020Mapping.STATUS, 0, 1)
55 .add(Rfc6020Mapping.USES, 0, MAX)
56 .add(Rfc6020Mapping.WHEN, 0, 1)
59 protected AugmentStatementImpl(final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
63 public static class Definition extends
64 AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
67 super(Rfc6020Mapping.AUGMENT);
71 public SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
72 Preconditions.checkArgument(!PATH_REL_PATTERN1.matcher(value).matches()
73 && !PATH_REL_PATTERN2.matcher(value).matches(),
74 "An argument for augment can be only absolute path; or descendant if used in uses");
76 return Utils.nodeIdentifierFromPath(ctx, value);
80 public AugmentStatement createDeclared(
81 final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
82 return new AugmentStatementImpl(ctx);
86 public EffectiveStatement<SchemaNodeIdentifier, AugmentStatement> createEffective(
87 final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
88 return new AugmentEffectiveStatementImpl(ctx);
92 public void onFullDefinitionDeclared(
93 final StmtContext.Mutable<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode)
94 throws SourceException {
95 SUBSTATEMENT_VALIDATOR.validate(augmentNode);
97 if (StmtContextUtils.isInExtensionBody(augmentNode)) {
101 final ModelActionBuilder augmentAction = augmentNode
102 .newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
103 final ModelActionBuilder.Prerequisite<StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>>> sourceCtxPrereq = augmentAction
104 .requiresCtx(augmentNode, ModelProcessingPhase.EFFECTIVE_MODEL);
105 final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target = augmentAction.mutatesEffectiveCtx(getSearchRoot(augmentNode), SchemaNodeIdentifierBuildNamespace.class, augmentNode.getStatementArgument());
106 augmentAction.apply(new ModelActionBuilder.InferenceAction() {
109 public void apply() throws InferenceException {
110 final StatementContextBase<?, ?, ?> augmentTargetCtx = (StatementContextBase<?, ?, ?>) target.get();
112 if (!AugmentUtils.isSupportedAugmentTarget(augmentTargetCtx) || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
113 augmentNode.setIsSupportedToBuildEffective(false);
116 final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) augmentNode;
118 AugmentUtils.copyFromSourceToTarget(augmentSourceCtx,
121 .addEffectiveSubstatement(augmentSourceCtx);
122 updateAugmentOrder(augmentSourceCtx);
123 } catch (SourceException e) {
124 LOG.warn(e.getMessage(), e);
129 private void updateAugmentOrder(
130 final StatementContextBase<?, ?, ?> augmentSourceCtx) {
131 Integer currentOrder = augmentSourceCtx
132 .getFromNamespace(StmtOrderingNamespace.class,
133 Rfc6020Mapping.AUGMENT);
134 if (currentOrder == null) {
139 augmentSourceCtx.setOrder(currentOrder);
140 augmentSourceCtx.addToNs(StmtOrderingNamespace.class,
141 Rfc6020Mapping.AUGMENT, currentOrder);
145 public void prerequisiteFailed(
146 final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed)
147 throws InferenceException {
148 throw new InferenceException("Augment target not found: "
149 + augmentNode.getStatementArgument(), augmentNode
150 .getStatementSourceReference());
155 private Mutable<?, ?, ?> getSearchRoot(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();
167 public SchemaNodeIdentifier getTargetNode() {
172 public Collection<? extends DataDefinitionStatement> getDataDefinitions() {
173 return allDeclared(DataDefinitionStatement.class);