Revert "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 com.google.common.base.Preconditions;
11 import java.util.Collection;
12 import java.util.regex.Pattern;
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.ModelActionBuilder.Prerequisite;
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.meta.StmtContext.Mutable;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
28 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
29 import org.opendaylight.yangtools.yang.parser.spi.source.StmtOrderingNamespace;
30 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.AugmentEffectiveStatementImpl;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class AugmentStatementImpl extends AbstractDeclaredStatement<SchemaNodeIdentifier> implements AugmentStatement {
36     private static final Logger LOG = LoggerFactory.getLogger(AugmentStatementImpl.class);
37     private static final Pattern PATH_REL_PATTERN1 = Pattern.compile("\\.\\.?\\s*/(.+)");
38     private static final Pattern PATH_REL_PATTERN2 = Pattern.compile("//.*");
39
40     protected AugmentStatementImpl(final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> context) {
41         super(context);
42     }
43
44     public static class Definition extends
45             AbstractStatementSupport<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> {
46
47         public Definition() {
48             super(Rfc6020Mapping.AUGMENT);
49         }
50
51         @Override
52         public SchemaNodeIdentifier parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
53             Preconditions.checkArgument(!PATH_REL_PATTERN1.matcher(value).matches()
54                 && !PATH_REL_PATTERN2.matcher(value).matches(),
55                 "An argument for augment can be only absolute path; or descendant if used in uses");
56
57             return Utils.nodeIdentifierFromPath(ctx, value);
58         }
59
60         @Override
61         public AugmentStatement createDeclared(
62                 final StmtContext<SchemaNodeIdentifier, AugmentStatement, ?> ctx) {
63             return new AugmentStatementImpl(ctx);
64         }
65
66         @Override
67         public EffectiveStatement<SchemaNodeIdentifier, AugmentStatement> createEffective(
68                 final StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> ctx) {
69             return new AugmentEffectiveStatementImpl(ctx);
70         }
71
72         @Override
73         public void onFullDefinitionDeclared(
74                 final StmtContext.Mutable<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>> augmentNode)
75                 throws SourceException {
76
77             if (StmtContextUtils.isInExtensionBody(augmentNode)) {
78                 return;
79             }
80
81             final ModelActionBuilder augmentAction = augmentNode
82                     .newInferenceAction(ModelProcessingPhase.EFFECTIVE_MODEL);
83             final ModelActionBuilder.Prerequisite<StmtContext<SchemaNodeIdentifier, AugmentStatement, EffectiveStatement<SchemaNodeIdentifier, AugmentStatement>>> sourceCtxPrereq = augmentAction
84                     .requiresCtx(augmentNode, ModelProcessingPhase.EFFECTIVE_MODEL);
85             final Prerequisite<Mutable<?, ?, EffectiveStatement<?, ?>>> target = augmentAction.mutatesEffectiveCtx(getSearchRoot(augmentNode), SchemaNodeIdentifierBuildNamespace.class, augmentNode.getStatementArgument());
86             augmentAction.apply(new ModelActionBuilder.InferenceAction() {
87
88                 @Override
89                 public void apply() throws InferenceException {
90                     final StatementContextBase<?, ?, ?> augmentTargetCtx = (StatementContextBase<?, ?, ?>) target.get();
91
92                     if (!AugmentUtils.isSupportedAugmentTarget(augmentTargetCtx) || StmtContextUtils.isInExtensionBody(augmentTargetCtx)) {
93                         augmentNode.setIsSupportedToBuildEffective(false);
94                         return;
95                     }
96                     final StatementContextBase<?, ?, ?> augmentSourceCtx = (StatementContextBase<?, ?, ?>) augmentNode;
97                     try {
98                         AugmentUtils.copyFromSourceToTarget(augmentSourceCtx,
99                                 augmentTargetCtx);
100                         augmentTargetCtx
101                                 .addEffectiveSubstatement(augmentSourceCtx);
102                         updateAugmentOrder(augmentSourceCtx);
103                     } catch (SourceException e) {
104                         LOG.warn(e.getMessage(), e);
105                     }
106
107                 }
108
109                 private void updateAugmentOrder(
110                         final StatementContextBase<?, ?, ?> augmentSourceCtx) {
111                     Integer currentOrder = augmentSourceCtx
112                             .getFromNamespace(StmtOrderingNamespace.class,
113                                     Rfc6020Mapping.AUGMENT);
114                     if (currentOrder == null) {
115                         currentOrder = 1;
116                     } else {
117                         currentOrder++;
118                     }
119                     augmentSourceCtx.setOrder(currentOrder);
120                     augmentSourceCtx.addToNs(StmtOrderingNamespace.class,
121                             Rfc6020Mapping.AUGMENT, currentOrder);
122                 }
123
124                 @Override
125                 public void prerequisiteFailed(
126                         final Collection<? extends ModelActionBuilder.Prerequisite<?>> failed)
127                         throws InferenceException {
128                     throw new InferenceException("Augment target not found: "
129                             + augmentNode.getStatementArgument(), augmentNode
130                             .getStatementSourceReference());
131                 }
132             });
133         }
134
135         private Mutable<?, ?, ?> getSearchRoot(Mutable<?, ?, ?> augmentContext) {
136             Mutable<?, ?, ?> parent = augmentContext.getParentContext();
137             // Augment is in uses - we need to augment instantiated nodes in parent.
138             if(Rfc6020Mapping.USES.equals(parent.getPublicDefinition())) {
139                 return parent.getParentContext();
140             }
141             return parent;
142         }
143     }
144
145     @Nonnull
146     @Override
147     public SchemaNodeIdentifier getTargetNode() {
148         return argument();
149     }
150
151     @Override
152     public Collection<? extends DataDefinitionStatement> getDataDefinitions() {
153         return allDeclared(DataDefinitionStatement.class);
154     }
155 }