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 org.opendaylight.yangtools.yang.parser.spi.source.StmtOrderingNamespace;
11 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
12 import java.util.Collection;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
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.meta.AbstractDeclaredStatement;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelActionBuilder.Prerequisite;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
29 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
30 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AugmentEffectiveStatementImpl;
33 public class AugmentStatementImpl extends
34 AbstractDeclaredStatement<SchemaNodeIdentifier> implements
37 private static final Logger LOG = LoggerFactory
38 .getLogger(AugmentStatementImpl.class);
40 protected AugmentStatementImpl(
41 StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
45 public static class Definition
47 AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
50 super(Rfc6020Mapping.AUGMENT);
54 public SchemaNodeIdentifier parseArgumentValue(
55 StmtContext<?, ?, ?> ctx, String value) throws SourceException {
56 return SchemaNodeIdentifier.create(
57 AugmentUtils.parseAugmentPath(ctx, value),
58 Utils.isXPathAbsolute(ctx, value));
62 public AugmentStatement createDeclared(
63 StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
64 return new AugmentStatementImpl(ctx);
68 public EffectiveStatement<SchemaNodeIdentifier, AugmentStatement> createEffective(
69 StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
70 return new AugmentEffectiveStatementImpl(ctx);
74 public void onFullDefinitionDeclared(
75 final StmtContext.Mutable<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode)
76 throws SourceException {
78 if (StmtContextUtils.isInExtensionBody(augmentNode)) {
82 final ModelActionBuilder augmentAction = augmentNode
83 .newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
84 final ModelActionBuilder.Prerequisite<StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>>> sourceCtxPrereq = augmentAction
85 .requiresCtx(augmentNode, ModelProcessingPhase.EFFECTIVE_MODEL);
86 final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target = augmentAction.mutatesEffectiveCtx(getSearchRoot(augmentNode), SchemaNodeIdentifierBuildNamespace.class, augmentNode.getStatementArgument());
87 augmentAction.apply(new ModelActionBuilder.InferenceAction() {
90 public void apply() throws InferenceException {
91 final StatementContextBase<?, ?, ?> augmentTargetCtx = (StatementContextBase<?, ?, ?>) target.get();
93 if (!AugmentUtils.isSupportedAugmentTarget(augmentTargetCtx) || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
94 augmentNode.setIsSupportedToBuildEffective(false);
97 final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) augmentNode;
99 AugmentUtils.copyFromSourceToTarget(augmentSourceCtx,
102 .addEffectiveSubstatement(augmentSourceCtx);
103 updateAugmentOrder(augmentSourceCtx);
104 } catch (SourceException e) {
105 LOG.warn(e.getMessage(), e);
110 private void updateAugmentOrder(
111 final StatementContextBase<?, ?, ?> augmentSourceCtx) {
112 Integer currentOrder = augmentSourceCtx
113 .getFromNamespace(StmtOrderingNamespace.class,
114 Rfc6020Mapping.AUGMENT);
115 if (currentOrder == null) {
120 augmentSourceCtx.setOrder(currentOrder);
121 augmentSourceCtx.addToNs(StmtOrderingNamespace.class,
122 Rfc6020Mapping.AUGMENT, currentOrder);
126 public void prerequisiteFailed(
127 final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed)
128 throws InferenceException {
129 throw new InferenceException("Augment target not found: "
130 + augmentNode.getStatementArgument(), augmentNode
131 .getStatementSourceReference());
136 private Mutable<?, ?, ?> getSearchRoot(Mutable<?, ?, ?> augmentContext) {
137 Mutable<?, ?, ?> parent = augmentContext.getParentContext();
138 // Augment is in uses - we need to augment instantiated nodes in parent.
139 if(Rfc6020Mapping.USES.equals(parent.getPublicDefinition())) {
140 return parent.getParentContext();
148 public SchemaNodeIdentifier getTargetNode() {
153 public Collection<? extends DataDefinitionStatement> getDataDefinitions() {
154 return allDeclared(DataDefinitionStatement.class);