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.meta.AbstractDeclaredStatement;
20 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
28 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
29 import org.opendaylight.yangtools.yang.parser.spi.source.StmtOrderingNamespace;
30 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AugmentEffectiveStatementImpl;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class AugmentStatementImpl extends AbstractDeclaredStatement<SchemaNodeIdentifier> implements AugmentStatement {
36 private static final Logger LOG = LoggerFactory.getLogger(AugmentStatementImpl.class);
37 private static final Pattern PATH_REL_PATTERN1 = Pattern.compile("\\.\\.?\\s*/(.+)");
38 private static final Pattern PATH_REL_PATTERN2 = Pattern.compile("//.*");
40 protected AugmentStatementImpl(final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
44 public static class Definition extends
45 AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
48 super(Rfc6020Mapping.AUGMENT);
52 public SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
53 Preconditions.checkArgument(!PATH_REL_PATTERN1.matcher(value).matches()
54 && !PATH_REL_PATTERN2.matcher(value).matches(),
55 "An argument for augment can be only absolute path; or descendant if used in uses");
57 return Utils.nodeIdentifierFromPath(ctx, value);
61 public AugmentStatement createDeclared(
62 final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
63 return new AugmentStatementImpl(ctx);
67 public EffectiveStatement<SchemaNodeIdentifier, AugmentStatement> createEffective(
68 final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
69 return new AugmentEffectiveStatementImpl(ctx);
73 public void onFullDefinitionDeclared(
74 final StmtContext.Mutable<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode)
75 throws SourceException {
77 if (StmtContextUtils.isInExtensionBody(augmentNode)) {
81 final ModelActionBuilder augmentAction = augmentNode
82 .newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
83 final ModelActionBuilder.Prerequisite<StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>>> sourceCtxPrereq = augmentAction
84 .requiresCtx(augmentNode, ModelProcessingPhase.EFFECTIVE_MODEL);
85 final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target = augmentAction.mutatesEffectiveCtx(getSearchRoot(augmentNode), SchemaNodeIdentifierBuildNamespace.class, augmentNode.getStatementArgument());
86 augmentAction.apply(new ModelActionBuilder.InferenceAction() {
89 public void apply() throws InferenceException {
90 final StatementContextBase<?, ?, ?> augmentTargetCtx = (StatementContextBase<?, ?, ?>) target.get();
92 if (!AugmentUtils.isSupportedAugmentTarget(augmentTargetCtx) || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
93 augmentNode.setIsSupportedToBuildEffective(false);
96 final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) augmentNode;
98 AugmentUtils.copyFromSourceToTarget(augmentSourceCtx,
101 .addEffectiveSubstatement(augmentSourceCtx);
102 updateAugmentOrder(augmentSourceCtx);
103 } catch (SourceException e) {
104 LOG.warn(e.getMessage(), e);
109 private void updateAugmentOrder(
110 final StatementContextBase<?, ?, ?> augmentSourceCtx) {
111 Integer currentOrder = augmentSourceCtx
112 .getFromNamespace(StmtOrderingNamespace.class,
113 Rfc6020Mapping.AUGMENT);
114 if (currentOrder == null) {
119 augmentSourceCtx.setOrder(currentOrder);
120 augmentSourceCtx.addToNs(StmtOrderingNamespace.class,
121 Rfc6020Mapping.AUGMENT, currentOrder);
125 public void prerequisiteFailed(
126 final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed)
127 throws InferenceException {
128 throw new InferenceException("Augment target not found: "
129 + augmentNode.getStatementArgument(), augmentNode
130 .getStatementSourceReference());
135 private Mutable<?, ?, ?> getSearchRoot(Mutable<?, ?, ?> augmentContext) {
136 Mutable<?, ?, ?> parent = augmentContext.getParentContext();
137 // Augment is in uses - we need to augment instantiated nodes in parent.
138 if(Rfc6020Mapping.USES.equals(parent.getPublicDefinition())) {
139 return parent.getParentContext();
147 public SchemaNodeIdentifier getTargetNode() {
152 public Collection<? extends DataDefinitionStatement> getDataDefinitions() {
153 return allDeclared(DataDefinitionStatement.class);