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