Bug 2366 - Effective statements impl for new yang parser.
[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 static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.EFFECTIVE_MODEL;
11
12 import java.util.Collection;
13
14 import javax.annotation.Nonnull;
15
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.ModelProcessingPhase;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
28 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
29 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AugmentEffectiveStatementImpl;
30
31 public class AugmentStatementImpl extends AbstractDeclaredStatement<SchemaNodeIdentifier> implements AugmentStatement {
32
33     protected AugmentStatementImpl(StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
34         super(context);
35     }
36
37     public static class Definition
38             extends
39             AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
40
41         public Definition() {
42             super(Rfc6020Mapping.AUGMENT);
43         }
44
45         @Override
46         public SchemaNodeIdentifier parseArgumentValue(StmtContext<?, ?, ?> ctx, String value) throws SourceException {
47             return SchemaNodeIdentifier.create(AugmentUtils.parseAugmentPath(ctx, value), Utils.isXPathAbsolute(value));
48         }
49
50         @Override
51         public AugmentStatement createDeclared(StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
52             return new AugmentStatementImpl(ctx);
53         }
54
55         @Override
56         public EffectiveStatement<SchemaNodeIdentifier, AugmentStatement> createEffective(
57                 StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
58             return new AugmentEffectiveStatementImpl(ctx);
59         }
60
61         @Override
62         public void onFullDefinitionDeclared(
63                 final StmtContext.Mutable<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode)
64                 throws SourceException {
65
66             final ModelActionBuilder augmentAction = augmentNode.newInferenceAction(EFFECTIVE_MODEL);
67             final ModelActionBuilder.Prerequisite<StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>>> sourceCtxPrereq = augmentAction
68                     .requiresCtx(augmentNode, ModelProcessingPhase.FULL_DECLARATION);
69
70             augmentAction.apply(new ModelActionBuilder.InferenceAction() {
71
72                 @Override
73                 public void apply() throws InferenceException {
74
75                     final StatementContextBase<?, ?, ?> augmentTargetCtx = AugmentUtils
76                             .getAugmentTargetCtx(augmentNode);
77                     StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) sourceCtxPrereq
78                             .get();
79
80                     try {
81                         AugmentUtils.copyFromSourceToTarget(augmentSourceCtx, augmentTargetCtx);
82                     } catch (SourceException e) {
83                         e.printStackTrace();
84                     }
85                 }
86
87                 @Override
88                 public void prerequisiteFailed(final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed)
89                         throws InferenceException {
90                     if (failed.contains(augmentAction)) {
91                         throw new InferenceException("Augment action failed", augmentNode.getStatementSourceReference());
92                     }
93                 }
94             });
95         }
96     }
97
98     @Nonnull
99     @Override
100     public SchemaNodeIdentifier getTargetNode() {
101         return argument();
102     }
103
104     @Override
105     public Collection<? extends DataDefinitionStatement> getDataDefinitions() {
106         return allDeclared(DataDefinitionStatement.class);
107     }
108 }