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