Updated SchemaNodeIdentifier namespace handling.
[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.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;
32
33 public class AugmentStatementImpl extends
34         AbstractDeclaredStatement<SchemaNodeIdentifier> implements
35         AugmentStatement {
36
37     private static final Logger LOG = LoggerFactory
38             .getLogger(AugmentStatementImpl.class);
39
40     protected AugmentStatementImpl(
41             StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
42         super(context);
43     }
44
45     public static class Definition
46             extends
47             AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
48
49         public Definition() {
50             super(Rfc6020Mapping.AUGMENT);
51         }
52
53         @Override
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));
59         }
60
61         @Override
62         public AugmentStatement createDeclared(
63                 StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
64             return new AugmentStatementImpl(ctx);
65         }
66
67         @Override
68         public EffectiveStatement<SchemaNodeIdentifier, AugmentStatement> createEffective(
69                 StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
70             return new AugmentEffectiveStatementImpl(ctx);
71         }
72
73         @Override
74         public void onFullDefinitionDeclared(
75                 final StmtContext.Mutable<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode)
76                 throws SourceException {
77
78             if (StmtContextUtils.isInExtensionBody(augmentNode)) {
79                 return;
80             }
81
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() {
88
89                 @Override
90                 public void apply() throws InferenceException {
91                     final StatementContextBase<?, ?, ?> augmentTargetCtx = (StatementContextBase<?, ?, ?>) target.get();
92
93                     if (!AugmentUtils.isSupportedAugmentTarget(augmentTargetCtx) || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
94                         augmentNode.setIsSupportedToBuildEffective(false);
95                         return;
96                     }
97                     final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) augmentNode;
98                     try {
99                         AugmentUtils.copyFromSourceToTarget(augmentSourceCtx,
100                                 augmentTargetCtx);
101                         augmentTargetCtx
102                                 .addEffectiveSubstatement(augmentSourceCtx);
103                         updateAugmentOrder(augmentSourceCtx);
104                     } catch (SourceException e) {
105                         LOG.warn(e.getMessage(), e);
106                     }
107
108                 }
109
110                 private void updateAugmentOrder(
111                         final StatementContextBase<?, ?, ?> augmentSourceCtx) {
112                     Integer currentOrder = augmentSourceCtx
113                             .getFromNamespace(StmtOrderingNamespace.class,
114                                     Rfc6020Mapping.AUGMENT);
115                     if (currentOrder == null) {
116                         currentOrder = 1;
117                     } else {
118                         currentOrder++;
119                     }
120                     augmentSourceCtx.setOrder(currentOrder);
121                     augmentSourceCtx.addToNs(StmtOrderingNamespace.class,
122                             Rfc6020Mapping.AUGMENT, currentOrder);
123                 }
124
125                 @Override
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());
132                 }
133             });
134         }
135
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();
141             }
142             return parent;
143         }
144     }
145
146     @Nonnull
147     @Override
148     public SchemaNodeIdentifier getTargetNode() {
149         return argument();
150     }
151
152     @Override
153     public Collection<? extends DataDefinitionStatement> getDataDefinitions() {
154         return allDeclared(DataDefinitionStatement.class);
155     }
156 }