Revert "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.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
32         AbstractDeclaredStatement<SchemaNodeIdentifier> implements
33         AugmentStatement {
34
35     private static final Logger LOG = LoggerFactory
36             .getLogger(AugmentStatementImpl.class);
37
38     protected AugmentStatementImpl(
39             StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
40         super(context);
41     }
42
43     public static class Definition
44             extends
45             AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
46
47         public Definition() {
48             super(Rfc6020Mapping.AUGMENT);
49         }
50
51         @Override
52         public SchemaNodeIdentifier parseArgumentValue(
53                 StmtContext<?, ?, ?> ctx, String value) throws SourceException {
54             return SchemaNodeIdentifier.create(
55                     AugmentUtils.parseAugmentPath(ctx, value),
56                     Utils.isXPathAbsolute(ctx, value));
57         }
58
59         @Override
60         public AugmentStatement createDeclared(
61                 StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
62             return new AugmentStatementImpl(ctx);
63         }
64
65         @Override
66         public EffectiveStatement<SchemaNodeIdentifier, AugmentStatement> createEffective(
67                 StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
68             return new AugmentEffectiveStatementImpl(ctx);
69         }
70
71         @Override
72         public void onFullDefinitionDeclared(
73                 final StmtContext.Mutable<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode)
74                 throws SourceException {
75
76             if (StmtContextUtils.isInExtensionBody(augmentNode)) {
77                 return;
78             }
79
80             final ModelActionBuilder augmentAction = augmentNode
81                     .newInferenceAction(ModelProcessingPhase.FULL_DECLARATION);
82             final ModelActionBuilder.Prerequisite<StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>>> sourceCtxPrereq = augmentAction
83                     .requiresCtx(augmentNode,
84                             ModelProcessingPhase.FULL_DECLARATION);
85
86             augmentAction.apply(new ModelActionBuilder.InferenceAction() {
87
88                 @Override
89                 public void apply() throws InferenceException {
90                     final StatementContextBase<?, ?, ?> augmentTargetCtx = AugmentUtils
91                             .getAugmentTargetCtx(augmentNode);
92
93                     if (augmentTargetCtx == null) {
94                         throw new InferenceException(
95                                 "Augment target not found: "
96                                         + augmentNode.getStatementArgument(),
97                                 augmentNode.getStatementSourceReference());
98                     }
99
100                     if (!AugmentUtils.isSupportedAugmentTarget(augmentTargetCtx) || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
101                         augmentNode.setIsSupportedToBuildEffective(false);
102                         return;
103                     }
104
105                     final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) augmentNode;
106
107                     try {
108                         AugmentUtils.copyFromSourceToTarget(augmentSourceCtx,
109                                 augmentTargetCtx);
110                         augmentTargetCtx
111                                 .addEffectiveSubstatement(augmentSourceCtx);
112                         updateAugmentOrder(augmentSourceCtx);
113                     } catch (SourceException e) {
114                         LOG.warn(e.getMessage(), e);
115                     }
116
117                 }
118
119                 private void updateAugmentOrder(
120                         final StatementContextBase<?, ?, ?> augmentSourceCtx) {
121                     Integer currentOrder = augmentSourceCtx
122                             .getFromNamespace(StmtOrderingNamespace.class,
123                                     Rfc6020Mapping.AUGMENT);
124                     if (currentOrder == null) {
125                         currentOrder = 1;
126                     } else {
127                         currentOrder++;
128                     }
129                     augmentSourceCtx.setOrder(currentOrder);
130                     augmentSourceCtx.addToNs(StmtOrderingNamespace.class,
131                             Rfc6020Mapping.AUGMENT, currentOrder);
132                 }
133
134                 @Override
135                 public void prerequisiteFailed(
136                         final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed)
137                         throws InferenceException {
138                     throw new InferenceException("Augment target not found: "
139                             + augmentNode.getStatementArgument(), augmentNode
140                             .getStatementSourceReference());
141                 }
142             });
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 }