Bug 2366 - Effective statments impl merge, retest & bugfix
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / AugmentStatementImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
9
10 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
11 import java.util.Collection;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.AugmentStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.DataDefinitionStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
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.ModelProcessingPhase;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AugmentEffectiveStatementImpl;
29
30 public class AugmentStatementImpl extends
31         AbstractDeclaredStatement<SchemaNodeIdentifier> implements
32         AugmentStatement {
33
34     private static final Logger LOG = LoggerFactory
35             .getLogger(AugmentStatementImpl.class);
36
37     protected AugmentStatementImpl(
38             StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
39         super(context);
40     }
41
42     public static class Definition
43             extends
44             AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
45
46         public Definition() {
47             super(Rfc6020Mapping.AUGMENT);
48         }
49
50         @Override
51         public SchemaNodeIdentifier parseArgumentValue(
52                 StmtContext<?, ?, ?> ctx, String value) throws SourceException {
53             return SchemaNodeIdentifier.create(
54                     AugmentUtils.parseAugmentPath(ctx, value),
55                     Utils.isXPathAbsolute(value));
56         }
57
58         @Override
59         public AugmentStatement createDeclared(
60                 StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
61             return new AugmentStatementImpl(ctx);
62         }
63
64         @Override
65         public EffectiveStatement<SchemaNodeIdentifier, AugmentStatement> createEffective(
66                 StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
67             return new AugmentEffectiveStatementImpl(ctx);
68         }
69
70         @Override
71         public void onFullDefinitionDeclared(
72                 final StmtContext.Mutable<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode)
73                 throws SourceException {
74
75             if(StmtContextUtils.isInExtensionBody(augmentNode)) {
76                 return;
77             }
78
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);
84
85             augmentAction.apply(new ModelActionBuilder.InferenceAction() {
86
87                 @Override
88                 public void apply() throws InferenceException {
89
90                     final StatementContextBase<?, ?, ?> augmentTargetCtx = AugmentUtils
91                             .getAugmentTargetCtx(augmentNode);
92
93                     if (augmentTargetCtx == null) {
94                         throw new InferenceException("Augment target not found: "+augmentNode.getStatementArgument(), augmentNode.getStatementSourceReference());
95                     }
96                     if (StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
97                         augmentNode.setIsSupportedToBuildEffective(false);
98                         return;
99                     }
100
101                     final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) augmentNode;
102
103                     try {
104                         AugmentUtils.copyFromSourceToTarget(augmentSourceCtx,
105                                 augmentTargetCtx);
106                         augmentTargetCtx.addEffectiveSubstatement(augmentSourceCtx);
107                     } catch (SourceException e) {
108                         LOG.warn(e.getMessage(), e);
109                     }
110
111                 }
112
113                 @Override
114                 public void prerequisiteFailed(
115                         final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed)
116                         throws InferenceException {
117                         throw new InferenceException("Augment target not found: "+augmentNode.getStatementArgument(),
118                                 augmentNode.getStatementSourceReference());
119                 }
120             });
121         }
122     }
123
124     @Nonnull
125     @Override
126     public SchemaNodeIdentifier getTargetNode() {
127         return argument();
128     }
129
130     @Override
131     public Collection<? extends DataDefinitionStatement> getDataDefinitions() {
132         return allDeclared(DataDefinitionStatement.class);
133     }
134 }