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