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.ModelProcessingPhase;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 import org.opendaylight.yangtools.yang.parser.spi.source.StmtOrderingNamespace;
28 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
29 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AugmentEffectiveStatementImpl;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class AugmentStatementImpl extends AbstractDeclaredStatement<SchemaNodeIdentifier> implements AugmentStatement {
34 private static final Logger LOG = LoggerFactory.getLogger(AugmentStatementImpl.class);
35 private static final Pattern PATH_REL_PATTERN1 = Pattern.compile("\\.\\.?\\s*/(.+)");
36 private static final Pattern PATH_REL_PATTERN2 = Pattern.compile("//.*");
38 protected AugmentStatementImpl(final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
42 public static class Definition extends
43 AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
46 super(Rfc6020Mapping.AUGMENT);
50 public SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
51 Preconditions.checkArgument(!PATH_REL_PATTERN1.matcher(value).matches()
52 && !PATH_REL_PATTERN2.matcher(value).matches(),
53 "An argument for augment can be only absolute path; or descendant if used in uses");
55 return Utils.nodeIdentifierFromPath(ctx, value);
59 public AugmentStatement createDeclared(
60 final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
61 return new AugmentStatementImpl(ctx);
65 public EffectiveStatement<SchemaNodeIdentifier, AugmentStatement> createEffective(
66 final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
67 return new AugmentEffectiveStatementImpl(ctx);
71 public void onFullDefinitionDeclared(
72 final StmtContext.Mutable<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode)
73 throws SourceException {
75 if (StmtContextUtils.isInExtensionBody(augmentNode)) {
79 final ModelActionBuilder augmentAction = augmentNode
80 .newInferenceAction(ModelProcessingPhase.FULL_DECLARATION);
81 final ModelActionBuilder.Prerequisite<StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>>> sourceCtxPrereq = augmentAction
82 .requiresCtx(augmentNode,
83 ModelProcessingPhase.FULL_DECLARATION);
85 augmentAction.apply(new ModelActionBuilder.InferenceAction() {
88 public void apply() throws InferenceException {
89 final StatementContextBase<?, ?, ?> augmentTargetCtx = AugmentUtils
90 .getAugmentTargetCtx(augmentNode);
92 if (augmentTargetCtx == null) {
93 throw new InferenceException(
94 "Augment target not found: "
95 + augmentNode.getStatementArgument(),
96 augmentNode.getStatementSourceReference());
99 if (!AugmentUtils.isSupportedAugmentTarget(augmentTargetCtx) || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
100 augmentNode.setIsSupportedToBuildEffective(false);
104 final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) augmentNode;
107 AugmentUtils.copyFromSourceToTarget(augmentSourceCtx,
110 .addEffectiveSubstatement(augmentSourceCtx);
111 updateAugmentOrder(augmentSourceCtx);
112 } catch (SourceException e) {
113 LOG.warn(e.getMessage(), e);
118 private void updateAugmentOrder(
119 final StatementContextBase<?, ?, ?> augmentSourceCtx) {
120 Integer currentOrder = augmentSourceCtx
121 .getFromNamespace(StmtOrderingNamespace.class,
122 Rfc6020Mapping.AUGMENT);
123 if (currentOrder == null) {
128 augmentSourceCtx.setOrder(currentOrder);
129 augmentSourceCtx.addToNs(StmtOrderingNamespace.class,
130 Rfc6020Mapping.AUGMENT, currentOrder);
134 public void prerequisiteFailed(
135 final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed)
136 throws InferenceException {
137 throw new InferenceException("Augment target not found: "
138 + augmentNode.getStatementArgument(), augmentNode
139 .getStatementSourceReference());
147 public SchemaNodeIdentifier getTargetNode() {
152 public Collection<? extends DataDefinitionStatement> getDataDefinitions() {
153 return allDeclared(DataDefinitionStatement.class);