Merge "Add hooking into NormalizedNodeParsers."
[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 java.util.Collection;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
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.source.SourceException;
26 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AugmentEffectiveStatementImpl;
28
29 public class AugmentStatementImpl extends
30         AbstractDeclaredStatement<SchemaNodeIdentifier> implements
31         AugmentStatement {
32
33     private static final Logger LOG = LoggerFactory
34             .getLogger(AugmentStatementImpl.class);
35
36     protected AugmentStatementImpl(
37             StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
38         super(context);
39     }
40
41     public static class Definition
42             extends
43             AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
44
45         public Definition() {
46             super(Rfc6020Mapping.AUGMENT);
47         }
48
49         @Override
50         public SchemaNodeIdentifier parseArgumentValue(
51                 StmtContext<?, ?, ?> ctx, String value) throws SourceException {
52             return SchemaNodeIdentifier.create(
53                     AugmentUtils.parseAugmentPath(ctx, value),
54                     Utils.isXPathAbsolute(value));
55         }
56
57         @Override
58         public AugmentStatement createDeclared(
59                 StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
60             return new AugmentStatementImpl(ctx);
61         }
62
63         @Override
64         public EffectiveStatement<SchemaNodeIdentifier, AugmentStatement> createEffective(
65                 StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
66             return new AugmentEffectiveStatementImpl(ctx);
67         }
68
69         @Override
70         public void onFullDefinitionDeclared(
71                 final StmtContext.Mutable<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode)
72                 throws SourceException {
73
74             final ModelActionBuilder augmentAction = augmentNode
75                     .newInferenceAction(ModelProcessingPhase.FULL_DECLARATION);
76             final ModelActionBuilder.Prerequisite<StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>>> sourceCtxPrereq = augmentAction
77                     .requiresCtx(augmentNode,
78                             ModelProcessingPhase.FULL_DECLARATION);
79
80             augmentAction.apply(new ModelActionBuilder.InferenceAction() {
81
82                 @Override
83                 public void apply() throws InferenceException {
84
85                     final StatementContextBase<?, ?, ?> augmentTargetCtx = AugmentUtils
86                             .getAugmentTargetCtx(augmentNode);
87
88                     if (augmentTargetCtx == null) {
89                         throw new InferenceException("Augment target not found: "+augmentNode.getStatementArgument(), augmentNode.getStatementSourceReference());
90                     }
91
92                     final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) sourceCtxPrereq
93                             .get();
94
95                     try {
96                         AugmentUtils.copyFromSourceToTarget(augmentSourceCtx,
97                                 augmentTargetCtx);
98                     } catch (SourceException e) {
99                         LOG.warn(e.getMessage(), e);
100                     }
101
102                 }
103
104                 @Override
105                 public void prerequisiteFailed(
106                         final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed)
107                         throws InferenceException {
108                         throw new InferenceException("Augment target not found: "+augmentNode.getStatementArgument(),
109                                 augmentNode.getStatementSourceReference());
110                 }
111             });
112         }
113     }
114
115     @Nonnull
116     @Override
117     public SchemaNodeIdentifier getTargetNode() {
118         return argument();
119     }
120
121     @Override
122     public Collection<? extends DataDefinitionStatement> getDataDefinitions() {
123         return allDeclared(DataDefinitionStatement.class);
124     }
125 }